Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

blockchain, indexers: generate root summaries #250

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 71 additions & 1 deletion blockchain/indexers/flatutreexoproofindex.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package indexers

import (
"bytes"
"crypto/sha256"
"encoding/hex"
"fmt"
"os"
Expand Down Expand Up @@ -93,6 +94,11 @@ type FlatUtreexoProofIndex struct {
// It keeps all the elements of the forest in order to generate proofs.
utreexoState *UtreexoState

// utreexoRootsState is the accumulator for all the roots at each of the
// blocks. This is so that we can serve to peers the proof that a set of
// roots at a block is correct.
utreexoRootsState utreexo.Pollard

// pStats are the proof size statistics that are kept for research purposes.
pStats proofStats

Expand Down Expand Up @@ -166,6 +172,42 @@ func (idx *FlatUtreexoProofIndex) consistentFlatFileState(tipHeight int32) error
return nil
}

// initUtreexoRootsState creates an accumulator from all the existing roots and
// holds it in memory so that the proofs for them can be generated.
func (idx *FlatUtreexoProofIndex) initUtreexoRootsState() error {
idx.utreexoRootsState = utreexo.NewAccumulator()

bestHeight := idx.rootsState.BestHeight()
for h := int32(1); h <= bestHeight; h++ {
stump, err := idx.fetchRoots(h)
if err != nil {
return err
}
bytes, err := blockchain.SerializeUtreexoRoots(stump.NumLeaves, stump.Roots)
if err != nil {
return err
}
rootHash := sha256.Sum256(bytes)

err = idx.utreexoRootsState.Modify(
[]utreexo.Leaf{{Hash: rootHash}}, nil, utreexo.Proof{})
if err != nil {
return err
}
}

bytes, err := blockchain.SerializeUtreexoRoots(
idx.utreexoState.state.GetNumLeaves(),
idx.utreexoState.state.GetRoots(),
)
if err != nil {
return err
}

rootHash := sha256.Sum256(bytes)
return idx.utreexoRootsState.Modify([]utreexo.Leaf{{Hash: rootHash}}, nil, utreexo.Proof{})
}

// Init initializes the flat utreexo proof index. This is part of the Indexer
// interface.
func (idx *FlatUtreexoProofIndex) Init(chain *blockchain.BlockChain,
Expand All @@ -186,6 +228,11 @@ func (idx *FlatUtreexoProofIndex) Init(chain *blockchain.BlockChain,
return err
}

err = idx.initUtreexoRootsState()
if err != nil {
return err
}

// Nothing to do if the node is not pruned.
//
// If the node is pruned, then we need to check if it started off as
Expand Down Expand Up @@ -383,6 +430,11 @@ func (idx *FlatUtreexoProofIndex) ConnectBlock(dbTx database.Tx, block *btcutil.
return err
}

err = idx.updateRootsState()
if err != nil {
return err
}

// Don't store proofs if the node is pruned.
if idx.config.Pruned {
return nil
Expand Down Expand Up @@ -572,7 +624,9 @@ func (idx *FlatUtreexoProofIndex) DisconnectBlock(dbTx database.Tx, block *btcut
return err
}

return nil
// Re-initializes to the current accumulator roots, effectively disconnecting
// a block.
return idx.initUtreexoRootsState()
}

// PruneBlock is invoked when an older block is deleted after it's been
Expand Down Expand Up @@ -862,6 +916,22 @@ func (idx *FlatUtreexoProofIndex) VerifyAccProof(toProve []utreexo.Hash,
return idx.utreexoState.state.Verify(toProve, *proof, false)
}

// updateRootsState updates the roots accumulator state from the roots of the current accumulator.
func (idx *FlatUtreexoProofIndex) updateRootsState() error {
idx.mtx.Lock()
numLeaves := idx.utreexoState.state.GetNumLeaves()
roots := idx.utreexoState.state.GetRoots()
idx.mtx.Unlock()

bytes, err := blockchain.SerializeUtreexoRoots(numLeaves, roots)
if err != nil {
return err
}

rootHash := sha256.Sum256(bytes)
return idx.utreexoRootsState.Modify([]utreexo.Leaf{{Hash: rootHash}}, nil, utreexo.Proof{})
}

// flatFilePath returns the path to the flatfile.
func flatFilePath(dataDir, dataName string) string {
flatFileName := dataName + "_" + flatFileNameSuffix
Expand Down
Loading