forked from ezdapps/backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.go
More file actions
583 lines (541 loc) · 19.3 KB
/
common.go
File metadata and controls
583 lines (541 loc) · 19.3 KB
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
// Copyright 2016 The go-daylight Authors
// This file is part of the go-daylight library.
//
// The go-daylight library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-daylight library 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-daylight library. If not, see <http://www.gnu.org/licenses/>.
package parser
import (
"flag"
"fmt"
"os"
"reflect"
"strconv"
"strings"
"bytes"
"github.com/AplaProject/go-apla/packages/consts"
"github.com/AplaProject/go-apla/packages/converter"
"github.com/AplaProject/go-apla/packages/crypto"
"github.com/AplaProject/go-apla/packages/model"
"github.com/AplaProject/go-apla/packages/smart"
"github.com/AplaProject/go-apla/packages/templatev2"
"github.com/AplaProject/go-apla/packages/utils"
"github.com/AplaProject/go-apla/packages/utils/tx"
"github.com/shopspring/decimal"
log "github.com/sirupsen/logrus"
)
// GetTxTypeAndUserID returns tx type, wallet and citizen id from the block data
func GetTxTypeAndUserID(binaryBlock []byte) (txType int64, keyID int64) {
tmp := binaryBlock[:]
txType = converter.BinToDecBytesShift(&binaryBlock, 1)
if consts.IsStruct(int(txType)) {
var txHead consts.TxHeader
converter.BinUnmarshal(&tmp, &txHead)
keyID = txHead.KeyID
}
return
}
func GetBlockDataFromBlockChain(blockID int64) (*utils.BlockData, error) {
BlockData := new(utils.BlockData)
block := &model.Block{}
_, err := block.Get(blockID)
if err != nil {
log.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("Getting block by ID")
return BlockData, utils.ErrInfo(err)
}
header, err := ParseBlockHeader(bytes.NewBuffer(block.Data))
if err != nil {
return nil, utils.ErrInfo(err)
}
BlockData = &header
BlockData.Hash = block.Hash
return BlockData, nil
}
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
}
func IsCustomTable(table string) (isCustom bool, err error) {
if table[0] >= '0' && table[0] <= '9' {
if off := strings.IndexByte(table, '_'); off > 0 {
prefix := table[:off]
tables := &model.Table{}
tables.SetTablePrefix(prefix)
found, err := tables.Get(table[off+1:])
if err != nil {
log.WithFields(log.Fields{"error": err, "type": consts.DBError}).Error("getting table")
return false, err
}
if found {
return true, nil
}
}
}
return false, nil
}
func IsState(transaction *model.DbTransaction, country string) (int64, error) {
ids, err := model.GetAllSystemStatesIDs()
if err != nil {
log.WithFields(log.Fields{"error": err, "type": consts.DBError}).Error("get all system states ids")
return 0, err
}
for _, id := range ids {
sp := &model.StateParameter{}
sp.SetTablePrefix(converter.Int64ToStr(id))
_, err = sp.Get(transaction, "state_name")
if err != nil {
log.WithFields(log.Fields{"error": err, "type": consts.DBError}).Error("state get by name transaction")
return 0, err
}
if strings.ToLower(sp.Name) == strings.ToLower(country) {
return id, nil
}
}
return 0, nil
}
func init() {
flag.Parse()
}
type ParserInterface interface {
Init() error
Validate() error
Action() error
Rollback() error
Header() *tx.Header
}
func GetTablePrefix(global string, stateId int64) (string, error) {
globalInt, err := strconv.Atoi(global)
if err != nil {
log.WithFields(log.Fields{"error": err, "type": consts.ConvertionError}).Error("converting global to int")
return "", err
}
stateIdStr := converter.Int64ToStr(stateId)
if globalInt == 1 {
return "global", nil
}
return stateIdStr, nil
}
func GetParser(p *Parser, txType string) (ParserInterface, error) {
switch txType {
case "FirstBlock":
return &FirstBlockParser{p}, nil
}
log.WithFields(log.Fields{"tx_type": txType, "type": consts.UnknownObject}).Error("unknown txType")
return nil, fmt.Errorf("Unknown txType: %s", txType)
}
type txMapsType struct {
Int64 map[string]int64
String map[string]string
Bytes map[string][]byte
Float64 map[string]float64
Money map[string]float64
Decimal map[string]decimal.Decimal
}
// Parser is a structure for parsing transactions
type Parser struct {
BlockData *utils.BlockData
PrevBlock *utils.BlockData
dataType int
blockData []byte
CurrentVersion string
MrklRoot []byte
PublicKeys [][]byte
TxBinaryData []byte // transaction binary data
TxFullData []byte // full transaction, with type and data
TxHash []byte
TxSlice [][]byte
TxMap map[string][]byte
TxIds int // count of transactions
TxKeyID int64
TxEcosystemIDStr string
TxEcosystemID int64
TxNodePosition uint32
TxTime int64
TxType int64
TxCost int64 // Maximum cost of executing contract
TxUsedCost decimal.Decimal // Used cost of CPU resources
TxPtr interface{} // Pointer to the corresponding struct in consts/struct.go
TxData map[string]interface{}
TxSmart *tx.SmartContract
TxContract *smart.Contract
TxHeader *tx.Header
txParser ParserInterface
DbTransaction *model.DbTransaction
AllPkeys map[string]string
}
func (p Parser) GetLogger() *log.Entry {
if p.BlockData != nil && p.PrevBlock != nil {
logger := log.WithFields(log.Fields{"block_id": p.BlockData.BlockID, "block_time": p.BlockData.Time, "block_wallet_id": p.BlockData.KeyID, "block_state_id": p.BlockData.EcosystemID, "block_hash": p.BlockData.Hash, "block_version": p.BlockData.Version, "prev_block_id": p.PrevBlock.BlockID, "prev_block_time": p.PrevBlock.Time, "prev_block_wallet_id": p.PrevBlock.KeyID, "prev_block_state_id": p.PrevBlock.EcosystemID, "prev_block_hash": p.PrevBlock.Hash, "prev_block_version": p.PrevBlock.Version, "tx_type": p.TxType, "tx_time": p.TxTime, "tx_state_id": p.TxEcosystemID, "tx_wallet_id": p.TxKeyID})
return logger
}
if p.BlockData != nil {
logger := log.WithFields(log.Fields{"block_id": p.BlockData.BlockID, "block_time": p.BlockData.Time, "block_wallet_id": p.BlockData.KeyID, "block_state_id": p.BlockData.EcosystemID, "block_hash": p.BlockData.Hash, "block_version": p.BlockData.Version, "tx_type": p.TxType, "tx_time": p.TxTime, "tx_state_id": p.TxEcosystemID, "tx_wallet_id": p.TxKeyID})
return logger
}
if p.PrevBlock != nil {
logger := log.WithFields(log.Fields{"prev_block_id": p.PrevBlock.BlockID, "prev_block_time": p.PrevBlock.Time, "prev_block_wallet_id": p.PrevBlock.KeyID, "prev_block_state_id": p.PrevBlock.EcosystemID, "prev_block_hash": p.PrevBlock.Hash, "prev_block_version": p.PrevBlock.Version, "tx_type": p.TxType, "tx_time": p.TxTime, "tx_state_id": p.TxEcosystemID, "tx_wallet_id": p.TxKeyID})
return logger
}
logger := log.WithFields(log.Fields{"tx_type": p.TxType, "tx_time": p.TxTime, "tx_state_id": p.TxEcosystemID, "tx_wallet_id": p.TxKeyID})
return logger
}
// ClearTmp deletes temporary files
func ClearTmp(blocks map[int64]string) {
for _, tmpFileName := range blocks {
os.Remove(tmpFileName)
}
}
// 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 utils.ErrInfo(fmt.Errorf("double tx in log_transactions %x", searchedHash))
}
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 utils.ErrInfo(fmt.Errorf("double tx in transactions %x", searchedHash))
}
}
if txQueue {
// check for duplicate transaction from queue
qtx := &model.QueueTx{}
found, err := qtx.GetByHash(searchedHash)
if found {
log.WithFields(log.Fields{"tx_hash": searchedHash, "type": consts.DuplicateObject}).Error("double tx in queue")
return utils.ErrInfo(fmt.Errorf("double tx in queue_tx %x", searchedHash))
}
if err != nil {
log.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("getting transaction from queue")
return utils.ErrInfo(err)
}
}
return nil
}
// InsertIntoBlockchain inserts a block into the blockchain
func InsertIntoBlockchain(transaction *model.DbTransaction, block *Block) error {
// for local tests
blockID := block.Header.BlockID
if block.Header.BlockID == 1 {
if *utils.StartBlockID != 0 {
blockID = *utils.StartBlockID
}
}
// record into the block chain
bl := &model.Block{}
err := bl.DeleteById(transaction, blockID)
if err != nil {
log.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("deleting block by id")
return err
}
b := &model.Block{
ID: blockID,
Hash: block.Header.Hash,
Data: block.BinData,
EcosystemID: block.Header.EcosystemID,
KeyID: block.Header.KeyID,
NodePosition: block.Header.NodePosition,
Time: block.Header.Time,
Tx: int32(len(block.Parsers)),
}
err = b.Create(transaction)
if err != nil {
log.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("creating block")
return err
}
return nil
}
func (p *Parser) CheckInputData(data map[string][]interface{}) error {
for k, list := range data {
for _, v := range list {
if !utils.CheckInputData(v, k) {
return fmt.Errorf("incorrect %s: %s", v, k)
}
}
}
return nil
}
// FormatBlockData returns formated block data
func (p *Parser) FormatBlockData() string {
result := ""
if p.BlockData != nil {
v := reflect.ValueOf(*p.BlockData)
typeOfT := v.Type()
if typeOfT.Kind() == reflect.Ptr {
typeOfT = typeOfT.Elem()
}
for i := 0; i < v.NumField(); i++ {
name := typeOfT.Field(i).Name
switch name {
case "BlockId", "Time", "UserId", "Level":
result += "[" + name + "] = " + fmt.Sprintf("%d\n", v.Field(i).Interface())
case "Sign", "Hash", "HeadHash":
result += "[" + name + "] = " + fmt.Sprintf("%x\n", v.Field(i).Interface())
default:
result += "[" + name + "] = " + fmt.Sprintf("%s\n", v.Field(i).Interface())
}
}
}
return result
}
// FormatTxMap returns the formated TxMap
func (p *Parser) FormatTxMap() string {
result := ""
for k, v := range p.TxMap {
switch k {
case "sign":
result += "[" + k + "] = " + fmt.Sprintf("%x\n", v)
default:
result += "[" + k + "] = " + fmt.Sprintf("%s\n", v)
}
}
return result
}
// ErrInfo returns the more detailed error
func (p *Parser) ErrInfo(verr interface{}) error {
var err error
switch verr.(type) {
case error:
err = verr.(error)
case string:
err = fmt.Errorf(verr.(string))
}
return fmt.Errorf("[ERROR] %s (%s)\n%s\n%s", err, utils.Caller(1), p.FormatBlockData(), p.FormatTxMap())
}
// BlockError writes the error of the transaction in the transactions_status table
func (p *Parser) BlockError(err error) {
if len(p.TxHash) == 0 {
return
}
errText := err.Error()
if len(errText) > 255 {
errText = errText[:255]
}
p.DeleteQueueTx(p.TxHash)
ts := &model.TransactionStatus{}
ts.SetError(errText, p.TxHash)
}
// AccessRights checks the access right by executing the condition value
func (p *Parser) AccessRights(condition string, iscondition bool) error {
logger := p.GetLogger()
sp := &model.StateParameter{}
sp.SetTablePrefix(converter.Int64ToStr(p.TxSmart.EcosystemID))
_, err := sp.Get(p.DbTransaction, condition)
if err != nil {
logger.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("getting state parameter by name transaction")
return err
}
conditions := sp.Value
if iscondition {
conditions = sp.Conditions
}
if len(conditions) > 0 {
ret, err := p.EvalIf(conditions)
if err != nil {
logger.WithFields(log.Fields{"type": consts.EvalError, "error": err, "conditions": conditions}).Error("evaluating conditions")
return err
}
if !ret {
logger.WithFields(log.Fields{"type": consts.AccessDenied}).Error("Access denied")
return fmt.Errorf(`Access denied`)
}
} else {
logger.WithFields(log.Fields{"type": consts.EmptyObject, "conditions": condition}).Error("No condition in state_parameters")
return fmt.Errorf(`There is not %s in state_parameters`, condition)
}
return nil
}
// AccessTable checks the access right to the table
func (p *Parser) AccessTable(table, action string) error {
logger := p.GetLogger()
govAccount, _ := templatev2.StateParam(int64(p.TxSmart.EcosystemID), `founder_account`)
if table == fmt.Sprintf(`%d_parameters`, p.TxSmart.EcosystemID) {
if p.TxContract != nil && p.TxKeyID == converter.StrToInt64(govAccount) {
return nil
} else {
logger.WithFields(log.Fields{"type": consts.AccessDenied}).Error("Access denied")
return fmt.Errorf(`Access denied`)
}
}
if isCustom, err := IsCustomTable(table); err != nil {
return err
// TODO: table != ... is left for compatibility temporarily. Remove it
} else if !isCustom && !strings.HasSuffix(table, `_citizenship_requests`) {
logger.WithFields(log.Fields{"table": table, "type": consts.InvalidObject}).Error("is not custom table")
return fmt.Errorf(table + ` is not a custom table`)
}
prefix := table[:strings.IndexByte(table, '_')]
tables := &model.Table{}
tables.SetTablePrefix(prefix)
tablePermission, err := tables.GetPermissions(table[strings.IndexByte(table, '_')+1:], "")
if err != nil {
logger.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("getting table permissions")
return err
}
if len(tablePermission[action]) > 0 {
ret, err := p.EvalIf(tablePermission[action])
if err != nil {
logger.WithFields(log.Fields{"action": action, "permissions": tablePermission[action], "error": err, "type": consts.EvalError}).Error("evaluating table permissions for action")
return err
}
if !ret {
logger.WithFields(log.Fields{"action": action, "permissions": tablePermission[action], "type": consts.EvalError}).Error("access denied")
return fmt.Errorf(`Access denied`)
}
}
return nil
}
// AccessColumns checks access rights to the columns
func (p *Parser) AccessColumns(table string, columns []string) error {
logger := p.GetLogger()
if table == fmt.Sprintf(`%d_parameters`, p.TxSmart.EcosystemID) {
govAccount, _ := templatev2.StateParam(int64(p.TxSmart.EcosystemID), `founder_account`)
if p.TxContract != nil && p.TxKeyID == converter.StrToInt64(govAccount) {
return nil
}
logger.WithFields(log.Fields{"type": consts.AccessDenied}).Error("Access Denied")
return fmt.Errorf(`Access denied`)
}
if isCustom, err := IsCustomTable(table); err != nil {
return err
} else if !isCustom && !strings.HasSuffix(table, `_parameters`) {
logger.WithFields(log.Fields{"table": table, "type": consts.InvalidObject}).Error("is not custom table")
return fmt.Errorf(table + ` is not a custom table`)
}
prefix := table[:strings.IndexByte(table, '_')]
tables := &model.Table{}
tables.SetTablePrefix(prefix)
columnsAndPermissions, err := tables.GetColumns(table, "")
if err != nil {
logger.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("getting table columns")
return err
}
for _, col := range columns {
var (
cond string
ok bool
)
cond, ok = columnsAndPermissions[converter.Sanitize(col, ``)]
if !ok {
cond, ok = columnsAndPermissions[`*`]
}
if ok && len(cond) > 0 {
ret, err := p.EvalIf(cond)
if err != nil {
logger.WithFields(log.Fields{"condition": cond, "column": col, "type": consts.EvalError}).Error("evaluating condition")
return err
}
if !ret {
logger.WithFields(log.Fields{"condition": cond, "column": col, "type": consts.AccessDenied}).Error("action denied")
return fmt.Errorf(`Access denied`)
}
}
}
return nil
}
func (p *Parser) AccessChange(table, name, global string, stateId int64) error {
logger := p.GetLogger()
prefix, err := GetTablePrefix(global, stateId)
if err != nil {
return err
}
var conditions string
switch table {
case "pages":
page := &model.Page{}
page.SetTablePrefix(prefix)
if _, err := page.Get(name); err != nil {
logger.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("getting page")
return err
}
conditions = page.Conditions
case "menus":
menu := &model.Menu{}
menu.SetTablePrefix(prefix)
if _, err := menu.Get(name); err != nil {
logger.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("getting menu")
return err
}
conditions = menu.Conditions
}
if len(conditions) > 0 {
ret, err := p.EvalIf(conditions)
if err != nil {
log.WithFields(log.Fields{"type": consts.EvalError, "error": err}).Error("evaluating conditions")
return err
}
if !ret {
log.WithFields(log.Fields{"type": consts.AccessDenied}).Error("Access denied")
return fmt.Errorf(`Access denied`)
}
} else {
log.WithFields(log.Fields{"type": consts.EmptyObject, "table": prefix + "_" + table}).Error("There is not conditions in")
return fmt.Errorf(`There is not conditions in %s`, prefix+`_`+table)
}
return nil
}
func (p *Parser) getEGSPrice(name string) (decimal.Decimal, error) {
logger := p.GetLogger()
syspar := &model.SystemParameter{}
fPrice, err := syspar.GetValueParameterByName("op_price", name)
if err != nil {
logger.WithFields(log.Fields{"error": err, "type": consts.DBError}).Error("getting value parameter by name")
return decimal.New(0, 0), p.ErrInfo(err)
}
if fPrice == nil {
return decimal.New(0, 0), nil
}
p.TxCost = 0
p.TxUsedCost, _ = decimal.NewFromString(*fPrice)
systemParam := &model.SystemParameter{}
_, err = systemParam.Get("fuel_rate")
if err != nil {
logger.WithFields(log.Fields{"error": err, "type": consts.DBError}).Fatal("getting system parameter")
}
fuelRate, err := decimal.NewFromString(systemParam.Value)
if err != nil {
logger.WithFields(log.Fields{"error": err, "type": consts.ConvertionError, "value": systemParam.Value}).Error("converting fuel rate system parameter from string to decimal")
return decimal.New(0, 0), p.ErrInfo(err)
}
if fuelRate.Cmp(decimal.New(0, 0)) <= 0 {
logger.Error("fuel rate is less than zero")
return decimal.New(0, 0), fmt.Errorf(`fuel rate must be greater than 0`)
}
return p.TxUsedCost.Mul(fuelRate), nil
}