Skip to content

Commit

Permalink
feat: batch get outpoint values
Browse files Browse the repository at this point in the history
  • Loading branch information
gazenw committed Jun 10, 2024
1 parent 0172f03 commit f4025e0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 15 deletions.
18 changes: 9 additions & 9 deletions modules/brc20/processor_inscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"golang.org/x/sync/errgroup"
)

func (p *Processor) processInscriptionTx(ctx context.Context, tx *types.Transaction, blockHeader types.BlockHeader, transfersInOutPoints map[wire.OutPoint]map[ordinals.SatPoint][]*entity.InscriptionTransfer) error {
func (p *Processor) processInscriptionTx(ctx context.Context, tx *types.Transaction, blockHeader types.BlockHeader, transfersInOutPoints map[wire.OutPoint]map[ordinals.SatPoint][]*entity.InscriptionTransfer, outpointValues map[wire.OutPoint]uint64) error {
ctx = logger.WithContext(ctx, slogx.String("tx_hash", tx.TxHash.String()))
envelopes := ordinals.ParseEnvelopesFromTx(tx)
inputOutPoints := lo.Map(tx.TxIn, func(txIn *types.TxIn, _ int) wire.OutPoint {
Expand All @@ -31,13 +31,17 @@ func (p *Processor) processInscriptionTx(ctx context.Context, tx *types.Transact
})
// cache outpoint values for future blocks
for outIndex, txOut := range tx.TxOut {
p.outPointValueCache.Add(wire.OutPoint{
outPoint := wire.OutPoint{
Hash: tx.TxHash,
Index: uint32(outIndex),
}, uint64(txOut.Value))
}
p.outPointValueCache.Add(outPoint, uint64(txOut.Value))
outpointValues[outPoint] = uint64(txOut.Value)
}
outPointsWithTransfers := lo.Keys(transfersInOutPoints)
txContainsTransfers := len(lo.Intersect(inputOutPoints, outPointsWithTransfers)) > 0
isCoinbase := tx.TxIn[0].PreviousOutTxHash.IsEqual(&chainhash.Hash{})
if len(envelopes) == 0 && len(transfersInOutPoints) == 0 && !isCoinbase {
if len(envelopes) == 0 && !txContainsTransfers && !isCoinbase {
// no inscription activity, skip
return nil
}
Expand All @@ -50,10 +54,6 @@ func (p *Processor) processInscriptionTx(ctx context.Context, tx *types.Transact
count int
})
idCounter := uint32(0)
inputValues, err := p.getOutPointValues(ctx, inputOutPoints)
if err != nil {
return errors.Wrap(err, "failed to get outpoint values")
}
for i, input := range tx.TxIn {
// skip coinbase inputs since there can't be an inscription in coinbase
if input.PreviousOutTxHash.IsEqual(&chainhash.Hash{}) {
Expand All @@ -64,7 +64,7 @@ func (p *Processor) processInscriptionTx(ctx context.Context, tx *types.Transact
Hash: input.PreviousOutTxHash,
Index: input.PreviousOutIndex,
}
inputValue := inputValues[inputOutPoint]
inputValue := outpointValues[inputOutPoint]

transfersInOutPoint := transfersInOutPoints[inputOutPoint]
for satPoint, transfers := range transfersInOutPoint {
Expand Down
40 changes: 34 additions & 6 deletions modules/brc20/processor_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ import (
func (p *Processor) Process(ctx context.Context, blocks []*types.Block) error {
for _, block := range blocks {
ctx = logger.WithContext(ctx, slogx.Uint64("height", uint64(block.Header.Height)))
logger.DebugContext(ctx, "Processing new block")
p.blockReward = p.getBlockSubsidy(uint64(block.Header.Height))
p.flotsamsSentAsFee = make([]*entity.Flotsam, 0)

// put coinbase tx (first tx) at the end of block
transactions := append(block.Transactions[1:], block.Transactions[0])

var inputOutPoints []wire.OutPoint
for _, tx := range block.Transactions {
for _, tx := range transactions {
for _, txIn := range tx.TxIn {
if txIn.PreviousOutTxHash == (chainhash.Hash{}) {
// skip coinbase input
Expand All @@ -39,16 +41,42 @@ func (p *Processor) Process(ctx context.Context, blocks []*types.Block) error {
})
}
}
// pre-fetch inscriptions in outpoints
transfersInOutPoints, err := p.getInscriptionTransfersInOutPoints(ctx, inputOutPoints)
if err != nil {
return errors.Wrap(err, "failed to get inscriptions in outpoints")
}
logger.DebugContext(ctx, "Got inscriptions in outpoints", slogx.Int("countOutPoints", len(transfersInOutPoints)))
// pre-fetch outpoint values for transactions with inscriptions/envelopes
outPointsToFetchValues := make([]wire.OutPoint, 0)
for _, tx := range transactions {
txInputOutPoints := lo.Map(tx.TxIn, func(txIn *types.TxIn, _ int) wire.OutPoint {
return wire.OutPoint{
Hash: txIn.PreviousOutTxHash,
Index: txIn.PreviousOutIndex,
}
})
envelopes := ordinals.ParseEnvelopesFromTx(tx)
outPointsWithTransfers := lo.Keys(transfersInOutPoints)
txContainsTransfers := len(lo.Intersect(txInputOutPoints, outPointsWithTransfers)) > 0
isCoinbase := tx.TxIn[0].PreviousOutTxHash.IsEqual(&chainhash.Hash{})
if len(envelopes) == 0 && !txContainsTransfers && !isCoinbase {
// no inscription activity, skip tx
continue
}
outPointsToFetchValues = append(outPointsToFetchValues, lo.Map(tx.TxIn, func(txIn *types.TxIn, _ int) wire.OutPoint {
return wire.OutPoint{
Hash: txIn.PreviousOutTxHash,
Index: txIn.PreviousOutIndex,
}
})...)
}
outPointValues, err := p.getOutPointValues(ctx, outPointsToFetchValues)
if err != nil {
return errors.Wrap(err, "failed to get input values")
}

// put coinbase tx (first tx) at the end of block
transactions := append(block.Transactions[1:], block.Transactions[0])
for _, tx := range transactions {
if err := p.processInscriptionTx(ctx, tx, block.Header, transfersInOutPoints); err != nil {
if err := p.processInscriptionTx(ctx, tx, block.Header, transfersInOutPoints, outPointValues); err != nil {
return errors.Wrap(err, "failed to process tx")
}
}
Expand Down

0 comments on commit f4025e0

Please sign in to comment.