fix: get all transfers in outpoints for all txs

This commit is contained in:
Gaze
2024-06-10 16:34:46 +07:00
parent b7b4607b6a
commit 5eb2380e4b
2 changed files with 31 additions and 9 deletions

View File

@@ -20,7 +20,7 @@ import (
"golang.org/x/sync/errgroup"
)
func (p *Processor) processInscriptionTx(ctx context.Context, tx *types.Transaction, blockHeader types.BlockHeader) error {
func (p *Processor) processInscriptionTx(ctx context.Context, tx *types.Transaction, blockHeader types.BlockHeader, transfersInOutPoints map[wire.OutPoint]map[ordinals.SatPoint][]*entity.InscriptionTransfer) 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 {
@@ -29,10 +29,6 @@ func (p *Processor) processInscriptionTx(ctx context.Context, tx *types.Transact
Index: txIn.PreviousOutIndex,
}
})
transfersInOutPoints, err := p.getInscriptionTransfersInOutPoints(ctx, inputOutPoints)
if err != nil {
return errors.Wrap(err, "failed to get inscriptions in outpoints")
}
// cache outpoint values for future blocks
for outIndex, txOut := range tx.TxOut {
p.outPointValueCache.Add(wire.OutPoint{
@@ -257,7 +253,7 @@ func (p *Processor) processInscriptionTx(ctx context.Context, tx *types.Transact
}
}
}
if err := p.updateInscriptionLocation(ctx, satPoint, flotsam, sentAsFee, tx, blockHeader); err != nil {
if err := p.updateInscriptionLocation(ctx, satPoint, flotsam, sentAsFee, tx, blockHeader, transfersInOutPoints); err != nil {
return errors.Wrap(err, "failed to update inscription location")
}
}
@@ -270,7 +266,7 @@ func (p *Processor) processInscriptionTx(ctx context.Context, tx *types.Transact
OutPoint: wire.OutPoint{},
Offset: p.lostSats + flotsam.Offset - totalOutputValue,
}
if err := p.updateInscriptionLocation(ctx, newSatPoint, flotsam, false, tx, blockHeader); err != nil {
if err := p.updateInscriptionLocation(ctx, newSatPoint, flotsam, false, tx, blockHeader, transfersInOutPoints); err != nil {
return errors.Wrap(err, "failed to update inscription location")
}
}
@@ -287,7 +283,7 @@ func (p *Processor) processInscriptionTx(ctx context.Context, tx *types.Transact
return nil
}
func (p *Processor) updateInscriptionLocation(ctx context.Context, newSatPoint ordinals.SatPoint, flotsam *entity.Flotsam, sentAsFee bool, tx *types.Transaction, blockHeader types.BlockHeader) error {
func (p *Processor) updateInscriptionLocation(ctx context.Context, newSatPoint ordinals.SatPoint, flotsam *entity.Flotsam, sentAsFee bool, tx *types.Transaction, blockHeader types.BlockHeader, transfersInOutPoints map[wire.OutPoint]map[ordinals.SatPoint][]*entity.InscriptionTransfer) error {
txOut := tx.TxOut[newSatPoint.OutPoint.Index]
if flotsam.OriginOld != nil {
entry, err := p.getInscriptionEntryById(ctx, flotsam.InscriptionId)
@@ -313,6 +309,9 @@ func (p *Processor) updateInscriptionLocation(ctx context.Context, newSatPoint o
// track transfers even if transfer count exceeds 2 (because we need to check for reinscriptions)
p.newInscriptionTransfers = append(p.newInscriptionTransfers, transfer)
p.newInscriptionEntryStates[entry.Id] = entry
// add new transfer to transfersInOutPoints cache
transfersInOutPoints[newSatPoint.OutPoint][newSatPoint] = append(transfersInOutPoints[newSatPoint.OutPoint][newSatPoint], transfer)
return nil
}
@@ -362,6 +361,8 @@ func (p *Processor) updateInscriptionLocation(ctx context.Context, newSatPoint o
p.newInscriptionEntries[entry.Id] = entry
p.newInscriptionEntryStates[entry.Id] = entry
// add new transfer to transfersInOutPoints cache
transfersInOutPoints[newSatPoint.OutPoint][newSatPoint] = append(transfersInOutPoints[newSatPoint.OutPoint][newSatPoint], transfer)
return nil
}
panic("unreachable")

View File

@@ -6,6 +6,8 @@ import (
"encoding/hex"
"slices"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"github.com/cockroachdb/errors"
"github.com/gaze-network/indexer-network/common/errs"
"github.com/gaze-network/indexer-network/core/types"
@@ -24,10 +26,29 @@ func (p *Processor) Process(ctx context.Context, blocks []*types.Block) error {
p.blockReward = p.getBlockSubsidy(uint64(block.Header.Height))
p.flotsamsSentAsFee = make([]*entity.Flotsam, 0)
var inputOutPoints []wire.OutPoint
for _, tx := range block.Transactions {
for _, txIn := range tx.TxIn {
if txIn.PreviousOutTxHash == (chainhash.Hash{}) {
// skip coinbase input
continue
}
inputOutPoints = append(inputOutPoints, wire.OutPoint{
Hash: txIn.PreviousOutTxHash,
Index: txIn.PreviousOutIndex,
})
}
}
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)))
// 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); err != nil {
if err := p.processInscriptionTx(ctx, tx, block.Header, transfersInOutPoints); err != nil {
return errors.Wrap(err, "failed to process tx")
}
}