From 7e8214228b3e9e3fa5ae1a66c233aae85f517163 Mon Sep 17 00:00:00 2001 From: jovijovi Date: Wed, 15 Dec 2021 18:26:44 +0800 Subject: [PATCH] chore: add comments --- merkle.go | 13 +++++++++++++ utils.go | 1 + 2 files changed, 14 insertions(+) diff --git a/merkle.go b/merkle.go index 66ef6e2..4b51e88 100644 --- a/merkle.go +++ b/merkle.go @@ -10,20 +10,25 @@ import ( "golang.org/x/crypto/sha3" ) +// HashProvider hash provider type HashProvider func() hash.Hash +// IHashFunc hash interface type IHashFunc interface { Hash(msg []byte) ([]byte, error) } +// ITree tree interface type ITree interface { BuildTree(opt ...OptionFunc) error } +// HashFunc hash function type HashFunc struct { Provider HashProvider } +// Hash returns message digest func (h *HashFunc) Hash(msg []byte) ([]byte, error) { provider := h.Provider() if _, err := provider.Write(msg); err != nil { @@ -33,16 +38,20 @@ func (h *HashFunc) Hash(msg []byte) ([]byte, error) { return provider.Sum(nil), nil } +// DefaultHashFunc returns default hash interface func DefaultHashFunc() IHashFunc { hashFunc := new(HashFunc) hashFunc.Provider = sha3.NewLegacyKeccak256 return hashFunc } +// Hash node hash type Hash = []byte +// Tree merkle tree type Tree [][]Hash +// Node merkle tree node type Node struct { Height int Hash []byte @@ -51,10 +60,13 @@ type Node struct { Payload []byte } +// Leaf merkle tree leaf type Leaf = Node +// Root merkle tree root type Root = Node +// NewLeaf returns a new leaf func NewLeaf() Leaf { return Leaf{ Height: 0, @@ -82,6 +94,7 @@ func (node *Root) Marshal() ([]byte, error) { return json.Marshal(node) } +// Leaves merkle tree leaves type Leaves []Leaf // Length returns length of leaves diff --git a/utils.go b/utils.go index d677726..6fb23be 100644 --- a/utils.go +++ b/utils.go @@ -4,6 +4,7 @@ import ( "fmt" ) +// Hex returns a hex string func Hex(b []byte) string { return fmt.Sprintf("%x", b) }