-
Notifications
You must be signed in to change notification settings - Fork 267
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement GetLeafData as described in ADR 002 #212
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,96 @@ | ||
package ipld | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"math" | ||
|
||
"github.com/ipfs/go-cid" | ||
coreiface "github.com/ipfs/interface-go-ipfs-core" | ||
"github.com/ipfs/interface-go-ipfs-core/path" | ||
) | ||
|
||
// ///////////////////////////////////// | ||
// Get Leaf Data | ||
// ///////////////////////////////////// | ||
|
||
// GetLeafData fetches and returns the data for leaf leafIndex of root rootCid. | ||
// It stops and returns an error if the provided context is cancelled before | ||
// finishing | ||
func GetLeafData( | ||
ctx context.Context, | ||
rootCid cid.Cid, | ||
leafIndex uint32, | ||
totalLeafs uint32, // this corresponds to the extended square width | ||
api coreiface.CoreAPI, | ||
) ([]byte, error) { | ||
// calculate the path to the leaf | ||
leafPath, err := leafPath(leafIndex, totalLeafs) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// use the root cid and the leafPath to create an ipld path | ||
p := path.Join(path.IpldPath(rootCid), leafPath...) | ||
|
||
// resolve the path | ||
node, err := api.ResolveNode(ctx, p) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// return the leaf, without the nmt-leaf-or-node byte | ||
return node.RawData()[1:], nil | ||
} | ||
|
||
func leafPath(index, total uint32) ([]string, error) { | ||
// ensure that the total is a power of two | ||
if total != nextPowerOf2(total) { | ||
return nil, errors.New("expected total to be a power of 2") | ||
} | ||
|
||
if total == 0 { | ||
return nil, nil | ||
} | ||
|
||
depth := int(math.Log2(float64(total))) | ||
cursor := index | ||
path := make([]string, depth) | ||
for i := depth - 1; i >= 0; i-- { | ||
if cursor%2 == 0 { | ||
path[i] = "0" | ||
} else { | ||
path[i] = "1" | ||
} | ||
cursor /= 2 | ||
} | ||
|
||
return path, nil | ||
} | ||
|
||
// nextPowerOf2 returns the next lowest power of 2 unless the input is a power | ||
// of two, in which case it returns the input | ||
func nextPowerOf2(v uint32) uint32 { | ||
if v == 1 { | ||
return 1 | ||
} | ||
// keep track of the input | ||
i := v | ||
|
||
// find the next highest power using bit mashing | ||
v-- | ||
v |= v >> 1 | ||
v |= v >> 2 | ||
v |= v >> 4 | ||
v |= v >> 8 | ||
v |= v >> 16 | ||
v++ | ||
|
||
// check if the input was the next highest power | ||
if i == v { | ||
return v | ||
} | ||
|
||
// return the next lowest power | ||
return v / 2 | ||
} |
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,201 @@ | ||
package ipld | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"crypto/rand" | ||
"crypto/sha256" | ||
"sort" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
cid "github.com/ipfs/go-cid" | ||
"github.com/ipfs/go-ipfs/core/coreapi" | ||
|
||
coremock "github.com/ipfs/go-ipfs/core/mock" | ||
format "github.com/ipfs/go-ipld-format" | ||
"github.com/lazyledger/lazyledger-core/p2p/ipld/plugin/nodes" | ||
"github.com/lazyledger/lazyledger-core/types" | ||
"github.com/lazyledger/nmt" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestLeafPath(t *testing.T) { | ||
type test struct { | ||
name string | ||
index, total uint32 | ||
expected []string | ||
} | ||
|
||
// test cases | ||
tests := []test{ | ||
{"nil", 0, 0, []string(nil)}, | ||
{"0 index 16 total leaves", 0, 16, strings.Split("0/0/0/0", "/")}, | ||
{"1 index 16 total leaves", 1, 16, strings.Split("0/0/0/1", "/")}, | ||
{"9 index 16 total leaves", 9, 16, strings.Split("1/0/0/1", "/")}, | ||
{"15 index 16 total leaves", 15, 16, strings.Split("1/1/1/1", "/")}, | ||
} | ||
|
||
for _, tt := range tests { | ||
tt := tt | ||
t.Run(tt.name, func(t *testing.T) { | ||
result, err := leafPath(tt.index, tt.total) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
assert.Equal(t, tt.expected, result) | ||
}, | ||
) | ||
} | ||
} | ||
|
||
func TestNextPowerOf2(t *testing.T) { | ||
type test struct { | ||
input uint32 | ||
expected uint32 | ||
} | ||
tests := []test{ | ||
{ | ||
input: 2, | ||
expected: 2, | ||
}, | ||
{ | ||
input: 11, | ||
expected: 8, | ||
}, | ||
{ | ||
input: 511, | ||
expected: 256, | ||
}, | ||
{ | ||
input: 1, | ||
expected: 1, | ||
}, | ||
{ | ||
input: 0, | ||
expected: 0, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
res := nextPowerOf2(tt.input) | ||
assert.Equal(t, tt.expected, res) | ||
} | ||
} | ||
|
||
func TestGetLeafData(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yay 👍🏼 |
||
type test struct { | ||
name string | ||
timeout time.Duration | ||
rootCid cid.Cid | ||
leaves [][]byte | ||
} | ||
|
||
// create a mock node | ||
ipfsNode, err := coremock.NewMockNode() | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
// issue a new API object | ||
ipfsAPI, err := coreapi.NewCoreAPI(ipfsNode) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
// create the context and batch needed for node collection from the tree | ||
ctx := context.Background() | ||
batch := format.NewBatch(ctx, ipfsAPI.Dag().Pinning()) | ||
|
||
// generate random data for the nmt | ||
data := generateRandNamespacedRawData(16, types.NamespaceSize, types.ShareSize) | ||
|
||
// create a random tree | ||
tree, err := createNmtTree(ctx, batch, data) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
// calculate the root | ||
root := tree.Root() | ||
|
||
// commit the data to IPFS | ||
err = batch.Commit() | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
// compute the root and create a cid for the root hash | ||
rootCid, err := nodes.CidFromNamespacedSha256(root.Bytes()) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
// test cases | ||
tests := []test{ | ||
{"16 leaves", time.Second, rootCid, data}, | ||
} | ||
|
||
for _, tt := range tests { | ||
tt := tt | ||
t.Run(tt.name, func(t *testing.T) { | ||
ctx, cancel := context.WithTimeout(context.Background(), tt.timeout) | ||
defer cancel() | ||
for i, leaf := range tt.leaves { | ||
data, err := GetLeafData(ctx, tt.rootCid, uint32(i), uint32(len(tt.leaves)), ipfsAPI) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
assert.Equal(t, leaf, data) | ||
} | ||
}, | ||
) | ||
} | ||
} | ||
|
||
// nmtcommitment generates the nmt root of some namespaced data | ||
func createNmtTree( | ||
ctx context.Context, | ||
batch *format.Batch, | ||
namespacedData [][]byte, | ||
) (*nmt.NamespacedMerkleTree, error) { | ||
na := nodes.NewNmtNodeAdder(ctx, batch) | ||
tree := nmt.New(sha256.New(), nmt.NamespaceIDSize(types.NamespaceSize), nmt.NodeVisitor(na.Visit)) | ||
for _, leaf := range namespacedData { | ||
err := tree.Push(leaf[:types.NamespaceSize], leaf[types.NamespaceSize:]) | ||
if err != nil { | ||
return tree, err | ||
} | ||
} | ||
|
||
return tree, nil | ||
} | ||
|
||
// this code is copy pasted from the plugin, and should likely be exported in the plugin instead | ||
func generateRandNamespacedRawData(total int, nidSize int, leafSize int) [][]byte { | ||
data := make([][]byte, total) | ||
for i := 0; i < total; i++ { | ||
nid := make([]byte, nidSize) | ||
_, err := rand.Read(nid) | ||
if err != nil { | ||
panic(err) | ||
} | ||
data[i] = nid | ||
} | ||
|
||
sortByteArrays(data) | ||
for i := 0; i < total; i++ { | ||
d := make([]byte, leafSize) | ||
_, err := rand.Read(d) | ||
if err != nil { | ||
panic(err) | ||
} | ||
data[i] = append(data[i], d...) | ||
} | ||
|
||
return data | ||
} | ||
|
||
func sortByteArrays(src [][]byte) { | ||
sort.Slice(src, func(i, j int) bool { return bytes.Compare(src[i], src[j]) < 0 }) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ResolveNode
is what will actually go and download the node (or retrieve it from the blockstore), right? The confusing bit is thatIpldPath
internally constructs something calledresolvedPath
: https://github.com/ipfs/interface-go-ipfs-core/blob/b935dfe5375eac7ea3c65b14b3f9a0242861d0b3/path/path.go#L133There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had to double check for the same reasons you mentioned. yeah, the tests in
TestGetLeafData
check this, and the docs.