Skip to content

Commit

Permalink
try with nmt branch
Browse files Browse the repository at this point in the history
  • Loading branch information
liamsi committed May 13, 2021
1 parent e9ba803 commit 1522021
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 10 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ require (
github.com/ipfs/go-ipfs-api v0.2.0
github.com/ipfs/go-ipfs-config v0.12.0
github.com/ipfs/go-ipld-format v0.2.0
github.com/ipfs/go-log/v2 v2.1.1
github.com/ipfs/go-path v0.0.9 // indirect
github.com/ipfs/go-verifcid v0.0.1
github.com/ipfs/interface-go-ipfs-core v0.4.0
github.com/lazyledger/nmt v0.4.0
github.com/lazyledger/nmt v0.4.1-0.20210513190421-7167fa69c531
github.com/lazyledger/rsmt2d v0.2.0
github.com/libp2p/go-buffer-pool v0.0.2
github.com/libp2p/go-libp2p v0.12.0
github.com/minio/highwayhash v1.0.1
github.com/multiformats/go-multihash v0.0.14
github.com/petermattis/goid v0.0.0-20180202154549-b0b1615b78e5 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -601,8 +601,8 @@ github.com/lazyledger/go-verifcid v0.0.1-lazypatch h1:jAVwUw+DhuCzx5IcYpFh6d6HWx
github.com/lazyledger/go-verifcid v0.0.1-lazypatch/go.mod h1:kXPYu0XqTNUKWA1h3M95UHjUqBzDwXVVt/RXZDjKJmQ=
github.com/lazyledger/merkletree v0.0.0-20201214195110-6901c4c3c75f h1:jbyPAH6o6hGte4RtZBaqWs2n4Fl6hS7qJGXX3qnjiy4=
github.com/lazyledger/merkletree v0.0.0-20201214195110-6901c4c3c75f/go.mod h1:10PA0NlnYtB8HrtwIDQAyTKWp8TEZ0zBZCGlYC/7+QE=
github.com/lazyledger/nmt v0.4.0 h1:RcNmU7nfRtgsmIBN2T+5eOSfuOGLkX3F+cINdWdphrQ=
github.com/lazyledger/nmt v0.4.0/go.mod h1:tY7ypPX26Sbkt6F8EbPl3AT33B5N0BJe4OVPbq849YI=
github.com/lazyledger/nmt v0.4.1-0.20210513190421-7167fa69c531 h1:EeLx8Yool0H+VpLAJCRpfbkE5mufmd1RRkoDG3ftjJs=
github.com/lazyledger/nmt v0.4.1-0.20210513190421-7167fa69c531/go.mod h1:tY7ypPX26Sbkt6F8EbPl3AT33B5N0BJe4OVPbq849YI=
github.com/lazyledger/rsmt2d v0.2.0 h1:RrKkAd9WTewCnOmAtvM5bz05PC+XzIYSrD+3V3odPtg=
github.com/lazyledger/rsmt2d v0.2.0/go.mod h1:EbB1gGbX51gBNm0hC5lMcbkgEyO3Wj2RYwba9mCUvPA=
github.com/libp2p/go-addr-util v0.0.1/go.mod h1:4ac6O7n9rIAKB1dnd+s8IbbMXkt+oBpzX4/+RACcnlQ=
Expand Down
2 changes: 1 addition & 1 deletion p2p/ipld/nmt_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func NewErasuredNamespacedMerkleTree(squareSize uint64, setters ...nmt.Option) E
if squareSize == 0 {
panic("cannot create a ErasuredNamespacedMerkleTree of squareSize == 0")
}
tree := nmt.New(sha256.New(), setters...)
tree := nmt.New(sha256.New, setters...)
return ErasuredNamespacedMerkleTree{squareSize: squareSize, options: setters, tree: tree}
}

Expand Down
2 changes: 1 addition & 1 deletion p2p/ipld/nmt_wrapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestRootErasuredNamespacedMerkleTree(t *testing.T) {
data := generateRandNamespacedRawData(size, types.NamespaceSize, types.MsgShareSize)
n := NewErasuredNamespacedMerkleTree(uint64(size))
tree := n.Constructor()
nmtTree := nmt.New(sha256.New())
nmtTree := nmt.New(sha256.New)

for i, d := range data {
tree.Push(d, rsmt2d.SquareIndex{Axis: uint(0), Cell: uint(i)})
Expand Down
2 changes: 1 addition & 1 deletion p2p/ipld/plugin/nodes/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func DataSquareRowOrColumnRawInputParser(r io.Reader, _mhType uint64, _mhLen int
collector := newNodeCollector()

n := nmt.New(
sha256.New(),
sha256.New,
nmt.NamespaceIDSize(namespaceSize),
nmt.NodeVisitor(collector.visit),
)
Expand Down
114 changes: 113 additions & 1 deletion p2p/ipld/read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/lazyledger/nmt"
"github.com/lazyledger/nmt/namespace"
"github.com/lazyledger/rsmt2d"
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -303,6 +304,117 @@ func TestRetrieveBlockData(t *testing.T) {
}
}

func TestRetrieveBlockData2Nodes(t *testing.T) {
type test struct {
name string
squareSize int
expectErr bool
errStr string
}

// issue two new API objects
net := mocknet.New(context.Background())
ipfsNode1, err := coremock.MockPublicNode(context.Background(), net)
if err != nil {
t.Error(err)
}
ipfsNode2, err := coremock.MockPublicNode(context.Background(), net)
if err != nil {
t.Error(err)
}
ipfsAPI1, err := coreapi.NewCoreAPI(ipfsNode1)
if err != nil {
t.Error(err)
}
ipfsAPI2, err := coreapi.NewCoreAPI(ipfsNode2)
if err != nil {
t.Error(err)
}

err = net.LinkAll()
assert.NoError(t, err)
err = net.ConnectAllButSelf()
assert.NoError(t, err)

// the max size of messages that won't get split
adjustedMsgSize := types.MsgShareSize - 2

tests := []test{
{"Empty block", 1, false, ""},
{"4 KB block", 4, false, ""},
{"16 KB block", 8, false, ""},
{"32 KB block", 16, false, ""},
//{"16 KB block timeout expected", 8, true, "timeout"},
//{"max square size", types.MaxSquareSize, false, ""},
}

for _, tc := range tests {
tc := tc

t.Run(fmt.Sprintf("%s size %d", tc.name, tc.squareSize), func(t *testing.T) {
// if we're using the race detector, skip some large tests due to time and
// concurrency constraints
if raceDetectorActive && tc.squareSize > 8 {
t.Skip("Not running large test due to time and concurrency constraints while race detector is active.")
}

background := context.Background()
blockData := generateRandomBlockData(tc.squareSize*tc.squareSize, adjustedMsgSize)
block := types.Block{
Data: blockData,
LastCommit: &types.Commit{},
}

// if an error is exected, don't put the block
if !tc.expectErr {
err := block.PutBlock(background, ipfsAPI1.Dag())
if err != nil {
t.Fatal(err)
}
}

shareData, _ := blockData.ComputeShares()
rawData := shareData.RawShares()

tree := NewErasuredNamespacedMerkleTree(uint64(tc.squareSize))
eds, err := rsmt2d.ComputeExtendedDataSquare(rawData, rsmt2d.NewRSGF8Codec(), tree.Constructor)
if err != nil {
t.Fatal(err)
}

rawRowRoots := eds.RowRoots()
rawColRoots := eds.ColumnRoots()
rowRoots := rootsToDigests(rawRowRoots)
colRoots := rootsToDigests(rawColRoots)

retrievalCtx, cancel := context.WithTimeout(background, time.Second*2)
defer cancel()

rblockData, err := RetrieveBlockData(
retrievalCtx,
&types.DataAvailabilityHeader{
RowsRoots: rowRoots,
ColumnRoots: colRoots,
},
ipfsAPI2,
rsmt2d.NewRSGF8Codec(),
)

if tc.expectErr {
require.Error(t, err)
require.Contains(t, err.Error(), tc.errStr)
return
}

require.NoError(t, err)

nsShares, _ := rblockData.ComputeShares()

assert.Equal(t, rawData, nsShares.RawShares())
})
}
}

func flatten(eds *rsmt2d.ExtendedDataSquare) [][]byte {
flattenedEDSSize := eds.Width() * eds.Width()
out := make([][]byte, flattenedEDSSize)
Expand All @@ -323,7 +435,7 @@ func getNmtRoot(
namespacedData [][]byte,
) (namespace.IntervalDigest, error) {
na := nodes.NewNmtNodeAdder(ctx, batch)
tree := nmt.New(sha256.New(), nmt.NamespaceIDSize(types.NamespaceSize), nmt.NodeVisitor(na.Visit))
tree := nmt.New(sha256.New, nmt.NamespaceIDSize(types.NamespaceSize), nmt.NodeVisitor(na.Visit))
for _, leaf := range namespacedData {
err := tree.Push(leaf)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ func (b *Block) fillDataAvailabilityHeader() {

// nmtcommitment generates the nmt root of some namespaced data
func nmtCommitment(namespacedData [][]byte) namespace.IntervalDigest {
tree := nmt.New(newBaseHashFunc(), nmt.NamespaceIDSize(NamespaceSize))
tree := nmt.New(newBaseHashFunc, nmt.NamespaceIDSize(NamespaceSize))
for _, leaf := range namespacedData {
mustPush(tree, leaf)
}
Expand Down Expand Up @@ -332,7 +332,7 @@ func (b *Block) PutBlock(ctx context.Context, nodeAdder format.NodeAdder) error

// iterate through each set of col and row leaves
for _, leafSet := range leaves {
tree := nmt.New(sha256.New(), nmt.NodeVisitor(batchAdder.Visit))
tree := nmt.New(sha256.New, nmt.NodeVisitor(batchAdder.Visit))
for _, share := range leafSet {
err = tree.Push(share)
if err != nil {
Expand Down

0 comments on commit 1522021

Please sign in to comment.