/
gencontract.go
154 lines (124 loc) · 4.98 KB
/
gencontract.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
package cmd
import (
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"os"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/spf13/cobra"
ethtypes "github.com/artela-network/artela/ethereum/types"
evmstate "github.com/artela-network/artela/x/evm/txs/support"
evmtypes "github.com/artela-network/artela/x/evm/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/server"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/cosmos-sdk/x/genutil"
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
)
// AddGenesisContractCmd returns add-genesis-contract cobra Command.
func AddGenesisContractCmd(defaultNodeHome string) *cobra.Command {
cmd := &cobra.Command{
Use: "add-genesis-contract [contract_address_hex] [bin_runtime_code_hex/bin_runtime_code_path]",
Short: "Add a genesis contract to genesis.json",
Long: `Add a genesis contract to genesis.json. The provided contract must specify
the address and the bin-runtime code.`,
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)
serverCtx := server.GetServerContextFromCmd(cmd)
config := serverCtx.Config
config.SetRoot(clientCtx.HomeDir)
rawAddr, err := hexutil.Decode(args[0])
if err != nil {
return errors.New("invalid address, please input a valid ethereum format address")
}
addr, err := sdk.AccAddressFromHexUnsafe(hex.EncodeToString(rawAddr))
if err != nil {
return errors.New("unable to parse address")
}
// load contract bin-runtime code
contractBin, err := hexutil.Decode(args[1])
if err != nil {
contractBin, err = os.ReadFile(args[1])
if err != nil {
return errors.New("failed to load contract bytecode")
}
}
// create concrete account type based on input parameters
var genAccount *ethtypes.EthAccount
balances := banktypes.Balance{Address: addr.String(), Coins: []sdk.Coin{}}
baseAccount := authtypes.NewBaseAccount(addr, nil, 0, 0)
genAccount = ðtypes.EthAccount{
BaseAccount: baseAccount,
CodeHash: crypto.Keccak256Hash(contractBin).Hex(),
}
if err := genAccount.Validate(); err != nil {
return fmt.Errorf("failed to validate new genesis contract: %w", err)
}
genFile := config.GenesisFile()
appState, genDoc, err := genutiltypes.GenesisStateFromGenFile(genFile)
if err != nil {
return fmt.Errorf("failed to unmarshal genesis state: %w", err)
}
authGenState := authtypes.GetGenesisStateFromAppState(clientCtx.Codec, appState)
accs, err := authtypes.UnpackAccounts(authGenState.Accounts)
if err != nil {
return fmt.Errorf("failed to get accounts from any: %w", err)
}
if accs.Contains(addr) {
return fmt.Errorf("cannot add account at existing address %s", addr)
}
// Add the new account to the set of genesis accounts and sanitize the
// accounts afterward.
accs = append(accs, genAccount)
accs = authtypes.SanitizeGenesisAccounts(accs)
genAccs, err := authtypes.PackAccounts(accs)
if err != nil {
return fmt.Errorf("failed to convert accounts into any's: %w", err)
}
authGenState.Accounts = genAccs
authGenStateBz, err := clientCtx.Codec.MarshalJSON(&authGenState)
if err != nil {
return fmt.Errorf("failed to marshal auth genesis state: %w", err)
}
appState[authtypes.ModuleName] = authGenStateBz
bankGenState := banktypes.GetGenesisStateFromAppState(clientCtx.Codec, appState)
bankGenState.Balances = append(bankGenState.Balances, balances)
bankGenState.Balances = banktypes.SanitizeGenesisBalances(bankGenState.Balances)
bankGenState.Supply = bankGenState.Supply.Add(balances.Coins...)
bankGenStateBz, err := clientCtx.Codec.MarshalJSON(bankGenState)
if err != nil {
return fmt.Errorf("failed to marshal bank genesis state: %w", err)
}
appState[banktypes.ModuleName] = bankGenStateBz
// generate evm state
var evmGenState evmstate.GenesisState
if err := clientCtx.Codec.UnmarshalJSON(appState[evmtypes.ModuleName], &evmGenState); err != nil {
return err
}
evmGenState.Accounts = append(evmGenState.Accounts, evmstate.GenesisAccount{
Address: genAccount.EthAddress().Hex(),
Code: hex.EncodeToString(contractBin),
})
evmGenStateBz, err := clientCtx.Codec.MarshalJSON(&evmGenState)
if err != nil {
return fmt.Errorf("failed to marshal bank genesis state: %w", err)
}
appState[evmtypes.ModuleName] = evmGenStateBz
appStateJSON, err := json.Marshal(appState)
if err != nil {
return fmt.Errorf("failed to marshal application genesis state: %w", err)
}
genDoc.AppState = appStateJSON
return genutil.ExportGenesisFile(genDoc, genFile)
},
}
cmd.Flags().String(flags.FlagHome, defaultNodeHome, "The application home directory")
flags.AddQueryFlagsToCmd(cmd)
return cmd
}