/
db.go
223 lines (200 loc) · 7.13 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
214
215
216
217
218
219
220
221
222
223
// Copyright (C) 2017, 2018, 2019 EGAAS S.A.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or (at
// your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
package transaction
import (
"bytes"
"errors"
"time"
"github.com/AplaProject/go-apla/packages/consts"
"github.com/AplaProject/go-apla/packages/model"
"github.com/AplaProject/go-apla/packages/utils"
log "github.com/sirupsen/logrus"
)
var (
ErrDuplicatedTx = errors.New("Duplicated transaction")
ErrNotComeTime = errors.New("Transaction processing time has not come")
ErrExpiredTime = errors.New("Transaction processing time is expired")
ErrEarlyTime = utils.WithBan(errors.New("Early transaction time"))
ErrEmptyKey = utils.WithBan(errors.New("KeyID is empty"))
)
// InsertInLogTx is inserting tx in log
func InsertInLogTx(t *Transaction, blockID int64) error {
ltx := &model.LogTransaction{Hash: t.TxHash, Block: blockID}
if err := ltx.Create(t.DbTransaction); 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(txHash []byte, transactions, txQueue bool) error {
logTx := &model.LogTransaction{}
found, err := logTx.GetByHash(txHash)
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": txHash, "type": consts.DuplicateObject}).Error("double tx in log transactions")
return ErrDuplicatedTx
}
if transactions {
// check for duplicate transaction
tx := &model.Transaction{}
_, err := tx.GetVerified(txHash)
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, txHash)
if found {
log.WithFields(log.Fields{"tx_hash": txHash, "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": 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 {
t, err := UnmarshallTransaction(bytes.NewBuffer(binaryTx), true)
if err != nil {
MarkTransactionBad(dbTransaction, hash, err.Error())
return err
}
if err = t.Check(time.Now().Unix(), true); err != nil {
if err != ErrEarlyTime {
MarkTransactionBad(dbTransaction, hash, err.Error())
return err
}
return nil
}
if t.TxKeyID == 0 {
errStr := "undefined keyID"
MarkTransactionBad(dbTransaction, hash, errStr)
return errors.New(errStr)
}
var found bool
tx := &model.Transaction{}
found, 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)
}
if found {
_, 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(t.TxType),
KeyID: t.TxKeyID,
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
}