From e91d7bacbd542721ab567e8817cad0630a8a927b Mon Sep 17 00:00:00 2001 From: Andrew Ayer Date: Thu, 18 Feb 2016 10:23:07 -0800 Subject: [PATCH] Minor cleanup to improve encapsulation --- auditing.go | 11 ++--------- cmd/common.go | 10 ++++------ scanner.go | 10 +++++----- 3 files changed, 11 insertions(+), 20 deletions(-) diff --git a/auditing.go b/auditing.go index 065564c..2d94f27 100644 --- a/auditing.go +++ b/auditing.go @@ -13,7 +13,7 @@ func reverseHashes (hashes []ct.MerkleTreeNode) { } } -func VerifyConsistencyProof (proof ct.ConsistencyProof, first *ct.SignedTreeHead, second *ct.SignedTreeHead) (bool, []ct.MerkleTreeNode) { +func VerifyConsistencyProof (proof ct.ConsistencyProof, first *ct.SignedTreeHead, second *ct.SignedTreeHead) (bool, *MerkleTreeBuilder) { if second.TreeSize < first.TreeSize { // Can't be consistent if tree got smaller return false, nil @@ -97,7 +97,7 @@ func VerifyConsistencyProof (proof ct.ConsistencyProof, first *ct.SignedTreeHead reverseHashes(leftHashes) - return true, leftHashes + return true, &MerkleTreeBuilder{stack: leftHashes, size: first.TreeSize} } func hashLeaf (leafBytes []byte) ct.MerkleTreeNode { @@ -120,13 +120,6 @@ type MerkleTreeBuilder struct { size uint64 // number of hashes added so far } -func ResumedMerkleTreeBuilder (hashes []ct.MerkleTreeNode, size uint64) *MerkleTreeBuilder { - return &MerkleTreeBuilder{ - stack: hashes, - size: size, - } -} - func (builder *MerkleTreeBuilder) Add (hash ct.MerkleTreeNode) { builder.stack = append(builder.stack, hash) builder.size++ diff --git a/cmd/common.go b/cmd/common.go index 7fd568c..e3342eb 100644 --- a/cmd/common.go +++ b/cmd/common.go @@ -13,7 +13,6 @@ import ( "path/filepath" "src.agwa.name/ctwatch" - "github.com/google/certificate-transparency/go/client" ) var batchSize = flag.Int("batch_size", 1000, "Max number of entries to request at per call to get-entries") @@ -133,14 +132,13 @@ func Main (argStateDir string, processCallback ctwatch.ProcessCallback) { os.Exit(3) } - logClient := client.New(logUri) opts := ctwatch.ScannerOptions{ BatchSize: *batchSize, NumWorkers: *numWorkers, ParallelFetch: *parallelFetch, Quiet: !*verbose, } - scanner := ctwatch.NewScanner(logUri, logKey, logClient, opts) + scanner := ctwatch.NewScanner(logUri, logKey, opts) latestSTH, err := scanner.GetSTH() if err != nil { @@ -161,7 +159,9 @@ func Main (argStateDir string, processCallback ctwatch.ProcessCallback) { if latestSTH.TreeSize > startIndex { var treeBuilder *ctwatch.MerkleTreeBuilder if prevSTH != nil { - valid, nodes, err := scanner.CheckConsistency(prevSTH, latestSTH) + var valid bool + var err error + valid, treeBuilder, err = scanner.CheckConsistency(prevSTH, latestSTH) if err != nil { fmt.Fprintf(os.Stderr, "%s: Error fetching consistency proof: %s: %s\n", os.Args[0], logUri, err) exitCode = 1 @@ -172,8 +172,6 @@ func Main (argStateDir string, processCallback ctwatch.ProcessCallback) { exitCode = 1 continue } - - treeBuilder = ctwatch.ResumedMerkleTreeBuilder(nodes, prevSTH.TreeSize) } else { treeBuilder = &ctwatch.MerkleTreeBuilder{} } diff --git a/scanner.go b/scanner.go index 7345008..e11318c 100644 --- a/scanner.go +++ b/scanner.go @@ -185,7 +185,7 @@ func (s *Scanner) GetSTH() (*ct.SignedTreeHead, error) { return latestSth, nil } -func (s *Scanner) CheckConsistency(first *ct.SignedTreeHead, second *ct.SignedTreeHead) (bool, []ct.MerkleTreeNode, error) { +func (s *Scanner) CheckConsistency(first *ct.SignedTreeHead, second *ct.SignedTreeHead) (bool, *MerkleTreeBuilder, error) { var proof ct.ConsistencyProof if first.TreeSize > second.TreeSize { @@ -203,8 +203,8 @@ func (s *Scanner) CheckConsistency(first *ct.SignedTreeHead, second *ct.SignedTr } } - valid, builderNodes := VerifyConsistencyProof(proof, first, second) - return valid, builderNodes, nil + valid, treeBuilder := VerifyConsistencyProof(proof, first, second) + return valid, treeBuilder, nil } func (s *Scanner) Scan(startIndex int64, endIndex int64, processCert ProcessCallback, treeBuilder *MerkleTreeBuilder) error { @@ -268,11 +268,11 @@ func (s *Scanner) Scan(startIndex int64, endIndex int64, processCert ProcessCall // Creates a new Scanner instance using |client| to talk to the log, and taking // configuration options from |opts|. -func NewScanner(logUri string, publicKey crypto.PublicKey, client *client.LogClient, opts ScannerOptions) *Scanner { +func NewScanner(logUri string, publicKey crypto.PublicKey, opts ScannerOptions) *Scanner { var scanner Scanner scanner.LogUri = logUri scanner.publicKey = publicKey - scanner.logClient = client + scanner.logClient = client.New(logUri) scanner.opts = opts return &scanner }