Skip to content

Commit fd88cbc

Browse files
authored
fix: add TestGetWrappedChunk (#4932)
1 parent 57bc1a8 commit fd88cbc

File tree

3 files changed

+85
-8
lines changed

3 files changed

+85
-8
lines changed

.github/workflows/beekeeper.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@ jobs:
173173
- name: Test act
174174
id: act
175175
run: timeout ${TIMEOUT} bash -c 'until beekeeper check --cluster-name local-dns --checks ci-act; do echo "waiting for act..."; sleep .3; done'
176+
- name: Test feeds
177+
id: feeds
178+
run: timeout ${TIMEOUT} beekeeper check --cluster-name local-dns --checks=ci-feed
176179
- name: Collect debug artifacts
177180
if: failure()
178181
run: |

pkg/feeds/getter_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright 2024 The Swarm Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package feeds
6+
7+
import (
8+
"bytes"
9+
"context"
10+
"encoding/binary"
11+
"testing"
12+
13+
soctesting "github.com/ethersphere/bee/v2/pkg/soc/testing"
14+
mockstorer "github.com/ethersphere/bee/v2/pkg/storer/mock"
15+
)
16+
17+
func TestGetWrappedChunk(t *testing.T) {
18+
storer := mockstorer.New()
19+
20+
// new format (wraps chunk)
21+
ch := soctesting.GenerateMockSOC(t, []byte("data")).Chunk()
22+
wch, err := GetWrappedChunk(context.Background(), storer.ChunkStore(), ch)
23+
if err != nil {
24+
t.Fatal(err)
25+
}
26+
27+
if !bytes.Equal(wch.Data()[8:], []byte("data")) {
28+
t.Fatal("data mismatch")
29+
}
30+
31+
// old format
32+
tt := []struct {
33+
name string
34+
addr []byte
35+
}{
36+
{
37+
name: "unencrypted",
38+
addr: wch.Address().Bytes(),
39+
},
40+
{
41+
name: "encrypted",
42+
addr: append(wch.Address().Bytes(), wch.Address().Bytes()...),
43+
},
44+
}
45+
46+
for _, tc := range tt {
47+
t.Run(tc.name, func(t *testing.T) {
48+
timestamp := make([]byte, 8)
49+
binary.BigEndian.PutUint64(timestamp, 1)
50+
ch = soctesting.GenerateMockSOC(t, append(timestamp, tc.addr...)).Chunk()
51+
52+
err = storer.Put(context.Background(), wch)
53+
if err != nil {
54+
t.Fatal(err)
55+
}
56+
57+
wch, err = GetWrappedChunk(context.Background(), storer.ChunkStore(), ch)
58+
if err != nil {
59+
t.Fatal(err)
60+
}
61+
62+
if !bytes.Equal(wch.Data()[8:], []byte("data")) {
63+
t.Fatal("data mismatch")
64+
}
65+
})
66+
}
67+
}

pkg/storage/inmemchunkstore/inmemchunkstore.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func (c *ChunkStore) Get(_ context.Context, addr swarm.Address) (swarm.Chunk, er
3232
c.mu.Lock()
3333
defer c.mu.Unlock()
3434

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

46-
chunkCount, ok := c.chunks[ch.Address().ByteString()]
46+
chunkCount, ok := c.chunks[c.key(ch.Address())]
4747
if !ok {
4848
chunkCount.chunk = swarm.NewChunk(ch.Address(), ch.Data()).WithStamp(ch.Stamp())
4949
}
5050
chunkCount.count++
51-
c.chunks[ch.Address().ByteString()] = chunkCount
51+
c.chunks[c.key(ch.Address())] = chunkCount
5252

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

60-
_, exists := c.chunks[addr.ByteString()]
60+
_, exists := c.chunks[c.key(addr)]
6161

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

69-
chunkCount := c.chunks[addr.ByteString()]
69+
chunkCount := c.chunks[c.key(addr)]
7070
chunkCount.count--
7171
if chunkCount.count <= 0 {
7272
delete(c.chunks, addr.ByteString())
7373
} else {
74-
c.chunks[addr.ByteString()] = chunkCount
74+
c.chunks[c.key(addr)] = chunkCount
7575
}
7676

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

84-
chunkCount := c.chunks[ch.Address().ByteString()]
84+
chunkCount := c.chunks[c.key(ch.Address())]
8585
chunkCount.chunk = ch
8686
if emplace {
8787
chunkCount.count++
8888
}
89-
c.chunks[ch.Address().ByteString()] = chunkCount
89+
c.chunks[c.key(ch.Address())] = chunkCount
9090

9191
return nil
9292
}
@@ -111,3 +111,10 @@ func (c *ChunkStore) Iterate(_ context.Context, fn storage.IterateChunkFn) error
111111
func (c *ChunkStore) Close() error {
112112
return nil
113113
}
114+
115+
func (c *ChunkStore) key(addr swarm.Address) string {
116+
if len(addr.Bytes()) < swarm.HashSize {
117+
return addr.ByteString()
118+
}
119+
return string(addr.Bytes()[:swarm.HashSize])
120+
}

0 commit comments

Comments
 (0)