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_external_incoming_tx.go
146 lines (112 loc) · 3.69 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
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) Name() string {
return "external_incoming_tx"
}
func (strategy *externalIncomingTx) Execute(ctx context.Context, c ClientInterface, opts []ModelOps) (*Transaction, error) {
logger := c.Logger()
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
}
if _, err := bt.NewTxFromString(strategy.Hex); err != nil {
return fmt.Errorf("invalid hex: %w", err)
}
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 _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
}