-
Notifications
You must be signed in to change notification settings - Fork 925
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
319 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package share | ||
|
||
import ( | ||
"context" | ||
"github.com/celestiaorg/celestia-node/share/ipld" | ||
"github.com/celestiaorg/nmt" | ||
"github.com/celestiaorg/nmt/namespace" | ||
"github.com/ipfs/go-blockservice" | ||
"github.com/ipfs/go-cid" | ||
) | ||
|
||
// ShareWithProof contains data with corresponding Merkle Proof | ||
type SharesWithProofs struct { | ||
// Share is a full data including namespace | ||
Shares []Share | ||
// Proof is a Merkle Proof of current share | ||
Proof nmt.Proof | ||
} | ||
|
||
// GetSharesWithProofsByNamespace walks the tree of a given root and returns its shares within the given namespace.ID. | ||
// If a share could not be retrieved, err is not nil, and the returned array | ||
// contains nil shares in place of the shares it was unable to retrieve. | ||
func GetSharesWithProofsByNamespace( | ||
ctx context.Context, | ||
bGetter blockservice.BlockGetter, | ||
root cid.Cid, | ||
nID namespace.ID, | ||
maxShares int, | ||
) (*SharesWithProofs, error) { | ||
ctx, span := tracer.Start(ctx, "get-shares-by-namespace") | ||
defer span.End() | ||
|
||
nodes, err := ipld.GetLeavesWithProofsByNamespace(ctx, bGetter, root, nID, maxShares) | ||
if nodes == nil { | ||
return nil, err | ||
} | ||
|
||
shares := make([]Share, 0, nodes.ProofEnd-nodes.ProofStart) | ||
leafs := make([][]byte, 0, nodes.ProofEnd-nodes.ProofStart) | ||
for _, leaf := range nodes.Leaves { | ||
if leaf != nil { | ||
shares = append(shares, leafToShare(leaf)) | ||
leafs = append(leafs, leaf.RawData()) | ||
} | ||
} | ||
|
||
// pack proofs for nmt.Proof | ||
rangeProofs := make([][]byte, 0, len(nodes.Proofs)) | ||
for i := len(nodes.Proofs) - 1; i >= 0; i-- { | ||
if nodes.Proofs[i] != nil { | ||
rangeProofs = append(rangeProofs, nodes.Proofs[i]) | ||
} | ||
|
||
} | ||
|
||
return &SharesWithProofs{ | ||
Shares: shares, | ||
Proof: nmt.NewInclusionProof(nodes.ProofStart, nodes.ProofEnd, rangeProofs, true), | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package share | ||
|
||
import ( | ||
"context" | ||
"github.com/celestiaorg/celestia-node/share/ipld" | ||
mdutils "github.com/ipfs/go-merkledag/test" | ||
"github.com/minio/sha256-simd" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"math/rand" | ||
"strconv" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestGetSharesWithProofsByNamespace(t *testing.T) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
bServ := mdutils.Bserv() | ||
|
||
var tests = []struct { | ||
rawData []Share | ||
}{ | ||
{rawData: RandShares(t, 4)}, | ||
{rawData: RandShares(t, 16)}, | ||
{rawData: RandShares(t, 64)}, | ||
} | ||
|
||
for i, tt := range tests { | ||
t.Run(strconv.Itoa(i), func(t *testing.T) { | ||
rand.Seed(time.Now().UnixNano()) | ||
// choose random range in shares f | ||
from := rand.Intn(len(tt.rawData) - 1) | ||
to := rand.Intn(len(tt.rawData) - 1) | ||
|
||
// | ||
expected := tt.rawData[from] | ||
nID := expected[:NamespaceSize] | ||
|
||
if to < from { | ||
tmp := from | ||
from, to = to, tmp | ||
} | ||
|
||
// change rawData to contain several shares with same nID | ||
for i := from; i <= to; i++ { | ||
tt.rawData[i] = expected | ||
} | ||
|
||
// put raw data in BlockService | ||
eds, err := AddShares(ctx, tt.rawData, bServ) | ||
require.NoError(t, err) | ||
|
||
var shares []Share | ||
for _, row := range eds.RowRoots() { | ||
rcid := ipld.MustCidFromNamespacedSha256(row) | ||
rowShares, err := GetSharesWithProofsByNamespace(ctx, bServ, rcid, nID, len(eds.RowRoots())) | ||
require.NoError(t, err) | ||
if rowShares != nil { | ||
//append shares to check integrity later | ||
shares = append(shares, rowShares.Shares...) | ||
|
||
// construct nodes from shares by prepending namespace | ||
var leafs [][]byte | ||
for _, sh := range rowShares.Shares { | ||
leafs = append(leafs, append(sh[:NamespaceSize], sh...)) | ||
} | ||
|
||
// validate proof | ||
verified := rowShares.Proof.VerifyNamespace( | ||
sha256.New(), | ||
nID, | ||
leafs, | ||
ipld.NamespacedSha256FromCID(rcid)) | ||
require.True(t, verified) | ||
} | ||
} | ||
|
||
// validate shares | ||
assert.Equal(t, to-from+1, len(shares)) | ||
for _, share := range shares { | ||
assert.Equal(t, expected, share) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters