diff --git a/p2p/ipld/read.go b/p2p/ipld/read.go index bce75912b5..00ec568d3b 100644 --- a/p2p/ipld/read.go +++ b/p2p/ipld/read.go @@ -55,7 +55,7 @@ func ValidateAvailability( type res struct { data []byte - err error + err error } resCh := make(chan res, len(samples)) for _, s := range samples { diff --git a/p2p/ipld/read_test.go b/p2p/ipld/read_test.go index e4d761db2b..b56177938d 100644 --- a/p2p/ipld/read_test.go +++ b/p2p/ipld/read_test.go @@ -31,7 +31,7 @@ import ( func TestValidateAvailability(t *testing.T) { const ( - msgs = 16 + msgs = 16 valShares = 10 ) @@ -52,7 +52,7 @@ func TestValidateAvailability(t *testing.T) { require.NoError(t, err) err = ValidateAvailability(ctx, ipfsAPI, &block.DataAvailabilityHeader, valShares, func(data namespace.PrefixedData) { - // TODO(@Wondertan): Check data passed to callback once PR with public ComputeShares method is merged. + // TODO(@Wondertan): Check data passed to callback once PR with public ComputeShares method is merged. }) assert.NoError(t, err) } diff --git a/p2p/ipld/sample.go b/p2p/ipld/sample.go index 2feee80182..90f3c2270f 100644 --- a/p2p/ipld/sample.go +++ b/p2p/ipld/sample.go @@ -1,99 +1,99 @@ package ipld import ( - "github.com/ipfs/go-cid" - "github.com/lazyledger/nmt/namespace" + "github.com/ipfs/go-cid" + "github.com/lazyledger/nmt/namespace" - "github.com/lazyledger/lazyledger-core/libs/rand" - "github.com/lazyledger/lazyledger-core/p2p/ipld/plugin/nodes" - "github.com/lazyledger/lazyledger-core/types" + "github.com/lazyledger/lazyledger-core/libs/rand" + "github.com/lazyledger/lazyledger-core/p2p/ipld/plugin/nodes" + "github.com/lazyledger/lazyledger-core/types" ) // Sample is a point in 2D space over square. type Sample struct { - Row, Col uint32 + Row, Col uint32 } // SampleSquare randomly picks *num* unique points from arbitrary *width* square // and returns them as samples. func SampleSquare(squareWidth uint32, num int) []*Sample { - ss := newSquareSampler(squareWidth, num) - ss.sample(num) - return ss.sampled() + ss := newSquareSampler(squareWidth, num) + ss.sample(num) + return ss.sampled() } // Leaf returns leaf info needed for retrieval using data provided with DAHeader. func (s *Sample) Leaf(dah *types.DataAvailabilityHeader) (cid.Cid, uint32, error) { - var ( - leaf uint32 - root namespace.IntervalDigest - ) - - // spread leaves retrieval from both Row and Column roots - if rand.Bool() { - root = dah.ColumnRoots[s.Col] - leaf = s.Row - } else { - root = dah.RowsRoots[s.Row] - leaf = s.Col - } - - rootCid, err := nodes.CidFromNamespacedSha256(root.Bytes()) - if err != nil { - return cid.Undef, 0, err - } - - return rootCid, leaf, nil + var ( + leaf uint32 + root namespace.IntervalDigest + ) + + // spread leaves retrieval from both Row and Column roots + if rand.Bool() { + root = dah.ColumnRoots[s.Col] + leaf = s.Row + } else { + root = dah.RowsRoots[s.Row] + leaf = s.Col + } + + rootCid, err := nodes.CidFromNamespacedSha256(root.Bytes()) + if err != nil { + return cid.Undef, 0, err + } + + return rootCid, leaf, nil } // Equals check whenever to samples are equal. func (s *Sample) Equals(to *Sample) bool { - return s.Row == to.Row && s.Col == s.Col + return s.Row == to.Row && s.Col == to.Col } type squareSampler struct { - squareWidth uint32 - samples []*Sample + squareWidth uint32 + samples []*Sample } func newSquareSampler(squareWidth uint32, expectedSamples int) *squareSampler { - return &squareSampler{ - squareWidth: squareWidth, - samples: make([]*Sample, 0, expectedSamples), - } + return &squareSampler{ + squareWidth: squareWidth, + samples: make([]*Sample, 0, expectedSamples), + } } func (ss *squareSampler) sample(num int) { - done := 0 - for done < num { - p := &Sample{ - Row: uint32(rand.Int31n(int32(ss.squareWidth))), - Col: uint32(rand.Int31n(int32(ss.squareWidth))), - } - - if ss.isSampled(p) { - continue - } - - done++ - ss.addSample(p) - } + done := 0 + for done < num { + p := &Sample{ + Row: uint32(rand.Int31n(int32(ss.squareWidth))), + Col: uint32(rand.Int31n(int32(ss.squareWidth))), + } + + if ss.isSampled(p) { + continue + } + + done++ + ss.addSample(p) + } } func (ss *squareSampler) sampled() []*Sample { - return ss.samples + return ss.samples } func (ss *squareSampler) addSample(p *Sample) { - ss.samples = append(ss.samples, p) + ss.samples = append(ss.samples, p) } func (ss *squareSampler) isSampled(p *Sample) bool { - for _, sp := range ss.samples { - if sp.Equals(p) { - return true - } - } + for _, sp := range ss.samples { + if sp.Equals(p) { + return true + } + } - return false + return false } diff --git a/p2p/ipld/sample_test.go b/p2p/ipld/sample_test.go index 53448a2ef7..073f47d059 100644 --- a/p2p/ipld/sample_test.go +++ b/p2p/ipld/sample_test.go @@ -7,8 +7,8 @@ import ( ) func TestSampleSquare(t *testing.T) { - tests := []struct{ - width uint32 + tests := []struct { + width uint32 samples int }{ {width: 10, samples: 5}, @@ -19,14 +19,14 @@ func TestSampleSquare(t *testing.T) { ss := SampleSquare(tt.width, tt.samples) assert.Len(t, ss, tt.samples) // check points are within width - for _, s := range ss{ + for _, s := range ss { assert.Less(t, s.Row, tt.width) assert.Less(t, s.Col, tt.width) } // checks samples are not equal - for i, s1 := range ss{ - for j, s2 := range ss{ - if i != j { + for i, s1 := range ss { + for j, s2 := range ss { + if i != j { assert.False(t, s1.Equals(s2)) } }