This repository has been 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 6
/
record_tx_strategy_external_incoming_tx.go
169 lines (129 loc) · 4.42 KB
/
record_tx_strategy_external_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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package bux
import (
"context"
"fmt"
"github.com/libsv/go-bt/v2"
"github.com/rs/zerolog"
)
type externalIncomingTx struct {
Hex string
broadcastNow bool // e.g. BEEF must be broadcasted now
allowBroadcastErrors bool // only BEEF cannot allow for broadcast errors
}
func (strategy *externalIncomingTx) Execute(ctx context.Context, c ClientInterface, opts []ModelOps) (*Transaction, error) {
logger := c.Logger()
// process
if !strategy.broadcastNow && c.IsITCEnabled() { // do not save transaction to database now, save IncomingTransaction instead and let task manager handle and process it
return _addTxToCheck(ctx, strategy, c, opts)
}
transaction, err := _createExternalTxToRecord(ctx, strategy, c, opts)
if err != nil {
return nil, fmt.Errorf("creation of external incoming tx failed. Reason: %w", err)
}
logger.Info().
Str("txID", transaction.ID).
Msg("start without ITC")
if strategy.broadcastNow || transaction.syncTransaction.BroadcastStatus == SyncStatusReady {
err = _externalIncomingBroadcast(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)
}
}
// record
if err = transaction.Save(ctx); err != nil {
return nil, fmt.Errorf("saving of Transaction failed. Reason: %w", err)
}
logger.Info().
Str("txID", transaction.ID).
Msg("External incoming tx execute complete")
return transaction, nil
}
func (strategy *externalIncomingTx) Validate() error {
if strategy.Hex == "" {
return ErrMissingFieldHex
}
return nil // is valid
}
func (strategy *externalIncomingTx) TxID() string {
btTx, _ := bt.NewTxFromString(strategy.Hex)
return btTx.TxID()
}
func (strategy *externalIncomingTx) LockKey() string {
return fmt.Sprintf("incoming-%s", strategy.TxID())
}
func (strategy *externalIncomingTx) ForceBroadcast(force bool) {
strategy.broadcastNow = force
}
func (strategy *externalIncomingTx) FailOnBroadcastError(forceFail bool) {
strategy.allowBroadcastErrors = !forceFail
}
func _addTxToCheck(ctx context.Context, tx *externalIncomingTx, c ClientInterface, opts []ModelOps) (*Transaction, error) {
logger := c.Logger()
incomingTx, err := newIncomingTransaction(tx.Hex, c.DefaultModelOptions(append(opts, New())...)...)
if err != nil {
return nil, fmt.Errorf("tx creation failed. Reason: %w", err)
}
logger.Info().
Str("txID", incomingTx.ID).
Msg("start ITC")
if err = incomingTx.Save(ctx); err != nil {
return nil, fmt.Errorf("adding new IncomingTx to check queue failed. Reason: %w", err)
}
result := incomingTx.toTransactionDto()
result.Status = statusProcessing
logger.Info().
Str("txID", incomingTx.ID).
Msg("complete ITC")
return result, nil
}
func _createExternalTxToRecord(ctx context.Context, eTx *externalIncomingTx, c ClientInterface, opts []ModelOps) (*Transaction, error) {
// Create NEW tx model
tx, err := txFromHex(eTx.Hex, c.DefaultModelOptions(append(opts, New())...)...)
if err != nil {
return nil, err
}
_hydrateExternalWithSync(tx)
if !tx.TransactionBase.hasOneKnownDestination(ctx, c) {
return nil, ErrNoMatchingOutputs
}
if err = tx.processUtxos(ctx); err != nil {
return nil, err
}
return tx, nil
}
func _hydrateExternalWithSync(tx *Transaction) {
sync := newSyncTransaction(
tx.ID,
tx.Client().DefaultSyncConfig(),
tx.GetOptions(true)...,
)
// to simplify: broadcast every external incoming txs
sync.BroadcastStatus = SyncStatusReady
sync.P2PStatus = SyncStatusSkipped // the sender of the Tx should have already notified us
sync.SyncStatus = SyncStatusPending // wait until transactions will be broadcasted
// Use the same metadata
sync.Metadata = tx.Metadata
sync.transaction = tx
tx.syncTransaction = sync
}
func _externalIncomingBroadcast(ctx context.Context, logger *zerolog.Logger, tx *Transaction, allowErrors bool) error {
logger.Info().Str("txID", tx.ID).Msg("start broadcast")
err := broadcastSyncTransaction(ctx, tx.syncTransaction)
if err == nil {
logger.Info().
Str("txID", tx.ID).
Msg("broadcast complete")
return nil
}
if allowErrors {
logger.Warn().
Str("txID", tx.ID).
Msgf("broadcasting failed, next try will be handled by task manager. Reason: %s", err)
// ignore error, transaction will be broadcasted in a cron task
return nil
}
return err
}