/
spent_info_history.go
624 lines (558 loc) · 15.9 KB
/
spent_info_history.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
/*---------------------------------------------------------------------------------------------
* Copyright (c) IBAX All rights reserved.
* See LICENSE in the project root for license information.
*--------------------------------------------------------------------------------------------*/
package models
import (
"bytes"
"encoding/hex"
"errors"
"fmt"
"github.com/IBAX-io/go-ibax/packages/consts"
"github.com/IBAX-io/go-ibax/packages/transaction"
"github.com/shopspring/decimal"
log "github.com/sirupsen/logrus"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
var initStart bool = true
type SpentInfoHistory struct {
Id int64 `gorm:"primary_key;not null"`
SenderId int64 `gorm:"column:sender_id;not null"`
RecipientId int64 `gorm:"column:recipient_id;not null"`
SenderBalance string `gorm:"type:decimal(30);default:'0';not null"`
RecipientBalance string `gorm:"type:decimal(30);default:'0';not null"`
Amount string `gorm:"column:amount;type:decimal(30);default:'0';not null"`
Block int64 `gorm:"column:block;not null;index"`
Hash []byte `gorm:"column:hash;not null;index"`
CreatedAt int64 `gorm:"column:created_at;not null"`
Ecosystem int64 `gorm:"not null"`
Type int `gorm:"not null"` //1:UTXO_TransferSelf 2:UTXO_Tx 3:fees 4:taxes 5:startUp 6:combustion
SubType int `gorm:"not null"` //type is 1 then 1:AccountUTXO 2:UTXO-Account
}
type spentInfoTxData struct {
Hash []byte
BlockId int64
TxData []byte
}
type utxoTxInfo struct {
UtxoType string
TransferType string
TxTime int64
SenderId int64
RecipientId int64
Amount decimal.Decimal
Ecosystem int64
}
var getUtxoTxData chan bool
const (
FeesType = "fees"
TaxesType = "taxes"
StartUpType = "startUp"
CombustionType = "combustion"
)
func (p *SpentInfoHistory) TableName() string {
return "spent_info_history"
}
func (p *SpentInfoHistory) CreateTable() (err error) {
err = nil
if !HasTableOrView(p.TableName()) {
if err = GetDB(nil).Migrator().CreateTable(p); err != nil {
return err
}
}
return err
}
func (p *SpentInfoHistory) GetLast() (bool, error) {
return isFound(GetDB(nil).Last(p))
}
func (p *SpentInfoHistory) RollbackTransaction() error {
return GetDB(nil).Where("block >= ?", p.Block).Delete(&SpentInfoHistory{}).Error
}
func (p *SpentInfoHistory) GetKeyBalance(keyId int64, ecosystem int64) (balance decimal.Decimal, err error) {
var f bool
f, err = isFound(GetDB(nil).Raw(`
SELECT CASE WHEN sender_id = ? THEN
sender_balance
ELSE
recipient_balance
END AS balance
FROM spent_info_history
WHERE(recipient_id = ? OR sender_id = ?) AND ecosystem = ? ORDER BY id DESC LIMIT 1`, keyId, keyId, keyId, ecosystem).Take(&balance))
if err != nil {
return
}
if !f {
return decimal.Zero, nil
}
return
}
func InitSpentInfoHistory() error {
var p SpentInfoHistory
err := p.CreateTable()
if err != nil {
return err
}
go utxoDataSyncSignalReceive()
return nil
}
func utxoDataSyncSignalReceive() {
if getUtxoTxData == nil {
getUtxoTxData = make(chan bool)
}
for {
select {
case <-getUtxoTxData:
err := utxoTxSync()
if err != nil {
log.WithFields(log.Fields{"error": err}).Error("utxo tx Sync Failed")
}
}
}
}
func SendUtxoTxSyncSignal() {
RealtimeWG.Add(1)
defer func() {
RealtimeWG.Done()
}()
select {
case getUtxoTxData <- true:
default:
//fmt.Printf("Get utxo tx Data len:%d,cap:%d\n", len(getUtxoTxData), cap(getUtxoTxData))
}
}
func utxoTxSync() error {
var insertData []SpentInfoHistory
var (
si TransactionData
st TransactionData
bkDiff int64
)
begin:
tr := &SpentInfoHistory{}
_, err := tr.GetLast()
if err != nil {
return fmt.Errorf("[utxo sync]get spent info history last failed:%s", err.Error())
}
f, err := si.GetLastByType(formatTxDataType(true))
if err != nil {
return fmt.Errorf("[utxo sync]get spent info last failed:%s", err.Error())
}
if f {
if initStart {
err = tr.RollbackOne()
if err != nil {
return err
} else {
initStart = false
goto begin
}
}
if tr.Block >= si.Block {
utxoTxCheck(si.Block)
return nil
}
}
f, err = st.GetFirstByType(tr.Block, formatTxDataType(true))
if err != nil {
return fmt.Errorf("[utxo sync]get spent info block:%d first failed:%s", tr.Block, err.Error())
}
if !f {
return nil
}
txList, err := getSpentInfoHashList(st.Block, st.Block+100)
if err != nil {
return fmt.Errorf("[utxo sync]get spent info hash list failed:%s", err.Error())
}
if txList == nil {
return nil
}
// map[ecosystem]map[key_id]balance
keysBalance := make(map[int64]map[int64]decimal.Decimal)
addInsertData := func(data SpentInfoHistory, amount decimal.Decimal) error {
if data.Type != 5 {
if _, ok := keysBalance[data.Ecosystem][data.SenderId]; !ok {
return fmt.Errorf("[utxo sync] ecosystem:%d, senderId:%d balance doen't not exist", data.Ecosystem, data.SenderId)
}
if _, ok := keysBalance[data.Ecosystem][data.RecipientId]; !ok {
return fmt.Errorf("[utxo sync] ecosystem:%d, recipientId:%d balance doen't not exist", data.Ecosystem, data.RecipientId)
}
if data.Type == 1 {
if data.SubType == 2 {
keysBalance[data.Ecosystem][data.SenderId] = keysBalance[data.Ecosystem][data.SenderId].Sub(amount)
data.SenderBalance = keysBalance[data.Ecosystem][data.SenderId].String()
data.RecipientBalance = keysBalance[data.Ecosystem][data.RecipientId].String()
insertData = append(insertData, data)
return nil
}
} else {
keysBalance[data.Ecosystem][data.SenderId] = keysBalance[data.Ecosystem][data.SenderId].Sub(amount)
}
} else {
ba := make(map[int64]decimal.Decimal)
ba[data.Ecosystem] = decimal.Zero
if _, ok := keysBalance[data.Ecosystem]; !ok {
keysBalance[data.Ecosystem] = ba
}
}
keysBalance[data.Ecosystem][data.RecipientId] = keysBalance[data.Ecosystem][data.RecipientId].Add(amount)
data.SenderBalance = keysBalance[data.Ecosystem][data.SenderId].String()
data.RecipientBalance = keysBalance[data.Ecosystem][data.RecipientId].String()
insertData = append(insertData, data)
return nil
}
for _, val := range *txList {
var (
data SpentInfoHistory
s1 SpentInfo
)
data.Hash = val.Hash
data.Block = val.BlockId
if bkDiff != val.BlockId { //block diff update keys balance
bkDiff = val.BlockId
if insertData != nil {
err = createUtxoTxBatches(GetDB(nil), &insertData)
if err != nil {
return err
}
insertData = nil
}
keys, err := s1.GetOutputKeysByBlockId(val.BlockId)
if err != nil {
return fmt.Errorf("[utxo sync]get block:%d output keys failed:%s", val.BlockId, err.Error())
}
for _, k := range keys {
var (
his SpentInfoHistory
)
ba := make(map[int64]decimal.Decimal)
balance, err := his.GetKeyBalance(k.OutputKeyId, k.Ecosystem)
if err != nil {
return fmt.Errorf("[utxo sync]get ecosystem:%d output keys:%d balance failed:%s", k.Ecosystem, k.OutputKeyId, err.Error())
}
//fmt.Printf("[blockid]%d,ecosystem:%d,key_id:%d,balance:%s\n", val.BlockId, k.Ecosystem, k.OutputKeyId, balance.String())
ba[k.OutputKeyId] = balance
if _, ok := keysBalance[k.Ecosystem]; !ok {
keysBalance[k.Ecosystem] = ba
} else {
keysBalance[k.Ecosystem][k.OutputKeyId] = balance
}
}
}
info, err := val.UnmarshalTransaction()
if err != nil {
return fmt.Errorf("[utxo sync]unmarshal utxo transaction failed:%s", err.Error())
}
data.CreatedAt = info.TxTime
if info.UtxoType == UtxoTx {
var (
si SpentInfo
outputList []SpentInfo
)
_, outputList, err = si.GetOutputs(val.Hash)
if err != nil {
return fmt.Errorf("[utxo sync]get out puts failed:%s", err.Error())
}
for _, v := range outputList {
amount, _ := decimal.NewFromString(v.OutputValue)
recipientId := v.OutputKeyId
if info.Ecosystem == 1 {
if v.Ecosystem == 1 {
switch v.Type {
case 20: //fees type
data.Amount = amount.String()
data.SenderId = info.SenderId
data.RecipientId = recipientId
data.Ecosystem = 1
data.Type = formatSpentInfoHistoryType(FeesType)
err = addInsertData(data, amount)
if err != nil {
return err
}
case 21: //taxes type
data.Amount = amount.String()
data.SenderId = info.SenderId
data.RecipientId = recipientId
data.Ecosystem = 1
data.Type = formatSpentInfoHistoryType(TaxesType)
err = addInsertData(data, amount)
if err != nil {
return err
}
case 26: //utxo tx
data.Amount = amount.String()
data.SenderId = info.SenderId
data.RecipientId = recipientId
data.Ecosystem = 1
data.Type = formatSpentInfoHistoryType(UtxoTx)
err = addInsertData(data, amount)
if err != nil {
return err
}
}
}
} else {
if v.Ecosystem == 1 {
switch v.Type {
case 20: //fees type
data.Amount = amount.String()
data.SenderId = info.SenderId
data.RecipientId = recipientId
data.Ecosystem = 1
data.Type = formatSpentInfoHistoryType(FeesType)
err = addInsertData(data, amount)
if err != nil {
return err
}
case 21: //taxes type
data.Amount = amount.String()
data.SenderId = info.SenderId
data.RecipientId = recipientId
data.Ecosystem = 1
data.Type = formatSpentInfoHistoryType(TaxesType)
err = addInsertData(data, amount)
if err != nil {
return err
}
}
} else {
switch v.Type {
case 23:
data.Amount = amount.String()
data.SenderId = info.SenderId
data.RecipientId = recipientId
data.Ecosystem = v.Ecosystem
data.Type = formatSpentInfoHistoryType(CombustionType)
err = addInsertData(data, amount)
if err != nil {
return err
}
case 20:
data.Amount = amount.String()
data.SenderId = info.SenderId
data.RecipientId = recipientId
data.Ecosystem = v.Ecosystem
data.Type = formatSpentInfoHistoryType(FeesType)
err = addInsertData(data, amount)
if err != nil {
return err
}
case 21:
data.Amount = amount.String()
data.SenderId = info.SenderId
data.RecipientId = recipientId
data.Ecosystem = v.Ecosystem
data.Type = formatSpentInfoHistoryType(TaxesType)
err = addInsertData(data, amount)
if err != nil {
return err
}
case 26:
data.Amount = amount.String()
data.SenderId = info.SenderId
data.RecipientId = recipientId
data.Ecosystem = v.Ecosystem
data.Type = formatSpentInfoHistoryType(UtxoTx)
err = addInsertData(data, amount)
if err != nil {
return err
}
}
}
}
}
} else if info.UtxoType == StartUpType {
var lt LogTransaction
f, err := lt.GetByHash(val.Hash)
if err != nil {
return err
}
if !f {
return fmt.Errorf("[utxo sync]get log hash doesn't exist hash:%s", hex.EncodeToString(val.Hash))
}
amount := decimal.New(StartUpSupply, int32(consts.MoneyDigits))
data.Type = formatSpentInfoHistoryType(info.UtxoType)
data.SenderId = 5555
data.RecipientId = lt.Address
data.Amount = amount.String()
data.Ecosystem = lt.EcosystemID
err = addInsertData(data, amount)
if err != nil {
return err
}
} else {
data.Type = formatSpentInfoHistoryType(info.UtxoType)
data.SubType = formatSpentInfoHistorySubType(info.TransferType)
data.SenderId = info.SenderId
data.RecipientId = info.RecipientId
data.Amount = info.Amount.String()
data.Ecosystem = info.Ecosystem
err = addInsertData(data, info.Amount)
if err != nil {
return err
}
}
}
if insertData != nil {
err = createUtxoTxBatches(GetDB(nil), &insertData)
if err != nil {
return err
}
insertData = nil
}
return utxoTxSync()
}
func createUtxoTxBatches(dbTx *gorm.DB, data *[]SpentInfoHistory) error {
if data == nil {
return nil
}
return dbTx.Clauses(clause.OnConflict{DoNothing: true}).CreateInBatches(data, 5000).Error
}
func (si *spentInfoTxData) UnmarshalTransaction() (*utxoTxInfo, error) {
if si.TxData == nil {
return nil, errors.New("transaction Data is null")
}
var (
result utxoTxInfo
)
lt := &LogTransaction{}
tx, err := transaction.UnmarshallTransaction(bytes.NewBuffer(si.TxData), false)
if err != nil {
return nil, err
}
if tx.IsSmartContract() {
result.Ecosystem = tx.SmartContract().TxSmart.Header.EcosystemID
if tx.SmartContract().TxSmart.UTXO != nil {
result.SenderId = tx.KeyID()
result.UtxoType = UtxoTx
} else if tx.SmartContract().TxSmart.TransferSelf != nil {
result.UtxoType = UtxoTransferSelf
result.SenderId = tx.KeyID()
result.RecipientId = tx.KeyID()
result.Amount, _ = decimal.NewFromString(tx.SmartContract().TxSmart.TransferSelf.Value)
if tx.SmartContract().TxSmart.TransferSelf.Source == "Account" && tx.SmartContract().TxSmart.TransferSelf.Target == "UTXO" {
result.TransferType = AccountUTXO
} else {
result.TransferType = UTXOAccount
}
} else {
return &result, errors.New("doesn't not UTXO transaction")
}
result.TxTime = tx.Timestamp()
if result.TxTime == 0 {
f, err := lt.GetTxTime(si.Hash)
if err == nil && f {
result.TxTime = lt.Timestamp
}
result.TxTime = lt.Timestamp
}
} else {
if si.BlockId == 1 {
result.UtxoType = StartUpType
f, err := lt.GetTxTime(si.Hash)
if err == nil && f {
result.TxTime = lt.Timestamp
}
return &result, nil
}
return nil, errors.New("doesn't not Smart Contract")
}
return &result, nil
}
func utxoTxCheck(lastBlockId int64) {
tx := &SpentInfoHistory{}
f, err := tx.GetLast()
if err == nil && f {
logTran := &LogTransaction{}
f, err = logTran.GetByHash(tx.Hash)
if err == nil {
if !f {
if tx.Block > lastBlockId {
tx.Block = lastBlockId
}
if tx.Block > 0 {
log.WithFields(log.Fields{"log hash doesn't exist": hex.EncodeToString(tx.Hash), "block": tx.Block}).Info("[utxo tx check] rollback data")
err = tx.RollbackTransaction()
if err == nil {
utxoTxCheck(tx.Block)
} else {
log.WithFields(log.Fields{"error": err, "block": tx.Block}).Error("[utxo tx check] rollback Failed")
}
}
}
} else {
log.WithFields(log.Fields{"error": err, "hash": hex.EncodeToString(tx.Hash)}).Error("[utxo tx check] get log transaction failed")
}
}
}
func (p *SpentInfoHistory) RollbackOne() error {
if p.Block > 0 {
err := p.RollbackTransaction()
if err != nil {
log.WithFields(log.Fields{"error": err, "block": p.Block}).Error("[spent info history] rollback one Failed")
return err
}
}
return nil
}
func getUtxoTxBasisGasFee(hash []byte) decimal.Decimal {
var hi SpentInfoHistory
gasFee := decimal.Zero
_, err := isFound(GetDB(nil).Table(hi.TableName()).Select("COALESCE(sum(amount),0)").
Where("hash = ? AND ecosystem = 1 AND (type = ? OR type = ?)", hash, formatSpentInfoHistoryType(FeesType), formatSpentInfoHistoryType(TaxesType)).Take(&gasFee))
if err != nil {
log.WithFields(log.Fields{"error": err, "hash": hex.EncodeToString(hash)}).Error("get utxo transaction gas fee failed")
}
return gasFee
}
func formatSpentInfoHistoryType(utxoType string) int {
switch utxoType {
case UtxoTransferSelf:
return 1
case UtxoTx:
return 2
case FeesType:
return 3
case TaxesType:
return 4
case StartUpType:
return 5
default: //CombustionType
return 6
}
}
func parseSpentInfoHistoryType(utxoType int) string {
switch utxoType {
case 1:
return UtxoTransferSelf
case 2:
return UtxoTx
case 3:
return FeesType
case 4:
return TaxesType
case 5:
return StartUpType
case 6:
return CombustionType
}
return ""
}
func formatSpentInfoHistorySubType(subType string) int {
switch subType {
case AccountUTXO:
return 1
case UTXOAccount:
return 2
}
return 0
}
func parseSpentInfoHistorySubType(subType int) string {
switch subType {
case 1:
return AccountUTXO
case 2:
return UTXOAccount
}
return ""
}