Skip to content

Commit

Permalink
Refactor chain indexer to use an address-utxo map per block
Browse files Browse the repository at this point in the history
  • Loading branch information
jdowning100 committed Dec 6, 2024
1 parent 7fb1864 commit 04a2df4
Showing 1 changed file with 27 additions and 32 deletions.
59 changes: 27 additions & 32 deletions core/chain_indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,14 +368,6 @@ func (c *ChainIndexer) indexerLoop(currentHeader *types.WorkObject, qiIndexerCh
continue
}
time1 := time.Since(start)
var validUtxoIndex bool
var addressOutpoints map[[20]byte][]*types.OutpointAndDenomination
var addressLockups map[[20]byte][]*types.Lockup
if c.indexAddressUtxos {
validUtxoIndex = true
addressOutpoints = make(map[[20]byte][]*types.OutpointAndDenomination)
addressLockups = make(map[[20]byte][]*types.Lockup)
}
time2 := time.Since(start)

var time3, time4, time5 time.Duration
Expand Down Expand Up @@ -432,10 +424,9 @@ func (c *ChainIndexer) indexerLoop(currentHeader *types.WorkObject, qiIndexerCh
time3 = time.Since(start)

// Remove all outpoints of the reorg headers (old chain)
err := c.reorgUtxoIndexer(prevHashStack, addressOutpoints, addressLockups, nodeCtx)
err := c.reorgUtxoIndexer(prevHashStack, nodeCtx)
if err != nil {
c.logger.Error("ChainIndexer: Failed to reorg utxo indexer", "err", err)
validUtxoIndex = false
}

time4 = time.Since(start)
Expand All @@ -447,7 +438,7 @@ func (c *ChainIndexer) indexerLoop(currentHeader *types.WorkObject, qiIndexerCh
c.logger.Error("ChainIndexer: Failed to read block during reorg")
continue
}
c.addOutpointsToIndexer(addressOutpoints, addressLockups, nodeCtx, config, block)
c.addOutpointsToIndexer(nodeCtx, config, block)
}
}

Expand All @@ -457,31 +448,15 @@ func (c *ChainIndexer) indexerLoop(currentHeader *types.WorkObject, qiIndexerCh
} else {
time3 = time.Since(start)
if c.indexAddressUtxos {
c.addOutpointsToIndexer(addressOutpoints, addressLockups, nodeCtx, config, block)
c.addOutpointsToIndexer(nodeCtx, config, block)
}
time4 = time.Since(start)
c.newHead(block.NumberU64(nodeCtx), false)
time5 = time.Since(start)
}

if c.indexAddressUtxos && validUtxoIndex {
err := rawdb.WriteAddressOutpoints(c.chainDb, addressOutpoints)
if err != nil {
panic(err)
}
err = rawdb.WriteAddressLockups(c.chainDb, addressLockups)
if err != nil {
panic(err)
}
}

time9 := time.Since(start)

for key, _ := range addressOutpoints {
delete(addressOutpoints, key)
}
addressOutpoints = nil

time10 := time.Since(start)
prevHeader, prevHash = block, block.Hash()

Expand Down Expand Up @@ -821,10 +796,11 @@ func (c *ChainIndexer) removeSectionHead(section uint64) {
}

// addOutpointsToIndexer removes the spent outpoints and adds new utxos to the indexer.
func (c *ChainIndexer) addOutpointsToIndexer(addressOutpointsWithBlockHeight map[[20]byte][]*types.OutpointAndDenomination, addressLockups map[[20]byte][]*types.Lockup, nodeCtx int, config params.ChainConfig, block *types.WorkObject) {
func (c *ChainIndexer) addOutpointsToIndexer(nodeCtx int, config params.ChainConfig, block *types.WorkObject) {

utxos := block.QiTransactions()

addressOutpointsWithBlockHeight := make(map[[20]byte][]*types.OutpointAndDenomination)
addressLockups := make(map[[20]byte][]*types.Lockup)
for _, tx := range utxos {
for _, in := range tx.TxIn() {

Expand Down Expand Up @@ -1044,13 +1020,24 @@ func (c *ChainIndexer) addOutpointsToIndexer(addressOutpointsWithBlockHeight map
}
}
}

err := rawdb.WriteAddressOutpoints(c.chainDb, addressOutpointsWithBlockHeight)
if err != nil {
panic(err)
}
err = rawdb.WriteAddressLockups(c.chainDb, addressLockups)
if err != nil {
panic(err)
}
}

// reorgUtxoIndexer adds back previously removed outpoints and removes newly added outpoints.
// This is done in reverse order from the old header to the common ancestor.
func (c *ChainIndexer) reorgUtxoIndexer(headers []*types.WorkObject, addressOutpoints map[[20]byte][]*types.OutpointAndDenomination, addressLockups map[[20]byte][]*types.Lockup, nodeCtx int) error {
for _, header := range headers {
func (c *ChainIndexer) reorgUtxoIndexer(headers []*types.WorkObject, nodeCtx int) error {

for _, header := range headers {
addressOutpoints := make(map[[20]byte][]*types.OutpointAndDenomination)
addressLockups := make(map[[20]byte][]*types.Lockup)
block := rawdb.ReadWorkObject(c.chainDb, header.NumberU64(nodeCtx), header.Hash(), types.BlockObject)
if block == nil {
c.logger.Errorf("ChainIndexer: Error reading block during reorg hash: %s", block.Hash().String())
Expand Down Expand Up @@ -1180,6 +1167,14 @@ func (c *ChainIndexer) reorgUtxoIndexer(headers []*types.WorkObject, addressOutp
}
}
}
err = rawdb.WriteAddressOutpoints(c.chainDb, addressOutpoints)
if err != nil {
panic(err)
}
err = rawdb.WriteAddressLockups(c.chainDb, addressLockups)
if err != nil {
panic(err)
}
}
return nil
}
Expand Down

0 comments on commit 04a2df4

Please sign in to comment.