CollapsedMerkleTree: rename stack to nodes
This commit is contained in:
parent
671814cd9b
commit
9c3e697231
50
auditing.go
50
auditing.go
|
@ -133,54 +133,54 @@ func hashChildren(left ct.MerkleTreeNode, right ct.MerkleTreeNode) ct.MerkleTree
|
||||||
}
|
}
|
||||||
|
|
||||||
type CollapsedMerkleTree struct {
|
type CollapsedMerkleTree struct {
|
||||||
stack []ct.MerkleTreeNode
|
nodes []ct.MerkleTreeNode
|
||||||
size uint64
|
size uint64
|
||||||
}
|
}
|
||||||
|
|
||||||
func calculateStackSize (size uint64) int {
|
func calculateNumNodes (size uint64) int {
|
||||||
stackSize := 0
|
numNodes := 0
|
||||||
for size > 0 {
|
for size > 0 {
|
||||||
stackSize += int(size & 1)
|
numNodes += int(size & 1)
|
||||||
size >>= 1
|
size >>= 1
|
||||||
}
|
}
|
||||||
return stackSize
|
return numNodes
|
||||||
}
|
}
|
||||||
func EmptyCollapsedMerkleTree () *CollapsedMerkleTree {
|
func EmptyCollapsedMerkleTree () *CollapsedMerkleTree {
|
||||||
return &CollapsedMerkleTree{}
|
return &CollapsedMerkleTree{}
|
||||||
}
|
}
|
||||||
func NewCollapsedMerkleTree (stack []ct.MerkleTreeNode, size uint64) (*CollapsedMerkleTree, error) {
|
func NewCollapsedMerkleTree (nodes []ct.MerkleTreeNode, size uint64) (*CollapsedMerkleTree, error) {
|
||||||
if len(stack) != calculateStackSize(size) {
|
if len(nodes) != calculateNumNodes(size) {
|
||||||
return nil, errors.New("NewCollapsedMerkleTree: incorrect stack size")
|
return nil, errors.New("NewCollapsedMerkleTree: nodes has incorrect size")
|
||||||
}
|
}
|
||||||
return &CollapsedMerkleTree{stack: stack, size: size}, nil
|
return &CollapsedMerkleTree{nodes: nodes, size: size}, nil
|
||||||
}
|
}
|
||||||
func CloneCollapsedMerkleTree (source *CollapsedMerkleTree) *CollapsedMerkleTree {
|
func CloneCollapsedMerkleTree (source *CollapsedMerkleTree) *CollapsedMerkleTree {
|
||||||
stack := make([]ct.MerkleTreeNode, len(source.stack))
|
nodes := make([]ct.MerkleTreeNode, len(source.nodes))
|
||||||
copy(stack, source.stack)
|
copy(nodes, source.nodes)
|
||||||
return &CollapsedMerkleTree{stack: stack, size: source.size}
|
return &CollapsedMerkleTree{nodes: nodes, size: source.size}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tree *CollapsedMerkleTree) Add(hash ct.MerkleTreeNode) {
|
func (tree *CollapsedMerkleTree) Add(hash ct.MerkleTreeNode) {
|
||||||
tree.stack = append(tree.stack, hash)
|
tree.nodes = append(tree.nodes, hash)
|
||||||
tree.size++
|
tree.size++
|
||||||
size := tree.size
|
size := tree.size
|
||||||
for size%2 == 0 {
|
for size%2 == 0 {
|
||||||
left, right := tree.stack[len(tree.stack)-2], tree.stack[len(tree.stack)-1]
|
left, right := tree.nodes[len(tree.nodes)-2], tree.nodes[len(tree.nodes)-1]
|
||||||
tree.stack = tree.stack[:len(tree.stack)-2]
|
tree.nodes = tree.nodes[:len(tree.nodes)-2]
|
||||||
tree.stack = append(tree.stack, hashChildren(left, right))
|
tree.nodes = append(tree.nodes, hashChildren(left, right))
|
||||||
size /= 2
|
size /= 2
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tree *CollapsedMerkleTree) CalculateRoot() ct.MerkleTreeNode {
|
func (tree *CollapsedMerkleTree) CalculateRoot() ct.MerkleTreeNode {
|
||||||
if len(tree.stack) == 0 {
|
if len(tree.nodes) == 0 {
|
||||||
return hashNothing()
|
return hashNothing()
|
||||||
}
|
}
|
||||||
i := len(tree.stack) - 1
|
i := len(tree.nodes) - 1
|
||||||
hash := tree.stack[i]
|
hash := tree.nodes[i]
|
||||||
for i > 0 {
|
for i > 0 {
|
||||||
i -= 1
|
i -= 1
|
||||||
hash = hashChildren(tree.stack[i], hash)
|
hash = hashChildren(tree.nodes[i], hash)
|
||||||
}
|
}
|
||||||
return hash
|
return hash
|
||||||
}
|
}
|
||||||
|
@ -191,23 +191,23 @@ func (tree *CollapsedMerkleTree) GetSize() uint64 {
|
||||||
|
|
||||||
func (tree *CollapsedMerkleTree) MarshalJSON() ([]byte, error) {
|
func (tree *CollapsedMerkleTree) MarshalJSON() ([]byte, error) {
|
||||||
return json.Marshal(map[string]interface{}{
|
return json.Marshal(map[string]interface{}{
|
||||||
"stack": tree.stack,
|
"nodes": tree.nodes,
|
||||||
"size": tree.size,
|
"size": tree.size,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tree *CollapsedMerkleTree) UnmarshalJSON(b []byte) error {
|
func (tree *CollapsedMerkleTree) UnmarshalJSON(b []byte) error {
|
||||||
var rawTree struct {
|
var rawTree struct {
|
||||||
Stack []ct.MerkleTreeNode `json:"stack"`
|
Nodes []ct.MerkleTreeNode `json:"nodes"`
|
||||||
Size uint64 `json:"size"`
|
Size uint64 `json:"size"`
|
||||||
}
|
}
|
||||||
if err := json.Unmarshal(b, &rawTree); err != nil {
|
if err := json.Unmarshal(b, &rawTree); err != nil {
|
||||||
return errors.New("Failed to unmarshal CollapsedMerkleTree: " + err.Error())
|
return errors.New("Failed to unmarshal CollapsedMerkleTree: " + err.Error())
|
||||||
}
|
}
|
||||||
if len(rawTree.Stack) != calculateStackSize(rawTree.Size) {
|
if len(rawTree.Nodes) != calculateNumNodes(rawTree.Size) {
|
||||||
return errors.New("Failed to unmarshal CollapsedMerkleTree: invalid stack size")
|
return errors.New("Failed to unmarshal CollapsedMerkleTree: nodes has incorrect length")
|
||||||
}
|
}
|
||||||
tree.size = rawTree.Size
|
tree.size = rawTree.Size
|
||||||
tree.stack = rawTree.Stack
|
tree.nodes = rawTree.Nodes
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue