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 1728475 commit 12cfa33
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 70 deletions.
4 changes: 2 additions & 2 deletions p2p/ipld/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func ValidateAvailability(

type res struct {
data []byte
err error
err error
}
resCh := make(chan res, len(samples))
for _, s := range samples {
Expand Down Expand Up @@ -91,7 +91,7 @@ func ValidateAvailability(

// the fact that we read the data, already gives us Merkle proof,
// thus the data availability is successfully validated :)
onLeafValidity( r.data)
onLeafValidity(r.data)
case <-ctx.Done():
err := ctx.Err()
if err == context.DeadlineExceeded {
Expand Down
8 changes: 4 additions & 4 deletions p2p/ipld/read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ import (

func TestValidateAvailability(t *testing.T) {
const (
shares = 2
squareSize = 8
shares = 2
squareSize = 8
adjustedMsgSize = types.MsgShareSize - 2
)

Expand Down Expand Up @@ -142,7 +142,7 @@ func TestGetLeafData(t *testing.T) {
data := generateRandNamespacedRawData(16, types.NamespaceSize, types.ShareSize)

// populate IPFS with generated data
rootCid := dataToIpfs(t, ctx, ipfsAPI, data)
rootCid := dataToIpfs(ctx, t, ipfsAPI, data)

// test cases
tests := []test{
Expand Down Expand Up @@ -458,7 +458,7 @@ func mockedIpfsAPI(t *testing.T) iface.CoreAPI {
return ipfsAPI
}

func dataToIpfs(t *testing.T, ctx context.Context, api iface.CoreAPI, data [][]byte) cid.Cid {
func dataToIpfs(ctx context.Context, t *testing.T, api iface.CoreAPI, data [][]byte) cid.Cid {
batch := format.NewBatch(ctx, api.Dag().Pinning())

// create a random tree
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 12cfa33

Please sign in to comment.