forked from berachain/erigon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testutil.go
215 lines (186 loc) · 6 KB
/
testutil.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
package stagedsync
import (
"fmt"
"math/big"
"testing"
"github.com/holiman/uint256"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/stretchr/testify/assert"
"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/core/state"
"github.com/ledgerwatch/erigon/core/types/accounts"
)
const (
staticCodeStaticIncarnations = iota // no incarnation changes, no code changes
changeCodeWithIncarnations // code changes with incarnation
changeCodeIndepenentlyOfIncarnations // code changes with and without incarnation
)
func compareCurrentState(
t *testing.T,
db1 kv.Tx,
db2 kv.Tx,
buckets ...string,
) {
for _, bucket := range buckets {
compareBucket(t, db1, db2, bucket)
}
}
func compareBucket(t *testing.T, db1, db2 kv.Tx, bucketName string) {
var err error
bucket1 := make(map[string][]byte)
err = db1.ForEach(bucketName, nil, func(k, v []byte) error {
bucket1[string(k)] = v
return nil
})
assert.NoError(t, err)
bucket2 := make(map[string][]byte)
err = db2.ForEach(bucketName, nil, func(k, v []byte) error {
bucket2[string(k)] = v
return nil
})
assert.NoError(t, err)
assert.Equalf(t, bucket1 /*expected*/, bucket2 /*actual*/, "bucket %q", bucketName)
}
type stateWriterGen func(uint64) state.WriterWithChangeSets
func hashedWriterGen(tx kv.RwTx) stateWriterGen {
return func(blockNum uint64) state.WriterWithChangeSets {
return state.NewDbStateWriter(tx, blockNum)
}
}
func plainWriterGen(tx kv.RwTx) stateWriterGen {
return func(blockNum uint64) state.WriterWithChangeSets {
return state.NewPlainStateWriter(tx, tx, blockNum)
}
}
type testGenHook func(n, from, numberOfBlocks uint64)
func generateBlocks2(t *testing.T, from uint64, numberOfBlocks uint64, blockWriter state.StateWriter, beforeBlock, afterBlock testGenHook, difficulty int) {
acc1 := accounts.NewAccount()
acc1.Incarnation = 1
acc1.Initialised = true
acc1.Balance.SetUint64(0)
acc2 := accounts.NewAccount()
acc2.Incarnation = 0
acc2.Initialised = true
acc2.Balance.SetUint64(0)
testAccounts := []*accounts.Account{
&acc1,
&acc2,
}
for blockNumber := uint64(1); blockNumber < from+numberOfBlocks; blockNumber++ {
beforeBlock(blockNumber, from, numberOfBlocks)
updateIncarnation := difficulty != staticCodeStaticIncarnations && blockNumber%10 == 0
for i, oldAcc := range testAccounts {
addr := libcommon.HexToAddress(fmt.Sprintf("0x1234567890%d", i))
newAcc := oldAcc.SelfCopy()
newAcc.Balance.SetUint64(blockNumber)
if updateIncarnation && oldAcc.Incarnation > 0 /* only update for contracts */ {
newAcc.Incarnation = oldAcc.Incarnation + 1
}
if blockNumber == 1 && newAcc.Incarnation > 0 {
if blockNumber >= from {
if err := blockWriter.CreateContract(addr); err != nil {
t.Fatal(err)
}
}
}
if blockNumber == 1 || updateIncarnation || difficulty == changeCodeIndepenentlyOfIncarnations {
if newAcc.Incarnation > 0 {
code := []byte(fmt.Sprintf("acc-code-%v", blockNumber))
codeHash, _ := common.HashData(code)
if blockNumber >= from {
if err := blockWriter.UpdateAccountCode(addr, newAcc.Incarnation, codeHash, code); err != nil {
t.Fatal(err)
}
}
newAcc.CodeHash = codeHash
}
}
if newAcc.Incarnation > 0 {
var oldValue, newValue uint256.Int
newValue.SetOne()
var location libcommon.Hash
location.SetBytes(big.NewInt(int64(blockNumber)).Bytes())
if blockNumber >= from {
if err := blockWriter.WriteAccountStorage(addr, newAcc.Incarnation, &location, &oldValue, &newValue); err != nil {
t.Fatal(err)
}
}
}
if blockNumber >= from {
if err := blockWriter.UpdateAccountData(addr, oldAcc, newAcc); err != nil {
t.Fatal(err)
}
}
testAccounts[i] = newAcc
}
afterBlock(blockNumber, from, numberOfBlocks)
}
}
func generateBlocks(t *testing.T, from uint64, numberOfBlocks uint64, stateWriterGen stateWriterGen, difficulty int) {
acc1 := accounts.NewAccount()
acc1.Incarnation = 1
acc1.Initialised = true
acc1.Balance.SetUint64(0)
acc2 := accounts.NewAccount()
acc2.Incarnation = 0
acc2.Initialised = true
acc2.Balance.SetUint64(0)
testAccounts := []*accounts.Account{
&acc1,
&acc2,
}
for blockNumber := uint64(1); blockNumber < from+numberOfBlocks; blockNumber++ {
updateIncarnation := difficulty != staticCodeStaticIncarnations && blockNumber%10 == 0
blockWriter := stateWriterGen(blockNumber)
for i, oldAcc := range testAccounts {
addr := libcommon.HexToAddress(fmt.Sprintf("0x1234567890%d", i))
newAcc := oldAcc.SelfCopy()
newAcc.Balance.SetUint64(blockNumber)
if updateIncarnation && oldAcc.Incarnation > 0 /* only update for contracts */ {
newAcc.Incarnation = oldAcc.Incarnation + 1
}
if blockNumber == 1 && newAcc.Incarnation > 0 {
if blockNumber >= from {
if err := blockWriter.CreateContract(addr); err != nil {
t.Fatal(err)
}
}
}
if blockNumber == 1 || updateIncarnation || difficulty == changeCodeIndepenentlyOfIncarnations {
if newAcc.Incarnation > 0 {
code := []byte(fmt.Sprintf("acc-code-%v", blockNumber))
codeHash, _ := common.HashData(code)
if blockNumber >= from {
if err := blockWriter.UpdateAccountCode(addr, newAcc.Incarnation, codeHash, code); err != nil {
t.Fatal(err)
}
}
newAcc.CodeHash = codeHash
}
}
if newAcc.Incarnation > 0 {
var oldValue, newValue uint256.Int
newValue.SetOne()
var location libcommon.Hash
location.SetBytes(big.NewInt(int64(blockNumber)).Bytes())
if blockNumber >= from {
if err := blockWriter.WriteAccountStorage(addr, newAcc.Incarnation, &location, &oldValue, &newValue); err != nil {
t.Fatal(err)
}
}
}
if blockNumber >= from {
if err := blockWriter.UpdateAccountData(addr, oldAcc, newAcc); err != nil {
t.Fatal(err)
}
}
testAccounts[i] = newAcc
}
if blockNumber >= from {
if err := blockWriter.WriteChangeSets(); err != nil {
t.Fatal(err)
}
}
}
}