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
/
model_incoming_transactions.go
349 lines (287 loc) · 10.2 KB
/
model_incoming_transactions.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package bux
import (
"context"
"encoding/json"
"errors"
"fmt"
"time"
"github.com/BuxOrg/bux/chainstate"
"github.com/libsv/go-bt/v2"
"github.com/mrz1836/go-datastore"
"github.com/rs/zerolog"
)
// IncomingTransaction is an object representing the incoming (external) transaction (for pre-processing)
//
// Gorm related models & indexes: https://gorm.io/docs/models.html - https://gorm.io/docs/indexes.html
type IncomingTransaction struct {
// Base model
Model `bson:",inline"`
// Standard transaction model base fields
TransactionBase `bson:",inline"`
// Model specific fields
Status SyncStatus `json:"status" toml:"status" yaml:"status" gorm:"<-;type:varchar(10);index;comment:This is the status of processing the transaction" bson:"status"`
StatusMessage string `json:"status_message" toml:"status_message" yaml:"status_message" gorm:"<-;type:varchar(512);comment:This is the status message or error" bson:"status_message"`
}
func emptyIncomingTx(opts ...ModelOps) *IncomingTransaction {
return &IncomingTransaction{
Model: *NewBaseModel(ModelIncomingTransaction, opts...),
TransactionBase: TransactionBase{},
Status: SyncStatusReady,
}
}
// newIncomingTransaction will start a new model
func newIncomingTransaction(hex string, opts ...ModelOps) (*IncomingTransaction, error) {
var btTx *bt.Tx
var err error
if btTx, err = bt.NewTxFromString(hex); err != nil {
return nil, err
}
tx := emptyIncomingTx(opts...)
tx.ID = btTx.TxID()
tx.Hex = hex
tx.parsedTx = btTx
return tx, nil
}
// getIncomingTransactionsToProcess will get the incoming transactions to process
func getIncomingTransactionsToProcess(ctx context.Context, queryParams *datastore.QueryParams,
opts ...ModelOps,
) ([]*IncomingTransaction, error) {
// Construct an empty model
var models []IncomingTransaction
conditions := map[string]interface{}{
statusField: statusReady,
}
if queryParams == nil {
queryParams = &datastore.QueryParams{
Page: 0,
PageSize: 0,
}
}
queryParams.OrderByField = idField
queryParams.SortDirection = datastore.SortAsc
// Get the record
if err := getModels(
ctx, NewBaseModel(ModelNameEmpty, opts...).Client().Datastore(),
&models, conditions, queryParams, defaultDatabaseReadTimeout,
); err != nil {
if errors.Is(err, datastore.ErrNoResults) {
return nil, nil
}
return nil, err
}
// Loop and enrich
txs := make([]*IncomingTransaction, 0)
for index := range models {
models[index].enrich(ModelIncomingTransaction, opts...)
txs = append(txs, &models[index])
}
return txs, nil
}
// GetModelName will get the name of the current model
func (m *IncomingTransaction) GetModelName() string {
return ModelIncomingTransaction.String()
}
// GetModelTableName will get the db table name of the current model
func (m *IncomingTransaction) GetModelTableName() string {
return tableIncomingTransactions
}
// Save will save the model into the Datastore
func (m *IncomingTransaction) Save(ctx context.Context) error {
return Save(ctx, m)
}
// GetID will get the ID
func (m *IncomingTransaction) GetID() string {
return m.ID
}
func (m *IncomingTransaction) toTransactionDto() *Transaction {
t := Transaction{}
t.Hex = m.Hex
t.parsedTx = m.parsedTx
t.rawXpubKey = m.rawXpubKey
t.setXPubID()
t.setID() //nolint:errcheck,gosec // error is not needed
t.Metadata = m.Metadata
t.NumberOfOutputs = uint32(len(m.parsedTx.Outputs))
t.NumberOfInputs = uint32(len(m.parsedTx.Inputs))
return &t
}
// BeforeCreating will fire before the model is being inserted into the Datastore
func (m *IncomingTransaction) BeforeCreating(ctx context.Context) error {
m.Client().Logger().Debug().
Str("txID", m.GetID()).
Msgf("starting: %s BeforeCreating hook...", m.Name())
// Set status
m.Status = SyncStatusReady
// Make sure ID is valid
if len(m.ID) == 0 {
return ErrMissingFieldID
}
if len(m.Hex) == 0 {
return ErrMissingFieldHex
}
// Attempt to parse
if len(m.Hex) > 0 && m.TransactionBase.parsedTx == nil {
m.TransactionBase.parsedTx, _ = bt.NewTxFromString(m.Hex)
}
// Require the tx to be parsed
if m.TransactionBase.parsedTx == nil {
return ErrTransactionNotParsed
}
// Check that the transaction has >= 1 known destination
if !m.TransactionBase.hasOneKnownDestination(ctx, m.Client()) {
return ErrNoMatchingOutputs
}
m.Client().Logger().Debug().
Str("txID", m.GetID()).
Msgf("end: %s BeforeCreating hook", m.Name())
return nil
}
// AfterCreated will fire after the model is created
func (m *IncomingTransaction) AfterCreated(_ context.Context) error {
m.Client().Logger().Debug().
Str("txID", m.GetID()).
Msgf("starting: %s AfterCreated hook...", m.Name())
// todo: this should be refactored into a task
if err := processIncomingTransaction(context.Background(), m.Client().Logger(), m); err != nil {
m.Client().Logger().Error().
Str("txID", m.GetID()).
Msgf("error processing incoming transaction: %v", err.Error())
}
m.Client().Logger().Debug().
Str("txID", m.GetID()).
Msgf("end: %s AfterCreated hook", m.Name())
return nil
}
// Migrate model specific migration on startup
func (m *IncomingTransaction) Migrate(client datastore.ClientInterface) error {
return client.IndexMetadata(client.GetTableName(tableIncomingTransactions), metadataField)
}
// processIncomingTransactions will process incoming transaction records
func processIncomingTransactions(ctx context.Context, logClient *zerolog.Logger, maxTransactions int,
opts ...ModelOps,
) error {
queryParams := &datastore.QueryParams{Page: 1, PageSize: maxTransactions}
// Get x records:
records, err := getIncomingTransactionsToProcess(
ctx, queryParams, opts...,
)
if err != nil {
return err
} else if len(records) == 0 {
return nil
}
if logClient != nil {
logClient.Info().Msgf("found %d incoming transactions to process", len(records))
}
// Process the incoming transaction
for index := range records {
if err = processIncomingTransaction(
ctx, logClient, records[index],
); err != nil {
return err
}
}
return nil
}
// processIncomingTransaction will process the incoming transaction record into a transaction, or save the failure
func processIncomingTransaction(ctx context.Context, logClient *zerolog.Logger,
incomingTx *IncomingTransaction,
) error {
if logClient == nil {
logClient = incomingTx.client.Logger()
}
logClient.Info().Str("txID", incomingTx.GetID()).Msgf("processIncomingTransaction(): transaction: %v", incomingTx)
// Successfully capture any panics, convert to readable string and log the error
defer recoverAndLog(incomingTx.client.Logger())
// Create the lock and set the release for after the function completes
unlock, err := newWriteLock(
ctx, fmt.Sprintf(lockKeyProcessIncomingTx, incomingTx.GetID()), incomingTx.Client().Cachestore(),
)
defer unlock()
if err != nil {
return err
}
// Find in mempool or on-chain
var txInfo *chainstate.TransactionInfo
if txInfo, err = incomingTx.Client().Chainstate().QueryTransactionFastest(
ctx, incomingTx.ID, chainstate.RequiredInMempool, defaultQueryTxTimeout,
); err != nil {
logClient.Error().
Str("txID", incomingTx.GetID()).
Msgf("error finding transaction %s on chain. Reason: %s", incomingTx.ID, err)
// TX might not have been broadcast yet? (race condition, or it was never broadcast...)
if errors.Is(err, chainstate.ErrTransactionNotFound) {
var provider string
// Broadcast and detect if there is a real error
if provider, err = incomingTx.Client().Chainstate().Broadcast(
ctx, incomingTx.ID, incomingTx.Hex, defaultQueryTxTimeout,
); err != nil {
bailAndSaveIncomingTransaction(ctx, incomingTx, "tx was not found using all providers, attempted broadcast, "+err.Error())
return err
}
// Broadcast was successful, so the transaction was accepted by the network, continue processing like before
logClient.Info().
Str("txID", incomingTx.GetID()).
Msgf("broadcast of transaction was successful using %s. Incoming tx will be processed again.", provider)
// allow propagation
time.Sleep(3 * time.Second)
return nil // reprocess it when triggering the task again
}
// Actual error occurred
bailAndSaveIncomingTransaction(ctx, incomingTx, err.Error())
return err
}
if !txInfo.Valid() {
logClient.Warn().Str("txID", incomingTx.ID).Msg("txInfo is invalid, will try again later")
if incomingTx.client.IsDebug() {
txInfoJSON, _ := json.Marshal(txInfo) //nolint:nolintlint,nilerr,govet,errchkjson // error is not needed
logClient.Debug().Str("txID", incomingTx.ID).Msg(string(txInfoJSON))
}
return nil
}
logClient.Info().Str("txID", incomingTx.ID).Msgf("found incoming transaction in %s", txInfo.Provider)
// Check if we have transaction in DB already
transaction, _ := getTransactionByID(
ctx, incomingTx.rawXpubKey, incomingTx.ID, incomingTx.client.DefaultModelOptions()...,
)
if transaction == nil {
// Create the new transaction model
if transaction, err = newTransactionFromIncomingTransaction(incomingTx); err != nil {
logClient.Error().Str("txID", incomingTx.ID).Msgf("creating a new tx failed. Reason: %s", err)
return err
}
if err = transaction.processUtxos(ctx); err != nil {
logClient.Error().
Str("txID", incomingTx.ID).
Msgf("processing utxos for tx failed. Reason: %s", err)
return err
}
}
transaction.setChainInfo(txInfo)
// Create status message
onChain := len(transaction.BlockHash) > 0 || transaction.BlockHeight > 0
message := "transaction was found in mempool by " + txInfo.Provider
if onChain {
message = "transaction was found on-chain by " + txInfo.Provider
}
// Save (add) the transaction (should NOT error)
if err = transaction.Save(ctx); err != nil {
bailAndSaveIncomingTransaction(ctx, incomingTx, err.Error())
return err
}
// Update (or delete?) the incoming transaction record
incomingTx.Status = statusComplete
incomingTx.StatusMessage = message
if err = incomingTx.Save(ctx); err != nil {
bailAndSaveIncomingTransaction(ctx, incomingTx, err.Error())
return err
}
// Done!
return nil
}
// bailAndSaveIncomingTransaction try to save the error message
func bailAndSaveIncomingTransaction(ctx context.Context, incomingTx *IncomingTransaction, errorMessage string) {
incomingTx.Status = statusError
incomingTx.StatusMessage = errorMessage
_ = incomingTx.Save(ctx)
}