forked from febe19/bazo-miner
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblockrollback.go
225 lines (201 loc) · 5.55 KB
/
blockrollback.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
package miner
import (
"errors"
"github.com/julwil/bazo-miner/protocol"
"github.com/julwil/bazo-miner/storage"
)
//Already validated block but not part of the current longest chain.
//No need for an additional state mutex, because this function is called while the blockValidation mutex is actively held.
func rollback(b *protocol.Block) error {
accTxSlice,
fundsTxSlice,
configTxSlice,
stakeTxSlice,
aggTxSlice,
updateTxSlice,
err := preValidateRollback(b)
if err != nil {
return err
}
data := blockData{
accTxSlice,
fundsTxSlice,
configTxSlice,
stakeTxSlice,
aggTxSlice,
nil,
updateTxSlice,
b,
}
//Going back to pre-block system parameters before the state is rolled back.
configStateChangeRollback(data.configTxSlice, b.Hash)
//TODO Does not throw error but crashes
validateStateRollback(data)
postValidateRollback(data)
return nil
}
func preValidateRollback(b *protocol.Block) (
accTxSlice []*protocol.AccTx,
fundsTxSlice []*protocol.FundsTx,
configTxSlice []*protocol.ConfigTx,
stakeTxSlice []*protocol.StakeTx,
aggTxSlice []*protocol.AggTx,
updateTxSlice []*protocol.UpdateTx,
err error) {
//Fetch all transactions from closed storage.
for _, hash := range b.AccTxData {
var accTx *protocol.AccTx
tx := storage.ReadClosedTx(hash)
if tx == nil {
//This should never happen, because all validated transactions are in closed storage.
return nil,
nil,
nil,
nil,
nil,
nil,
errors.New("CRITICAL: Validated accTx was not in the confirmed tx storage")
} else {
accTx = tx.(*protocol.AccTx)
}
accTxSlice = append(accTxSlice, accTx)
}
for _, hash := range b.FundsTxData {
var fundsTx *protocol.FundsTx
tx := storage.ReadClosedTx(hash)
if tx == nil {
return nil,
nil,
nil,
nil,
nil,
nil,
errors.New("CRITICAL: Validated fundsTx was not in the confirmed tx storage")
} else {
fundsTx = tx.(*protocol.FundsTx)
}
fundsTxSlice = append(fundsTxSlice, fundsTx)
}
for _, hash := range b.ConfigTxData {
var configTx *protocol.ConfigTx
tx := storage.ReadClosedTx(hash)
if tx == nil {
return nil,
nil,
nil,
nil,
nil,
nil,
errors.New("CRITICAL: Validated configTx was not in the confirmed tx storage")
} else {
configTx = tx.(*protocol.ConfigTx)
}
configTxSlice = append(configTxSlice, configTx)
}
for _, hash := range b.StakeTxData {
var stakeTx *protocol.StakeTx
tx := storage.ReadClosedTx(hash)
if tx == nil {
return nil,
nil,
nil,
nil,
nil,
nil,
errors.New("CRITICAL: Validated stakeTx was not in the confirmed tx storage")
} else {
stakeTx = tx.(*protocol.StakeTx)
}
stakeTxSlice = append(stakeTxSlice, stakeTx)
}
for _, hash := range b.AggTxData {
var aggTx *protocol.AggTx
tx := storage.ReadClosedTx(hash)
if tx == nil {
tx = storage.ReadOpenTx(hash)
if tx != nil {
}
return nil,
nil,
nil,
nil,
nil,
nil,
errors.New("CRITICAL: Aggregated Transaction was not in the confirmed tx storage")
} else {
aggTx = tx.(*protocol.AggTx)
}
aggTxSlice = append(aggTxSlice, aggTx)
}
return accTxSlice,
fundsTxSlice,
configTxSlice,
stakeTxSlice,
aggTxSlice,
updateTxSlice,
nil
}
func validateStateRollback(data blockData) {
collectSlashRewardRollback(activeParameters.SlashReward, data.block)
collectBlockRewardRollback(activeParameters.BlockReward, data.block.Beneficiary)
collectTxFeesRollback(data.accTxSlice, data.fundsTxSlice, data.configTxSlice, data.stakeTxSlice, data.block.Beneficiary)
stakeStateChangeRollback(data.stakeTxSlice)
fundsStateChangeRollback(data.fundsTxSlice)
aggregatedStateRollback(data.aggTxSlice, data.block.HashWithoutTx, data.block.Beneficiary)
accStateChangeRollback(data.accTxSlice)
}
func postValidateRollback(data blockData) {
//Put all validated txs into invalidated state.
for _, tx := range data.accTxSlice {
storage.WriteOpenTx(tx)
storage.DeleteClosedTx(tx)
}
for _, tx := range data.fundsTxSlice {
storage.WriteOpenTx(tx)
storage.DeleteClosedTx(tx)
}
for _, tx := range data.configTxSlice {
storage.WriteOpenTx(tx)
storage.DeleteClosedTx(tx)
}
for _, tx := range data.stakeTxSlice {
storage.WriteOpenTx(tx)
storage.DeleteClosedTx(tx)
}
for _, tx := range data.aggTxSlice {
//Reopen FundsTx per aggTx
for _, aggregatedTxHash := range tx.AggregatedTxSlice {
trx := storage.ReadClosedTx(aggregatedTxHash)
//Only move transactions which are validated the first time in this block to the Mempool.
switch trx.(type) {
case *protocol.FundsTx:
if trx.(*protocol.FundsTx).Block == data.block.HashWithoutTx {
storage.WriteOpenTx(trx)
storage.DeleteClosedTx(trx)
}
case *protocol.AggTx:
if trx.(*protocol.AggTx).Block == data.block.HashWithoutTx {
storage.WriteOpenTx(trx)
storage.DeleteClosedTx(trx)
}
}
}
storage.WriteOpenTx(tx)
storage.DeleteClosedTx(tx)
}
collectStatisticsRollback(data.block)
//For transactions we switch from closed to open. However, we do not write back blocks
//to open storage, because in case of rollback the chain they belonged to is likely to starve.
storage.DeleteClosedBlock(data.block.Hash)
storage.WriteToReceivedStash(data.block) //Write it to received stash, it will be deleted after X new blocks.
//Save the previous block as the last closed block.
storage.DeleteAllLastClosedBlock()
prevBlock := storage.ReadClosedBlock(data.block.PrevHash)
if prevBlock == nil {
prevBlock = storage.ReadClosedBlock(data.block.PrevHashWithoutTx)
}
if prevBlock == nil {
return
}
storage.WriteLastClosedBlock(prevBlock)
}