-
Notifications
You must be signed in to change notification settings - Fork 0
/
apply_genesis.go
107 lines (94 loc) · 2.94 KB
/
apply_genesis.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
// Copyright 2015 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 evmcore
import (
"math"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/log"
"github.com/corex-mn/go-corex/inter"
"github.com/corex-mn/go-corex/opera"
"github.com/corex-mn/go-corex/opera/genesis"
)
// ApplyGenesis writes or updates the genesis block in db.
func ApplyGenesis(statedb *state.StateDB, g opera.Genesis, maxMemoryUsage int) (*EvmBlock, error) {
mem := 0
capEvm := func(usage int) {
mem += usage
if mem > maxMemoryUsage {
_, _ = statedb.Commit(true)
_ = statedb.Database().TrieDB().Cap(common.StorageSize(maxMemoryUsage / 8))
mem = 0
}
}
g.Accounts.ForEach(func(addr common.Address, account genesis.Account) {
if account.SelfDestruct {
statedb.Suicide(addr)
_, _ = statedb.Commit(true)
}
statedb.SetBalance(addr, account.Balance)
statedb.SetCode(addr, account.Code)
statedb.SetNonce(addr, account.Nonce)
capEvm(1024 + len(account.Code))
})
g.Storage.ForEach(func(addr common.Address, key common.Hash, value common.Hash) {
statedb.SetState(addr, key, value)
capEvm(512)
})
// initial block
root, err := flush(statedb, true)
if err != nil {
return nil, err
}
block := genesisBlock(g, root)
return block, nil
}
func flush(statedb *state.StateDB, clean bool) (root common.Hash, err error) {
root, err = statedb.Commit(clean)
if err != nil {
return
}
err = statedb.Database().TrieDB().Commit(root, false, nil)
if err != nil {
return
}
if !clean {
err = statedb.Database().TrieDB().Cap(0)
}
return
}
// genesisBlock makes genesis block with pretty hash.
func genesisBlock(g opera.Genesis, root common.Hash) *EvmBlock {
block := &EvmBlock{
EvmHeader: EvmHeader{
Number: big.NewInt(0),
Time: g.Time,
GasLimit: math.MaxUint64,
Root: root,
TxHash: common.Hash(inter.EmptyTxHash),
},
}
return block
}
// MustApplyGenesis writes the genesis block and state to db, panicking on error.
func MustApplyGenesis(g opera.Genesis, statedb *state.StateDB, maxMemoryUsage int) *EvmBlock {
block, err := ApplyGenesis(statedb, g, maxMemoryUsage)
if err != nil {
log.Crit("ApplyGenesis", "err", err)
}
return block
}