Rename MerkleTreeBuilder to CollapsedMerkleTree

This commit is contained in:
Andrew Ayer 2017-01-06 14:39:08 -08:00
parent cf742c121e
commit 9ceedea9ef
4 changed files with 61 additions and 61 deletions

View File

@ -132,7 +132,7 @@ func hashChildren(left ct.MerkleTreeNode, right ct.MerkleTreeNode) ct.MerkleTree
return hasher.Sum(nil) return hasher.Sum(nil)
} }
type MerkleTreeBuilder struct { type CollapsedMerkleTree struct {
stack []ct.MerkleTreeNode stack []ct.MerkleTreeNode
numLeaves uint64 // number of hashes added so far numLeaves uint64 // number of hashes added so far
} }
@ -145,69 +145,69 @@ func calculateStackSize (numLeaves uint64) int {
} }
return stackSize return stackSize
} }
func EmptyMerkleTreeBuilder () *MerkleTreeBuilder { func EmptyCollapsedMerkleTree () *CollapsedMerkleTree {
return &MerkleTreeBuilder{} return &CollapsedMerkleTree{}
} }
func NewMerkleTreeBuilder (stack []ct.MerkleTreeNode, numLeaves uint64) (*MerkleTreeBuilder, error) { func NewCollapsedMerkleTree (stack []ct.MerkleTreeNode, numLeaves uint64) (*CollapsedMerkleTree, error) {
if len(stack) != calculateStackSize(numLeaves) { if len(stack) != calculateStackSize(numLeaves) {
return nil, errors.New("NewMerkleTreeBuilder: incorrect stack size") return nil, errors.New("NewCollapsedMerkleTree: incorrect stack size")
} }
return &MerkleTreeBuilder{stack: stack, numLeaves: numLeaves}, nil return &CollapsedMerkleTree{stack: stack, numLeaves: numLeaves}, nil
} }
func CloneMerkleTreeBuilder (source *MerkleTreeBuilder) *MerkleTreeBuilder { 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 &MerkleTreeBuilder{stack: stack, numLeaves: source.numLeaves} return &CollapsedMerkleTree{stack: stack, numLeaves: source.numLeaves}
} }
func (builder *MerkleTreeBuilder) Add(hash ct.MerkleTreeNode) { func (tree *CollapsedMerkleTree) Add(hash ct.MerkleTreeNode) {
builder.stack = append(builder.stack, hash) tree.stack = append(tree.stack, hash)
builder.numLeaves++ tree.numLeaves++
numLeaves := builder.numLeaves numLeaves := tree.numLeaves
for numLeaves%2 == 0 { for numLeaves%2 == 0 {
left, right := builder.stack[len(builder.stack)-2], builder.stack[len(builder.stack)-1] left, right := tree.stack[len(tree.stack)-2], tree.stack[len(tree.stack)-1]
builder.stack = builder.stack[:len(builder.stack)-2] tree.stack = tree.stack[:len(tree.stack)-2]
builder.stack = append(builder.stack, hashChildren(left, right)) tree.stack = append(tree.stack, hashChildren(left, right))
numLeaves /= 2 numLeaves /= 2
} }
} }
func (builder *MerkleTreeBuilder) CalculateRoot() ct.MerkleTreeNode { func (tree *CollapsedMerkleTree) CalculateRoot() ct.MerkleTreeNode {
if len(builder.stack) == 0 { if len(tree.stack) == 0 {
return hashNothing() return hashNothing()
} }
i := len(builder.stack) - 1 i := len(tree.stack) - 1
hash := builder.stack[i] hash := tree.stack[i]
for i > 0 { for i > 0 {
i -= 1 i -= 1
hash = hashChildren(builder.stack[i], hash) hash = hashChildren(tree.stack[i], hash)
} }
return hash return hash
} }
func (builder *MerkleTreeBuilder) GetNumLeaves() uint64 { func (tree *CollapsedMerkleTree) GetNumLeaves() uint64 {
return builder.numLeaves return tree.numLeaves
} }
func (builder *MerkleTreeBuilder) MarshalJSON() ([]byte, error) { func (tree *CollapsedMerkleTree) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{ return json.Marshal(map[string]interface{}{
"stack": builder.stack, "stack": tree.stack,
"num_leaves": builder.numLeaves, "num_leaves": tree.numLeaves,
}) })
} }
func (builder *MerkleTreeBuilder) UnmarshalJSON(b []byte) error { func (tree *CollapsedMerkleTree) UnmarshalJSON(b []byte) error {
var rawBuilder struct { var rawTree struct {
Stack []ct.MerkleTreeNode `json:"stack"` Stack []ct.MerkleTreeNode `json:"stack"`
NumLeaves uint64 `json:"num_leaves"` NumLeaves uint64 `json:"num_leaves"`
} }
if err := json.Unmarshal(b, &rawBuilder); err != nil { if err := json.Unmarshal(b, &rawTree); err != nil {
return errors.New("Failed to unmarshal MerkleTreeBuilder: " + err.Error()) return errors.New("Failed to unmarshal CollapsedMerkleTree: " + err.Error())
} }
if len(rawBuilder.Stack) != calculateStackSize(rawBuilder.NumLeaves) { if len(rawTree.Stack) != calculateStackSize(rawTree.NumLeaves) {
return errors.New("Failed to unmarshal MerkleTreeBuilder: invalid stack size") return errors.New("Failed to unmarshal CollapsedMerkleTree: invalid stack size")
} }
builder.numLeaves = rawBuilder.NumLeaves tree.numLeaves = rawTree.NumLeaves
builder.stack = rawBuilder.Stack tree.stack = rawTree.Stack
return nil return nil
} }

View File

@ -97,7 +97,7 @@ func loadLogList () ([]certspotter.LogInfo, error) {
type logHandle struct { type logHandle struct {
scanner *certspotter.Scanner scanner *certspotter.Scanner
state *LogState state *LogState
position *certspotter.MerkleTreeBuilder position *certspotter.CollapsedMerkleTree
verifiedSTH *ct.SignedTreeHead verifiedSTH *ct.SignedTreeHead
} }
@ -134,7 +134,7 @@ func makeLogHandle(logInfo *certspotter.LogInfo) (*logHandle, error) {
} }
if legacySTH != nil { if legacySTH != nil {
log.Printf("Initializing log state from legacy state directory") log.Printf("Initializing log state from legacy state directory")
ctlog.position, err = ctlog.scanner.MakeMerkleTreeBuilder(legacySTH) ctlog.position, err = ctlog.scanner.MakeCollapsedMerkleTree(legacySTH)
if err != nil { if err != nil {
return nil, fmt.Errorf("Error reconstructing Merkle Tree for legacy STH: %s", err) return nil, fmt.Errorf("Error reconstructing Merkle Tree for legacy STH: %s", err)
} }
@ -226,18 +226,18 @@ func (ctlog *logHandle) scan (processCallback certspotter.ProcessCallback) error
endIndex := int64(ctlog.verifiedSTH.TreeSize) endIndex := int64(ctlog.verifiedSTH.TreeSize)
if endIndex > startIndex { if endIndex > startIndex {
treeBuilder := certspotter.CloneMerkleTreeBuilder(ctlog.position) tree := certspotter.CloneCollapsedMerkleTree(ctlog.position)
if err := ctlog.scanner.Scan(startIndex, endIndex, processCallback, treeBuilder); err != nil { if err := ctlog.scanner.Scan(startIndex, endIndex, processCallback, tree); err != nil {
return fmt.Errorf("Error scanning log (if this error persists, it should be construed as misbehavior by the log): %s", err) return fmt.Errorf("Error scanning log (if this error persists, it should be construed as misbehavior by the log): %s", err)
} }
rootHash := treeBuilder.CalculateRoot() rootHash := tree.CalculateRoot()
if !bytes.Equal(rootHash, ctlog.verifiedSTH.SHA256RootHash[:]) { if !bytes.Equal(rootHash, ctlog.verifiedSTH.SHA256RootHash[:]) {
return fmt.Errorf("Log has misbehaved: log entries at tree size %d do not correspond to signed tree root", ctlog.verifiedSTH.TreeSize) return fmt.Errorf("Log has misbehaved: log entries at tree size %d do not correspond to signed tree root", ctlog.verifiedSTH.TreeSize)
} }
ctlog.position = treeBuilder ctlog.position = tree
if err := ctlog.state.StoreLogPosition(ctlog.position); err != nil { if err := ctlog.state.StoreLogPosition(ctlog.position); err != nil {
return fmt.Errorf("Error storing log position: %s", err) return fmt.Errorf("Error storing log position: %s", err)
} }
@ -266,7 +266,7 @@ func processLog(logInfo* certspotter.LogInfo, processCallback certspotter.Proces
} }
if *allTime { if *allTime {
ctlog.position = certspotter.EmptyMerkleTreeBuilder() ctlog.position = certspotter.EmptyCollapsedMerkleTree()
if *verbose { if *verbose {
log.Printf("Scanning all %d entries in the log because -all_time option specified", ctlog.verifiedSTH.TreeSize) log.Printf("Scanning all %d entries in the log because -all_time option specified", ctlog.verifiedSTH.TreeSize)
} }
@ -275,7 +275,7 @@ func processLog(logInfo* certspotter.LogInfo, processCallback certspotter.Proces
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.GetNumLeaves())
} }
} else if state.IsFirstRun() { } else if state.IsFirstRun() {
ctlog.position, err = ctlog.scanner.MakeMerkleTreeBuilder(ctlog.verifiedSTH) ctlog.position, err = ctlog.scanner.MakeCollapsedMerkleTree(ctlog.verifiedSTH)
if err != nil { if err != nil {
log.Printf("Error reconstructing Merkle Tree: %s", err) log.Printf("Error reconstructing Merkle Tree: %s", err)
return 1 return 1
@ -284,7 +284,7 @@ func processLog(logInfo* certspotter.LogInfo, processCallback certspotter.Proces
log.Printf("First run of Cert Spotter; not scanning %d existing entries because -all_time option not specified", ctlog.verifiedSTH.TreeSize) log.Printf("First run of Cert Spotter; not scanning %d existing entries because -all_time option not specified", ctlog.verifiedSTH.TreeSize)
} }
} else { } else {
ctlog.position = certspotter.EmptyMerkleTreeBuilder() ctlog.position = certspotter.EmptyCollapsedMerkleTree()
if *verbose { if *verbose {
log.Printf("New log; scanning all %d entries in the log", ctlog.verifiedSTH.TreeSize) log.Printf("New log; scanning all %d entries in the log", ctlog.verifiedSTH.TreeSize)
} }

View File

@ -128,18 +128,18 @@ func (logState *LogState) RemoveUnverifiedSTH (sth *ct.SignedTreeHead) error {
return nil return nil
} }
func (logState *LogState) GetLogPosition () (*certspotter.MerkleTreeBuilder, error) { func (logState *LogState) GetLogPosition () (*certspotter.CollapsedMerkleTree, error) {
builder := new(certspotter.MerkleTreeBuilder) tree := new(certspotter.CollapsedMerkleTree)
if err := readJSONFile(filepath.Join(logState.path, "position"), builder); err != nil { if err := readJSONFile(filepath.Join(logState.path, "position"), tree); err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
return nil, nil return nil, nil
} else { } else {
return nil, err return nil, err
} }
} }
return builder, nil return tree, nil
} }
func (logState *LogState) StoreLogPosition (builder *certspotter.MerkleTreeBuilder) error { func (logState *LogState) StoreLogPosition (tree *certspotter.CollapsedMerkleTree) error {
return writeJSONFile(filepath.Join(logState.path, "position"), builder, 0666) return writeJSONFile(filepath.Join(logState.path, "position"), tree, 0666)
} }

View File

@ -91,7 +91,7 @@ func (s *Scanner) processerJob(id int, entries <-chan ct.LogEntry, processCert P
wg.Done() wg.Done()
} }
func (s *Scanner) fetch(r fetchRange, entries chan<- ct.LogEntry, treeBuilder *MerkleTreeBuilder) error { func (s *Scanner) fetch(r fetchRange, entries chan<- ct.LogEntry, tree *CollapsedMerkleTree) error {
success := false success := false
retries := FETCH_RETRIES retries := FETCH_RETRIES
retryWait := FETCH_RETRY_WAIT retryWait := FETCH_RETRY_WAIT
@ -113,8 +113,8 @@ func (s *Scanner) fetch(r fetchRange, entries chan<- ct.LogEntry, treeBuilder *M
retries = FETCH_RETRIES retries = FETCH_RETRIES
retryWait = FETCH_RETRY_WAIT retryWait = FETCH_RETRY_WAIT
for _, logEntry := range logEntries { for _, logEntry := range logEntries {
if treeBuilder != nil { if tree != nil {
treeBuilder.Add(hashLeaf(logEntry.LeafBytes)) tree.Add(hashLeaf(logEntry.LeafBytes))
} }
logEntry.Index = r.start logEntry.Index = r.start
entries <- logEntry entries <- logEntry
@ -234,9 +234,9 @@ func (s *Scanner) CheckConsistency(first *ct.SignedTreeHead, second *ct.SignedTr
return VerifyConsistencyProof(proof, first, second), nil return VerifyConsistencyProof(proof, first, second), nil
} }
func (s *Scanner) MakeMerkleTreeBuilder(sth *ct.SignedTreeHead) (*MerkleTreeBuilder, error) { func (s *Scanner) MakeCollapsedMerkleTree(sth *ct.SignedTreeHead) (*CollapsedMerkleTree, error) {
if sth.TreeSize == 0 { if sth.TreeSize == 0 {
return &MerkleTreeBuilder{}, nil return &CollapsedMerkleTree{}, nil
} }
entries, err := s.logClient.GetEntries(int64(sth.TreeSize - 1), int64(sth.TreeSize - 1)) entries, err := s.logClient.GetEntries(int64(sth.TreeSize - 1), int64(sth.TreeSize - 1))
@ -248,30 +248,30 @@ func (s *Scanner) MakeMerkleTreeBuilder(sth *ct.SignedTreeHead) (*MerkleTreeBuil
} }
leafHash := hashLeaf(entries[0].LeafBytes) leafHash := hashLeaf(entries[0].LeafBytes)
var builder *MerkleTreeBuilder var tree *CollapsedMerkleTree
if sth.TreeSize > 1 { if sth.TreeSize > 1 {
auditPath, _, err := s.logClient.GetAuditProof(leafHash, sth.TreeSize) auditPath, _, err := s.logClient.GetAuditProof(leafHash, sth.TreeSize)
if err != nil { if err != nil {
return nil, err return nil, err
} }
reverseHashes(auditPath) reverseHashes(auditPath)
builder, err = NewMerkleTreeBuilder(auditPath, sth.TreeSize - 1) tree, err = NewCollapsedMerkleTree(auditPath, sth.TreeSize - 1)
if err != nil { if err != nil {
return nil, fmt.Errorf("Error returned bad audit proof for %x to %d", leafHash, sth.TreeSize) return nil, fmt.Errorf("Error returned bad audit proof for %x to %d", leafHash, sth.TreeSize)
} }
} else { } else {
builder = EmptyMerkleTreeBuilder() tree = EmptyCollapsedMerkleTree()
} }
builder.Add(leafHash) tree.Add(leafHash)
if !bytes.Equal(builder.CalculateRoot(), sth.SHA256RootHash[:]) { if !bytes.Equal(tree.CalculateRoot(), sth.SHA256RootHash[:]) {
return nil, fmt.Errorf("Calculated root hash does not match signed tree head at size %d", sth.TreeSize) return nil, fmt.Errorf("Calculated root hash does not match signed tree head at size %d", sth.TreeSize)
} }
return builder, nil return tree, nil
} }
func (s *Scanner) Scan(startIndex int64, endIndex int64, processCert ProcessCallback, treeBuilder *MerkleTreeBuilder) error { func (s *Scanner) Scan(startIndex int64, endIndex int64, processCert ProcessCallback, tree *CollapsedMerkleTree) error {
s.Log("Starting scan...") s.Log("Starting scan...")
s.certsProcessed = 0 s.certsProcessed = 0
@ -300,7 +300,7 @@ func (s *Scanner) Scan(startIndex int64, endIndex int64, processCert ProcessCall
for start := startIndex; start < int64(endIndex); { for start := startIndex; start < int64(endIndex); {
end := min(start+int64(s.opts.BatchSize), int64(endIndex)) - 1 end := min(start+int64(s.opts.BatchSize), int64(endIndex)) - 1
if err := s.fetch(fetchRange{start, end}, jobs, treeBuilder); err != nil { if err := s.fetch(fetchRange{start, end}, jobs, tree); err != nil {
return err return err
} }
start = end + 1 start = end + 1