From 09746c6a518342b28ac9f92c1a6184c3cf7376dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Federico=20Kunze=20K=C3=BCllmer?= <31522760+fedekunze@users.noreply.github.com> Date: Wed, 28 Jul 2021 05:47:29 -0400 Subject: [PATCH] feat: import ethermint without forking (#378) * cmd: cleanup * update gh action * lint * more cleanup * update config * register denom --- .github/workflows/test.yml | 8 +-- app/app.go | 5 -- client/config.go | 23 --------- client/testnet.go | 2 +- {types => cmd/config}/config.go | 34 +++++++------ {types => cmd/config}/config_test.go | 9 ++-- cmd/ethermintd/flags.go | 51 ------------------- cmd/ethermintd/genaccounts.go | 6 +-- cmd/ethermintd/main.go | 13 +++++ cmd/ethermintd/root.go | 7 +-- ethereum/rpc/namespaces/personal/api.go | 24 ++++++--- {cmd/ethermintd => server}/config/config.go | 27 +++++----- .../config/config_test.go | 0 {cmd/ethermintd => server}/config/toml.go | 6 +++ server/evmrpc.go | 3 +- server/flags.go | 37 ++++++++++++++ server/start.go | 2 +- testutil/network/network.go | 2 +- types/chain_id.go | 6 --- types/coin.go | 7 +++ types/hdpath.go | 32 ++++++++++++ 21 files changed, 166 insertions(+), 138 deletions(-) rename {types => cmd/config}/config.go (70%) rename {types => cmd/config}/config_test.go (90%) rename {cmd/ethermintd => server}/config/config.go (84%) rename {cmd/ethermintd => server}/config/config_test.go (100%) rename {cmd/ethermintd => server}/config/toml.go (74%) create mode 100644 server/flags.go create mode 100644 types/hdpath.go diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index cd75e651..39a9c62c 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -82,7 +82,7 @@ jobs: run: | excludelist+=" $(find ./ -type f -name '*.pb.go')" for filename in ${excludelist}; do - filename=$(echo $filename | sed 's/^./github.com\/cosmos\/ethermint/g') + filename=$(echo $filename | sed 's/^./github.com\/tharsis\/ethermint/g') echo "Excluding ${filename} from coverage report..." sed -i.bak "/$(echo $filename | sed 's/\//\\\//g')/d" coverage.txt done @@ -121,7 +121,7 @@ jobs: run: | excludelist+=" $(find ./ -type f -name '*.pb.go')" for filename in ${excludelist}; do - filename=$(echo $filename | sed 's/^./github.com\/cosmos\/ethermint/g') + filename=$(echo $filename | sed 's/^./github.com\/tharsis\/ethermint/g') echo "Excluding ${filename} from coverage report..." sed -i.bak "/$(echo $filename | sed 's/\//\\\//g')/d" coverage.txt done @@ -160,7 +160,7 @@ jobs: run: | excludelist+=" $(find ./ -type f -name '*.pb.go')" for filename in ${excludelist}; do - filename=$(echo $filename | sed 's/^./github.com\/cosmos\/ethermint/g') + filename=$(echo $filename | sed 's/^./github.com\/tharsis\/ethermint/g') echo "Excluding ${filename} from coverage report..." sed -i.bak "/$(echo $filename | sed 's/\//\\\//g')/d" coverage.txt done @@ -199,7 +199,7 @@ jobs: run: | excludelist+=" $(find ./ -type f -name '*.pb.go')" for filename in ${excludelist}; do - filename=$(echo $filename | sed 's/^./github.com\/cosmos\/ethermint/g') + filename=$(echo $filename | sed 's/^./github.com\/tharsis\/ethermint/g') echo "Excluding ${filename} from coverage report..." sed -i.bak "/$(echo $filename | sed 's/\//\\\//g')/d" coverage.txt done diff --git a/app/app.go b/app/app.go index 22f121b6..8999d230 100644 --- a/app/app.go +++ b/app/app.go @@ -104,11 +104,6 @@ import ( ) func init() { - // set the address prefixes - config := sdk.GetConfig() - ethermint.SetBech32Prefixes(config) - ethermint.SetBip44CoinType(config) - userHomeDir, err := os.UserHomeDir() if err != nil { panic(err) diff --git a/client/config.go b/client/config.go index 3662e98f..beb14cb0 100644 --- a/client/config.go +++ b/client/config.go @@ -42,29 +42,6 @@ func InitConfig(cmd *cobra.Command) error { return viper.BindPFlag(cli.OutputFlag, cmd.PersistentFlags().Lookup(cli.OutputFlag)) } -// GenerateChainID wraps a cobra command with a RunE function with base 10 integer chain-id random generation -// when a chain-id is not provided. -func GenerateChainID(baseCmd *cobra.Command) *cobra.Command { - // Copy base run command to be used after chain verification - baseRunE := baseCmd.RunE - - // Function to replace command's RunE function - generateFn := func(cmd *cobra.Command, args []string) error { - chainID, _ := cmd.Flags().GetString(flags.FlagChainID) - - if chainID != "" { - if err := cmd.Flags().Set(flags.FlagChainID, ethermint.GenerateRandomChainID()); err != nil { - return fmt.Errorf("could not set random chain-id: %w", err) - } - } - - return baseRunE(cmd, args) - } - - baseCmd.RunE = generateFn - return baseCmd -} - // ValidateChainID wraps a cobra command with a RunE function with base 10 integer chain-id verification. func ValidateChainID(baseCmd *cobra.Command) *cobra.Command { // Copy base run command to be used after chain verification diff --git a/client/testnet.go b/client/testnet.go index b8e5322b..7dcaa9c1 100644 --- a/client/testnet.go +++ b/client/testnet.go @@ -39,8 +39,8 @@ import ( mintypes "github.com/cosmos/cosmos-sdk/x/mint/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" - "github.com/tharsis/ethermint/cmd/ethermintd/config" "github.com/tharsis/ethermint/crypto/hd" + "github.com/tharsis/ethermint/server/config" ethermint "github.com/tharsis/ethermint/types" evmtypes "github.com/tharsis/ethermint/x/evm/types" ) diff --git a/types/config.go b/cmd/config/config.go similarity index 70% rename from types/config.go rename to cmd/config/config.go index 5b131286..58cfc2d0 100644 --- a/types/config.go +++ b/cmd/config/config.go @@ -1,11 +1,9 @@ -package types +package config import ( - "math/big" - sdk "github.com/cosmos/cosmos-sdk/types" - ethaccounts "github.com/ethereum/go-ethereum/accounts" + ethermint "github.com/tharsis/ethermint/types" ) const ( @@ -24,17 +22,11 @@ const ( Bech32PrefixConsAddr = EthBech32Prefix + sdk.PrefixValidator + sdk.PrefixConsensus // Bech32PrefixConsPub defines the Bech32 prefix of a consensus node public key Bech32PrefixConsPub = EthBech32Prefix + sdk.PrefixValidator + sdk.PrefixConsensus + sdk.PrefixPublic - - // Bip44CoinType satisfies EIP84. See https://github.com/ethereum/EIPs/issues/84 for more info. - Bip44CoinType = 60 ) -var ( - // BIP44HDPath is the BIP44 HD path used on Ethereum. - BIP44HDPath = ethaccounts.DefaultBaseDerivationPath.String() - - // PowerReduction defines the default power reduction value for staking - PowerReduction = sdk.NewIntFromBigInt(new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil)) +const ( + // DisplayDenom defines the denomination displayed to users in client applications. + DisplayDenom = "photon" ) // SetBech32Prefixes sets the global prefixes to be used when serializing addresses and public keys to Bech32 strings. @@ -46,6 +38,18 @@ func SetBech32Prefixes(config *sdk.Config) { // SetBip44CoinType sets the global coin type to be used in hierarchical deterministic wallets. func SetBip44CoinType(config *sdk.Config) { - config.SetCoinType(Bip44CoinType) - config.SetFullFundraiserPath(BIP44HDPath) + config.SetCoinType(ethermint.Bip44CoinType) + config.SetPurpose(sdk.Purpose) // Shared + config.SetFullFundraiserPath(ethermint.BIP44HDPath) // nolint: staticcheck +} + +// RegisterDenoms registers the base and display denominations to the SDK. +func RegisterDenoms() { + if err := sdk.RegisterDenom(DisplayDenom, sdk.OneDec()); err != nil { + panic(err) + } + + if err := sdk.RegisterDenom(ethermint.AttoPhoton, sdk.NewDecWithPrec(1, ethermint.BaseDenomUnit)); err != nil { + panic(err) + } } diff --git a/types/config_test.go b/cmd/config/config_test.go similarity index 90% rename from types/config_test.go rename to cmd/config/config_test.go index c3fc1f00..9f1bd541 100644 --- a/types/config_test.go +++ b/cmd/config/config_test.go @@ -1,9 +1,10 @@ -package types +package config import ( "testing" "github.com/stretchr/testify/require" + ethermint "github.com/tharsis/ethermint/types" "github.com/cosmos/cosmos-sdk/crypto/hd" sdk "github.com/cosmos/cosmos-sdk/types" @@ -41,14 +42,14 @@ func TestSetCoinType(t *testing.T) { require.Equal(t, sdk.FullFundraiserPath, config.GetFullFundraiserPath()) SetBip44CoinType(config) - require.Equal(t, Bip44CoinType, int(config.GetCoinType())) + require.Equal(t, int(ethermint.Bip44CoinType), int(config.GetCoinType())) require.Equal(t, sdk.GetConfig().GetCoinType(), config.GetCoinType()) require.Equal(t, sdk.GetConfig().GetFullFundraiserPath(), config.GetFullFundraiserPath()) } func TestHDPath(t *testing.T) { - params := *hd.NewFundraiserParams(0, Bip44CoinType, 0) + params := *hd.NewFundraiserParams(0, ethermint.Bip44CoinType, 0) hdPath := params.String() require.Equal(t, "m/44'/60'/0'/0/0", hdPath) - require.Equal(t, hdPath, BIP44HDPath) + require.Equal(t, hdPath, ethermint.BIP44HDPath) } diff --git a/cmd/ethermintd/flags.go b/cmd/ethermintd/flags.go index c863aa6f..7d74d36b 100644 --- a/cmd/ethermintd/flags.go +++ b/cmd/ethermintd/flags.go @@ -3,10 +3,7 @@ package main import ( "fmt" - "github.com/cosmos/cosmos-sdk/client/flags" - "github.com/cosmos/cosmos-sdk/crypto/keyring" "github.com/spf13/cobra" - "github.com/spf13/viper" "github.com/tharsis/ethermint/version" ) @@ -29,51 +26,3 @@ var ( }, } ) - -var ( - fromPassphrase string - ethNodeWS string - ethNodeHTTP string - statsdEnabled bool - statsdPrefix string - statsdAddress string - statsdStuckFunc string - evmDebug bool -) - -// addTxFlags adds common flags for commands to post tx -func addTxFlags(cmd *cobra.Command) *cobra.Command { - cmd.PersistentFlags().String(flags.FlagChainID, "testnet", "Specify Chain ID for sending Tx") - cmd.PersistentFlags().String(flags.FlagFrom, "", "Name or address of private key with which to sign") - cmd.PersistentFlags().StringVar(&fromPassphrase, "from-passphrase", "12345678", "Passphrase for private key specified with 'from'") - cmd.PersistentFlags().StringVar(ðNodeWS, "eth-node-ws", "ws://localhost:8546", "WebSocket endpoint for an Ethereum node.") - cmd.PersistentFlags().StringVar(ðNodeHTTP, "eth-node-http", "http://localhost:8545", "HTTP endpoint for an Ethereum node.") - cmd.PersistentFlags().BoolVar(&statsdEnabled, "statsd-enabled", false, "Enabled StatsD reporting.") - cmd.PersistentFlags().StringVar(&statsdPrefix, "statsd-prefix", "ethermintd", "Specify StatsD compatible metrics prefix.") - cmd.PersistentFlags().StringVar(&statsdAddress, "statsd-address", "localhost:8125", "UDP address of a StatsD compatible metrics aggregator.") - cmd.PersistentFlags().StringVar(&statsdStuckFunc, "statsd-stuck-func", "5m", "Sets a duration to consider a function to be stuck (e.g. in deadlock).") - cmd.PersistentFlags().String(flags.FlagFees, "", "Fees to pay along with transaction; eg: 10aphoton") - cmd.PersistentFlags().String(flags.FlagGasPrices, "", "Gas prices to determine the transaction fee (e.g. 10aphoton)") - cmd.PersistentFlags().String(flags.FlagNode, "tcp://localhost:26657", ": to tendermint rpc interface for this chain") - cmd.PersistentFlags().Float64(flags.FlagGasAdjustment, flags.DefaultGasAdjustment, "adjustment factor to be multiplied against the estimate returned by the tx simulation; if the gas limit is set manually this flag is ignored ") - cmd.PersistentFlags().StringP(flags.FlagBroadcastMode, "b", flags.BroadcastSync, "Transaction broadcasting mode (sync|async|block)") - cmd.PersistentFlags().BoolVar(&evmDebug, "evm-debug", false, "Enable EVM debug traces") - cmd.PersistentFlags().String(flags.FlagKeyringBackend, keyring.BackendFile, "Select keyring's backend") - - // --gas can accept integers and "simulate" - // cmd.PersistentFlags().Var(&flags.GasFlagVar, "gas", fmt.Sprintf( - // "gas limit to set per-transaction; set to %q to calculate required gas automatically (default %d)", - // flags.GasFlagAuto, flags.DefaultGasLimit, - // )) - - // viper.BindPFlag(flags.FlagTrustNode, cmd.Flags().Lookup(flags.FlagTrustNode)) - - // TODO: we need to handle the errors for these, decide if we should return error upward and handle - // nolint: errcheck - viper.BindPFlag(flags.FlagNode, cmd.Flags().Lookup(flags.FlagNode)) - // nolint: errcheck - viper.BindPFlag(flags.FlagKeyringBackend, cmd.Flags().Lookup(flags.FlagKeyringBackend)) - // nolint: errcheck - cmd.MarkFlagRequired(flags.FlagChainID) - return cmd -} diff --git a/cmd/ethermintd/genaccounts.go b/cmd/ethermintd/genaccounts.go index 7548de6a..e50ee817 100644 --- a/cmd/ethermintd/genaccounts.go +++ b/cmd/ethermintd/genaccounts.go @@ -11,7 +11,6 @@ import ( "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" - "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/server" sdk "github.com/cosmos/cosmos-sdk/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" @@ -45,8 +44,7 @@ contain valid denominations. Accounts may optionally be supplied with vesting pa Args: cobra.ExactArgs(2), RunE: func(cmd *cobra.Command, args []string) error { clientCtx := client.GetClientContextFromCmd(cmd) - depCdc := clientCtx.JSONCodec - cdc := depCdc.(codec.Codec) + cdc := clientCtx.Codec serverCtx := server.GetServerContextFromCmd(cmd) config := serverCtx.Config @@ -173,7 +171,7 @@ contain valid denominations. Accounts may optionally be supplied with vesting pa appState[authtypes.ModuleName] = authGenStateBz - bankGenState := banktypes.GetGenesisStateFromAppState(depCdc, appState) + bankGenState := banktypes.GetGenesisStateFromAppState(cdc, appState) bankGenState.Balances = append(bankGenState.Balances, balances) bankGenState.Balances = banktypes.SanitizeGenesisBalances(bankGenState.Balances) bankGenState.Supply = bankGenState.Supply.Add(balances.Coins...) diff --git a/cmd/ethermintd/main.go b/cmd/ethermintd/main.go index 241b9e4c..f67973af 100644 --- a/cmd/ethermintd/main.go +++ b/cmd/ethermintd/main.go @@ -5,11 +5,16 @@ import ( "github.com/cosmos/cosmos-sdk/server" svrcmd "github.com/cosmos/cosmos-sdk/server/cmd" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/tharsis/ethermint/app" + cmdcfg "github.com/tharsis/ethermint/cmd/config" ) func main() { + setupConfig() + cmdcfg.RegisterDenoms() + rootCmd, _ := NewRootCmd() if err := svrcmd.Execute(rootCmd, app.DefaultNodeHome); err != nil { @@ -22,3 +27,11 @@ func main() { } } } + +func setupConfig() { + // set the address prefixes + config := sdk.GetConfig() + cmdcfg.SetBech32Prefixes(config) + cmdcfg.SetBip44CoinType(config) + config.Seal() +} diff --git a/cmd/ethermintd/root.go b/cmd/ethermintd/root.go index f37376e6..8b532339 100644 --- a/cmd/ethermintd/root.go +++ b/cmd/ethermintd/root.go @@ -34,10 +34,11 @@ import ( "github.com/tharsis/ethermint/app" ethermintclient "github.com/tharsis/ethermint/client" - ethermintconfig "github.com/tharsis/ethermint/cmd/ethermintd/config" "github.com/tharsis/ethermint/crypto/hd" "github.com/tharsis/ethermint/encoding" "github.com/tharsis/ethermint/server" + servercfg "github.com/tharsis/ethermint/server/config" + ethermint "github.com/tharsis/ethermint/types" ) const EnvPrefix = "ETHERMINT" @@ -77,7 +78,7 @@ func NewRootCmd() (*cobra.Command, params.EncodingConfig) { return err } - customAppTemplate, customAppConfig := ethermintconfig.AppConfig() + customAppTemplate, customAppConfig := servercfg.AppConfig(ethermint.AttoPhoton) return sdkserver.InterceptConfigsPreRunHandler(cmd, customAppTemplate, customAppConfig) }, @@ -114,7 +115,7 @@ func NewRootCmd() (*cobra.Command, params.EncodingConfig) { txCommand(), ethermintclient.KeyCommands(app.DefaultNodeHome), ) - rootCmd = addTxFlags(rootCmd) + rootCmd = server.AddTxFlags(rootCmd) // add rosetta rootCmd.AddCommand(sdkserver.RosettaCommand(encodingConfig.InterfaceRegistry, encodingConfig.Marshaler)) diff --git a/ethereum/rpc/namespaces/personal/api.go b/ethereum/rpc/namespaces/personal/api.go index 1cdb1748..567033fa 100644 --- a/ethereum/rpc/namespaces/personal/api.go +++ b/ethereum/rpc/namespaces/personal/api.go @@ -27,15 +27,25 @@ import ( // PrivateAccountAPI is the personal_ prefixed set of APIs in the Web3 JSON-RPC spec. type PrivateAccountAPI struct { - ethAPI *eth.PublicAPI - logger log.Logger + ethAPI *eth.PublicAPI + logger log.Logger + hdPathIter ethermint.HDPathIterator } // NewAPI creates an instance of the public Personal Eth API. func NewAPI(logger log.Logger, ethAPI *eth.PublicAPI) *PrivateAccountAPI { + cfg := sdk.GetConfig() + basePath := cfg.GetFullFundraiserPath() + + iterator, err := ethermint.NewHDPathIterator(basePath, true) + if err != nil { + panic(err) + } + return &PrivateAccountAPI{ - ethAPI: ethAPI, - logger: logger.With("api", "personal"), + ethAPI: ethAPI, + logger: logger.With("api", "personal"), + hdPathIter: iterator, } } @@ -109,14 +119,16 @@ func (api *PrivateAccountAPI) NewAccount(password string) (common.Address, error name := "key_" + time.Now().UTC().Format(time.RFC3339) // create the mnemonic and save the account - info, _, err := api.ethAPI.ClientCtx().Keyring.NewMnemonic(name, keyring.English, ethermint.BIP44HDPath, password, hd.EthSecp256k1) + hdPath := api.hdPathIter() + + info, _, err := api.ethAPI.ClientCtx().Keyring.NewMnemonic(name, keyring.English, hdPath.String(), password, hd.EthSecp256k1) if err != nil { return common.Address{}, err } addr := common.BytesToAddress(info.GetPubKey().Address().Bytes()) api.logger.Info("Your new key was generated", "address", addr.String()) - api.logger.Info("Please backup your key file!", "path", os.Getenv("HOME")+"/.ethermint/"+name) + api.logger.Info("Please backup your key file!", "path", os.Getenv("HOME")+"/.ethermint/"+name) // TODO: pass the correct binary api.logger.Info("Please remember your password!") return addr, nil } diff --git a/cmd/ethermintd/config/config.go b/server/config/config.go similarity index 84% rename from cmd/ethermintd/config/config.go rename to server/config/config.go index 88ee7cbb..aad2fbab 100644 --- a/cmd/ethermintd/config/config.go +++ b/server/config/config.go @@ -3,8 +3,6 @@ package config import ( "github.com/cosmos/cosmos-sdk/server/config" "github.com/spf13/viper" - - ethermint "github.com/tharsis/ethermint/types" ) const ( @@ -25,7 +23,7 @@ func GetDefaultAPINamespaces() []string { // AppConfig helps to override default appConfig template and configs. // return "", nil if no custom configuration is required for the application. -func AppConfig() (string, interface{}) { +func AppConfig(denom string) (string, interface{}) { // Optionally allow the chain developer to overwrite the SDK's default // server config. srvCfg := config.DefaultConfig() @@ -42,7 +40,9 @@ func AppConfig() (string, interface{}) { // own app.toml to override, or use this default value. // // In ethermint, we set the min gas prices to 0. - srvCfg.MinGasPrices = "0" + ethermint.AttoPhoton + if denom != "" { + srvCfg.MinGasPrices = "0" + denom + } customAppConfig := Config{ Config: *srvCfg, @@ -79,10 +79,11 @@ type EVMRPCConfig struct { // DefaultEVMConfig returns an EVM config with the JSON-RPC API enabled by default func DefaultEVMConfig() *EVMRPCConfig { return &EVMRPCConfig{ - Enable: true, - API: GetDefaultAPINamespaces(), - RPCAddress: DefaultEVMAddress, - WsAddress: DefaultEVMWSAddress, + Enable: true, + API: GetDefaultAPINamespaces(), + RPCAddress: DefaultEVMAddress, + WsAddress: DefaultEVMWSAddress, + EnableUnsafeCORS: false, } } @@ -96,16 +97,16 @@ type Config struct { // GetConfig returns a fully parsed Config object. func GetConfig(v *viper.Viper) Config { - cfg := config.GetConfig(v) return Config{ Config: cfg, EVMRPC: EVMRPCConfig{ - Enable: v.GetBool("evm-rpc.enable"), - API: v.GetStringSlice("evm-rpc.api"), - RPCAddress: v.GetString("evm-rpc.address"), - WsAddress: v.GetString("evm-rpc.ws-address"), + Enable: v.GetBool("evm-rpc.enable"), + API: v.GetStringSlice("evm-rpc.api"), + RPCAddress: v.GetString("evm-rpc.address"), + WsAddress: v.GetString("evm-rpc.ws-address"), + EnableUnsafeCORS: v.GetBool("evm-rpc.enable-unsafe-cors"), }, } } diff --git a/cmd/ethermintd/config/config_test.go b/server/config/config_test.go similarity index 100% rename from cmd/ethermintd/config/config_test.go rename to server/config/config_test.go diff --git a/cmd/ethermintd/config/toml.go b/server/config/toml.go similarity index 74% rename from cmd/ethermintd/config/toml.go rename to server/config/toml.go index 3cb9f976..dd185b1b 100644 --- a/cmd/ethermintd/config/toml.go +++ b/server/config/toml.go @@ -16,4 +16,10 @@ address = "{{ .EVMRPC.RPCAddress }}" # Address defines the EVM WebSocket server address to bind to. ws-address = "{{ .EVMRPC.WsAddress }}" + +# API defines a list of JSON-RPC namespaces that should be enabled +api = "{{ .EVMRPC.API }}" + +# EnableUnsafeCORS defines if CORS should be enabled (unsafe - use it at your own risk) +enable-unsafe-cors = "{{ .EVMRPC.EnableUnsafeCORS }}" ` diff --git a/server/evmrpc.go b/server/evmrpc.go index 6c60e660..225ca51d 100644 --- a/server/evmrpc.go +++ b/server/evmrpc.go @@ -12,8 +12,9 @@ import ( "github.com/cosmos/cosmos-sdk/server" "github.com/cosmos/cosmos-sdk/server/types" ethrpc "github.com/ethereum/go-ethereum/rpc" - "github.com/tharsis/ethermint/cmd/ethermintd/config" "github.com/tharsis/ethermint/ethereum/rpc" + + "github.com/tharsis/ethermint/server/config" ) // StartEVMRPC start evm rpc server diff --git a/server/flags.go b/server/flags.go new file mode 100644 index 00000000..27095d46 --- /dev/null +++ b/server/flags.go @@ -0,0 +1,37 @@ +package server + +import ( + "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/crypto/keyring" + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +// AddTxFlags adds common flags for commands to post tx +func AddTxFlags(cmd *cobra.Command) *cobra.Command { + cmd.PersistentFlags().String(flags.FlagChainID, "testnet", "Specify Chain ID for sending Tx") + cmd.PersistentFlags().String(flags.FlagFrom, "", "Name or address of private key with which to sign") + cmd.PersistentFlags().String(flags.FlagFees, "", "Fees to pay along with transaction; eg: 10aphoton") + cmd.PersistentFlags().String(flags.FlagGasPrices, "", "Gas prices to determine the transaction fee (e.g. 10aphoton)") + cmd.PersistentFlags().String(flags.FlagNode, "tcp://localhost:26657", ": to tendermint rpc interface for this chain") + cmd.PersistentFlags().Float64(flags.FlagGasAdjustment, flags.DefaultGasAdjustment, "adjustment factor to be multiplied against the estimate returned by the tx simulation; if the gas limit is set manually this flag is ignored ") + cmd.PersistentFlags().StringP(flags.FlagBroadcastMode, "b", flags.BroadcastSync, "Transaction broadcasting mode (sync|async|block)") + cmd.PersistentFlags().String(flags.FlagKeyringBackend, keyring.BackendFile, "Select keyring's backend") + + // --gas can accept integers and "simulate" + // cmd.PersistentFlags().Var(&flags.GasFlagVar, "gas", fmt.Sprintf( + // "gas limit to set per-transaction; set to %q to calculate required gas automatically (default %d)", + // flags.GasFlagAuto, flags.DefaultGasLimit, + // )) + + // viper.BindPFlag(flags.FlagTrustNode, cmd.Flags().Lookup(flags.FlagTrustNode)) + + // TODO: we need to handle the errors for these, decide if we should return error upward and handle + // nolint: errcheck + viper.BindPFlag(flags.FlagNode, cmd.Flags().Lookup(flags.FlagNode)) + // nolint: errcheck + viper.BindPFlag(flags.FlagKeyringBackend, cmd.Flags().Lookup(flags.FlagKeyringBackend)) + // nolint: errcheck + cmd.MarkFlagRequired(flags.FlagChainID) + return cmd +} diff --git a/server/start.go b/server/start.go index 6fb06522..b314461b 100644 --- a/server/start.go +++ b/server/start.go @@ -40,8 +40,8 @@ import ( ethlog "github.com/ethereum/go-ethereum/log" - "github.com/tharsis/ethermint/cmd/ethermintd/config" ethdebug "github.com/tharsis/ethermint/ethereum/rpc/namespaces/debug" + "github.com/tharsis/ethermint/server/config" ) // Tendermint full-node start flags diff --git a/testutil/network/network.go b/testutil/network/network.go index 03a4303a..78ae44bb 100644 --- a/testutil/network/network.go +++ b/testutil/network/network.go @@ -50,9 +50,9 @@ import ( stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" "github.com/tharsis/ethermint/app" - srvconfig "github.com/tharsis/ethermint/cmd/ethermintd/config" "github.com/tharsis/ethermint/crypto/hd" "github.com/tharsis/ethermint/encoding" + srvconfig "github.com/tharsis/ethermint/server/config" ethermint "github.com/tharsis/ethermint/types" ) diff --git a/types/chain_id.go b/types/chain_id.go index 1c54fbe2..7bc316b1 100644 --- a/types/chain_id.go +++ b/types/chain_id.go @@ -7,7 +7,6 @@ import ( "strings" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" - tmrand "github.com/tendermint/tendermint/libs/rand" ) var ( @@ -47,8 +46,3 @@ func ParseChainID(chainID string) (*big.Int, error) { return chainIDInt, nil } - -// GenerateRandomChainID returns a random chain-id in the valid format. -func GenerateRandomChainID() string { - return fmt.Sprintf("ethermint-%d", 10+tmrand.Intn(10000)) -} diff --git a/types/coin.go b/types/coin.go index d7e396ed..493017c9 100644 --- a/types/coin.go +++ b/types/coin.go @@ -1,6 +1,8 @@ package types import ( + "math/big" + sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -19,6 +21,11 @@ const ( BaseDenomUnit = 18 ) +var ( + // PowerReduction defines the default power reduction value for staking + PowerReduction = sdk.NewIntFromBigInt(new(big.Int).Exp(big.NewInt(10), big.NewInt(BaseDenomUnit), nil)) +) + // NewPhotonCoin is a utility function that returns an "aphoton" coin with the given sdk.Int amount. // The function will panic if the provided amount is negative. func NewPhotonCoin(amount sdk.Int) sdk.Coin { diff --git a/types/hdpath.go b/types/hdpath.go new file mode 100644 index 00000000..cf2db7c4 --- /dev/null +++ b/types/hdpath.go @@ -0,0 +1,32 @@ +package types + +import ( + ethaccounts "github.com/ethereum/go-ethereum/accounts" +) + +var ( + // Bip44CoinType satisfies EIP84. See https://github.com/ethereum/EIPs/issues/84 for more info. + Bip44CoinType uint32 = 60 + + // BIP44HDPath is the default BIP44 HD path used on Ethereum. + BIP44HDPath = ethaccounts.DefaultBaseDerivationPath.String() +) + +type ( + HDPathIterator func() ethaccounts.DerivationPath +) + +// HDPathIterator receives a base path as a string and a boolean for the desired iterator type and +// returns a function that iterates over the base HD path, returning the string. +func NewHDPathIterator(basePath string, ledgerIter bool) (HDPathIterator, error) { + hdPath, err := ethaccounts.ParseDerivationPath(basePath) + if err != nil { + return nil, err + } + + if ledgerIter { + return ethaccounts.LedgerLiveIterator(hdPath), nil + } + + return ethaccounts.DefaultIterator(hdPath), nil +}