-
Notifications
You must be signed in to change notification settings - Fork 42
/
transaction.go
339 lines (302 loc) · 8.97 KB
/
transaction.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
package processing
import (
"strconv"
"sync"
"time"
"github.com/lbryio/chainquery/datastore"
"github.com/lbryio/chainquery/lbrycrd"
"github.com/lbryio/chainquery/metrics"
"github.com/lbryio/chainquery/model"
"github.com/lbryio/chainquery/sockety"
"github.com/lbryio/chainquery/util"
"github.com/lbryio/lbry.go/v2/extras/errors"
"github.com/lbryio/lbry.go/v2/extras/stop"
"github.com/OdyseeTeam/sockety/socketyapi"
"github.com/sirupsen/logrus"
"github.com/volatiletech/sqlboiler/v4/boil"
"github.com/volatiletech/sqlboiler/v4/queries/qm"
)
// MaxParallelTxProcessing sets the maximum concurrent transactions to process in a block.
var MaxParallelTxProcessing int
type txToProcess struct {
tx *lbrycrd.TxRawResult
blockTime uint64
blockHeight uint64
failcount int
}
type txProcessResult struct {
tx *lbrycrd.TxRawResult
blockTime uint64
blockHeight uint64
err error
failcount int
}
func initTxWorkers(s *stop.Group, nrWorkers int, jobs <-chan txToProcess, results chan<- txProcessResult) {
for i := 0; i < nrWorkers; i++ {
s.Add(1)
go func(worker int) {
defer s.Done()
txProcessor(s, jobs, results, worker)
q(strconv.Itoa(worker) + " - WORKER TX - Finished all jobs")
}(i)
}
}
func txProcessor(s *stop.Group, jobs <-chan txToProcess, results chan<- txProcessResult, worker int) {
for {
select {
case <-s.Ch():
return
case job := <-jobs:
q(strconv.Itoa(worker) + " - WORKER TX - Start new job " + job.tx.Txid)
err := ProcessTx(job.tx, job.blockTime, job.blockHeight)
if err != nil {
metrics.ProcessingFailures.WithLabelValues("transaction").Inc()
logrus.Debugf("processing tx failed %d times %s: %s", job.failcount+1, job.tx.Txid, err.Error())
} else if job.failcount > 0 {
logrus.Debugf("processing tx success after %d times %s", job.failcount, job.tx.Txid)
}
result := txProcessResult{
tx: job.tx,
blockTime: job.blockTime,
blockHeight: job.blockHeight,
err: err,
failcount: job.failcount + 1}
q(strconv.Itoa(worker) + " - WORKER TX - Finished new job " + job.tx.Txid)
select {
case <-s.Ch():
q(strconv.Itoa(worker) + " - WORKER TX - discard finished job and stop " + job.tx.Txid)
return
default:
q(strconv.Itoa(worker) + " - WORKER TX - Start sending result of job " + job.tx.Txid)
results <- result
q(strconv.Itoa(worker) + " - WORKER TX - End sending result of job " + job.tx.Txid)
}
}
}
}
type txDebitCredits struct {
addrDCMap map[string]*addrDebitCredits
mutex *sync.RWMutex
}
func newTxDebitCredits() *txDebitCredits {
t := txDebitCredits{}
v := make(map[string]*addrDebitCredits)
t.addrDCMap = v
t.mutex = &sync.RWMutex{}
return &t
}
type addrDebitCredits struct {
debits float64
credits float64
}
func (addDC *addrDebitCredits) Debits() float64 {
return addDC.debits
}
func (addDC *addrDebitCredits) Credits() float64 {
return addDC.credits
}
func (txDC *txDebitCredits) subtract(address string, value float64) {
txDC.mutex.Lock()
if txDC.addrDCMap[address] == nil {
addrDC := addrDebitCredits{}
txDC.addrDCMap[address] = &addrDC
}
txDC.addrDCMap[address].debits = txDC.addrDCMap[address].debits + value
txDC.mutex.Unlock()
}
func (txDC *txDebitCredits) add(address string, value float64) {
txDC.mutex.Lock()
if txDC.addrDCMap[address] == nil {
addrDC := addrDebitCredits{}
txDC.addrDCMap[address] = &addrDC
}
txDC.addrDCMap[address].credits = txDC.addrDCMap[address].credits + value
txDC.mutex.Unlock()
}
// ProcessTx processes an individual transaction from a block.
func ProcessTx(jsonTx *lbrycrd.TxRawResult, blockTime uint64, blockHeight uint64) error {
defer metrics.Processing(time.Now(), "transaction")
defer util.TimeTrack(time.Now(), "processTx "+jsonTx.Txid+" -- ", "daemonprofile")
//Save transaction before the id is used anywhere else otherwise it will be 0
transaction, err := saveUpdateTransaction(jsonTx)
if err != nil {
return err
}
txDbCrAddrMap := newTxDebitCredits()
_, err = createUpdateVoutAddresses(transaction, &jsonTx.Vout, blockTime)
if err != nil {
return errors.Prefix("Vout Address Creation Error", err)
}
_, err = createUpdateVinAddresses(transaction, &jsonTx.Vin, blockTime)
if err != nil {
return errors.Prefix("Vin Address Creation Error", err)
}
// Process the inputs of the tranasction
err = saveUpdateInputs(transaction, jsonTx, txDbCrAddrMap)
if err != nil {
return err
}
// Process the outputs of the transaction
err = saveUpdateOutputs(transaction, jsonTx, txDbCrAddrMap, blockHeight)
if err != nil {
return err
}
//Set the send and receive values for the transaction
err = setSendReceive(transaction, txDbCrAddrMap)
if err != nil {
return err
}
go sockety.SendNotification(socketyapi.SendNotificationArgs{
Service: socketyapi.BlockChain,
Type: "new_tx",
IDs: []string{"transactions", jsonTx.Txid},
Data: map[string]interface{}{"transaction": jsonTx},
})
return nil
}
func saveUpdateTransaction(jsonTx *lbrycrd.TxRawResult) (*model.Transaction, error) {
transaction := &model.Transaction{}
// Error is not helpful. It returns an error if there is nothing in the database.
foundTx, _ := model.Transactions(qm.Where(model.TransactionColumns.Hash+"=?", jsonTx.Txid)).OneG()
if foundTx != nil {
transaction = foundTx
}
transaction.BlockHashID.SetValid(jsonTx.BlockHash)
transaction.InputCount = uint(len(jsonTx.Vin))
transaction.OutputCount = uint(len(jsonTx.Vout))
transaction.TransactionTime.SetValid(uint64(jsonTx.Time))
transaction.TransactionSize = uint64(jsonTx.Size)
transaction.Hash = jsonTx.Txid
transaction.Version = int(jsonTx.Version)
transaction.LockTime = uint(jsonTx.LockTime)
transaction.CreatedTime = time.Unix(jsonTx.Blocktime, 0)
transactionAmount := 0.0
for _, vout := range jsonTx.Vout {
transactionAmount += vout.Value
}
transaction.Value = transactionAmount
if foundTx != nil {
if err := transaction.UpdateG(boil.Infer()); err != nil {
return transaction, err
}
} else {
if err := transaction.InsertG(boil.Infer()); err != nil {
return nil, err
}
}
return transaction, nil
}
func saveUpdateInputs(transaction *model.Transaction, jsonTx *lbrycrd.TxRawResult, txDbCrAddrMap *txDebitCredits) error {
vins := jsonTx.Vin
vinjobs := make(chan vinToProcess)
errorchan := make(chan error)
workers := util.Min(len(vins), MaxParallelVinProcessing)
sQ := stop.New(nil)
initVinWorkers(sQ, workers, vinjobs, errorchan)
// Queue
q("VIN SYNC started")
sQ.Add(1)
go func() {
defer sQ.Done()
//q("VIN start queueing")
for i := range vins {
select {
case <-sQ.Ch():
return
default:
//q("VIN start passing new job")
vinjobs <- vinToProcess{jsonVin: &vins[i], tx: transaction, txDC: txDbCrAddrMap, vin: uint64(i)}
//q("VIN end pass new job")
}
}
//q("VIN end queueing")
close(vinjobs)
}()
//Error check
leftToProcess := len(vins)
for err := range errorchan {
leftToProcess--
if err != nil {
q("VIN error..stopping")
sQ.StopAndWait()
q("VIN error..stopped")
return errors.Prefix("Vin Error->", err)
}
q("VIN processing..." + strconv.Itoa(leftToProcess))
if leftToProcess == 0 {
q("VIN stopping...")
sQ.StopAndWait()
q("VIN stopped...")
q("VIN returning")
return nil
}
continue
}
q("VIN SYNC ended")
return nil
}
func saveUpdateOutputs(transaction *model.Transaction, jsonTx *lbrycrd.TxRawResult, txDbCrAddrMap *txDebitCredits, blockHeight uint64) error {
vouts := jsonTx.Vout
workers := util.Min(len(vouts), MaxParallelVoutProcessing)
voutjobs := make(chan voutToProcess)
errorchan := make(chan error)
sQ := stop.New(nil)
initVoutWorkers(sQ, workers, voutjobs, errorchan)
// Queue
q("VOUT SYNC started")
sQ.Add(1)
go func() {
defer sQ.Done()
q("VOUT start queueing")
for i := range vouts {
select {
case <-sQ.Ch():
return
default:
q("VOUT start passing new job")
voutjobs <- voutToProcess{jsonVout: &vouts[i], tx: transaction, txDC: txDbCrAddrMap, blockHeight: blockHeight}
q("VOUT end pass new job")
}
}
q("VOUT SYNC finished")
close(voutjobs)
}()
//Error check
leftToProcess := len(vouts)
var voutErr error
for err := range errorchan {
leftToProcess--
if err != nil {
q("VOUT error found...")
if voutErr == nil {
voutErr = errors.Prefix("Vout Error->", err)
}
}
if leftToProcess == 0 {
q("VOUT stopping...")
sQ.StopAndWait()
q("VOUT stopped")
q("VOUT returning")
return voutErr
}
continue
}
q("VOUT SYNC ended")
return voutErr
}
func setSendReceive(transaction *model.Transaction, txDbCrAddrMap *txDebitCredits) error {
for addr, DC := range txDbCrAddrMap.addrDCMap {
address := datastore.GetAddress(addr)
if address == nil {
return errors.Err("missing address for setting amounts!")
}
txAddr := datastore.GetTxAddress(transaction.ID, address.ID)
txAddr.CreditAmount = DC.Credits()
txAddr.DebitAmount = DC.Debits()
err := datastore.UpdateTxAddressAmounts(txAddr)
if err != nil {
return err //Should never happen or something is wrong
}
}
return nil
}