Skip to content

Commit

Permalink
refactor!(share/ipld): use nmt.Proof instead of a custom one
Browse files Browse the repository at this point in the history
  • Loading branch information
vgonkivs committed Mar 22, 2023
1 parent 30bf2a4 commit 2e962fd
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 66 deletions.
5 changes: 3 additions & 2 deletions share/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/ipfs/go-cid"
format "github.com/ipfs/go-ipld-format"

"github.com/celestiaorg/nmt"
"github.com/celestiaorg/nmt/namespace"

"github.com/celestiaorg/celestia-node/share/ipld"
Expand Down Expand Up @@ -50,7 +51,7 @@ func GetSharesByNamespace(
root cid.Cid,
nID namespace.ID,
maxShares int,
) ([]Share, *ipld.Proof, error) {
) ([]Share, *nmt.Proof, error) {
ctx, span := tracer.Start(ctx, "get-shares-by-namespace")
defer span.End()

Expand All @@ -68,7 +69,7 @@ func GetSharesByNamespace(
shares[i] = leafToShare(leaf)
}
}
return shares, data.CollectProofs(), err
return shares, data.CollectProof(), err
}

// leafToShare converts an NMT leaf into a Share.
Expand Down
25 changes: 10 additions & 15 deletions share/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"github.com/stretchr/testify/require"

"github.com/celestiaorg/celestia-app/pkg/wrapper"
"github.com/celestiaorg/nmt"
"github.com/celestiaorg/nmt/namespace"
"github.com/celestiaorg/rsmt2d"

Expand Down Expand Up @@ -358,7 +357,7 @@ func TestGetSharesWithProofsByNamespace(t *testing.T) {
rcid := ipld.MustCidFromNamespacedSha256(row)
rowShares, proof, err := GetSharesByNamespace(ctx, bServ, rcid, nID, len(eds.RowRoots()))
require.NoError(t, err)
if rowShares != nil {
if len(rowShares) > 0 {
require.NotNil(t, proof)
// append shares to check integrity later
shares = append(shares, rowShares...)
Expand All @@ -369,23 +368,19 @@ func TestGetSharesWithProofsByNamespace(t *testing.T) {
leaves = append(leaves, append(sh[:NamespaceSize], sh...))
}

proofNodes := make([][]byte, 0, len(proof.Nodes))
for _, n := range proof.Nodes {
proofNodes = append(proofNodes, ipld.NamespacedSha256FromCID(n))
}

// construct new proof
inclusionProof := nmt.NewInclusionProof(
proof.Start,
proof.End,
proofNodes,
ipld.NMTIgnoreMaxNamespace)
// verify namespace
verified := proof.VerifyNamespace(
sha256.New(),
nID,
leaves,
ipld.NamespacedSha256FromCID(rcid))
require.True(t, verified)

// verify inclusion
verified := inclusionProof.VerifyNamespace(
verified = proof.VerifyInclusion(
sha256.New(),
nID,
leaves,
rowShares,
ipld.NamespacedSha256FromCID(rcid))
require.True(t, verified)
}
Expand Down
20 changes: 3 additions & 17 deletions share/getter.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"github.com/celestiaorg/nmt"
"github.com/celestiaorg/nmt/namespace"
"github.com/celestiaorg/rsmt2d"

"github.com/celestiaorg/celestia-node/share/ipld"
)

// Getter interface provides a set of accessors for shares by the Root.
Expand Down Expand Up @@ -44,7 +42,7 @@ func (ns NamespacedShares) Flatten() []Share {
// NamespacedRow represents all shares with proofs within a specific namespace of a single EDS row.
type NamespacedRow struct {
Shares []Share
Proof *ipld.Proof
Proof *nmt.Proof
}

// Verify validates NamespacedShares by checking every row with nmt inclusion proof.
Expand Down Expand Up @@ -78,20 +76,8 @@ func (row *NamespacedRow) verify(rowRoot []byte, nID namespace.ID) bool {
leaves = append(leaves, append(sh[:NamespaceSize], sh...))
}

proofNodes := make([][]byte, 0, len(row.Proof.Nodes))
for _, n := range row.Proof.Nodes {
proofNodes = append(proofNodes, ipld.NamespacedSha256FromCID(n))
}

// construct new proof
inclusionProof := nmt.NewInclusionProof(
row.Proof.Start,
row.Proof.End,
proofNodes,
ipld.NMTIgnoreMaxNamespace)

// verify inclusion
return inclusionProof.VerifyNamespace(
// verify namespace
return row.Proof.VerifyNamespace(
sha256.New(),
nID,
leaves,
Expand Down
22 changes: 15 additions & 7 deletions share/ipld/namespace_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/ipfs/go-cid"
ipld "github.com/ipfs/go-ipld-format"

"github.com/celestiaorg/nmt"
"github.com/celestiaorg/nmt/namespace"
)

Expand Down Expand Up @@ -116,23 +117,30 @@ func (n *NamespaceData) CollectLeaves() []ipld.Node {
return n.leaves[n.bounds.lowest : n.bounds.highest+1]
}

// CollectProofs returns proofs within the bounds in case if `WithProofs` option was passed,
// CollectProof returns proofs within the bounds in case if `WithProofs` option was passed,
// otherwise nil will be returned.
func (n *NamespaceData) CollectProofs() *Proof {
func (n *NamespaceData) CollectProof() *nmt.Proof {
if n.proofs == nil {
return nil
}

// return an empty Proof if leaves are not available
if n.noLeaves() {
return &Proof{}
return &nmt.Proof{}
}

return &Proof{
Start: int(n.bounds.lowest),
End: int(n.bounds.highest) + 1,
Nodes: n.proofs.Nodes(),
nodes := make([][]byte, len(n.proofs.Nodes()))
for i, node := range n.proofs.Nodes() {
nodes[i] = NamespacedSha256FromCID(node)
}

proof := nmt.NewInclusionProof(
int(n.bounds.lowest),
int(n.bounds.highest)+1,
nodes,
NMTIgnoreMaxNamespace,
)
return &proof
}

type fetchedBounds struct {
Expand Down
25 changes: 9 additions & 16 deletions share/p2p/shrexnd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (
"net"
"time"

"github.com/ipfs/go-cid"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/protocol"

"github.com/celestiaorg/go-libp2p-messenger/serde"
"github.com/celestiaorg/nmt"
"github.com/celestiaorg/nmt/namespace"

"github.com/celestiaorg/celestia-node/share"
Expand Down Expand Up @@ -141,22 +141,15 @@ func (c *Client) doRequest(
func convertToNamespacedShares(rows []*pb.Row) (share.NamespacedShares, error) {
shares := make([]share.NamespacedRow, 0, len(rows))
for _, row := range rows {
var proof *ipld.Proof
var proof *nmt.Proof
if row.Proof != nil {
cids := make([]cid.Cid, 0, len(row.Proof.Nodes))
for _, node := range row.Proof.Nodes {
cid, err := cid.Cast(node)
if err != nil {
return nil, fmt.Errorf("casting proofs node to cid: %w", err)
}
cids = append(cids, cid)
}

proof = &ipld.Proof{
Nodes: cids,
Start: int(row.Proof.Start),
End: int(row.Proof.End),
}
tmpProof := nmt.NewInclusionProof(
int(row.Proof.Start),
int(row.Proof.End),
row.Proof.Nodes,
ipld.NMTIgnoreMaxNamespace,
)
proof = &tmpProof
}

shares = append(shares, share.NamespacedRow{
Expand Down
12 changes: 3 additions & 9 deletions share/p2p/shrexnd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,10 @@ func (srv *Server) respondInternalError(stream network.Stream) {
func namespacedSharesToResponse(shares share.NamespacedShares) *pb.GetSharesByNamespaceResponse {
rows := make([]*pb.Row, 0, len(shares))
for _, row := range shares {
// construct proof
nodes := make([][]byte, 0, len(row.Proof.Nodes))
for _, cid := range row.Proof.Nodes {
nodes = append(nodes, cid.Bytes())
}

proof := &pb.Proof{
Start: int64(row.Proof.Start),
End: int64(row.Proof.End),
Nodes: nodes,
Start: int64(row.Proof.Start()),
End: int64(row.Proof.End()),
Nodes: row.Proof.Nodes(),
}

row := &pb.Row{
Expand Down

0 comments on commit 2e962fd

Please sign in to comment.