diff --git a/record_tx_strategy_external_incoming_tx.go b/record_tx_strategy_external_incoming_tx.go index 07b75420..a0dec99a 100644 --- a/record_tx_strategy_external_incoming_tx.go +++ b/record_tx_strategy_external_incoming_tx.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/libsv/go-bt/v2" + zLogger "github.com/mrz1836/go-logger" ) type externalIncomingTx struct { @@ -21,24 +22,14 @@ func (tx *externalIncomingTx) Execute(ctx context.Context, c ClientInterface, op } transaction, err := _createExternalTxToRecord(ctx, tx, c, opts) - - logger.Info(ctx, fmt.Sprintf("ExternalIncomingTx.Execute(): start without ITC, TxID: %s", transaction.ID)) - if err != nil { return nil, fmt.Errorf("ExternalIncomingTx.Execute(): creation of external incoming tx failed. Reason: %w", err) } + logger.Info(ctx, fmt.Sprintf("ExternalIncomingTx.Execute(): start without ITC, TxID: %s", transaction.ID)) + if transaction.syncTransaction.BroadcastStatus == SyncStatusReady { - logger.Info(ctx, fmt.Sprintf("ExternalIncomingTx.Execute(): start broadcast, TxID: %s", transaction.ID)) - - if err = broadcastSyncTransaction(ctx, transaction.syncTransaction); err != nil { - // ignore error, transaction will be broadcaset in a cron task - logger. - Warn(ctx, fmt.Sprintf("ExternalIncomingTx.Execute(): broadcasting failed. Reason: %s, TxID: %s", err, transaction.ID)) - } else { - logger. - Info(ctx, fmt.Sprintf("ExternalIncomingTx.Execute(): broadcast complete, TxID: %s", transaction.ID)) - } + _externalIncomingBroadcast(ctx, logger, transaction) // ignore error, transaction will be broadcaset in a cron task } // record @@ -125,3 +116,16 @@ func _hydrateExternalWithSync(tx *Transaction) { sync.transaction = tx tx.syncTransaction = sync } + +func _externalIncomingBroadcast(ctx context.Context, logger zLogger.GormLoggerInterface, tx *Transaction) { + logger.Info(ctx, fmt.Sprintf("ExternalIncomingTx.Execute(): start broadcast, TxID: %s", tx.ID)) + + if err := broadcastSyncTransaction(ctx, tx.syncTransaction); err != nil { + // ignore error, transaction will be broadcaset in a cron task + logger. + Warn(ctx, fmt.Sprintf("ExternalIncomingTx.Execute(): broadcasting failed. Reason: %s, TxID: %s", err, tx.ID)) + } else { + logger. + Info(ctx, fmt.Sprintf("ExternalIncomingTx.Execute(): broadcast complete, TxID: %s", tx.ID)) + } +} diff --git a/record_tx_strategy_internal_incoming_tx.go b/record_tx_strategy_internal_incoming_tx.go index 533d18e4..7fedbafb 100644 --- a/record_tx_strategy_internal_incoming_tx.go +++ b/record_tx_strategy_internal_incoming_tx.go @@ -4,6 +4,8 @@ import ( "context" "errors" "fmt" + + zLogger "github.com/mrz1836/go-logger" ) type internalIncomingTx struct { @@ -23,28 +25,10 @@ func (tx *internalIncomingTx) Execute(ctx context.Context, c ClientInterface, op } if tx.BroadcastNow || syncTx.BroadcastStatus == SyncStatusReady { - logger.Info(ctx, fmt.Sprintf("InternalIncomingTx.Execute(): start broadcast, TxID: %s", transaction.ID)) - syncTx.transaction = transaction - err := broadcastSyncTransaction(ctx, syncTx) - if err != nil { - logger. - Warn(ctx, fmt.Sprintf("InternalIncomingTx.Execute(): broadcasting failed. Reason: %s, TxID: %s", err, transaction.ID)) - - if syncTx.BroadcastStatus == SyncStatusSkipped { // revert status to ready after fail to re-run broadcasting, this can happen when we received internal BEEF tx - syncTx.BroadcastStatus = SyncStatusReady - - if err = syncTx.Save(ctx); err != nil { - logger. - Error(ctx, fmt.Sprintf("InternalIncomingTx.Execute(): changing synctx.BroadcastStatus from Pending to Ready failed. Reason: %s, TxID: %s", err, transaction.ID)) - } - } + transaction.syncTransaction = syncTx - // ignore broadcast error - will be repeted by task manager - } else { - logger. - Info(ctx, fmt.Sprintf("InternalIncomingTx.Execute(): broadcast complete, TxID: %s", transaction.ID)) - } + _internalIncomingBroadcast(ctx, logger, transaction) // ignore broadcast error - will be repeted by task manager } logger.Info(ctx, fmt.Sprintf("InternalIncomingTx.Execute(): complete, TxID: %s", transaction.ID)) @@ -66,3 +50,28 @@ func (tx *internalIncomingTx) TxID() string { func (tx *internalIncomingTx) ForceBroadcast(force bool) { tx.BroadcastNow = force } + +func _internalIncomingBroadcast(ctx context.Context, logger zLogger.GormLoggerInterface, transaction *Transaction) { + logger.Info(ctx, fmt.Sprintf("InternalIncomingTx.Execute(): start broadcast, TxID: %s", transaction.ID)) + + syncTx := transaction.syncTransaction + err := broadcastSyncTransaction(ctx, syncTx) + if err != nil { + logger. + Warn(ctx, fmt.Sprintf("InternalIncomingTx.Execute(): broadcasting failed. Reason: %s, TxID: %s", err, transaction.ID)) + + if syncTx.BroadcastStatus == SyncStatusSkipped { // revert status to ready after fail to re-run broadcasting, this can happen when we received internal BEEF tx + syncTx.BroadcastStatus = SyncStatusReady + + if err = syncTx.Save(ctx); err != nil { + logger. + Error(ctx, fmt.Sprintf("InternalIncomingTx.Execute(): changing synctx.BroadcastStatus from Pending to Ready failed. Reason: %s, TxID: %s", err, transaction.ID)) + } + } + + // ignore broadcast error - will be repeted by task manager + } else { + logger. + Info(ctx, fmt.Sprintf("InternalIncomingTx.Execute(): broadcast complete, TxID: %s", transaction.ID)) + } +} diff --git a/record_tx_strategy_outgoing_tx.go b/record_tx_strategy_outgoing_tx.go index 0b5b9c8e..b33efe3e 100644 --- a/record_tx_strategy_outgoing_tx.go +++ b/record_tx_strategy_outgoing_tx.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/libsv/go-bt/v2" + zLogger "github.com/mrz1836/go-logger" ) type outgoingTx struct { @@ -27,29 +28,13 @@ func (tx *outgoingTx) Execute(ctx context.Context, c ClientInterface, opts []Mod } if transaction.syncTransaction.P2PStatus == SyncStatusReady { - logger.Info(ctx, fmt.Sprintf("OutgoingTx.Execute(): start p2p, TxID: %s", transaction.ID)) - - if err = processP2PTransaction(ctx, transaction.syncTransaction, transaction); err != nil { - logger. - Error(ctx, fmt.Sprintf("OutgoingTx.Execute(): processP2PTransaction failed. Reason: %s, TxID: %s", err, transaction.ID)) - + if err = _outgoingNotifyP2p(ctx, logger, transaction); err != nil { return nil, err // reject transaction if P2P notification failed } - - logger.Info(ctx, fmt.Sprintf("OutgoingTx.Execute(): p2p complete, TxID: %s", transaction.ID)) } if transaction.syncTransaction.BroadcastStatus == SyncStatusReady { - logger.Info(ctx, fmt.Sprintf("OutgoingTx.Execute(): start broadcast, TxID: %s", transaction.ID)) - - if err = broadcastSyncTransaction(ctx, transaction.syncTransaction); err != nil { - // ignore error, transaction will be broadcasted by cron task - logger. - Warn(ctx, fmt.Sprintf("OutgoingTx.Execute(): broadcasting failed. Reason: %s, TxID: %s", err, transaction.ID)) - } else { - logger. - Info(ctx, fmt.Sprintf("OutgoingTx.Execute(): broadcast complete, TxID: %s", transaction.ID)) - } + _outgoingBroadcast(ctx, logger, transaction) // ignore error, transaction will be broadcasted by cron task } // record @@ -180,3 +165,30 @@ func _getP2pSyncStatus(tx *Transaction) SyncStatus { return p2pStatus } + +func _outgoingNotifyP2p(ctx context.Context, logger zLogger.GormLoggerInterface, tx *Transaction) error { + logger.Info(ctx, fmt.Sprintf("OutgoingTx.Execute(): start p2p, TxID: %s", tx.ID)) + + if err := processP2PTransaction(ctx, tx.syncTransaction, tx); err != nil { + logger. + Error(ctx, fmt.Sprintf("OutgoingTx.Execute(): processP2PTransaction failed. Reason: %s, TxID: %s", err, tx.ID)) + + return err + } + + logger.Info(ctx, fmt.Sprintf("OutgoingTx.Execute(): p2p complete, TxID: %s", tx.ID)) + return nil +} + +func _outgoingBroadcast(ctx context.Context, logger zLogger.GormLoggerInterface, tx *Transaction) { + logger.Info(ctx, fmt.Sprintf("OutgoingTx.Execute(): start broadcast, TxID: %s", tx.ID)) + + if err := broadcastSyncTransaction(ctx, tx.syncTransaction); err != nil { + // ignore error, transaction will be broadcasted by cron task + logger. + Warn(ctx, fmt.Sprintf("OutgoingTx.Execute(): broadcasting failed. Reason: %s, TxID: %s", err, tx.ID)) + } else { + logger. + Info(ctx, fmt.Sprintf("OutgoingTx.Execute(): broadcast complete, TxID: %s", tx.ID)) + } +}