forked from erigontech/erigon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testutil.go
141 lines (123 loc) · 3.88 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
package stagedsync
import (
"context"
"fmt"
"math/big"
"testing"
"github.com/holiman/uint256"
"github.com/ledgerwatch/turbo-geth/common"
"github.com/ledgerwatch/turbo-geth/core/state"
"github.com/ledgerwatch/turbo-geth/core/types/accounts"
"github.com/ledgerwatch/turbo-geth/ethdb"
"github.com/stretchr/testify/assert"
)
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 ethdb.Database,
db2 ethdb.Database,
buckets ...string,
) {
for _, bucket := range buckets {
compareBucket(t, db1, db2, bucket)
}
}
func compareBucket(t *testing.T, db1, db2 ethdb.Database, bucketName string) {
var err error
bucket1 := make(map[string][]byte)
err = db1.Walk(bucketName, nil, 0, func(k, v []byte) (bool, error) {
bucket1[string(k)] = v
return true, nil
})
assert.Nil(t, err)
bucket2 := make(map[string][]byte)
err = db2.Walk(bucketName, nil, 0, func(k, v []byte) (bool, error) {
bucket2[string(k)] = v
return true, nil
})
assert.Nil(t, err)
assert.Equal(t, bucket1 /*expected*/, bucket2 /*actual*/)
}
type stateWriterGen func(uint64) state.WriterWithChangeSets
func hashedWriterGen(db ethdb.Database) stateWriterGen {
return func(blockNum uint64) state.WriterWithChangeSets {
return state.NewDbStateWriter(db, blockNum)
}
}
func plainWriterGen(db ethdb.Database) stateWriterGen {
return func(blockNum uint64) state.WriterWithChangeSets {
return state.NewPlainStateWriter(db, nil, blockNum)
}
}
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,
}
ctx := context.Background()
for blockNumber := uint64(1); blockNumber < from+numberOfBlocks; blockNumber++ {
updateIncarnation := difficulty != staticCodeStaticIncarnations && blockNumber%10 == 0
blockWriter := stateWriterGen(blockNumber)
for i, oldAcc := range testAccounts {
addr := common.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 common.Hash
location.SetBytes(big.NewInt(int64(blockNumber)).Bytes())
if blockNumber >= from {
if err := blockWriter.WriteAccountStorage(ctx, addr, newAcc.Incarnation, &location, &oldValue, &newValue); err != nil {
t.Fatal(err)
}
}
}
if blockNumber >= from {
if err := blockWriter.UpdateAccountData(ctx, addr, oldAcc /* original */, newAcc /* new account */); err != nil {
t.Fatal(err)
}
}
testAccounts[i] = newAcc
}
if blockNumber >= from {
if err := blockWriter.WriteChangeSets(); err != nil {
t.Fatal(err)
}
}
}
}