Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
chore(BUX-298): try to sync tx again if there is no merle proof
Browse files Browse the repository at this point in the history
  • Loading branch information
pawellewandowski98 committed Oct 20, 2023
1 parent e05ecbf commit 5dbd5eb
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions model_sync_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"runtime"
Expand Down Expand Up @@ -640,13 +641,28 @@ 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
if txInfo, err = syncTx.Client().Chainstate().QueryTransaction(
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",
)
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 5dbd5eb

Please sign in to comment.