forked from AplaProject/go-apla
-
Notifications
You must be signed in to change notification settings - Fork 0
/
block.go
129 lines (112 loc) · 3.82 KB
/
block.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
// 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 rollback
import (
"bytes"
"fmt"
"github.com/ug93tad/go-apla/packages/block"
"github.com/ug93tad/go-apla/packages/consts"
"github.com/ug93tad/go-apla/packages/model"
"github.com/ug93tad/go-apla/packages/smart"
"github.com/ug93tad/go-apla/packages/transaction"
"github.com/ug93tad/go-apla/packages/utils"
log "github.com/sirupsen/logrus"
)
// BlockRollback is blocking rollback
func RollbackBlock(data []byte, deleteBlock bool) error {
buf := bytes.NewBuffer(data)
if buf.Len() == 0 {
log.WithFields(log.Fields{"type": consts.EmptyObject}).Error("empty buffer")
return fmt.Errorf("empty buffer")
}
block, err := block.UnmarshallBlock(buf, false)
if err != nil {
return err
}
dbTransaction, err := model.StartTransaction()
if err != nil {
log.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("starting transaction")
return err
}
err = rollbackBlock(dbTransaction, block)
if err != nil {
dbTransaction.Rollback()
return err
}
if deleteBlock {
b := &model.Block{}
err = b.DeleteById(dbTransaction, block.Header.BlockID)
if err != nil {
log.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("deleting block by id")
dbTransaction.Rollback()
return err
}
}
err = dbTransaction.Commit()
return err
}
func rollbackBlock(dbTransaction *model.DbTransaction, block *block.Block) error {
// rollback transactions in reverse order
logger := block.GetLogger()
for i := len(block.Transactions) - 1; i >= 0; i-- {
t := block.Transactions[i]
t.DbTransaction = dbTransaction
_, err := model.MarkTransactionUnusedAndUnverified(dbTransaction, t.TxHash)
if err != nil {
logger.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("starting transaction")
return err
}
_, err = model.DeleteLogTransactionsByHash(dbTransaction, t.TxHash)
if err != nil {
logger.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("deleting log transactions by hash")
return err
}
ts := &model.TransactionStatus{}
err = ts.UpdateBlockID(dbTransaction, 0, t.TxHash)
if err != nil {
logger.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("updating block id in transaction status")
return err
}
_, err = model.DeleteQueueTxByHash(dbTransaction, t.TxHash)
if err != nil {
logger.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("deleting transacion from queue by hash")
return err
}
if t.TxContract != nil {
if _, err := t.CallContract(smart.CallInit | smart.CallRollback); err != nil {
return err
}
if err = rollbackTransaction(t.TxHash, t.DbTransaction, logger); err != nil {
return err
}
} else {
MethodName := consts.TxTypes[int(t.TxType)]
txParser, err := transaction.GetTransaction(t, MethodName)
if err != nil {
return utils.ErrInfo(err)
}
result := txParser.Init()
if _, ok := result.(error); ok {
return utils.ErrInfo(result.(error))
}
result = txParser.Rollback()
if _, ok := result.(error); ok {
return utils.ErrInfo(result.(error))
}
}
}
return nil
}