Skip to content

Commit

Permalink
chore(ipld): go fmt && golangci-lint run
Browse files Browse the repository at this point in the history
  • Loading branch information
Wondertan committed Apr 7, 2021
1 parent 3274e50 commit 48ce236
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 67 deletions.
2 changes: 1 addition & 1 deletion p2p/ipld/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions p2p/ipld/read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (

func TestValidateAvailability(t *testing.T) {
const (
msgs = 16
msgs = 16
valShares = 10
)

Expand All @@ -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)
}
Expand Down
116 changes: 58 additions & 58 deletions p2p/ipld/sample.go
Original file line number Diff line number Diff line change
@@ -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
}
12 changes: 6 additions & 6 deletions p2p/ipld/sample_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
)

func TestSampleSquare(t *testing.T) {
tests := []struct{
width uint32
tests := []struct {
width uint32
samples int
}{
{width: 10, samples: 5},
Expand All @@ -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))
}
}
Expand Down

0 comments on commit 48ce236

Please sign in to comment.