Skip to content

Commit

Permalink
use NodeAdder instead of the full IPFS API object
Browse files Browse the repository at this point in the history
review feedback
  • Loading branch information
evan-forbes committed Mar 5, 2021
1 parent 3c735de commit 29244a0
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 50 deletions.
2 changes: 1 addition & 1 deletion consensus/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -1079,7 +1079,7 @@ func (cs *State) defaultDecideProposal(height int64, round int32) {
}
// post data to ipfs
// TODO(evan): use some other context
err := block.PutBlock(context.Background(), cs.IpfsAPI)
err := block.PutBlock(context.Background(), cs.IpfsAPI.Dag().Pinning())
if err != nil {
cs.Logger.Error(fmt.Sprintf("failure to post block data to IPFS: %s", err.Error()))
}
Expand Down
2 changes: 2 additions & 0 deletions p2p/ipld/plugin/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,8 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/lazyledger/go-ipfs v0.7.0-lazypatch h1:ejAoRPRg5GC1d2MlK2mp0mmNWQKwqZNADBovAaRcfV0=
github.com/lazyledger/go-ipfs v0.7.0-lazypatch/go.mod h1:WHRSOcXhNUz8Pp0G1CNnlI6oEl0ODIXz/0XcBLw+1Ys=
github.com/lazyledger/go-ipfs v0.8.0-lazypatch h1:8Dkw7Or6d0BmpFYFcxwgqWZ047BPGCsWtG7v9+H0ofk=
github.com/lazyledger/go-ipfs v0.8.0-lazypatch/go.mod h1:CE4cJkjUmwW5LwJP26KKEAZ11ZED0DxzSryfv5RMf6E=
github.com/lazyledger/go-leopard v0.0.0-20200604113236-298f93361181 h1:mUeCGuCgjZVadW4CzA2dMBq7p2BqaoCfpnKjxMmSaSE=
github.com/lazyledger/go-leopard v0.0.0-20200604113236-298f93361181/go.mod h1:v1o1CRihQ9i7hizx23KK4aR79lxA6VDUIzUCfDva0XQ=
github.com/lazyledger/go-verifcid v0.0.1-lazypatch h1:jAVwUw+DhuCzx5IcYpFh6d6HWxRRz8nhJ3rQo+vlFAc=
Expand Down
64 changes: 15 additions & 49 deletions types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import (
"github.com/gogo/protobuf/proto"
gogotypes "github.com/gogo/protobuf/types"
format "github.com/ipfs/go-ipld-format"
ipfsapi "github.com/ipfs/interface-go-ipfs-core"
"github.com/ipfs/interface-go-ipfs-core/path"

"github.com/lazyledger/lazyledger-core/p2p/ipld/plugin/nodes"
"github.com/lazyledger/nmt"
Expand Down Expand Up @@ -264,25 +262,29 @@ func mustPush(rowTree *nmt.NamespacedMerkleTree, id namespace.ID, data []byte) {
}
}

func (b *Block) PutBlock(ctx context.Context, api ipfsapi.CoreAPI) error {
if api == nil {
return errors.New("no ipfs api object provided")
func (b *Block) PutBlock(ctx context.Context, nodeAdder format.NodeAdder) error {
if nodeAdder == nil {
return errors.New("no ipfs node adder provided")
}

// recomputing the erasured data
namespacedShares := b.Data.computeShares()
shares := namespacedShares.RawShares()

fmt.Println("ns shares", len(namespacedShares))

extendedDataSquare, err := rsmt2d.ComputeExtendedDataSquare(shares, rsmt2d.RSGF8, rsmt2d.NewDefaultTree)
if err != nil {
panic(fmt.Sprintf("unexpected error: %v", err))
}

squareWidth := extendedDataSquare.Width()
originalDataWidth := squareWidth / 2

fmt.Println("origianl square width", squareWidth/2)
// add namespaces to erasured shares and chunk into tree sized portions
leaves := make([][][]byte, 2*squareWidth)
// this is adding the namespace back to Q1 shares and the parity ns to the rest
// it's also isolating the leaves
for outerIdx := uint(0); outerIdx < squareWidth; outerIdx++ {
rowLeaves := make([][]byte, squareWidth)
colLeaves := make([][]byte, squareWidth)
Expand All @@ -307,15 +309,19 @@ func (b *Block) PutBlock(ctx context.Context, api ipfsapi.CoreAPI) error {

// create the ipld nodes using the plugin
var allNodes []format.Node
for _, leafSet := range leaves {
for i, leafSet := range leaves {
if len(leafSet) == 0 {
fmt.Println("found empty leaf set!!", i)
continue
}
ipldNodes, err := generateIPLDNodes(leafSet)
if err != nil {
return err
}
allNodes = append(allNodes, ipldNodes...)
}

return format.NewBatch(ctx, pinningAdder{CoreAPI: api}).AddMany(ctx, allNodes)
return format.NewBatch(ctx, nodeAdder).AddMany(ctx, allNodes)
}

func generateIPLDNodes(namespacedLeaves [][]byte) ([]format.Node, error) {
Expand All @@ -340,47 +346,6 @@ func copyOfParityNamespaceID() []byte {
return out
}

// TODO(evan): put pinningAdder somewhere else

// pinningAdder wraps the core ipfs api in order to pin nodes to the local dag
// fulfills the ipld.NodeAdder interface
type pinningAdder struct {
ipfsapi.CoreAPI
}

// Add fulfills the NodeAdder interface by pinning a single ipld nod to
// the local dag
func (p pinningAdder) Add(ctx context.Context, nd format.Node) error {
// add the node to the dag
err := p.Dag().Add(ctx, nd)
if err != nil {
return err
}

// pin the node to the dag
return p.Pin().Add(ctx, path.IpldPath(nd.Cid()))
}

// AddMany fulfills the NodeAdder interface by pinning multiple ipld nodes to
// the local dag
func (p pinningAdder) AddMany(ctx context.Context, nds []format.Node) error {
// add the nodes to the dag
err := p.Dag().AddMany(ctx, nds)
if err != nil {
return err
}

// pin the nodes locally
for _, n := range nds {
err = p.Pin().Add(ctx, path.IpldPath(n.Cid()))
if err != nil {
return err
}
}

return nil
}

// Hash computes and returns the block hash.
// If the block is incomplete, block hash is nil for safety.
func (b *Block) Hash() tmbytes.HexBytes {
Expand Down Expand Up @@ -1340,6 +1305,7 @@ func (msgs Messages) splitIntoShares(shareSize int) NamespacedShares {
if err != nil {
panic(fmt.Sprintf("app accepted a Message that can not be encoded %#v", m))
}
fmt.Println("namespaceID", m.NamespaceID)
shares = appendToShares(shares, m.NamespaceID, rawData, shareSize)
}
return shares
Expand Down
73 changes: 73 additions & 0 deletions types/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package types
import (
// it is ok to use math/rand here: we do not need a cryptographically secure random
// number generator here and we can run the tests a bit faster
stdbytes "bytes"
"context"
"crypto/rand"
"encoding/hex"
"math"
Expand All @@ -12,6 +14,8 @@ import (
"time"

gogotypes "github.com/gogo/protobuf/types"
coreapi "github.com/ipfs/go-ipfs/core/coreapi"
coremock "github.com/ipfs/go-ipfs/core/mock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand All @@ -25,6 +29,7 @@ import (
tmversion "github.com/lazyledger/lazyledger-core/proto/tendermint/version"
tmtime "github.com/lazyledger/lazyledger-core/types/time"
"github.com/lazyledger/lazyledger-core/version"
"github.com/lazyledger/nmt/namespace"
)

func TestMain(m *testing.M) {
Expand Down Expand Up @@ -1306,3 +1311,71 @@ func TestCommit_ValidateBasic(t *testing.T) {
})
}
}

func TestPutBlock(t *testing.T) {
// mock ipfs node
ipfsNode, err := coremock.NewMockNode()
if err != nil {
t.Error(err)
}

ipfsAPI, err := coreapi.NewCoreAPI(ipfsNode)
if err != nil {
t.Error(err)
}

blockData := Data{
Messages: Messages{
MessagesList: []Message{
{
NamespaceID: namespace.ID([]byte{1, 1, 1, 1, 1, 1, 1, 1}),
Data: stdbytes.Repeat([]byte{2}, ShareSize*10),
},
// {
// NamespaceID: []byte{1, 1, 1, 1, 1, 1, 1, 1},
// Data: stdbytes.Repeat([]byte{2}, ShareSize*4),
// },
// {
// NamespaceID: []byte{1, 1, 1, 1, 1, 1, 1, 1},
// Data: stdbytes.Repeat([]byte{2}, ShareSize*4),
// },
// {
// NamespaceID: []byte{1, 1, 1, 1, 1, 1, 1, 1},
// Data: stdbytes.Repeat([]byte{2}, ShareSize*4),
// },
},
},
}

testCases := []struct {
name string
blockData Data
expectErr bool
errString string
}{
{
name: "basic",
blockData: blockData,
expectErr: false,
},
}
ctx := context.Background()
for _, tc := range testCases {
tc := tc

block := &Block{Data: tc.blockData}

t.Run(tc.name, func(t *testing.T) {
err = block.PutBlock(ctx, ipfsAPI.Dag().Pinning())
if tc.expectErr {
require.Error(t, err)
require.Contains(t, err.Error(), tc.errString)
} else {
require.NoError(t, err)
}

// // check if the block is pinned to IPFS
// ipfsAPI.Pin().IsPinned(ctx, path.IpldPath())
})
}
}

0 comments on commit 29244a0

Please sign in to comment.