Skip to content

Commit

Permalink
test: always save newer payload of soc
Browse files Browse the repository at this point in the history
  • Loading branch information
nugaon committed Nov 7, 2024
1 parent a9ba53b commit 4893749
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions pkg/storer/internal/chunkstore/chunkstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package chunkstore_test

import (
"bytes"
"context"
"errors"
"fmt"
Expand All @@ -13,7 +14,9 @@ import (
"os"
"testing"

"github.com/ethersphere/bee/v2/pkg/crypto"
"github.com/ethersphere/bee/v2/pkg/sharky"
"github.com/ethersphere/bee/v2/pkg/soc"
"github.com/ethersphere/bee/v2/pkg/storer/internal/transaction"

"github.com/ethersphere/bee/v2/pkg/storage"
Expand Down Expand Up @@ -153,6 +156,64 @@ func TestChunkStore(t *testing.T) {
}
})

// TODO: remove this when postage stamping is refactored for GSOC.
t.Run("put two SOCs with different payloads", func(t *testing.T) {
key, _ := crypto.GenerateSecp256k1Key()
signer := crypto.NewDefaultSigner(key)

// chunk data to upload
chunk1 := chunktest.FixtureChunk("7000")
chunk2 := chunktest.FixtureChunk("0033")
id := make([]byte, swarm.HashSize)
s1 := soc.New(id, chunk1)
s2 := soc.New(id, chunk2)
sch1, err := s1.Sign(signer)
if err != nil {
t.Fatal(err)
}
sch1 = sch1.WithStamp(chunk1.Stamp())
sch2, err := s2.Sign(signer)
if err != nil {
t.Fatal(err)
}
sch2 = sch2.WithStamp(chunk2.Stamp())

// Put the first SOC into the chunk store
err = st.Run(context.Background(), func(s transaction.Store) error {
return s.ChunkStore().Put(context.TODO(), sch1)
})
if err != nil {
t.Fatalf("failed putting first single owner chunk: %v", err)
}

// Put the second SOC into the chunk store
err = st.Run(context.Background(), func(s transaction.Store) error {
return s.ChunkStore().Put(context.TODO(), sch2)
})
if err != nil {
t.Fatalf("failed putting second single owner chunk: %v", err)
}

// Retrieve the chunk from the chunk store
var retrievedChunk swarm.Chunk
err = st.Run(context.Background(), func(s transaction.Store) error {
retrievedChunk, err = s.ChunkStore().Get(context.TODO(), sch1.Address())
return err
})
if err != nil {
t.Fatalf("failed retrieving chunk: %v", err)
}
schRetrieved, err := soc.FromChunk(retrievedChunk)
if err != nil {
t.Fatalf("failed converting chunk to SOC: %v", err)
}

// Verify that the retrieved chunk contains the latest payload
if !bytes.Equal(chunk2.Data(), schRetrieved.WrappedChunk().Data()) {
t.Fatalf("expected payload %s, got %s", chunk2.Data(), schRetrieved.WrappedChunk().Data())
}
})

t.Run("get chunks", func(t *testing.T) {
for _, ch := range testChunks {
readCh, err := st.ChunkStore().Get(context.TODO(), ch.Address())
Expand Down

0 comments on commit 4893749

Please sign in to comment.