-
Notifications
You must be signed in to change notification settings - Fork 15
/
config.go
719 lines (629 loc) · 21.4 KB
/
config.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
// Copyright 2016 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/>.
package core
import (
hexlib "encoding/hex"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"math/big"
"os"
"sort"
"sync"
"path/filepath"
"reflect"
"github.com/ellaism/go-ellaism/common"
"github.com/ellaism/go-ellaism/core/state"
"github.com/ellaism/go-ellaism/core/types"
"github.com/ellaism/go-ellaism/core/vm"
"github.com/ellaism/go-ellaism/ethdb"
"github.com/ellaism/go-ellaism/logger"
"github.com/ellaism/go-ellaism/logger/glog"
"github.com/ellaism/go-ellaism/p2p/discover"
"io"
"strings"
)
var (
ErrChainConfigNotFound = errors.New("chain config not found")
ErrChainConfigForkNotFound = errors.New("chain config fork not found")
ErrHashKnownBad = errors.New("known bad hash")
ErrHashKnownFork = validateError("known fork hash mismatch")
)
// SufficientChainConfig holds necessary data for externalizing a given blockchain configuration.
type SufficientChainConfig struct {
ID string `json:"id,omitempty"` // deprecated in favor of 'Identity', method decoding should id -> identity
Identity string `json:"identity"`
Name string `json:"name,omitempty"`
State *StateConfig `json:"state"` // don't omitempty for clarity of potential custom options
Network int `json:"network"` // eth.NetworkId (mainnet=1, morden=2)
Consensus string `json:"consensus"` // pow type (ethash OR ethash-test)
Genesis *GenesisDump `json:"genesis"`
ChainConfig *ChainConfig `json:"chainConfig"`
Bootstrap []string `json:"bootstrap"`
ParsedBootstrap []*discover.Node `json:"-"`
}
// StateConfig hold variable data for statedb.
type StateConfig struct {
StartingNonce uint64 `json:"startingNonce,omitempty"`
}
// GenesisDump is the geth JSON format.
// https://github.com/ethereumproject/wiki/wiki/Ethereum-Chain-Spec-Format#subformat-genesis
type GenesisDump struct {
Nonce prefixedHex `json:"nonce"`
Timestamp prefixedHex `json:"timestamp"`
ParentHash prefixedHex `json:"parentHash"`
ExtraData prefixedHex `json:"extraData"`
GasLimit prefixedHex `json:"gasLimit"`
Difficulty prefixedHex `json:"difficulty"`
Mixhash prefixedHex `json:"mixhash"`
Coinbase prefixedHex `json:"coinbase"`
// Alloc maps accounts by their address.
Alloc map[hex]*GenesisDumpAlloc `json:"alloc"`
}
// GenesisDumpAlloc is a GenesisDump.Alloc entry.
type GenesisDumpAlloc struct {
Code prefixedHex `json:"-"` // skip field for json encode
Storage map[hex]hex `json:"-"`
Balance string `json:"balance"` // decimal string
}
type GenesisAccount struct {
Address common.Address `json:"address"`
Balance *big.Int `json:"balance"`
}
// ChainConfig is stored in the database on a per block basis. This means
// that any network, identified by its genesis block, can have its own
// set of configuration options.
type ChainConfig struct {
// Forks holds fork block requirements. See ErrHashKnownFork.
Forks Forks `json:"forks"`
// BadHashes holds well known blocks with consensus issues. See ErrHashKnownBad.
BadHashes []*BadHash `json:"badHashes"`
}
type Fork struct {
Name string `json:"name"`
// Block is the block number where the hard-fork commences on
// the Ethereum network.
Block *big.Int `json:"block"`
// Used to improve sync for a known network split
RequiredHash common.Hash `json:"requiredHash"`
// Configurable features.
Features []*ForkFeature `json:"features"`
}
// Forks implements sort interface, sorting by block number
type Forks []*Fork
func (fs Forks) Len() int { return len(fs) }
func (fs Forks) Less(i, j int) bool {
iF := fs[i]
jF := fs[j]
return iF.Block.Cmp(jF.Block) < 0
}
func (fs Forks) Swap(i, j int) {
fs[i], fs[j] = fs[j], fs[i]
}
// ForkFeatures are designed to decouple the implementation feature upgrades from Forks themselves.
// For example, there are several 'set-gasprice' features, each using a different gastable,
// as well as protocol upgrades including 'eip155', 'ecip1010', ... etc.
type ForkFeature struct {
ID string `json:"id"`
Options ChainFeatureConfigOptions `json:"options"` // no * because they have to be iterable(?)
optionsLock sync.RWMutex
ParsedOptions map[string]interface{} `json:"-"` // don't include in JSON dumps, since its for holding parsed JSON in mem
parsedOptionsLock sync.RWMutex
// TODO Derive Oracle contracts from fork struct (Version, Registrar, Release)
}
// These are the raw key-value configuration options made available
// by an external JSON file.
type ChainFeatureConfigOptions map[string]interface{}
type BadHash struct {
Block *big.Int
Hash common.Hash
}
func (c *SufficientChainConfig) IsValid() (string, bool) {
// entirely empty
if reflect.DeepEqual(c, SufficientChainConfig{}) {
return "all empty", false
}
if c.Identity == "" {
return "identity/id", false
}
if c.Network == 0 {
return "networkId", false
}
if c := c.Consensus; c == "" || (c != "ethash" && c != "ethash-test") {
return "consensus", false
}
if c.Genesis == nil {
return "genesis", false
}
if len(c.Genesis.Nonce) == 0 {
return "genesis.nonce", false
}
if len(c.Genesis.GasLimit) == 0 {
return "genesis.gasLimit", false
}
if len(c.Genesis.Difficulty) == 0 {
return "genesis.difficulty", false
}
if _, e := c.Genesis.Header(); e != nil {
return "genesis.header(): " + e.Error(), false
}
if c.ChainConfig == nil {
return "chainConfig", false
}
if len(c.ChainConfig.Forks) == 0 {
return "forks", false
}
if cid := c.ChainConfig.GetChainID(); cid.Cmp(new(big.Int)) == 0 {
return "diehard chainid", false
}
return "", true
}
// Header returns the mapping.
func (g *GenesisDump) Header() (*types.Header, error) {
var h types.Header
var err error
if err = g.Nonce.Decode(h.Nonce[:]); err != nil {
return nil, fmt.Errorf("malformed nonce: %s", err)
}
if h.Time, err = g.Timestamp.Int(); err != nil {
return nil, fmt.Errorf("malformed timestamp: %s", err)
}
if err = g.ParentHash.Decode(h.ParentHash[:]); err != nil {
return nil, fmt.Errorf("malformed parentHash: %s", err)
}
if h.Extra, err = g.ExtraData.Bytes(); err != nil {
return nil, fmt.Errorf("malformed extraData: %s", err)
}
if h.GasLimit, err = g.GasLimit.Int(); err != nil {
return nil, fmt.Errorf("malformed gasLimit: %s", err)
}
if h.Difficulty, err = g.Difficulty.Int(); err != nil {
return nil, fmt.Errorf("malformed difficulty: %s", err)
}
if err = g.Mixhash.Decode(h.MixDigest[:]); err != nil {
return nil, fmt.Errorf("malformed mixhash: %s", err)
}
if err := g.Coinbase.Decode(h.Coinbase[:]); err != nil {
return nil, fmt.Errorf("malformed coinbase: %s", err)
}
return &h, nil
}
// SortForks sorts a ChainConfiguration's forks by block number smallest to bigget (chronologically).
// This should need be called only once after construction
func (c *ChainConfig) SortForks() *ChainConfig {
sort.Sort(c.Forks)
return c
}
// GetChainID gets the chainID for a chainconfig.
// It assumes chain configuration has a "Diehard" fork.
// It assumes eip155 will only be set on the "Diehard" fork.
// It returns big.Int zero-value if no chainID is set for eip155,
// which means that it implicitly assumes to be only called for blocks >= Diehard fork block num.
// If called/configured "incorrectly" it will "pass the buck" by returning a non-error (ie zero value).
func (c *ChainConfig) GetChainID() *big.Int {
n := new(big.Int)
fork := c.ForkByName("Diehard")
if feat, _, ok := c.GetFeature(fork.Block, "eip155"); ok {
if val, ok := feat.GetBigInt("chainID"); ok {
n.Set(val)
}
}
return n
}
// IsHomestead returns whether num is either equal to the homestead block or greater.
func (c *ChainConfig) IsHomestead(num *big.Int) bool {
return true
}
// IsDiehard returns whether num is greater than or equal to the Diehard block, but less than explosion.
func (c *ChainConfig) IsDiehard(num *big.Int) bool {
return true
}
// IsExplosion returns whether num is either equal to the explosion block or greater.
func (c *ChainConfig) IsExplosion(num *big.Int) bool {
feat, fork, configured := c.GetFeature(num, "difficulty")
if configured {
//name, exists := feat.GetString("type")
if name, exists := feat.GetString("type"); exists && name == "ecip1010" {
block := big.NewInt(0)
if length, ok := feat.GetBigInt("length"); ok {
block = block.Add(fork.Block, length)
} else {
panic("Fork feature ecip1010 requires length value.")
}
return num.Cmp(block) >= 0
}
}
return false
}
// ForkByName looks up a Fork by its name, assumed to be unique
func (c *ChainConfig) ForkByName(name string) *Fork {
for i := range c.Forks {
if c.Forks[i].Name == name {
return c.Forks[i]
}
}
return &Fork{}
}
// GetFeature looks up fork features by id, where id can (currently) be [difficulty, gastable, eip155].
// GetFeature returns the feature|nil, the latest fork configuring a given id, and if the given feature id was found at all
// If queried feature is not found, returns ForkFeature{}, Fork{}, false.
// If queried block number and/or feature is a zero-value, returns ForkFeature{}, Fork{}, false.
func (c *ChainConfig) GetFeature(num *big.Int, id string) (*ForkFeature, *Fork, bool) {
var okForkFeature = &ForkFeature{}
var okFork = &Fork{}
var found = false
if num != nil && id != "" {
for _, f := range c.Forks {
if f.Block.Cmp(num) > 0 {
continue
}
for _, ff := range f.Features {
if ff.ID == id {
okForkFeature = ff
okFork = f
found = true
}
}
}
}
return okForkFeature, okFork, found
}
// HasFeature looks up if fork feature exists on any fork at any block in the configuration.
// In case of multiple same-'id'd features, returns latest (assuming forks are sorted).
func (c *ChainConfig) HasFeature(id string) (*ForkFeature, *Fork, bool) {
var okForkFeature = &ForkFeature{}
var okFork = &Fork{}
var found = false
if id != "" {
for _, f := range c.Forks {
for _, ff := range f.Features {
if ff.ID == id {
okForkFeature = ff
okFork = f
found = true
}
}
}
}
return okForkFeature, okFork, found
}
func (c *ChainConfig) HeaderCheck(h *types.Header) error {
for _, fork := range c.Forks {
if fork.Block.Cmp(h.Number) != 0 {
continue
}
if !fork.RequiredHash.IsEmpty() && fork.RequiredHash != h.Hash() {
return ErrHashKnownFork
}
}
for _, bad := range c.BadHashes {
if bad.Block.Cmp(h.Number) != 0 {
continue
}
if bad.Hash == h.Hash() {
return ErrHashKnownBad
}
}
return nil
}
func (c *ChainConfig) GetSigner(blockNumber *big.Int) types.Signer {
feature, _, configured := c.GetFeature(blockNumber, "eip155")
if configured {
if chainId, ok := feature.GetBigInt("chainID"); ok {
return types.NewChainIdSigner(chainId)
} else {
panic(fmt.Errorf("chainID is not set for EIP-155 at %v", blockNumber))
}
}
return types.BasicSigner{}
}
// GasTable returns the gas table corresponding to the current fork
// The returned GasTable's fields shouldn't, under any circumstances, be changed.
func (c *ChainConfig) GasTable(num *big.Int) *vm.GasTable {
f, _, configured := c.GetFeature(num, "gastable")
if !configured {
return DefaultHomeSteadGasTable
}
name, ok := f.GetString("type")
if !ok {
name = ""
} // will wall to default panic
switch name {
case "homestead":
return DefaultHomeSteadGasTable
case "eip150":
return DefaultGasRepriceGasTable
case "eip160":
return DefaultDiehardGasTable
default:
panic(fmt.Errorf("Unsupported gastable value '%v' at block: %v", name, num))
}
}
// WriteToJSONFile writes a given config to a specified file path.
// It doesn't run any checks on the file path so make sure that's already squeaky clean.
func (c *SufficientChainConfig) WriteToJSONFile(path string) error {
jsonConfig, err := json.MarshalIndent(c, "", " ")
if err != nil {
return fmt.Errorf("Could not marshal json from chain config: %v", err)
}
if err := ioutil.WriteFile(path, jsonConfig, 0644); err != nil {
return fmt.Errorf("Could not write external chain config file: %v", err)
}
return nil
}
func parseExternalChainConfig(f io.Reader) (*SufficientChainConfig, error) {
var config = &SufficientChainConfig{}
if err := json.NewDecoder(f).Decode(config); err != nil {
return nil, fmt.Errorf("%v: %s", f, err)
}
// Make JSON 'id' -> 'identity' (for backwards compatibility)
if config.ID != "" && config.Identity == "" {
config.Identity = config.ID
}
// Make 'ethash' default (backwards compatibility)
if config.Consensus == "" {
config.Consensus = "ethash"
}
// Parse bootstrap nodes
config.ParsedBootstrap = ParseBootstrapNodeStrings(config.Bootstrap)
if invalid, ok := config.IsValid(); !ok {
return nil, fmt.Errorf("Invalid chain configuration file. Please check the existence and integrity of keys and values for: %v", invalid)
}
config.ChainConfig = config.ChainConfig.SortForks()
return config, nil
}
// ReadExternalChainConfigFromFile reads a flagged external json file for blockchain configuration.
// It returns a valid and full ("hard") configuration or an error.
func ReadExternalChainConfigFromFile(incomingPath string) (*SufficientChainConfig, error) {
// ensure flag arg cleanliness
flaggedExternalChainConfigPath := filepath.Clean(incomingPath)
// ensure file exists and that it is NOT a directory
if info, err := os.Stat(flaggedExternalChainConfigPath); os.IsNotExist(err) {
return nil, fmt.Errorf("ERROR: No existing chain configuration file found at: %s", flaggedExternalChainConfigPath)
} else if info.IsDir() {
return nil, fmt.Errorf("ERROR: Specified configuration file cannot be a directory: %s", flaggedExternalChainConfigPath)
}
f, err := os.Open(flaggedExternalChainConfigPath)
if err != nil {
return nil, fmt.Errorf("failed to read external chain configuration file: %s", err)
}
defer f.Close()
config, err := parseExternalChainConfig(f)
if err != nil {
return nil, err
}
return config, nil
}
// ParseBootstrapNodeStrings is a helper function to parse stringified bs nodes, ie []"enode://e809c4a2fec7daed400e5e28564e23693b23b2cc5a019b612505631bbe7b9ccf709c1796d2a3d29ef2b045f210caf51e3c4f5b6d3587d43ad5d6397526fa6179@174.112.32.157:30303",...
// to usable Nodes. It takes a slice of strings and returns a slice of Nodes.
func ParseBootstrapNodeStrings(nodeStrings []string) []*discover.Node {
// Otherwise parse and use the CLI bootstrap nodes
bootnodes := []*discover.Node{}
for _, url := range nodeStrings {
url = strings.TrimSpace(url)
if url == "" {
continue
}
node, err := discover.ParseNode(url)
if err != nil {
glog.V(logger.Error).Infof("Bootstrap URL %s: %v\n", url, err)
continue
}
bootnodes = append(bootnodes, node)
}
return bootnodes
}
// GetString gets and option value for an options with key 'name',
// returning value as a string.
func (o *ForkFeature) GetString(name string) (string, bool) {
o.parsedOptionsLock.Lock()
defer o.parsedOptionsLock.Unlock()
if o.ParsedOptions == nil {
o.ParsedOptions = make(map[string]interface{})
} else {
val, ok := o.ParsedOptions[name]
if ok {
return val.(string), ok
}
}
o.optionsLock.RLock()
defer o.optionsLock.RUnlock()
val, ok := o.Options[name].(string)
o.ParsedOptions[name] = val //expect it as a string in config
return val, ok
}
// GetBigInt gets and option value for an options with key 'name',
// returning value as a *big.Int and ok if it exists.
func (o *ForkFeature) GetBigInt(name string) (*big.Int, bool) {
i := new(big.Int)
o.parsedOptionsLock.Lock()
defer o.parsedOptionsLock.Unlock()
if o.ParsedOptions == nil {
o.ParsedOptions = make(map[string]interface{})
} else {
val, ok := o.ParsedOptions[name]
if ok {
if vv, ok := val.(*big.Int); ok {
return i.Set(vv), true
}
}
}
o.optionsLock.RLock()
originalValue, ok := o.Options[name]
o.optionsLock.RUnlock()
if !ok {
return nil, false
}
// interface{} type assertion for _61_ is float64
if value, ok := originalValue.(float64); ok {
i.SetInt64(int64(value))
o.ParsedOptions[name] = i
return i, true
}
// handle other user-generated incoming options with some, albeit limited, degree of lenience
if value, ok := originalValue.(int64); ok {
i.SetInt64(value)
o.ParsedOptions[name] = i
return i, true
}
if value, ok := originalValue.(int); ok {
i.SetInt64(int64(value))
o.ParsedOptions[name] = i
return i, true
}
if value, ok := originalValue.(string); ok {
ii, ok := new(big.Int).SetString(value, 0)
if ok {
i.Set(ii)
o.ParsedOptions[name] = i
}
return i, ok
}
return nil, false
}
// WriteGenesisBlock writes the genesis block to the database as block number 0
func WriteGenesisBlock(chainDb ethdb.Database, genesis *GenesisDump) (*types.Block, error) {
statedb, err := state.New(common.Hash{}, chainDb)
if err != nil {
return nil, err
}
for addrHex, account := range genesis.Alloc {
var addr common.Address
if err := addrHex.Decode(addr[:]); err != nil {
return nil, fmt.Errorf("malformed addres %q: %s", addrHex, err)
}
balance, ok := new(big.Int).SetString(account.Balance, 0)
if !ok {
return nil, fmt.Errorf("malformed account %q balance %q", addrHex, account.Balance)
}
statedb.AddBalance(addr, balance)
code, err := account.Code.Bytes()
if err != nil {
return nil, fmt.Errorf("malformed account %q code: %s", addrHex, err)
}
statedb.SetCode(addr, code)
for key, value := range account.Storage {
var k, v common.Hash
if err := key.Decode(k[:]); err != nil {
return nil, fmt.Errorf("malformed account %q key: %s", addrHex, err)
}
if err := value.Decode(v[:]); err != nil {
return nil, fmt.Errorf("malformed account %q value: %s", addrHex, err)
}
statedb.SetState(addr, k, v)
}
}
root, stateBatch := statedb.CommitBatch()
header, err := genesis.Header()
if err != nil {
return nil, err
}
header.Root = root
gblock := types.NewBlock(header, nil, nil, nil)
if block := GetBlock(chainDb, gblock.Hash()); block != nil {
glog.V(logger.Debug).Infof("Genesis block %s already exists in chain -- writing canonical number", block.Hash().Hex())
err := WriteCanonicalHash(chainDb, block.Hash(), block.NumberU64())
if err != nil {
return nil, err
}
return block, nil
}
if err := stateBatch.Write(); err != nil {
return nil, fmt.Errorf("cannot write state: %v", err)
}
if err := WriteTd(chainDb, gblock.Hash(), header.Difficulty); err != nil {
return nil, err
}
if err := WriteBlock(chainDb, gblock); err != nil {
return nil, err
}
if err := WriteBlockReceipts(chainDb, gblock.Hash(), nil); err != nil {
return nil, err
}
if err := WriteCanonicalHash(chainDb, gblock.Hash(), gblock.NumberU64()); err != nil {
return nil, err
}
if err := WriteHeadBlockHash(chainDb, gblock.Hash()); err != nil {
return nil, err
}
return gblock, nil
}
func WriteGenesisBlockForTesting(db ethdb.Database, accounts ...GenesisAccount) *types.Block {
dump := GenesisDump{
GasLimit: "0x47E7C4",
Difficulty: "0x020000",
Alloc: make(map[hex]*GenesisDumpAlloc, len(accounts)),
}
for _, a := range accounts {
dump.Alloc[hex(hexlib.EncodeToString(a.Address[:]))] = &GenesisDumpAlloc{
Balance: a.Balance.String(),
}
}
block, err := WriteGenesisBlock(db, &dump)
if err != nil {
panic(err)
}
return block
}
// MakeGenesisDump makes a genesis dump
func MakeGenesisDump(chaindb ethdb.Database) (*GenesisDump, error) {
genesis := GetBlock(chaindb, GetCanonicalHash(chaindb, 0))
if genesis == nil {
return nil, nil
}
// Settings.
genesisHeader := genesis.Header()
nonce := fmt.Sprintf(`0x%x`, genesisHeader.Nonce)
time := common.BigToHash(genesisHeader.Time).Hex()
parentHash := genesisHeader.ParentHash.Hex()
gasLimit := common.BigToHash(genesisHeader.GasLimit).Hex()
difficulty := common.BigToHash(genesisHeader.Difficulty).Hex()
mixHash := genesisHeader.MixDigest.Hex()
coinbase := genesisHeader.Coinbase.Hex()
var dump = &GenesisDump{
Nonce: prefixedHex(nonce), // common.ToHex(n)), // common.ToHex(
Timestamp: prefixedHex(time),
ParentHash: prefixedHex(parentHash),
//ExtraData: prefixedHex(extra),
GasLimit: prefixedHex(gasLimit),
Difficulty: prefixedHex(difficulty),
Mixhash: prefixedHex(mixHash),
Coinbase: prefixedHex(coinbase),
//Alloc: ,
}
if genesisHeader.Extra != nil && len(genesisHeader.Extra) > 0 {
dump.ExtraData = prefixedHex(common.ToHex(genesisHeader.Extra))
}
// State allocations.
genState, err := state.New(genesis.Root(), chaindb)
if err != nil {
return nil, err
}
stateDump := genState.RawDump([]common.Address{})
stateAccounts := stateDump.Accounts
dump.Alloc = make(map[hex]*GenesisDumpAlloc, len(stateAccounts))
for address, acct := range stateAccounts {
if common.IsHexAddress(address) {
dump.Alloc[hex(address)] = &GenesisDumpAlloc{
Balance: acct.Balance,
}
} else {
return nil, fmt.Errorf("Invalid address in genesis state: %v", address)
}
}
return dump, nil
}