This repository was archived by the owner on Apr 2, 2024. It is now read-only.
generated from mrz1836/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrecord_tx_strategy_internal_incoming_tx.go
109 lines (85 loc) · 2.7 KB
/
record_tx_strategy_internal_incoming_tx.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package bux
import (
"context"
"errors"
"fmt"
"github.com/libsv/go-bt/v2"
"github.com/rs/zerolog"
)
type internalIncomingTx struct {
Tx *Transaction
broadcastNow bool // e.g. BEEF must be broadcasted now
allowBroadcastErrors bool // only BEEF cannot allow for broadcast errors
}
func (strategy *internalIncomingTx) Name() string {
return "internal_incoming_tx"
}
func (strategy *internalIncomingTx) Execute(ctx context.Context, c ClientInterface, _ []ModelOps) (*Transaction, error) {
logger := c.Logger()
logger.Info().
Str("txID", strategy.Tx.ID).
Msg("start recording transaction")
// process
transaction := strategy.Tx
syncTx, err := GetSyncTransactionByID(ctx, transaction.ID, transaction.GetOptions(false)...)
if err != nil || syncTx == nil {
return nil, fmt.Errorf("getting syncTx failed. Reason: %w", err)
}
if strategy.broadcastNow || syncTx.BroadcastStatus == SyncStatusReady {
syncTx.transaction = transaction
transaction.syncTransaction = syncTx
err := _internalIncomingBroadcast(ctx, logger, transaction, strategy.allowBroadcastErrors)
if err != nil {
logger.Error().
Str("txID", transaction.ID).
Msgf("broadcasting failed, transaction rejected! Reason: %s", err)
return nil, fmt.Errorf("broadcasting failed, transaction rejected! Reason: %w", err)
}
}
logger.Info().
Str("txID", transaction.ID).
Msg("complete")
return transaction, nil
}
func (strategy *internalIncomingTx) Validate() error {
if strategy.Tx == nil {
return errors.New("tx cannot be nil")
}
if _, err := bt.NewTxFromString(strategy.Tx.Hex); err != nil {
return fmt.Errorf("invalid hex: %w", err)
}
return nil // is valid
}
func (strategy *internalIncomingTx) TxID() string {
return strategy.Tx.ID
}
func (strategy *internalIncomingTx) LockKey() string {
return fmt.Sprintf("incoming-%s", strategy.Tx.ID)
}
func (strategy *internalIncomingTx) ForceBroadcast(force bool) {
strategy.broadcastNow = force
}
func (strategy *internalIncomingTx) FailOnBroadcastError(forceFail bool) {
strategy.allowBroadcastErrors = !forceFail
}
func _internalIncomingBroadcast(ctx context.Context, logger *zerolog.Logger, transaction *Transaction, allowErrors bool) error {
logger.Info().
Str("txID", transaction.ID).
Msg("start broadcast")
syncTx := transaction.syncTransaction
err := broadcastSyncTransaction(ctx, syncTx)
if err == nil {
logger.Info().
Str("txID", transaction.ID).
Msg("broadcast complete")
return nil
}
if allowErrors {
logger.Warn().
Str("txID", transaction.ID).
Msgf("broadcasting failed, next try will be handled by task manager. Reason: %s", err)
// ignore broadcast error - will be repeated by task manager
return nil
}
return err
}