From 57e8c978d89d1c785a636e67a98842b7162b6772 Mon Sep 17 00:00:00 2001 From: zelig Date: Thu, 23 Nov 2023 10:09:30 +0100 Subject: [PATCH] fix(replicas): fix lint errors; add comments --- pkg/replicas/export_test.go | 1 - pkg/replicas/getter_test.go | 15 ++++----------- pkg/replicas/putter_test.go | 8 +------- pkg/replicas/replica_test.go | 7 ++++++- pkg/replicas/replicas.go | 12 +++++++++++- 5 files changed, 22 insertions(+), 21 deletions(-) diff --git a/pkg/replicas/export_test.go b/pkg/replicas/export_test.go index f226eb320d1..7dcb8833c78 100644 --- a/pkg/replicas/export_test.go +++ b/pkg/replicas/export_test.go @@ -8,4 +8,3 @@ var ( Counts = counts Signer = signer ) - diff --git a/pkg/replicas/getter_test.go b/pkg/replicas/getter_test.go index 8fc52131a79..0f0f7d095f6 100644 --- a/pkg/replicas/getter_test.go +++ b/pkg/replicas/getter_test.go @@ -10,7 +10,6 @@ import ( "errors" "fmt" "io" - "sync" "sync/atomic" "testing" "time" @@ -69,7 +68,7 @@ func TestGetter(t *testing.T) { } retryInterval := replicas.RetryInterval defer func() { replicas.RetryInterval = retryInterval }() - replicas.RetryInterval = 20 * time.Millisecond + replicas.RetryInterval = 10 * time.Millisecond for level, c := range replicas.Counts { for j := 0; j <= 16; j++ { @@ -81,10 +80,7 @@ func TestGetter(t *testing.T) { now := time.Now() var addrs []swarm.Address var latencies []time.Duration - wg := sync.WaitGroup{} - wg.Add(1) go func() { - wg.Done() for addr := range store.c { if ch.Address().Equal(addr) { continue @@ -92,11 +88,9 @@ func TestGetter(t *testing.T) { addrs = append(addrs, addr) latencies = append(latencies, time.Now().Sub(now)) } - }() g := replicas.NewGetter(store, redundancy.Level(level)) _, err := g.Get(ctx, ch.Address()) - wg.Wait() t.Run("correctly returned error", func(t *testing.T) { if j <= c { if err != nil { @@ -104,8 +98,8 @@ func TestGetter(t *testing.T) { } return } - esg, ok := err.(*replicas.ErrSwarmageddon) - if !ok { + var esg *replicas.ErrSwarmageddon + if !errors.As(err, &esg) { t.Fatalf("incorrect error. want Swarmageddon. got %v", err) } errs := esg.Unwrap() @@ -117,7 +111,7 @@ func TestGetter(t *testing.T) { } expLen := replicas.Counts[level] + 1 if length+1 != expLen { - t.Fatalf("incorrect error. want %d. got %d", expLen, length) + t.Fatalf("incorrect error. want %d. got %d", expLen, length+1) } }) if j < c { @@ -136,7 +130,6 @@ func TestGetter(t *testing.T) { t.Run("latency", func(t *testing.T) { for i, latency := range latencies { multiplier := latency / replicas.RetryInterval - fmt.Printf("%d,lat=%v,mm=%v\n", i, latency, multiplier) if multiplier > 0 && i < replicas.Counts[multiplier-1] { t.Fatalf("incorrect latency for retrieving replica %d: %v", i, err) } diff --git a/pkg/replicas/putter_test.go b/pkg/replicas/putter_test.go index 3b30b2484aa..d5ea2f256d4 100644 --- a/pkg/replicas/putter_test.go +++ b/pkg/replicas/putter_test.go @@ -51,12 +51,6 @@ func (tbp *testBasePutter) Put(ctx context.Context, ch swarm.Chunk) error { return tbp.store.Put(ctx, ch) } -func newTestBasePutter(store storage.ChunkStore) storage.Putter { - return &testBasePutter{ - store: store, - } -} - func TestPutter(t *testing.T) { t.Run("Levels and dispersion", func(t *testing.T) { tcs := []struct { @@ -81,7 +75,7 @@ func TestPutter(t *testing.T) { t.Fatal(err) } ctx := context.Background() - ctx = context.WithValue(ctx, "redundancyLevel", tc.level) + ctx = replicas.SetLevel(ctx, tc.level) ch, err := cac.New(buf) if err != nil { t.Fatal(err) diff --git a/pkg/replicas/replica_test.go b/pkg/replicas/replica_test.go index 8f87b410ce9..23bce23a0e9 100644 --- a/pkg/replicas/replica_test.go +++ b/pkg/replicas/replica_test.go @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// replicas_test just contains helper functions to verify dispersion and replication package replicas_test import ( @@ -16,6 +17,8 @@ import ( "github.com/ethersphere/bee/pkg/swarm" ) +// dispersed verifies that a set of addresses are maximally dispersed without repetition +// func dispersed(level redundancy.Level, ch swarm.Chunk, strict bool, addrs ...swarm.Address) error { count := replicas.Counts[level] nhoods := make(map[byte]bool) @@ -41,6 +44,8 @@ func dispersed(level redundancy.Level, ch swarm.Chunk, strict bool, addrs ...swa return nil } +// replicated verifies that the replica chunks are indeed replicas +// of the original chunk wrapped in soc func replicated(store storage.ChunkStore, ch swarm.Chunk, addrs ...swarm.Address) error { ctx := context.Background() for _, addr := range addrs { @@ -51,7 +56,7 @@ func replicated(store storage.ChunkStore, ch swarm.Chunk, addrs ...swarm.Address sch, err := soc.FromChunk(chunk) if err != nil { - return nil + return err } if !sch.WrappedChunk().Equal(ch) { return errors.New("invalid replica: does not wrap original content addressed chunk") diff --git a/pkg/replicas/replicas.go b/pkg/replicas/replicas.go index 3c53ee274d2..71ff22ae8da 100644 --- a/pkg/replicas/replicas.go +++ b/pkg/replicas/replicas.go @@ -21,7 +21,11 @@ import ( "github.com/ethersphere/bee/pkg/swarm" ) +type redundancyLevelType struct{} + var ( + // redundancyLevel is the context key for the redundancy level + redundancyLevel redundancyLevelType // RetryInterval is the duration between successive additional requests RetryInterval = 300 * time.Millisecond // @@ -36,10 +40,15 @@ var ( owner, _ = hex.DecodeString("dc5b20847f43d67928f49cd4f85d696b5a7617b5") ) +// SetLevel sets the redundancy level in the context +func SetLevel(ctx context.Context, level redundancy.Level) context.Context { + return context.WithValue(ctx, redundancyLevel, level) +} + // getLevelFromContext is a helper function to extract the redundancy level from the context func getLevelFromContext(ctx context.Context) redundancy.Level { rlevel := redundancy.PARANOID - if val := ctx.Value("redundancyLevel"); val != nil { + if val := ctx.Value(redundancyLevel); val != nil { rlevel = val.(redundancy.Level) } return rlevel @@ -72,6 +81,7 @@ type replica struct { addr, id []byte // byte slice of SOC address and SOC ID } +// replicate returns a replica params strucure seeded with a byte of entropy as argument func (rr *replicator) replicate(i uint8) (sp *replica) { // change the last byte of the address to create SOC ID id := make([]byte, 32)