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
392 lines (329 loc) · 11.7 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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
package bux
import (
"context"
"errors"
"fmt"
"runtime/debug"
"strings"
"time"
"github.com/BuxOrg/bux/chainstate"
"github.com/BuxOrg/bux/taskmanager"
"github.com/libsv/go-bt/v2"
"github.com/mrz1836/go-datastore"
zLogger "github.com/mrz1836/go-logger"
)
// 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"`
}
// newIncomingTransaction will start a new model
func newIncomingTransaction(txID, hex string, opts ...ModelOps) (tx *IncomingTransaction) {
// Create the model
tx = &IncomingTransaction{
Model: *NewBaseModel(ModelIncomingTransaction, opts...),
TransactionBase: TransactionBase{
ID: txID,
Hex: hex,
},
Status: SyncStatusReady,
}
// Attempt to parse
if len(hex) > 0 {
tx.TransactionBase.parsedTx, _ = bt.NewTxFromString(hex)
}
return
}
// getIncomingTransactionByID will get the incoming transactions to process
func getIncomingTransactionByID(ctx context.Context, id string, opts ...ModelOps) (*IncomingTransaction, error) {
// Construct an empty tx
tx := newIncomingTransaction("", "", opts...)
tx.ID = id
// Get the record
if err := Get(ctx, tx, nil, false, defaultDatabaseReadTimeout, false); err != nil {
if errors.Is(err, datastore.ErrNoResults) {
return nil, nil
}
return nil, err
}
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
}
// BeforeCreating will fire before the model is being inserted into the Datastore
func (m *IncomingTransaction) BeforeCreating(ctx context.Context) error {
m.DebugLog("starting: [" + m.name.String() + "] BeforeCreating hook...")
// 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(), m.GetOptions(false)...) {
return ErrNoMatchingOutputs
}
// Match a known destination
// todo: this can be optimized searching X records at a time vs loop->query->loop->query
matchingOutput := false
lockingScript := ""
opts := m.GetOptions(false)
for index := range m.TransactionBase.parsedTx.Outputs {
lockingScript = m.TransactionBase.parsedTx.Outputs[index].LockingScript.String()
destination, err := getDestinationWithCache(ctx, m.Client(), "", "", lockingScript, opts...)
if err != nil {
m.Client().Logger().Warn(ctx, "error getting destination: "+err.Error())
} else if destination != nil && destination.LockingScript == lockingScript {
matchingOutput = true
break
}
}
// Does not match any known destination
if !matchingOutput {
return ErrNoMatchingOutputs
}
m.DebugLog("end: " + m.Name() + " BeforeCreating hook")
return nil
}
// AfterCreated will fire after the model is created
func (m *IncomingTransaction) AfterCreated(ctx context.Context) error {
m.DebugLog("starting: " + m.Name() + " AfterCreated hook...")
// todo: this should be refactored into a task
// go func(incomingTx *IncomingTransaction) {
if err := processIncomingTransaction(context.Background(), nil, m); err != nil {
m.Client().Logger().Error(ctx, "error processing incoming transaction: "+err.Error())
}
// }(m)
m.DebugLog("end: " + m.Name() + " AfterCreated hook...")
return nil
}
// Migrate model specific migration on startup
func (m *IncomingTransaction) Migrate(client datastore.ClientInterface) error {
return client.IndexMetadata(client.GetTableName(tableIncomingTransactions), metadataField)
}
// RegisterTasks will register the model specific tasks on client initialization
func (m *IncomingTransaction) RegisterTasks() error {
// No task manager loaded?
tm := m.Client().Taskmanager()
if tm == nil {
return nil
}
// Register the task locally (cron task - set the defaults)
processTask := m.Name() + "_process"
ctx := context.Background()
// Register the task
if err := tm.RegisterTask(&taskmanager.Task{
Name: processTask,
RetryLimit: 1,
Handler: func(client ClientInterface) error {
if taskErr := taskProcessIncomingTransactions(ctx, client.Logger(), WithClient(client)); taskErr != nil {
client.Logger().Error(ctx, "error running "+processTask+" task: "+taskErr.Error())
}
return nil
},
}); err != nil {
return err
}
// Run the task periodically
return tm.RunTask(ctx, &taskmanager.TaskOptions{
Arguments: []interface{}{m.Client()},
RunEveryPeriod: m.Client().GetTaskPeriod(processTask),
TaskName: processTask,
})
}
// processIncomingTransactions will process incoming transaction records
func processIncomingTransactions(ctx context.Context, logClient zLogger.GormLoggerInterface, 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(ctx, fmt.Sprintf("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 zLogger.GormLoggerInterface,
incomingTx *IncomingTransaction) error {
if logClient != nil {
logClient.Info(ctx, fmt.Sprintf("processing incoming transaction: %v", incomingTx))
}
// Successfully capture any panics, convert to readable string and log the error
defer func() {
if err := recover(); err != nil {
incomingTx.Client().Logger().Error(ctx,
fmt.Sprintf(
"panic: %v - stack trace: %v", err,
strings.ReplaceAll(string(debug.Stack()), "\n", ""),
),
)
}
}()
// 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 {
if logClient != nil {
logClient.Error(ctx, fmt.Sprintf("error finding transaction %s on chain: %s", incomingTx.ID, err.Error()))
}
// 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
if logClient != nil {
logClient.Info(ctx, fmt.Sprintf("broadcast of transaction was successful using %s", provider))
}
// allow propagation
time.Sleep(3 * time.Second)
if txInfo, err = incomingTx.Client().Chainstate().QueryTransactionFastest(
ctx, incomingTx.ID, chainstate.RequiredInMempool, defaultQueryTxTimeout,
); err != nil {
incomingTx.Status = statusReady
incomingTx.StatusMessage = "tx was not found on-chain, attempting to broadcast using provider: " + provider
_ = incomingTx.Save(ctx)
return err
}
} else {
// Actual error occurred
bailAndSaveIncomingTransaction(ctx, incomingTx, err.Error())
return err
}
}
if logClient != nil {
logClient.Info(ctx, fmt.Sprintf("found incoming transaction %s in %s", incomingTx.ID, txInfo.Provider))
}
// Create the new transaction model
transaction := newTransactionFromIncomingTransaction(incomingTx)
// Get the transaction by ID
if tx, _ := getTransactionByID(
ctx, transaction.rawXpubKey, transaction.TransactionBase.ID, transaction.client.DefaultModelOptions()...,
); tx != nil {
transaction = tx
}
// Add additional information (if found on-chain)
transaction.BlockHeight = uint64(txInfo.BlockHeight)
transaction.BlockHash = txInfo.BlockHash
// 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)
}