Only calculate root hash when needed to verify an STH

This commit is contained in:
Andrew Ayer 2025-06-17 10:45:56 -04:00
parent b87b33a41b
commit b05a66f634

View File

@ -504,12 +504,11 @@ func saveStateWorker(ctx context.Context, config *Config, ctlog *loglist.Log, st
if batch.begin != state.DownloadPosition.Size() { if batch.begin != state.DownloadPosition.Size() {
panic(fmt.Errorf("saveStateWorker: expected batch to start at %d but got %d instead", state.DownloadPosition.Size(), batch.begin)) panic(fmt.Errorf("saveStateWorker: expected batch to start at %d but got %d instead", state.DownloadPosition.Size(), batch.begin))
} }
rootHash := state.DownloadPosition.CalculateRoot()
for { for {
for len(batch.sths) > 0 && batch.sths[0].TreeSize == state.DownloadPosition.Size() { for len(batch.sths) > 0 && batch.sths[0].TreeSize == state.DownloadPosition.Size() {
sth := batch.sths[0] sth := batch.sths[0]
batch.sths = batch.sths[1:] batch.sths = batch.sths[1:]
if sth.RootHash != rootHash { if rootHash := state.DownloadPosition.CalculateRoot(); sth.RootHash != rootHash {
return &verifyEntriesError{ return &verifyEntriesError{
sth: &sth.SignedTreeHead, sth: &sth.SignedTreeHead,
entriesRootHash: rootHash, entriesRootHash: rootHash,
@ -536,7 +535,6 @@ func saveStateWorker(ctx context.Context, config *Config, ctlog *loglist.Log, st
batch.entries = batch.entries[1:] batch.entries = batch.entries[1:]
leafHash := merkletree.HashLeaf(entry.LeafInput()) leafHash := merkletree.HashLeaf(entry.LeafInput())
state.DownloadPosition.Add(leafHash) state.DownloadPosition.Add(leafHash)
rootHash = state.DownloadPosition.CalculateRoot()
} }
if err := config.State.StoreLogState(ctx, ctlog.LogID, state); err != nil { if err := config.State.StoreLogState(ctx, ctlog.LogID, state); err != nil {