-
Notifications
You must be signed in to change notification settings - Fork 14
/
genesis.go
65 lines (53 loc) · 2.15 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
package keeper
import (
"bytes"
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
ethermint "github.com/evmos/ethermint/types"
"github.com/evmos/ethermint/x/evm/types"
abci "github.com/tendermint/tendermint/abci/types"
fxtypes "github.com/functionx/fx-core/v3/types"
)
// InitGenesis initializes genesis state based on exported genesis
func (k *Keeper) InitGenesis(ctx sdk.Context, accountKeeper types.AccountKeeper, data types.GenesisState) []abci.ValidatorUpdate {
k.SetParams(ctx, data.Params)
// ensure evm module account is set
if addr := accountKeeper.GetModuleAddress(types.ModuleName); addr == nil {
panic("the EVM module account has not been set")
}
for _, account := range data.Accounts {
address := common.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))
}
ethAcct, ok := acc.(ethermint.EthAccountI)
if !ok {
panic(fmt.Errorf("account %s must be an EthAccount interface, got %T", account.Address, acc))
}
code := common.Hex2Bytes(account.Code)
codeHash := crypto.Keccak256Hash(code)
if !bytes.Equal(ethAcct.GetCodeHash().Bytes(), codeHash.Bytes()) {
panic("code don't match codeHash")
}
k.SetCode(ctx, codeHash.Bytes(), code)
for _, storage := range account.Storage {
k.SetState(ctx, address, common.HexToHash(storage.Key), common.HexToHash(storage.Value).Bytes())
}
}
// init logic contract
initContract := []fxtypes.Contract{fxtypes.GetERC20(), fxtypes.GetWFX()}
for _, contract := range initContract {
if len(contract.Code) <= 0 || contract.Address == common.HexToAddress(fxtypes.EmptyEvmAddress) {
panic(fmt.Sprintf("invalid contract: %s", contract.Address.String()))
}
if err := k.CreateContractWithCode(ctx, contract.Address, contract.Code); err != nil {
panic(fmt.Sprintf("create contract %s with code error %s", contract.Address.String(), err.Error()))
}
}
return []abci.ValidatorUpdate{}
}