-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathgenesis_test.go
407 lines (385 loc) · 16.1 KB
/
genesis_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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
// Modifications Copyright 2024 The Kaia Authors
// Modifications Copyright 2018 The klaytn Authors
// Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum 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-ethereum 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-ethereum library. If not, see <http://www.gnu.org/licenses/>.
//
// This file is derived from core/genesis_test.go (2018/06/04).
// Modified and improved for the klaytn development.
// Modified and improved for the Kaia development.
package blockchain
import (
"fmt"
"math/big"
"reflect"
"testing"
"github.com/davecgh/go-spew/spew"
"github.com/kaiachain/kaia/blockchain/types"
"github.com/kaiachain/kaia/blockchain/vm"
"github.com/kaiachain/kaia/common"
"github.com/kaiachain/kaia/consensus/gxhash"
"github.com/kaiachain/kaia/params"
"github.com/kaiachain/kaia/storage/database"
"github.com/stretchr/testify/assert"
)
// TestDefaultGenesisBlock tests the genesis block generation functions: DefaultGenesisBlock, DefaultKairosGenesisBlock
func TestDefaultGenesisBlock(t *testing.T) {
block := genMainnetGenesisBlock().ToBlock(common.Hash{}, nil)
if block.Hash() != params.MainnetGenesisHash {
t.Errorf("wrong Mainnet genesis hash, got %v, want %v", block.Hash(), params.MainnetGenesisHash)
}
block = genKairosGenesisBlock().ToBlock(common.Hash{}, nil)
if block.Hash() != params.KairosGenesisHash {
t.Errorf("wrong Kairos genesis hash, got %v, want %v", block.Hash(), params.KairosGenesisHash)
}
}
// TestHardCodedChainConfigUpdate tests the public network's chainConfig update.
func TestHardCodedChainConfigUpdate(t *testing.T) {
mainnetGenesisBlock, kairosGenesisBlock := genMainnetGenesisBlock(), genKairosGenesisBlock()
tests := []struct {
name string
newHFBlock *big.Int
originHFBlock *big.Int
fn func(database.DBManager, *big.Int) (*params.ChainConfig, common.Hash, error)
wantConfig *params.ChainConfig // expect value of the SetupGenesisBlock's first return value
wantHash common.Hash
wantErr error
wantStoredConfig *params.ChainConfig // expect value of the stored config in DB
resetFn func(*big.Int)
}{
{
name: "Mainnet chainConfig update",
newHFBlock: big.NewInt(3),
fn: func(db database.DBManager, newHFBlock *big.Int) (*params.ChainConfig, common.Hash, error) {
mainnetGenesisBlock.MustCommit(db)
mainnetGenesisBlock.Config.IstanbulCompatibleBlock = newHFBlock
return SetupGenesisBlock(db, mainnetGenesisBlock, params.MainnetNetworkId, false, false)
},
wantHash: params.MainnetGenesisHash,
wantConfig: mainnetGenesisBlock.Config,
wantStoredConfig: mainnetGenesisBlock.Config,
},
// TODO-Kaia: add more Mainnet test cases after Mainnet hard fork block numbers are added
{
// Because of the fork-ordering check logic, the istanbulCompatibleBlock should be less than the londonCompatibleBlock
name: "Kairos chainConfig update - correct hard-fork block number order",
newHFBlock: big.NewInt(79999999),
fn: func(db database.DBManager, newHFBlock *big.Int) (*params.ChainConfig, common.Hash, error) {
kairosGenesisBlock.MustCommit(db)
kairosGenesisBlock.Config.IstanbulCompatibleBlock = newHFBlock
return SetupGenesisBlock(db, kairosGenesisBlock, params.KairosNetworkId, false, false)
},
wantHash: params.KairosGenesisHash,
wantConfig: kairosGenesisBlock.Config,
wantStoredConfig: kairosGenesisBlock.Config,
},
{
// This test fails because the new istanbulCompatibleBlock(90909999) is larger than londonCompatibleBlock(80295291)
name: "Kairos chainConfig update - wrong hard-fork block number order",
newHFBlock: big.NewInt(90909999),
fn: func(db database.DBManager, newHFBlock *big.Int) (*params.ChainConfig, common.Hash, error) {
kairosGenesisBlock.MustCommit(db)
kairosGenesisBlock.Config.IstanbulCompatibleBlock = newHFBlock
return SetupGenesisBlock(db, kairosGenesisBlock, params.KairosNetworkId, false, false)
},
wantHash: common.Hash{},
wantConfig: kairosGenesisBlock.Config,
wantStoredConfig: nil,
wantErr: fmt.Errorf("unsupported fork ordering: %v enabled at %v, but %v enabled at %v",
"istanbulBlock", big.NewInt(90909999), "londonBlock", big.NewInt(80295291)),
},
{
name: "incompatible config in DB",
newHFBlock: big.NewInt(3),
fn: func(db database.DBManager, newHFBlock *big.Int) (*params.ChainConfig, common.Hash, error) {
// Commit the 'old' genesis block with Istanbul transition at #2.
// Advance to block #4, past the Istanbul transition block of customGenesis.
genesis := genMainnetGenesisBlock()
genesisBlock := genesis.MustCommit(db)
bc, _ := NewBlockChain(db, nil, genesis.Config, gxhash.NewFullFaker(), vm.Config{})
defer bc.Stop()
blocks, _ := GenerateChain(genesis.Config, genesisBlock, gxhash.NewFaker(), db, 4, nil)
bc.InsertChain(blocks)
// This should return a compatibility error.
newConfig := *genesis
newConfig.Config.IstanbulCompatibleBlock = newHFBlock
return SetupGenesisBlock(db, &newConfig, params.MainnetNetworkId, true, false)
},
wantHash: params.MainnetGenesisHash,
wantConfig: mainnetGenesisBlock.Config,
wantStoredConfig: params.MainnetChainConfig,
wantErr: ¶ms.ConfigCompatError{
What: "Istanbul Block",
StoredConfig: params.MainnetChainConfig.IstanbulCompatibleBlock,
NewConfig: big.NewInt(3),
RewindTo: 2,
},
},
}
for _, test := range tests {
db := database.NewMemoryDBManager()
config, hash, err := test.fn(db, test.newHFBlock)
// Check the return values
assert.Equal(t, test.wantErr, err, test.name+": err is mismatching")
assert.Equal(t, test.wantConfig, config, test.name+": config is mismatching")
assert.Equal(t, test.wantHash, hash, test.name+": hash is mismatching")
// Check stored genesis block
if test.wantHash != (common.Hash{}) {
stored := db.ReadBlock(test.wantHash, 0)
assert.Equal(t, test.wantHash, stored.Hash(), test.name+": stored genesis block is not compatible")
}
// Check stored chainConfig
storedChainConfig := db.ReadChainConfig(test.wantHash)
assert.Equal(t, test.wantStoredConfig, storedChainConfig, test.name+": stored chainConfig is not compatible")
}
}
func TestSetupGenesis(t *testing.T) {
var (
customGenesisHash = common.HexToHash("0x4eb4035b7a09619a9950c9a4751cc331843f2373ef38263d676b4a132ba4059c")
customChainId = uint64(4343)
customGenesis = genCustomGenesisBlock(customChainId)
)
tests := []struct {
name string
fn func(database.DBManager) (*params.ChainConfig, common.Hash, error)
wantConfig *params.ChainConfig
wantHash common.Hash
wantErr error
}{
{
name: "genesis without ChainConfig",
fn: func(db database.DBManager) (*params.ChainConfig, common.Hash, error) {
return SetupGenesisBlock(db, new(Genesis), params.UnusedNetworkId, false, false)
},
wantErr: errGenesisNoConfig,
wantConfig: params.AllGxhashProtocolChanges,
},
{
name: "no block in DB, genesis == nil, Mainnet networkId",
fn: func(db database.DBManager) (*params.ChainConfig, common.Hash, error) {
return SetupGenesisBlock(db, nil, params.MainnetNetworkId, false, false)
},
wantHash: params.MainnetGenesisHash,
wantConfig: params.MainnetChainConfig,
},
{
name: "no block in DB, genesis == nil, Kairos networkId",
fn: func(db database.DBManager) (*params.ChainConfig, common.Hash, error) {
return SetupGenesisBlock(db, nil, params.KairosNetworkId, false, false)
},
wantHash: params.KairosGenesisHash,
wantConfig: params.KairosChainConfig,
},
{
name: "no block in DB, genesis == customGenesis, private network",
fn: func(db database.DBManager) (*params.ChainConfig, common.Hash, error) {
return SetupGenesisBlock(db, customGenesis, customChainId, true, false)
},
wantHash: customGenesisHash,
wantConfig: customGenesis.Config,
},
{
name: "Mainnet block in DB, genesis == nil, Mainnet networkId",
fn: func(db database.DBManager) (*params.ChainConfig, common.Hash, error) {
genMainnetGenesisBlock().MustCommit(db)
return SetupGenesisBlock(db, nil, params.MainnetNetworkId, false, false)
},
wantHash: params.MainnetGenesisHash,
wantConfig: params.MainnetChainConfig,
},
{
name: "Kairos block in DB, genesis == nil, Kairos networkId",
fn: func(db database.DBManager) (*params.ChainConfig, common.Hash, error) {
genKairosGenesisBlock().MustCommit(db)
return SetupGenesisBlock(db, nil, params.KairosNetworkId, false, false)
},
wantHash: params.KairosGenesisHash,
wantConfig: params.KairosChainConfig,
},
{
name: "custom block in DB, genesis == nil, custom networkId",
fn: func(db database.DBManager) (*params.ChainConfig, common.Hash, error) {
customGenesis.MustCommit(db)
return SetupGenesisBlock(db, nil, customChainId, true, false)
},
wantHash: customGenesisHash,
wantConfig: customGenesis.Config,
},
{
name: "Mainnet block in DB, genesis == Kairos",
fn: func(db database.DBManager) (*params.ChainConfig, common.Hash, error) {
genMainnetGenesisBlock().MustCommit(db)
return SetupGenesisBlock(db, genKairosGenesisBlock(), params.KairosNetworkId, false, false)
},
wantErr: &GenesisMismatchError{Stored: params.MainnetGenesisHash, New: params.KairosGenesisHash},
wantHash: params.KairosGenesisHash,
wantConfig: params.KairosChainConfig,
},
{
name: "Kairos block in DB, genesis == Mainnet",
fn: func(db database.DBManager) (*params.ChainConfig, common.Hash, error) {
genKairosGenesisBlock().MustCommit(db)
return SetupGenesisBlock(db, genMainnetGenesisBlock(), params.MainnetNetworkId, false, false)
},
wantErr: &GenesisMismatchError{Stored: params.KairosGenesisHash, New: params.MainnetGenesisHash},
wantHash: params.MainnetGenesisHash,
wantConfig: params.MainnetChainConfig,
},
{
name: "Mainnet block in DB, genesis == custom",
fn: func(db database.DBManager) (*params.ChainConfig, common.Hash, error) {
genMainnetGenesisBlock().MustCommit(db)
return SetupGenesisBlock(db, genCustomGenesisBlock(customChainId), customChainId, true, false)
},
wantErr: &GenesisMismatchError{Stored: params.MainnetGenesisHash, New: customGenesisHash},
wantHash: customGenesisHash,
wantConfig: customGenesis.Config,
},
{
name: "Kairos block in DB, genesis == custom",
fn: func(db database.DBManager) (*params.ChainConfig, common.Hash, error) {
genKairosGenesisBlock().MustCommit(db)
return SetupGenesisBlock(db, customGenesis, customChainId, true, false)
},
wantErr: &GenesisMismatchError{Stored: params.KairosGenesisHash, New: customGenesisHash},
wantHash: customGenesisHash,
wantConfig: customGenesis.Config,
},
{
name: "custom block in DB, genesis == Mainnet",
fn: func(db database.DBManager) (*params.ChainConfig, common.Hash, error) {
customGenesis.MustCommit(db)
return SetupGenesisBlock(db, genMainnetGenesisBlock(), params.MainnetNetworkId, false, false)
},
wantErr: &GenesisMismatchError{Stored: customGenesisHash, New: params.MainnetGenesisHash},
wantHash: params.MainnetGenesisHash,
wantConfig: params.MainnetChainConfig,
},
{
name: "custom block in DB, genesis == Kairos",
fn: func(db database.DBManager) (*params.ChainConfig, common.Hash, error) {
customGenesis.MustCommit(db)
return SetupGenesisBlock(db, genKairosGenesisBlock(), params.KairosNetworkId, false, false)
},
wantErr: &GenesisMismatchError{Stored: customGenesisHash, New: params.KairosGenesisHash},
wantHash: params.KairosGenesisHash,
wantConfig: params.KairosChainConfig,
},
{
name: "compatible config in DB",
fn: func(db database.DBManager) (*params.ChainConfig, common.Hash, error) {
customGenesis.MustCommit(db)
return SetupGenesisBlock(db, customGenesis, customChainId, true, false)
},
wantHash: customGenesisHash,
wantConfig: customGenesis.Config,
},
{
name: "incompatible config in DB",
fn: func(db database.DBManager) (*params.ChainConfig, common.Hash, error) {
// Commit the 'old' genesis block with Istanbul transition at #2.
// Advance to block #4, past the Istanbul transition block of customGenesis.
genesis := customGenesis.MustCommit(db)
bc, _ := NewBlockChain(db, nil, customGenesis.Config, gxhash.NewFullFaker(), vm.Config{})
defer bc.Stop()
blocks, _ := GenerateChain(customGenesis.Config, genesis, gxhash.NewFaker(), db, 4, nil)
bc.InsertChain(blocks)
// This should return a compatibility error.
newConfig := *customGenesis
newConfig.Config.IstanbulCompatibleBlock = big.NewInt(3)
return SetupGenesisBlock(db, &newConfig, customChainId, true, false)
},
wantHash: customGenesisHash,
wantConfig: customGenesis.Config,
wantErr: ¶ms.ConfigCompatError{
What: "Istanbul Block",
StoredConfig: big.NewInt(2),
NewConfig: big.NewInt(3),
RewindTo: 1,
},
},
}
for _, test := range tests {
db := database.NewMemoryDBManager()
config, hash, err := test.fn(db)
// Check the return values.
if !reflect.DeepEqual(err, test.wantErr) {
spew := spew.ConfigState{DisablePointerAddresses: true, DisableCapacities: true}
t.Errorf("%s: returned error %#v, want %#v", test.name, spew.NewFormatter(err), spew.NewFormatter(test.wantErr))
}
if !reflect.DeepEqual(config, test.wantConfig) {
t.Errorf("%s:\nreturned %v\nwant %v", test.name, config, test.wantConfig)
}
if hash != test.wantHash {
t.Errorf("%s: returned hash %s, want %s", test.name, hash.Hex(), test.wantHash.Hex())
} else if err == nil {
// Check database content.
stored := db.ReadBlock(test.wantHash, 0)
if stored.Hash() != test.wantHash {
t.Errorf("%s: block in DB has hash %s, want %s", test.name, stored.Hash(), test.wantHash)
}
}
}
}
// Test that SetupGenesisBlock writes the genesis state trie if not present.
func TestGenesisRestoreState(t *testing.T) {
db := database.NewMemoryDBManager()
// Setup first to Commit the Genesis block
_, hash, err := SetupGenesisBlock(db, nil, params.MainnetNetworkId, false, false)
assert.Nil(t, err)
assert.Equal(t, params.MainnetGenesisHash, hash)
// Simulate state migration by deleting the state trie root node
header := db.ReadHeader(hash, 0)
root := header.Root.ExtendZero()
db.DeleteTrieNode(root)
// Setup again to restore the state trie
_, hash, err = SetupGenesisBlock(db, nil, params.MainnetNetworkId, false, false)
assert.Nil(t, err)
assert.Equal(t, params.MainnetGenesisHash, hash)
ok, _ := db.HasTrieNode(root)
assert.True(t, ok)
}
func genMainnetGenesisBlock() *Genesis {
genesis := DefaultGenesisBlock()
genesis.Config = params.MainnetChainConfig.Copy()
genesis.Governance = SetGenesisGovernance(genesis)
InitDeriveSha(genesis.Config)
return genesis
}
func genKairosGenesisBlock() *Genesis {
genesis := DefaultKairosGenesisBlock()
genesis.Config = params.KairosChainConfig.Copy()
genesis.Governance = SetGenesisGovernance(genesis)
InitDeriveSha(genesis.Config)
return genesis
}
func genCustomGenesisBlock(customChainId uint64) *Genesis {
genesis := &Genesis{
Config: ¶ms.ChainConfig{
ChainID: new(big.Int).SetUint64(customChainId),
IstanbulCompatibleBlock: big.NewInt(2),
DeriveShaImpl: types.ImplDeriveShaConcat,
},
Alloc: GenesisAlloc{
common.HexToAddress("0x0100000000000000000000000000000000000000"): {
Balance: big.NewInt(1), Storage: map[common.Hash]common.Hash{{1}: {1}},
},
},
}
genesis.Config.SetDefaultsForGenesis()
genesis.Governance = SetGenesisGovernance(genesis)
InitDeriveSha(genesis.Config)
return genesis
}