Skip to content

Commit

Permalink
fix: rm redundant storing of roots
Browse files Browse the repository at this point in the history
  • Loading branch information
alainjr10 committed Jul 5, 2024
1 parent a6fa73f commit 90d347a
Show file tree
Hide file tree
Showing 4 changed files with 311 additions and 141 deletions.
2 changes: 0 additions & 2 deletions blockchain/indexers/cfindex.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ var (
// block hashes to cfilters.
cfIndexKeys = [][]byte{
[]byte("cf0byhashidx"), // bucket for basic filter indexes
[]byte("cf1byhashidx"), // bucket for UtreexoCFilter
}

// cfHeaderKeys is an array of db bucket names used to house indexes of
Expand All @@ -48,7 +47,6 @@ var (
// block hashes to cf hashes.
cfHashKeys = [][]byte{
[]byte("cf0hashbyhashidx"),
[]byte("cf1hashbyhashidx"),
}

maxFilterType = uint8(len(cfHeaderKeys) - 1)
Expand Down
19 changes: 18 additions & 1 deletion blockchain/indexers/flatutreexoproofindex.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,24 @@ func (idx *FlatUtreexoProofIndex) ConnectBlock(dbTx database.Tx, block *btcutil.
}
}

return nil
blockHash := block.Hash()
height, err := idx.chain.BlockHeightByHash(blockHash)
if err != nil {
return err
}

roots, leaves, err := idx.FetchUtreexoState(height)
if err != nil {
return err
}

// serialize the hashes of the utreexo roots hash
serializedUtreexo, err := blockchain.SerializeUtreexoRootsHash(leaves, roots)
if err != nil {
return err
}

return storeUtreexoCFilterHeader(dbTx, block, serializedUtreexo, wire.UtreexoCFilter)
}

// calcProofOverhead calculates the overhead of the current utreexo accumulator proof
Expand Down
26 changes: 5 additions & 21 deletions blockchain/indexers/utreexoproofindex.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,34 +238,18 @@ func (idx *UtreexoProofIndex) Create(dbTx database.Tx) error {
return nil
}

// storeFilter stores a given filter, and performs the steps needed to
// generate the filter's header.
func storeUtreexoCFilter(dbTx database.Tx, block *btcutil.Block, filterData []byte,
// storeUtreexoCFilter stores a given utreexocfilter header
func storeUtreexoCFilterHeader(dbTx database.Tx, block *btcutil.Block, filterData []byte,
filterType wire.FilterType) error {
if uint8(filterType) > maxFilterType {
return errors.New("unsupported filter type")
}

// Figure out which buckets to use.
fkey := cfIndexKeys[filterType]
// Figure out which header bucket to use.
hkey := cfHeaderKeys[filterType]
hashkey := cfHashKeys[filterType]

// Start by storing the filter.
h := block.Hash()
err := dbStoreFilterIdxEntry(dbTx, fkey, h, filterData)
if err != nil {
return err
}

// Next store the filter hash.
filterHash := chainhash.DoubleHashH(filterData)
err = dbStoreFilterIdxEntry(dbTx, hashkey, h, filterHash[:])
if err != nil {
return err
}

// Then fetch the previous block's filter header.
// fetch the previous block's filter header.
var prevHeader *chainhash.Hash
ph := &block.MsgBlock().Header.PrevBlock
if ph.IsEqual(&zeroHash) {
Expand Down Expand Up @@ -381,7 +365,7 @@ func (idx *UtreexoProofIndex) ConnectBlock(dbTx database.Tx, block *btcutil.Bloc
return err
}

return storeUtreexoCFilter(dbTx, block, serializedUtreexo, wire.UtreexoCFilter)
return storeUtreexoCFilterHeader(dbTx, block, serializedUtreexo, wire.UtreexoCFilter)
}

// getUndoData returns the data needed for undo. For pruned nodes, we fetch the data from
Expand Down
Loading

0 comments on commit 90d347a

Please sign in to comment.