This is an empirical implementation of Merkle Tree. It's target to skcrach the high extendable, readable coding framework of the Merkle Tree.
Theorectically, Merkle tree could be adapted to different data types, tree structures, and hash functions, etc. Therefore, we adopt follow design to make our implementation extendable.
-
Data type extension friendly
We design the
Data
interface. Any type of content implementing theData
interface could constructure a merkle tree with our implementation.type Data interface { CalculateHash() ([]byte, error) Equals([]byte, error) }
-
Tree structure extension friendly
We design the
Children
slice in node structure to put the children of the node,
so that does not limit the number of children.- Binary Merkle Tree: put left and right child of the node in
Children
successively. - Multiway Merkle Tree: put all children of the node in
Children
successively.
type MerkleNode struct { Children []*MerkleNode // allow Multiway Trees Hash []byte // allow different hash function Data Data }
- Binary Merkle Tree: put left and right child of the node in
-
Hash function extension friendly
We design the byte slice
Hash
in node structure to hold the data digest,
so that does not limit the digest length.
-
Godoc support
Require
godoc
, usego get -v golang.org/x/tools/cmd/godoc
to install it.Developers could read the API document of the whole package
(RepoRoot)$ godoc -http=:8080
then visit
http://localhost:8080/pkg/go-merkle/
in web browser -
Comments in line
All key programming reasons have been commented in line
doc.go
: the independent package overview file for godocmn.go
: the node-level data structures and APIsmt.go
: the tree-level data structures and APIs
- Run all unit tests
(RepoRoot)$ go test .