-
Notifications
You must be signed in to change notification settings - Fork 84
enable the user to choose a concrete tree implementation #8 #9
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
Changes from all commits
54642a7
51a97d9
34ac38d
589b0b3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package rsmt2d | ||
|
||
import ( | ||
"crypto/sha256" | ||
"fmt" | ||
|
||
"github.com/lazyledger/merkletree" | ||
) | ||
|
||
// TreeConstructorFn creates a fresh Tree instance to be used as the Merkle inside of rsmt2d. | ||
type TreeConstructorFn = func() Tree | ||
|
||
type Tree interface { | ||
Push(data []byte) | ||
// TODO(ismail): is this general enough? | ||
Prove(idx int) (merkleRoot []byte, proofSet [][]byte, proofIndex uint64, numLeaves uint64) | ||
Root() []byte | ||
} | ||
Comment on lines
+13
to
+18
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. Please don't merge yet. Trying to distill a minimal interface. This one is basically nebolouslabs' API (and these are the methods used). |
||
|
||
var _ Tree = &DefaultTree{} | ||
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. How would developers use their own tree implementation for the library? Create a struct that implements Tree, then supply a function that creates a new tree? 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.
Yes, exactly. |
||
|
||
type DefaultTree struct { | ||
*merkletree.Tree | ||
leaves [][]byte | ||
root []byte | ||
} | ||
|
||
func NewDefaultTree() Tree { | ||
return &DefaultTree{ | ||
Tree: merkletree.New(sha256.New()), | ||
leaves: make([][]byte, 0, 128), | ||
} | ||
} | ||
|
||
func (d *DefaultTree) Push(data []byte) { | ||
d.leaves = append(d.leaves, data) | ||
} | ||
|
||
func (d *DefaultTree) Prove(idx int) (merkleRoot []byte, proofSet [][]byte, proofIndex uint64, numLeaves uint64) { | ||
if err := d.Tree.SetIndex(uint64(idx)); err != nil { | ||
panic(fmt.Sprintf("don't call prove on a already used tree: %v", err)) | ||
} | ||
for _, l := range d.leaves { | ||
d.Tree.Push(l) | ||
} | ||
return d.Tree.Prove() | ||
} | ||
|
||
func (d *DefaultTree) Root() []byte { | ||
if d.root == nil { | ||
for _, l := range d.leaves { | ||
d.Tree.Push(l) | ||
} | ||
d.root = d.Tree.Root() | ||
} | ||
return d.root | ||
} |
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.
Compare: https://github.com/lazyledger/nmt/blob/1a5d9124ad10e40d0d743d7695e1983992089a2f/nmt.go#L110
https://github.com/lazyledger/nmt/blob/1a5d9124ad10e40d0d743d7695e1983992089a2f/proof.go#L19