From ec098b5991baac2ca75fc68106fde409a59a9a01 Mon Sep 17 00:00:00 2001 From: Siggi Date: Wed, 19 Nov 2025 15:20:14 +0100 Subject: [PATCH] WIP keep track of lowest block id when set tx mined is done --- model/update-tx-mined.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/model/update-tx-mined.go b/model/update-tx-mined.go index 9dded88a8b..1afbd3654b 100644 --- a/model/update-tx-mined.go +++ b/model/update-tx-mined.go @@ -8,6 +8,7 @@ import ( "github.com/bsv-blockchain/go-bt/v2/chainhash" subtreepkg "github.com/bsv-blockchain/go-subtree" + txmap "github.com/bsv-blockchain/go-tx-map" "github.com/bsv-blockchain/teranode/errors" "github.com/bsv-blockchain/teranode/settings" "github.com/bsv-blockchain/teranode/stores/utxo" @@ -177,8 +178,16 @@ func updateTxMinedStatus(ctx context.Context, logger ulogger.Logger, tSettings * blockInvalidError error blockInvalidErrorMu = sync.Mutex{} setMinedErrorCount = atomic.Uint64{} + lowBlockIdsOnChain = txmap.NewSyncedMap[uint32, bool]() ) + lowestBlockID := uint32(0) + for bID := range chainBlockIDsMap { + if lowestBlockID == 0 || bID < lowestBlockID { + lowestBlockID = bID + } + } + for subtreeIdx, subtree := range block.SubtreeSlices { subtreeIdx := subtreeIdx subtree := subtree @@ -245,6 +254,12 @@ func updateTxMinedStatus(ctx context.Context, logger ulogger.Logger, tSettings * blockInvalidError = errors.NewBlockInvalidError("[UpdateTxMinedStatus][%s] block contains a transaction already on our chain: %s, blockID %d", block.Hash().String(), hash.String(), bID) blockInvalidErrorMu.Unlock() } + + if lowestBlockID > bID { + // we cannot determine whether the transaction is on our chain, the blockID is too low + // we need to do a lookup from the DB to be sure + lowBlockIdsOnChain.Set(bID, true) + } } } } @@ -282,6 +297,12 @@ func updateTxMinedStatus(ctx context.Context, logger ulogger.Logger, tSettings * blockInvalidError = errors.NewBlockInvalidError("[UpdateTxMinedStatus][%s] block contains a transaction already on our chain: %s, blockID %d", block.Hash().String(), hash.String(), bID) blockInvalidErrorMu.Unlock() } + + if lowestBlockID > bID { + // we cannot determine whether the transaction is on our chain, the blockID is too low + // we need to do a lookup from the DB to be sure + lowBlockIdsOnChain.Set(bID, true) + } } } } @@ -309,6 +330,12 @@ func updateTxMinedStatus(ctx context.Context, logger ulogger.Logger, tSettings * } } + // check lowBlockIdsOnChain are not on our chain, that would make the block invalid + if lowBlockIdsOnChain.Length() > 0 { + logger.Warnf("[UpdateTxMinedStatus][%s] checking %d low blockIDs for tx on our chain", block.Hash().String(), lowBlockIdsOnChain.Length()) + // TODO: implement a method to check from the DB whether these blockIDs contain any transactions on our chain + } + // if the block was found to be invalid, return that error if blockInvalidError != nil { return blockInvalidError