From 5dbd5eb3914589d90814a61a88220918a24de2a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Lewandowski?= Date: Fri, 20 Oct 2023 09:09:37 +0200 Subject: [PATCH] chore(BUX-298): try to sync tx again if there is no merle proof --- model_sync_transactions.go | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/model_sync_transactions.go b/model_sync_transactions.go index 68405eb3..0ebdb323 100644 --- a/model_sync_transactions.go +++ b/model_sync_transactions.go @@ -4,6 +4,7 @@ import ( "context" "database/sql" "encoding/hex" + "encoding/json" "errors" "fmt" "runtime" @@ -640,6 +641,19 @@ func processSyncTransaction(ctx context.Context, syncTx *SyncTransaction, transa return err } + // Get the transaction + if transaction == nil { + if transaction, err = getTransactionByID( + ctx, "", syncTx.ID, syncTx.GetOptions(false)..., + ); err != nil { + return err + } + } + + if transaction == nil { + return ErrMissingTransaction + } + // Find on-chain var txInfo *chainstate.TransactionInfo // only mAPI currently provides merkle proof, so QueryTransaction should be used here @@ -647,6 +661,8 @@ func processSyncTransaction(ctx context.Context, syncTx *SyncTransaction, transa ctx, syncTx.ID, chainstate.RequiredOnChain, defaultQueryTxTimeout, ); err != nil { if errors.Is(err, chainstate.ErrTransactionNotFound) { + syncTx.client.Logger().Info(ctx, fmt.Sprintf("processSyncTransaction(): Transaction %s not found on-chain, will try again later", syncTx.ID)) + bailAndSaveSyncTransaction( ctx, syncTx, SyncStatusReady, syncActionSync, "all", "transaction not found on-chain", ) @@ -655,17 +671,15 @@ func processSyncTransaction(ctx context.Context, syncTx *SyncTransaction, transa return err } - // Get the transaction - if transaction == nil { - if transaction, err = getTransactionByID( - ctx, "", syncTx.ID, syncTx.GetOptions(false)..., - ); err != nil { - return err - } - } + // validate txInfo + if txInfo.BlockHash == "" || txInfo.MerkleProof == nil || txInfo.MerkleProof.TxOrID == "" || len(txInfo.MerkleProof.Nodes) == 0 { + syncTx.client.Logger().Warn(ctx, fmt.Sprintf("processSyncTransaction(): txInfo for %s is invalid, will try again later", syncTx.ID)) - if transaction == nil { - return ErrMissingTransaction + if syncTx.client.IsDebug() { + txInfoJSON, _ := json.Marshal(txInfo) //nolint:nolintlint,nilerr // error is not needed + syncTx.DebugLog(string(txInfoJSON)) + } + return nil } // Add additional information (if found on-chain) @@ -700,6 +714,7 @@ func processSyncTransaction(ctx context.Context, syncTx *SyncTransaction, transa return err } + syncTx.client.Logger().Info(ctx, fmt.Sprintf("processSyncTransaction(): Transaction %s processed successfully", syncTx.ID)) // Done! return nil }