CollapsedMerkleTree: rename numLeaves to size

This commit is contained in:
Andrew Ayer 2017-01-06 14:41:51 -08:00
parent 9ceedea9ef
commit 671814cd9b
2 changed files with 21 additions and 21 deletions

View File

@ -134,41 +134,41 @@ func hashChildren(left ct.MerkleTreeNode, right ct.MerkleTreeNode) ct.MerkleTree
type CollapsedMerkleTree struct { type CollapsedMerkleTree struct {
stack []ct.MerkleTreeNode stack []ct.MerkleTreeNode
numLeaves uint64 // number of hashes added so far size uint64
} }
func calculateStackSize (numLeaves uint64) int { func calculateStackSize (size uint64) int {
stackSize := 0 stackSize := 0
for numLeaves > 0 { for size > 0 {
stackSize += int(numLeaves & 1) stackSize += int(size & 1)
numLeaves >>= 1 size >>= 1
} }
return stackSize return stackSize
} }
func EmptyCollapsedMerkleTree () *CollapsedMerkleTree { func EmptyCollapsedMerkleTree () *CollapsedMerkleTree {
return &CollapsedMerkleTree{} return &CollapsedMerkleTree{}
} }
func NewCollapsedMerkleTree (stack []ct.MerkleTreeNode, numLeaves uint64) (*CollapsedMerkleTree, error) { func NewCollapsedMerkleTree (stack []ct.MerkleTreeNode, size uint64) (*CollapsedMerkleTree, error) {
if len(stack) != calculateStackSize(numLeaves) { if len(stack) != calculateStackSize(size) {
return nil, errors.New("NewCollapsedMerkleTree: incorrect stack size") return nil, errors.New("NewCollapsedMerkleTree: incorrect stack size")
} }
return &CollapsedMerkleTree{stack: stack, numLeaves: numLeaves}, nil return &CollapsedMerkleTree{stack: stack, size: size}, nil
} }
func CloneCollapsedMerkleTree (source *CollapsedMerkleTree) *CollapsedMerkleTree { func CloneCollapsedMerkleTree (source *CollapsedMerkleTree) *CollapsedMerkleTree {
stack := make([]ct.MerkleTreeNode, len(source.stack)) stack := make([]ct.MerkleTreeNode, len(source.stack))
copy(stack, source.stack) copy(stack, source.stack)
return &CollapsedMerkleTree{stack: stack, numLeaves: source.numLeaves} return &CollapsedMerkleTree{stack: stack, size: source.size}
} }
func (tree *CollapsedMerkleTree) Add(hash ct.MerkleTreeNode) { func (tree *CollapsedMerkleTree) Add(hash ct.MerkleTreeNode) {
tree.stack = append(tree.stack, hash) tree.stack = append(tree.stack, hash)
tree.numLeaves++ tree.size++
numLeaves := tree.numLeaves size := tree.size
for numLeaves%2 == 0 { for size%2 == 0 {
left, right := tree.stack[len(tree.stack)-2], tree.stack[len(tree.stack)-1] left, right := tree.stack[len(tree.stack)-2], tree.stack[len(tree.stack)-1]
tree.stack = tree.stack[:len(tree.stack)-2] tree.stack = tree.stack[:len(tree.stack)-2]
tree.stack = append(tree.stack, hashChildren(left, right)) tree.stack = append(tree.stack, hashChildren(left, right))
numLeaves /= 2 size /= 2
} }
} }
@ -185,29 +185,29 @@ func (tree *CollapsedMerkleTree) CalculateRoot() ct.MerkleTreeNode {
return hash return hash
} }
func (tree *CollapsedMerkleTree) GetNumLeaves() uint64 { func (tree *CollapsedMerkleTree) GetSize() uint64 {
return tree.numLeaves return tree.size
} }
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, "stack": tree.stack,
"num_leaves": tree.numLeaves, "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"` Stack []ct.MerkleTreeNode `json:"stack"`
NumLeaves uint64 `json:"num_leaves"` 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.NumLeaves) { if len(rawTree.Stack) != calculateStackSize(rawTree.Size) {
return errors.New("Failed to unmarshal CollapsedMerkleTree: invalid stack size") return errors.New("Failed to unmarshal CollapsedMerkleTree: invalid stack size")
} }
tree.numLeaves = rawTree.NumLeaves tree.size = rawTree.Size
tree.stack = rawTree.Stack tree.stack = rawTree.Stack
return nil return nil
} }

View File

@ -222,7 +222,7 @@ func (ctlog *logHandle) audit () error {
} }
func (ctlog *logHandle) scan (processCallback certspotter.ProcessCallback) error { func (ctlog *logHandle) scan (processCallback certspotter.ProcessCallback) error {
startIndex := int64(ctlog.position.GetNumLeaves()) startIndex := int64(ctlog.position.GetSize())
endIndex := int64(ctlog.verifiedSTH.TreeSize) endIndex := int64(ctlog.verifiedSTH.TreeSize)
if endIndex > startIndex { if endIndex > startIndex {
@ -272,7 +272,7 @@ func processLog(logInfo* certspotter.LogInfo, processCallback certspotter.Proces
} }
} else if ctlog.position != nil { } else if ctlog.position != nil {
if *verbose { if *verbose {
log.Printf("Existing log; scanning %d new entries since previous scan", ctlog.verifiedSTH.TreeSize-ctlog.position.GetNumLeaves()) log.Printf("Existing log; scanning %d new entries since previous scan", ctlog.verifiedSTH.TreeSize-ctlog.position.GetSize())
} }
} else if state.IsFirstRun() { } else if state.IsFirstRun() {
ctlog.position, err = ctlog.scanner.MakeCollapsedMerkleTree(ctlog.verifiedSTH) ctlog.position, err = ctlog.scanner.MakeCollapsedMerkleTree(ctlog.verifiedSTH)