Skip to content

Commit

Permalink
fix(replicas): fix lint errors; add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
zelig committed Nov 24, 2023
1 parent 9d519c0 commit 57e8c97
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 21 deletions.
1 change: 0 additions & 1 deletion pkg/replicas/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@ var (
Counts = counts
Signer = signer
)

15 changes: 4 additions & 11 deletions pkg/replicas/getter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"errors"
"fmt"
"io"
"sync"
"sync/atomic"
"testing"
"time"
Expand Down Expand Up @@ -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++ {

Expand All @@ -81,31 +80,26 @@ 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
}
addrs = append(addrs, addr)
latencies = append(latencies, time.Now().Sub(now))

Check failure on line 89 in pkg/replicas/getter_test.go

View workflow job for this annotation

GitHub Actions / Lint

S1012: should use `time.Since` instead of `time.Now().Sub` (gosimple)
}

}()
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 {
t.Fatalf("expected no error. got %v", err)
}
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()
Expand All @@ -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 {
Expand All @@ -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)
}
Expand Down
8 changes: 1 addition & 7 deletions pkg/replicas/putter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion pkg/replicas/replica_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -16,6 +17,8 @@ import (
"github.com/ethersphere/bee/pkg/swarm"
)

// dispersed verifies that a set of addresses are maximally dispersed without repetition
//

Check failure on line 21 in pkg/replicas/replica_test.go

View workflow job for this annotation

GitHub Actions / Lint

File is not `gofmt`-ed with `-s` (gofmt)
func dispersed(level redundancy.Level, ch swarm.Chunk, strict bool, addrs ...swarm.Address) error {
count := replicas.Counts[level]
nhoods := make(map[byte]bool)
Expand All @@ -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 {
Expand All @@ -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")
Expand Down
12 changes: 11 additions & 1 deletion pkg/replicas/replicas.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 57e8c97

Please sign in to comment.