Replace MerkleTreeBuilder.Finish with non-mutating CalculateRoot

This commit is contained in:
Andrew Ayer 2016-11-25 17:43:07 -08:00
parent ef18092eb9
commit b63a024876
2 changed files with 9 additions and 8 deletions

View File

@ -147,14 +147,15 @@ func (builder *MerkleTreeBuilder) Add(hash ct.MerkleTreeNode) {
} }
} }
func (builder *MerkleTreeBuilder) Finish() ct.MerkleTreeNode { func (builder *MerkleTreeBuilder) CalculateRoot() ct.MerkleTreeNode {
if len(builder.stack) == 0 { if len(builder.stack) == 0 {
panic("MerkleTreeBuilder.Finish called on an empty tree") panic("MerkleTreeBuilder.CalculateRoot called on an empty tree")
} }
for len(builder.stack) > 1 { i := len(builder.stack) - 1
left, right := builder.stack[len(builder.stack)-2], builder.stack[len(builder.stack)-1] hash := builder.stack[i]
builder.stack = builder.stack[:len(builder.stack)-2] for i > 0 {
builder.stack = append(builder.stack, hashChildren(left, right)) i -= 1
hash = hashChildren(builder.stack[i], hash)
} }
return builder.stack[0] return hash
} }

View File

@ -246,7 +246,7 @@ func Main(argStateDir string, processCallback certspotter.ProcessCallback) int {
continue continue
} }
rootHash := treeBuilder.Finish() rootHash := treeBuilder.CalculateRoot()
if !bytes.Equal(rootHash, latestSTH.SHA256RootHash[:]) { if !bytes.Equal(rootHash, latestSTH.SHA256RootHash[:]) {
log.Printf("Validation of log entries failed - calculated tree root (%x) does not match signed tree root (%s). If this error persists for an extended period, it should be construed as misbehavior by the log.\n", rootHash, latestSTH.SHA256RootHash) log.Printf("Validation of log entries failed - calculated tree root (%x) does not match signed tree root (%s). If this error persists for an extended period, it should be construed as misbehavior by the log.\n", rootHash, latestSTH.SHA256RootHash)
exitCode |= 8 exitCode |= 8