forked from febe19/bazo-miner
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstaterollback_test.go
206 lines (168 loc) · 6.32 KB
/
staterollback_test.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
package miner
import (
"github.com/julwil/bazo-miner/crypto"
"math/rand"
"reflect"
"testing"
"time"
"github.com/julwil/bazo-miner/protocol"
"github.com/julwil/bazo-miner/storage"
)
//Rollback tests for all tx types
func TestFundsStateChangeRollback(t *testing.T) {
cleanAndPrepare()
randVar := rand.New(rand.NewSource(time.Now().Unix()))
accAHash := protocol.SerializeHashContent(accA.Address)
accBHash := protocol.SerializeHashContent(accB.Address)
minerAccHash := protocol.SerializeHashContent(validatorAcc.Address)
var testSize uint32
testSize = 1000
b := newBlock([32]byte{}, [crypto.COMM_PROOF_LENGTH]byte{}, 1)
var funds []*protocol.FundsTx
var feeA, feeB uint64
//State snapshot
rollBackA := accA.Balance
rollBackB := accB.Balance
//Record transaction amounts in this variables
balanceA := accA.Balance
balanceB := accB.Balance
loopMax := int(randVar.Uint32()%testSize + 1)
for i := 0; i < loopMax+1; i++ {
ftx, _ := protocol.ConstrFundsTx(0x01, randVar.Uint64()%1000000+1, randVar.Uint64()%100+1, uint32(i), accAHash, accBHash, PrivKeyAccA, PrivKeyMultiSig, nil)
if addTx(b, ftx) == nil {
funds = append(funds, ftx)
balanceA -= ftx.Amount
feeA += ftx.Fee
balanceB += ftx.Amount
} else {
t.Errorf("Block rejected a valid transaction: %v\n", ftx)
}
ftx2, _ := protocol.ConstrFundsTx(0x01, randVar.Uint64()%1000+1, randVar.Uint64()%100+1, uint32(i), accBHash, accAHash, PrivKeyAccB, PrivKeyMultiSig, nil)
if addTx(b, ftx2) == nil {
funds = append(funds, ftx2)
balanceB -= ftx2.Amount
feeB += ftx2.Fee
balanceA += ftx2.Amount
} else {
t.Errorf("Block rejected a valid transaction: %v\n", ftx2)
}
}
fundsStateChange(funds)
if accA.Balance != balanceA || accB.Balance != balanceB {
t.Error("State update failed!")
}
fundsStateChangeRollback(funds)
if accA.Balance != rollBackA || accB.Balance != rollBackB {
t.Error("Rollback failed!")
}
//collectTxFees is checked below in its own test (to additionally cover overflow scenario)
balBeforeRew := validatorAcc.Balance
reward := 5
collectBlockReward(uint64(reward), minerAccHash)
if validatorAcc.Balance != balBeforeRew+uint64(reward) {
t.Error("Block reward collection failed!")
}
collectBlockRewardRollback(uint64(reward), minerAccHash)
if validatorAcc.Balance != balBeforeRew {
t.Error("Block reward collection rollback failed!")
}
}
func TestAccStateChangeRollback(t *testing.T) {
cleanAndPrepare()
randVar := rand.New(rand.NewSource(time.Now().Unix()))
var testSize uint32
testSize = 1000
var accs []*protocol.AccTx
//Store accs that are to be changed and rolled back in a accTx slice
nullAddress := [64]byte{}
loopMax := int(randVar.Uint32()%testSize) + 1
for i := 0; i < loopMax; i++ {
tx, _, _ := protocol.ConstrAccTx(0, randVar.Uint64()%1000, nullAddress, PrivKeyRoot, nil, nil)
accs = append(accs, tx)
}
accStateChange(accs)
for _, acc := range accs {
accHash := protocol.SerializeHashContent(acc.PubKey)
acc := storage.State[accHash]
if acc == nil {
t.Errorf("Account State failed to update for the following account: %v\n", acc)
}
}
accStateChangeRollback(accs)
for _, acc := range accs {
accHash := protocol.SerializeHashContent(acc.PubKey)
acc := storage.State[accHash]
if acc != nil {
t.Errorf("Account State failed to rollback the following account: %v\n", acc)
}
}
}
func TestConfigStateChangeRollback(t *testing.T) {
cleanAndPrepare()
var configSlice []*protocol.ConfigTx
tx, _ := protocol.ConstrConfigTx(uint8(rand.Uint32()%256), 1, 1000, rand.Uint64(), 0, PrivKeyRoot)
tx2, _ := protocol.ConstrConfigTx(uint8(rand.Uint32()%256), 2, 2000, rand.Uint64(), 0, PrivKeyRoot)
tx3, _ := protocol.ConstrConfigTx(uint8(rand.Uint32()%256), 3, 3000, rand.Uint64(), 0, PrivKeyRoot)
tx4, _ := protocol.ConstrConfigTx(uint8(rand.Uint32()%256), 4, 4000, rand.Uint64(), 0, PrivKeyRoot)
tx5, _ := protocol.ConstrConfigTx(uint8(rand.Uint32()%256), 5, 5000, rand.Uint64(), 0, PrivKeyRoot)
configSlice = append(configSlice, tx)
configSlice = append(configSlice, tx2)
configSlice = append(configSlice, tx3)
configSlice = append(configSlice, tx4)
configSlice = append(configSlice, tx5)
before := *activeParameters
configStateChange(configSlice, [32]byte{'0', '1', '2'})
if reflect.DeepEqual(before, *activeParameters) {
t.Error("No config state change.")
}
configStateChangeRollback(configSlice, [32]byte{'0', '1', '2'})
if !reflect.DeepEqual(before, *activeParameters) {
t.Error("Config state rollback failed.")
}
}
func TestCollectTxFeesRollback(t *testing.T) {
cleanAndPrepare()
randVar := rand.New(rand.NewSource(time.Now().Unix()))
var funds, funds2 []*protocol.FundsTx
accAHash := protocol.SerializeHashContent(accA.Address)
accBHash := protocol.SerializeHashContent(accB.Address)
minerHash := protocol.SerializeHashContent(validatorAcc.Address)
minerBal := validatorAcc.Balance
//Rollback everything
var fee uint64
loopMax := int(randVar.Uint64() % 1000)
for i := 0; i < loopMax+1; i++ {
tx, _ := protocol.ConstrFundsTx(0x01, randVar.Uint64()%1000000+1, randVar.Uint64()%100+1, uint32(i), accAHash, accBHash, PrivKeyAccA, nil, nil)
funds = append(funds, tx)
fee += tx.Fee
}
collectTxFees(nil, funds, nil, nil, minerHash)
if minerBal+fee != validatorAcc.Balance {
t.Errorf("%v + %v != %v\n", minerBal, fee, validatorAcc.Balance)
}
collectTxFeesRollback(nil, funds, nil, nil, minerHash)
if minerBal != validatorAcc.Balance {
t.Errorf("Tx fees rollback failed: %v != %v\n", minerBal, validatorAcc.Balance)
}
validatorAcc.Balance = MAX_MONEY - 100
var fee2 uint64
minerBal = validatorAcc.Balance
//Miner gets fees, the miner account balance will overflow at some point
for i := 2; i < 100; i++ {
tx, _ := protocol.ConstrFundsTx(0x01, randVar.Uint64()%1000000+1, uint64(i), uint32(i), accAHash, accBHash, PrivKeyAccA, nil, nil)
funds2 = append(funds2, tx)
fee2 += tx.Fee
}
accABal := accA.Balance
accBBal := accB.Balance
//Should throw an error and result in a rollback, because of acc balance overflow
tmpBlock := newBlock([32]byte{}, [crypto.COMM_PROOF_LENGTH]byte{}, 1)
tmpBlock.Beneficiary = minerHash
data := blockData{nil, funds2, nil, nil, tmpBlock}
if err := validateState(data); err == nil ||
minerBal != validatorAcc.Balance ||
accA.Balance != accABal ||
accB.Balance != accBBal {
t.Errorf("No rollback resulted, %v != %v\n", minerBal, validatorAcc.Balance)
}
}