-
Notifications
You must be signed in to change notification settings - Fork 179
/
genesis.go
171 lines (143 loc) · 4.68 KB
/
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
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
package evm
import (
"fmt"
"github.com/cosmos/cosmos-sdk/server"
sdk "github.com/cosmos/cosmos-sdk/types"
authexported "github.com/cosmos/cosmos-sdk/x/auth/exported"
ethcmn "github.com/ethereum/go-ethereum/common"
ethermint "github.com/okex/exchain/app/types"
"github.com/okex/exchain/x/evm/types"
"github.com/spf13/viper"
abci "github.com/tendermint/tendermint/abci/types"
)
// InitGenesis initializes genesis state based on exported genesis
func InitGenesis(ctx sdk.Context, k Keeper, accountKeeper types.AccountKeeper, data GenesisState) []abci.ValidatorUpdate { // nolint: interfacer
logger := ctx.Logger().With("module", types.ModuleName)
k.SetParams(ctx, data.Params)
csdb := types.CreateEmptyCommitStateDB(k.GenerateCSDBParams(), ctx)
mode := viper.GetString(server.FlagEvmImportMode)
if mode == "" {
// for some UT
mode = defaultMode
}
initImportEnv(viper.GetString(server.FlagEvmImportPath), mode, viper.GetUint64(server.FlagGoroutineNum))
for _, account := range data.Accounts {
address := ethcmn.HexToAddress(account.Address)
accAddress := sdk.AccAddress(address.Bytes())
// check that the EVM balance the matches the account balance
acc := accountKeeper.GetAccount(ctx, accAddress)
if acc == nil {
panic(fmt.Errorf("account not found for address %s", account.Address))
}
ethAcc, ok := acc.(*ethermint.EthAccount)
if !ok {
panic(
fmt.Errorf("account %s must be an %T type, got %T",
account.Address, ðermint.EthAccount{}, acc,
),
)
}
evmBalance := acc.GetCoins().AmountOf(sdk.DefaultBondDenom)
csdb.SetNonce(address, acc.GetSequence())
csdb.SetBalance(address, evmBalance.BigInt())
switch mode {
case defaultMode:
if account.Code != nil {
csdb.SetCode(address, account.Code)
codeCount++
}
for _, storage := range account.Storage {
k.SetStateDirectly(ctx, address, storage.Key, storage.Value)
storageCount++
}
case filesMode:
importFromFile(ctx, logger, k, address, ethAcc.CodeHash)
case dbMode:
importFromDB(ctx, k, address, ethAcc.CodeHash)
default:
panic("unsupported import mode")
}
}
// wait for all data to be imported from files
if mode == filesMode {
wg.Wait()
}
// set contract deployment whitelist into store
csdb.SetContractDeploymentWhitelist(data.ContractDeploymentWhitelist)
// set contract blocked list into store
csdb.SetContractBlockedList(data.ContractBlockedList)
logger.Debug("Import finished", "code", codeCount, "storage", storageCount)
// set state objects and code to store
_, err := csdb.Commit(false)
if err != nil {
panic(err)
}
// set storage to store
// NOTE: don't delete empty object to prevent import-export simulation failure
err = csdb.Finalise(false)
if err != nil {
panic(err)
}
k.SetChainConfig(ctx, data.ChainConfig)
return []abci.ValidatorUpdate{}
}
// ExportGenesis exports genesis state of the EVM module
func ExportGenesis(ctx sdk.Context, k Keeper, ak types.AccountKeeper) GenesisState {
logger := ctx.Logger().With("module", types.ModuleName)
mode := viper.GetString(server.FlagEvmExportMode)
if mode == "" {
// for some UT
mode = defaultMode
}
initExportEnv(viper.GetString(server.FlagEvmExportPath), mode, viper.GetUint64(server.FlagGoroutineNum))
// nolint: prealloc
var ethGenAccounts []types.GenesisAccount
csdb := types.CreateEmptyCommitStateDB(k.GenerateCSDBParams(), ctx)
ak.IterateAccounts(ctx, func(account authexported.Account) bool {
ethAccount, ok := account.(*ethermint.EthAccount)
if !ok {
// ignore non EthAccounts
return false
}
addr := ethAccount.EthAddress()
code, storage := []byte(nil), types.Storage(nil)
var err error
switch mode {
case defaultMode:
code = csdb.GetCode(addr)
if code != nil {
codeCount++
}
if storage, err = k.GetAccountStorage(ctx, addr); err != nil {
panic(err)
}
storageCount += uint64(len(storage))
case filesMode:
exportToFile(ctx, k, addr)
case dbMode:
exportToDB(ctx, k, addr, ethAccount.CodeHash)
default:
panic("unsupported export mode")
}
genAccount := types.GenesisAccount{
Address: addr.String(),
Code: code,
Storage: storage,
}
ethGenAccounts = append(ethGenAccounts, genAccount)
return false
})
// wait for all data to be written into files or db
if mode == filesMode || mode == dbMode {
wg.Wait()
}
logger.Debug("Export finished", "code", codeCount, "storage", storageCount)
config, _ := k.GetChainConfig(ctx)
return GenesisState{
Accounts: ethGenAccounts,
ChainConfig: config,
Params: k.GetParams(ctx),
ContractDeploymentWhitelist: csdb.GetContractDeploymentWhitelist(),
ContractBlockedList: csdb.GetContractBlockedList(),
}
}