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 {
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
for numLeaves > 0 {
stackSize += int(numLeaves & 1)
numLeaves >>= 1
for size > 0 {
stackSize += int(size & 1)
size >>= 1
}
return stackSize
}
func EmptyCollapsedMerkleTree () *CollapsedMerkleTree {
return &CollapsedMerkleTree{}
}
func NewCollapsedMerkleTree (stack []ct.MerkleTreeNode, numLeaves uint64) (*CollapsedMerkleTree, error) {
if len(stack) != calculateStackSize(numLeaves) {
func NewCollapsedMerkleTree (stack []ct.MerkleTreeNode, size uint64) (*CollapsedMerkleTree, error) {
if len(stack) != calculateStackSize(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 {
stack := make([]ct.MerkleTreeNode, len(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) {
tree.stack = append(tree.stack, hash)
tree.numLeaves++
numLeaves := tree.numLeaves
for numLeaves%2 == 0 {
tree.size++
size := tree.size
for size%2 == 0 {
left, right := tree.stack[len(tree.stack)-2], tree.stack[len(tree.stack)-1]
tree.stack = tree.stack[:len(tree.stack)-2]
tree.stack = append(tree.stack, hashChildren(left, right))
numLeaves /= 2
size /= 2
}
}
@ -185,29 +185,29 @@ func (tree *CollapsedMerkleTree) CalculateRoot() ct.MerkleTreeNode {
return hash
}
func (tree *CollapsedMerkleTree) GetNumLeaves() uint64 {
return tree.numLeaves
func (tree *CollapsedMerkleTree) GetSize() uint64 {
return tree.size
}
func (tree *CollapsedMerkleTree) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"stack": tree.stack,
"num_leaves": tree.numLeaves,
"size": tree.size,
})
}
func (tree *CollapsedMerkleTree) UnmarshalJSON(b []byte) error {
var rawTree struct {
Stack []ct.MerkleTreeNode `json:"stack"`
NumLeaves uint64 `json:"num_leaves"`
Size uint64 `json:"size"`
}
if err := json.Unmarshal(b, &rawTree); err != nil {
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")
}
tree.numLeaves = rawTree.NumLeaves
tree.size = rawTree.Size
tree.stack = rawTree.Stack
return nil
}

View File

@ -222,7 +222,7 @@ func (ctlog *logHandle) audit () error {
}
func (ctlog *logHandle) scan (processCallback certspotter.ProcessCallback) error {
startIndex := int64(ctlog.position.GetNumLeaves())
startIndex := int64(ctlog.position.GetSize())
endIndex := int64(ctlog.verifiedSTH.TreeSize)
if endIndex > startIndex {
@ -272,7 +272,7 @@ func processLog(logInfo* certspotter.LogInfo, processCallback certspotter.Proces
}
} else if ctlog.position != nil {
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() {
ctlog.position, err = ctlog.scanner.MakeCollapsedMerkleTree(ctlog.verifiedSTH)