forked from AplaProject/go-apla
/
db.go
213 lines (189 loc) · 6.67 KB
/
db.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
package transaction
import (
"errors"
"github.com/GenesisCommunity/go-genesis/packages/consts"
"github.com/GenesisCommunity/go-genesis/packages/crypto"
"github.com/GenesisCommunity/go-genesis/packages/model"
"github.com/GenesisCommunity/go-genesis/packages/utils"
log "github.com/sirupsen/logrus"
)
var ErrDuplicatedTx = errors.New("Duplicated transaction")
// InsertInLogTx is inserting tx in log
func InsertInLogTx(transaction *model.DbTransaction, binaryTx []byte, time int64) error {
txHash, err := crypto.Hash(binaryTx)
if err != nil {
log.WithFields(log.Fields{"error": err, "type": consts.CryptoError}).Fatal("hashing binary tx")
}
ltx := &model.LogTransaction{Hash: txHash, Time: time}
err = ltx.Create(transaction)
if err != nil {
log.WithFields(log.Fields{"error": err, "type": consts.DBError}).Error("insert logged transaction")
return utils.ErrInfo(err)
}
return nil
}
// CheckLogTx checks if this transaction exists
// And it would have successfully passed a frontal test
func CheckLogTx(txBinary []byte, transactions, txQueue bool) error {
searchedHash, err := crypto.Hash(txBinary)
if err != nil {
log.WithFields(log.Fields{"type": consts.CryptoError, "error": err}).Fatal(err)
}
logTx := &model.LogTransaction{}
found, err := logTx.GetByHash(searchedHash)
if err != nil {
log.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("getting log transaction by hash")
return utils.ErrInfo(err)
}
if found {
log.WithFields(log.Fields{"tx_hash": searchedHash, "type": consts.DuplicateObject}).Error("double tx in log transactions")
return ErrDuplicatedTx
}
if transactions {
// check for duplicate transaction
tx := &model.Transaction{}
_, err := tx.GetVerified(searchedHash)
if err != nil {
log.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("getting verified transaction")
return utils.ErrInfo(err)
}
if len(tx.Hash) > 0 {
log.WithFields(log.Fields{"tx_hash": tx.Hash, "type": consts.DuplicateObject}).Error("double tx in transactions")
return ErrDuplicatedTx
}
}
if txQueue {
// check for duplicate transaction from queue
qtx := &model.QueueTx{}
found, err := qtx.GetByHash(nil, searchedHash)
if found {
log.WithFields(log.Fields{"tx_hash": searchedHash, "type": consts.DuplicateObject}).Error("double tx in queue")
return ErrDuplicatedTx
}
if err != nil {
log.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("getting transaction from queue")
return utils.ErrInfo(err)
}
}
return nil
}
// DeleteQueueTx deletes a transaction from the queue
func DeleteQueueTx(dbTransaction *model.DbTransaction, hash []byte) error {
delQueueTx := &model.QueueTx{Hash: hash}
err := delQueueTx.DeleteTx(dbTransaction)
if err != nil {
log.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("deleting transaction from queue")
return utils.ErrInfo(err)
}
// Because we process transactions with verified=0 in queue_parser_tx, after processing we need to delete them
_, err = model.DeleteTransactionIfUnused(dbTransaction, hash)
if err != nil {
log.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("deleting transaction if unused")
return utils.ErrInfo(err)
}
return nil
}
func MarkTransactionBad(dbTransaction *model.DbTransaction, hash []byte, errText string) error {
if hash == nil {
return nil
}
model.MarkTransactionUsed(dbTransaction, hash)
if len(errText) > 255 {
errText = errText[:255]
}
// set loglevel as error because default level setups to "error"
log.WithFields(log.Fields{"type": consts.BadTxError, "tx_hash": string(hash), "error": errText}).Error("tx marked as bad")
// looks like there is not hash in queue_tx in this moment
qtx := &model.QueueTx{}
_, err := qtx.GetByHash(dbTransaction, hash)
if err != nil {
log.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("getting tx by hash from queue")
}
if qtx.FromGate == 0 {
m := &model.TransactionStatus{}
err = m.SetError(dbTransaction, errText, hash)
if err != nil {
log.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("setting transaction status error")
return utils.ErrInfo(err)
}
}
err = DeleteQueueTx(dbTransaction, hash)
if err != nil {
log.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("deleting transaction from queue")
return utils.ErrInfo(err)
}
return nil
}
// TxParser writes transactions into the queue
func ProcessQueueTransaction(dbTransaction *model.DbTransaction, hash, binaryTx []byte, myTx bool) error {
// get parameters for "struct" transactions
txType, keyID := GetTxTypeAndUserID(binaryTx)
header, err := CheckTransaction(binaryTx)
if err != nil {
MarkTransactionBad(dbTransaction, hash, err.Error())
return err
}
if !( /*txType > 127 ||*/ consts.IsStruct(int(txType))) {
if header == nil {
log.WithFields(log.Fields{"type": consts.EmptyObject}).Error("tx header is nil")
return utils.ErrInfo(errors.New("header is nil"))
}
keyID = header.KeyID
}
if keyID == 0 {
errStr := "undefined keyID"
MarkTransactionBad(dbTransaction, hash, errStr)
return errors.New(errStr)
}
tx := &model.Transaction{}
_, err = tx.Get(hash)
if err != nil {
log.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("getting transaction by hash")
return utils.ErrInfo(err)
}
counter := tx.Counter
counter++
_, err = model.DeleteTransactionByHash(hash)
if err != nil {
log.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("deleting transaction by hash")
return utils.ErrInfo(err)
}
// put with verified=1
newTx := &model.Transaction{
Hash: hash,
Data: binaryTx,
Type: int8(txType),
KeyID: keyID,
Counter: counter,
Verified: 1,
HighRate: tx.HighRate,
}
err = newTx.Create()
if err != nil {
log.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("creating new transaction")
return utils.ErrInfo(err)
}
// remove transaction from the queue (with verified=0)
err = DeleteQueueTx(dbTransaction, hash)
if err != nil {
log.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("deleting transaction from queue")
return utils.ErrInfo(err)
}
return nil
}
// AllTxParser parses new transactions
func ProcessTransactionsQueue(dbTransaction *model.DbTransaction) error {
all, err := model.GetAllUnverifiedAndUnusedTransactions()
if err != nil {
log.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("getting all unverified and unused transactions")
return err
}
for _, data := range all {
err := ProcessQueueTransaction(dbTransaction, data.Hash, data.Data, false)
if err != nil {
return utils.ErrInfo(err)
}
log.Debug("transaction parsed successfully")
}
return nil
}