Skip to content

Commit

Permalink
Merge branch 'master' into bmt-clean
Browse files Browse the repository at this point in the history
  • Loading branch information
mfw78 authored Jan 15, 2025
2 parents a9746d7 + fd88cbc commit 13f672f
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 8 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @istae @acha-bill
3 changes: 3 additions & 0 deletions .github/workflows/beekeeper.yml
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ jobs:
- name: Test act
id: act
run: timeout ${TIMEOUT} bash -c 'until beekeeper check --cluster-name local-dns --checks ci-act; do echo "waiting for act..."; sleep .3; done'
- name: Test feeds
id: feeds
run: timeout ${TIMEOUT} beekeeper check --cluster-name local-dns --checks=ci-feed
- name: Collect debug artifacts
if: failure()
run: |
Expand Down
67 changes: 67 additions & 0 deletions pkg/feeds/getter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2024 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package feeds

import (
"bytes"
"context"
"encoding/binary"
"testing"

soctesting "github.com/ethersphere/bee/v2/pkg/soc/testing"
mockstorer "github.com/ethersphere/bee/v2/pkg/storer/mock"
)

func TestGetWrappedChunk(t *testing.T) {
storer := mockstorer.New()

// new format (wraps chunk)
ch := soctesting.GenerateMockSOC(t, []byte("data")).Chunk()
wch, err := GetWrappedChunk(context.Background(), storer.ChunkStore(), ch)
if err != nil {
t.Fatal(err)
}

if !bytes.Equal(wch.Data()[8:], []byte("data")) {
t.Fatal("data mismatch")
}

// old format
tt := []struct {
name string
addr []byte
}{
{
name: "unencrypted",
addr: wch.Address().Bytes(),
},
{
name: "encrypted",
addr: append(wch.Address().Bytes(), wch.Address().Bytes()...),
},
}

for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
timestamp := make([]byte, 8)
binary.BigEndian.PutUint64(timestamp, 1)
ch = soctesting.GenerateMockSOC(t, append(timestamp, tc.addr...)).Chunk()

err = storer.Put(context.Background(), wch)
if err != nil {
t.Fatal(err)
}

wch, err = GetWrappedChunk(context.Background(), storer.ChunkStore(), ch)
if err != nil {
t.Fatal(err)
}

if !bytes.Equal(wch.Data()[8:], []byte("data")) {
t.Fatal("data mismatch")
}
})
}
}
23 changes: 15 additions & 8 deletions pkg/storage/inmemchunkstore/inmemchunkstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (c *ChunkStore) Get(_ context.Context, addr swarm.Address) (swarm.Chunk, er
c.mu.Lock()
defer c.mu.Unlock()

chunk, ok := c.chunks[addr.ByteString()]
chunk, ok := c.chunks[c.key(addr)]
if !ok {
return nil, storage.ErrNotFound
}
Expand All @@ -43,12 +43,12 @@ func (c *ChunkStore) Put(_ context.Context, ch swarm.Chunk) error {
c.mu.Lock()
defer c.mu.Unlock()

chunkCount, ok := c.chunks[ch.Address().ByteString()]
chunkCount, ok := c.chunks[c.key(ch.Address())]
if !ok {
chunkCount.chunk = swarm.NewChunk(ch.Address(), ch.Data()).WithStamp(ch.Stamp())
}
chunkCount.count++
c.chunks[ch.Address().ByteString()] = chunkCount
c.chunks[c.key(ch.Address())] = chunkCount

return nil
}
Expand All @@ -57,7 +57,7 @@ func (c *ChunkStore) Has(_ context.Context, addr swarm.Address) (bool, error) {
c.mu.Lock()
defer c.mu.Unlock()

_, exists := c.chunks[addr.ByteString()]
_, exists := c.chunks[c.key(addr)]

return exists, nil
}
Expand All @@ -66,12 +66,12 @@ func (c *ChunkStore) Delete(_ context.Context, addr swarm.Address) error {
c.mu.Lock()
defer c.mu.Unlock()

chunkCount := c.chunks[addr.ByteString()]
chunkCount := c.chunks[c.key(addr)]
chunkCount.count--
if chunkCount.count <= 0 {
delete(c.chunks, addr.ByteString())
} else {
c.chunks[addr.ByteString()] = chunkCount
c.chunks[c.key(addr)] = chunkCount
}

return nil
Expand All @@ -81,12 +81,12 @@ func (c *ChunkStore) Replace(_ context.Context, ch swarm.Chunk, emplace bool) er
c.mu.Lock()
defer c.mu.Unlock()

chunkCount := c.chunks[ch.Address().ByteString()]
chunkCount := c.chunks[c.key(ch.Address())]
chunkCount.chunk = ch
if emplace {
chunkCount.count++
}
c.chunks[ch.Address().ByteString()] = chunkCount
c.chunks[c.key(ch.Address())] = chunkCount

return nil
}
Expand All @@ -111,3 +111,10 @@ func (c *ChunkStore) Iterate(_ context.Context, fn storage.IterateChunkFn) error
func (c *ChunkStore) Close() error {
return nil
}

func (c *ChunkStore) key(addr swarm.Address) string {
if len(addr.Bytes()) < swarm.HashSize {
return addr.ByteString()
}
return string(addr.Bytes()[:swarm.HashSize])
}

0 comments on commit 13f672f

Please sign in to comment.