From d982e0961a515dd87d4a2b614a779c15d9955003 Mon Sep 17 00:00:00 2001 From: Austin Abell Date: Mon, 8 Jul 2019 15:26:33 -0400 Subject: [PATCH] Daemon framework (#66) * Implements daemon framework with basic functionality * Comment out unused functions for linter --- app/ethermint.go | 79 +++++++++++++++++++++++++++++++--------------- cmd/emintd/main.go | 78 +++++++++++++++++++++++++++++++++++++++++++-- go.mod | 1 + 3 files changed, 129 insertions(+), 29 deletions(-) diff --git a/app/ethermint.go b/app/ethermint.go index 7231ae0e..984ec4c5 100644 --- a/app/ethermint.go +++ b/app/ethermint.go @@ -1,15 +1,19 @@ package app import ( - "github.com/cosmos/ethermint/x/evm" "os" + "github.com/cosmos/ethermint/x/evm" + bam "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/module" "github.com/cosmos/cosmos-sdk/x/auth" "github.com/cosmos/cosmos-sdk/x/bank" distr "github.com/cosmos/cosmos-sdk/x/distribution" + "github.com/cosmos/cosmos-sdk/x/genaccounts" + "github.com/cosmos/cosmos-sdk/x/genutil" "github.com/cosmos/cosmos-sdk/x/gov" "github.com/cosmos/cosmos-sdk/x/mint" "github.com/cosmos/cosmos-sdk/x/params" @@ -35,6 +39,22 @@ var ( // default home directories for the application CLI DefaultCLIHome = os.ExpandEnv("$HOME/.emintcli") + // DefaultNodeHome sets the folder where the applcation data and configuration will be stored + DefaultNodeHome = os.ExpandEnv("$HOME/.emintd") + + // ModuleBasics is in charge of setting up basic module elements + ModuleBasics = module.NewBasicManager( + genaccounts.AppModuleBasic{}, + genutil.AppModuleBasic{}, + auth.AppModuleBasic{}, + bank.AppModuleBasic{}, + params.AppModuleBasic{}, + evm.AppModuleBasic{}, + staking.AppModuleBasic{}, + distr.AppModuleBasic{}, + slashing.AppModuleBasic{}, + ) + storeKeyAccount = sdk.NewKVStoreKey("acc") storeKeyStorage = sdk.NewKVStoreKey("contract_storage") storeKeyMain = sdk.NewKVStoreKey("main") @@ -46,34 +66,41 @@ var ( storeKeyTransParams = sdk.NewTransientStoreKey("transient_params") ) -type ( - // EthermintApp implements an extended ABCI application. It is an application - // that may process transactions through Ethereum's EVM running atop of - // Tendermint consensus. - EthermintApp struct { - *bam.BaseApp +// MakeCodec generates the necessary codecs for Amino +func MakeCodec() *codec.Codec { + var cdc = codec.New() + ModuleBasics.RegisterCodec(cdc) + sdk.RegisterCodec(cdc) + codec.RegisterCrypto(cdc) + return cdc +} - cdc *codec.Codec +// EthermintApp implements an extended ABCI application. It is an application +// that may process transactions through Ethereum's EVM running atop of +// Tendermint consensus. +type EthermintApp struct { + *bam.BaseApp - accountKey *sdk.KVStoreKey - storageKey *sdk.KVStoreKey - mainKey *sdk.KVStoreKey - stakeKey *sdk.KVStoreKey - slashingKey *sdk.KVStoreKey - govKey *sdk.KVStoreKey - supplyKey *sdk.KVStoreKey - paramsKey *sdk.KVStoreKey - tParamsKey *sdk.TransientStoreKey + cdc *codec.Codec - accountKeeper auth.AccountKeeper - supplyKeeper supply.Keeper - bankKeeper bank.Keeper - stakeKeeper staking.Keeper - slashingKeeper slashing.Keeper - govKeeper gov.Keeper - paramsKeeper params.Keeper - } -) + accountKey *sdk.KVStoreKey + storageKey *sdk.KVStoreKey + mainKey *sdk.KVStoreKey + stakeKey *sdk.KVStoreKey + slashingKey *sdk.KVStoreKey + govKey *sdk.KVStoreKey + supplyKey *sdk.KVStoreKey + paramsKey *sdk.KVStoreKey + tParamsKey *sdk.TransientStoreKey + + accountKeeper auth.AccountKeeper + supplyKeeper supply.Keeper + bankKeeper bank.Keeper + stakeKeeper staking.Keeper + slashingKeeper slashing.Keeper + govKeeper gov.Keeper + paramsKeeper params.Keeper +} // NewEthermintApp returns a reference to a new initialized Ethermint // application. diff --git a/cmd/emintd/main.go b/cmd/emintd/main.go index 33176840..7b25ca01 100644 --- a/cmd/emintd/main.go +++ b/cmd/emintd/main.go @@ -1,7 +1,79 @@ package main +import ( + "github.com/cosmos/cosmos-sdk/server" + "github.com/cosmos/cosmos-sdk/x/genaccounts" + genaccscli "github.com/cosmos/cosmos-sdk/x/genaccounts/client/cli" + "github.com/cosmos/cosmos-sdk/x/staking" + + "github.com/spf13/cobra" + "github.com/tendermint/tendermint/libs/cli" + + sdk "github.com/cosmos/cosmos-sdk/types" + genutilcli "github.com/cosmos/cosmos-sdk/x/genutil/client/cli" + emintapp "github.com/cosmos/ethermint/app" +) + func main() { - // TODO: Implement daemon command and logic - // - // Ref: https://github.com/cosmos/ethermint/issues/433 + cobra.EnableCommandSorting = false + + cdc := emintapp.MakeCodec() + + config := sdk.GetConfig() + config.SetBech32PrefixForAccount(sdk.Bech32PrefixAccAddr, sdk.Bech32PrefixAccPub) + config.SetBech32PrefixForValidator(sdk.Bech32PrefixValAddr, sdk.Bech32PrefixValPub) + config.SetBech32PrefixForConsensusNode(sdk.Bech32PrefixConsAddr, sdk.Bech32PrefixConsPub) + config.Seal() + + ctx := server.NewDefaultContext() + + rootCmd := &cobra.Command{ + Use: "emintd", + Short: "Ethermint App Daemon (server)", + PersistentPreRunE: server.PersistentPreRunEFn(ctx), + } + // CLI commands to initialize the chain + rootCmd.AddCommand( + genutilcli.InitCmd(ctx, cdc, emintapp.ModuleBasics, emintapp.DefaultNodeHome), + genutilcli.CollectGenTxsCmd(ctx, cdc, genaccounts.AppModuleBasic{}, emintapp.DefaultNodeHome), + genutilcli.GenTxCmd(ctx, cdc, emintapp.ModuleBasics, staking.AppModuleBasic{}, genaccounts.AppModuleBasic{}, emintapp.DefaultNodeHome, emintapp.DefaultCLIHome), + genutilcli.ValidateGenesisCmd(ctx, cdc, emintapp.ModuleBasics), + + // AddGenesisAccountCmd allows users to add accounts to the genesis file + genaccscli.AddGenesisAccountCmd(ctx, cdc, emintapp.DefaultNodeHome, emintapp.DefaultCLIHome), + ) + + // TODO: Add export app state and TM validators commands + // server.AddCommands(ctx, cdc, rootCmd, newApp, exportAppStateAndTMValidators) + + // prepare and add flags + executor := cli.PrepareBaseCmd(rootCmd, "EM", emintapp.DefaultNodeHome) + err := executor.Execute() + if err != nil { + panic(err) + } } + +// func newApp(logger log.Logger, db dbm.DB, traceStore io.Writer) abci.Application { +// return emintapp.NewEthermintApp(logger, db) +// } + +// func exportAppStateAndTMValidators( +// logger log.Logger, db dbm.DB, traceStore io.Writer, height int64, forZeroHeight bool, jailWhiteList []string, +// ) (json.RawMessage, []tmtypes.GenesisValidator, error) { + +// // if height != -1 { +// // emintApp := emintapp.NewEthermintApp(logger, db) +// // err := emintApp.LoadHeight(height) +// // if err != nil { +// // return nil, nil, err +// // } +// // return emintApp.ExportAppStateAndValidators(forZeroHeight, jailWhiteList) +// // } + +// // emintApp := emintapp.NewEthermintApp(logger, db) + +// // return emintApp.ExportAppStateAndValidators(forZeroHeight, jailWhiteList) +// // TODO: Unstub method +// return json.RawMessage{}, []tmtypes.GenesisValidator{}, nil +// } diff --git a/go.mod b/go.mod index be5a83f2..c2bf3e9a 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,7 @@ require ( github.com/golangci/golangci-lint v1.17.1 // indirect github.com/google/uuid v1.0.0 // indirect github.com/gordonklaus/ineffassign v0.0.0-20190601041439-ed7b1b5ee0f8 // indirect + github.com/gorilla/mux v1.7.0 github.com/hashicorp/golang-lru v0.5.0 // indirect github.com/huin/goupnp v1.0.0 // indirect github.com/influxdata/influxdb v1.7.7 // indirect