From 69567e29d512e7dcb97502adabd84d2d1917589a Mon Sep 17 00:00:00 2001 From: Austin Abell Date: Wed, 18 Sep 2019 14:50:06 -0400 Subject: [PATCH] Override auth module codec for all references to Ethermint codec (#103) --- cmd/emintcli/main.go | 13 +++++-- types/account_retriever.go | 77 -------------------------------------- x/evm/client/cli/tx.go | 2 +- x/evm/client/utils/tx.go | 31 --------------- 4 files changed, 11 insertions(+), 112 deletions(-) delete mode 100644 types/account_retriever.go diff --git a/cmd/emintcli/main.go b/cmd/emintcli/main.go index a36d5e99..2196e69e 100644 --- a/cmd/emintcli/main.go +++ b/cmd/emintcli/main.go @@ -12,6 +12,7 @@ import ( sdkrpc "github.com/cosmos/cosmos-sdk/client/rpc" sdk "github.com/cosmos/cosmos-sdk/types" emintkeys "github.com/cosmos/ethermint/keys" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" bankcmd "github.com/cosmos/cosmos-sdk/x/bank/client/cli" @@ -27,6 +28,8 @@ func main() { cdc := emintapp.MakeCodec() + authtypes.ModuleCdc = cdc + // Read in the configuration file for the sdk config := sdk.GetConfig() // TODO: Remove or change prefix if usable to generate Ethereum address @@ -76,9 +79,13 @@ func queryCmd(cdc *amino.Codec) *cobra.Command { } // TODO: Possibly add these query commands from other modules - //queryCmd.AddCommand( - // ... - //) + queryCmd.AddCommand( + authcmd.GetAccountCmd(cdc), + client.LineBreak, + authcmd.QueryTxsByEventsCmd(cdc), + authcmd.QueryTxCmd(cdc), + client.LineBreak, + ) // add modules' query commands emintapp.ModuleBasics.AddQueryCommands(queryCmd, cdc) diff --git a/types/account_retriever.go b/types/account_retriever.go deleted file mode 100644 index 51372c03..00000000 --- a/types/account_retriever.go +++ /dev/null @@ -1,77 +0,0 @@ -package types - -import ( - "fmt" - - "github.com/cosmos/cosmos-sdk/codec" - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/auth/exported" - auth "github.com/cosmos/cosmos-sdk/x/auth/types" -) - -// ** Modified version of github.com/cosmos/cosmos-sdk/x/auth/types/account_retriever.go -// ** to allow passing in a codec for decoding Account types -// AccountRetriever defines the properties of a type that can be used to -// retrieve accounts. -type AccountRetriever struct { - querier auth.NodeQuerier - codec *codec.Codec -} - -// * Modified to allow a codec to be passed in -// NewAccountRetriever initialises a new AccountRetriever instance. -func NewAccountRetriever(querier auth.NodeQuerier, codec *codec.Codec) AccountRetriever { - if codec == nil { - codec = auth.ModuleCdc - } - return AccountRetriever{querier: querier, codec: codec} -} - -// GetAccount queries for an account given an address and a block height. An -// error is returned if the query or decoding fails. -func (ar AccountRetriever) GetAccount(addr sdk.AccAddress) (exported.Account, error) { - account, _, err := ar.GetAccountWithHeight(addr) - return account, err -} - -// GetAccountWithHeight queries for an account given an address. Returns the -// height of the query with the account. An error is returned if the query -// or decoding fails. -func (ar AccountRetriever) GetAccountWithHeight(addr sdk.AccAddress) (exported.Account, int64, error) { - // ** This line was changed to use non-static codec - bs, err := ar.codec.MarshalJSON(auth.NewQueryAccountParams(addr)) - if err != nil { - return nil, 0, err - } - - res, height, err := ar.querier.QueryWithData(fmt.Sprintf("custom/%s/%s", auth.QuerierRoute, auth.QueryAccount), bs) - if err != nil { - return nil, 0, err - } - - var account exported.Account - // ** This line was changed to use non-static codec - if err := ar.codec.UnmarshalJSON(res, &account); err != nil { - return nil, 0, err - } - - return account, height, nil -} - -// EnsureExists returns an error if no account exists for the given address else nil. -func (ar AccountRetriever) EnsureExists(addr sdk.AccAddress) error { - if _, err := ar.GetAccount(addr); err != nil { - return err - } - return nil -} - -// GetAccountNumberSequence returns sequence and account number for the given address. -// It returns an error if the account couldn't be retrieved from the state. -func (ar AccountRetriever) GetAccountNumberSequence(addr sdk.AccAddress) (uint64, uint64, error) { - acc, err := ar.GetAccount(addr) - if err != nil { - return 0, 0, err - } - return acc.GetAccountNumber(), acc.GetSequence(), nil -} diff --git a/x/evm/client/cli/tx.go b/x/evm/client/cli/tx.go index da49a85e..24b842fc 100644 --- a/x/evm/client/cli/tx.go +++ b/x/evm/client/cli/tx.go @@ -118,7 +118,7 @@ func GetCmdGenETHTx(cdc *codec.Codec) *cobra.Command { payload := args[3] - txBldr, err = emintUtils.PrepareTxBuilder(txBldr, cliCtx) + txBldr, err = utils.PrepareTxBuilder(txBldr, cliCtx) if err != nil { return err } diff --git a/x/evm/client/utils/tx.go b/x/evm/client/utils/tx.go index 06f6a829..749d7020 100644 --- a/x/evm/client/utils/tx.go +++ b/x/evm/client/utils/tx.go @@ -342,34 +342,3 @@ func getFromFields(from string, genOnly bool) (sdk.AccAddress, string, error) { return info.GetAddress(), info.GetName(), nil } - -// * Overriden function from cosmos-sdk/auth/client/utils/tx.go -// PrepareTxBuilder populates a TxBuilder in preparation for the build of a Tx. -func PrepareTxBuilder(txBldr authtypes.TxBuilder, cliCtx context.CLIContext) (authtypes.TxBuilder, error) { - from := cliCtx.GetFromAddress() - - // * Function is needed to override to use different account getter (to not use static codec) - accGetter := emint.NewAccountRetriever(cliCtx, cliCtx.Codec) - if err := accGetter.EnsureExists(from); err != nil { - return txBldr, err - } - - txbldrAccNum, txbldrAccSeq := txBldr.AccountNumber(), txBldr.Sequence() - // TODO: (ref #1903) Allow for user supplied account number without - // automatically doing a manual lookup. - if txbldrAccNum == 0 || txbldrAccSeq == 0 { - num, seq, err := accGetter.GetAccountNumberSequence(from) - if err != nil { - return txBldr, err - } - - if txbldrAccNum == 0 { - txBldr = txBldr.WithAccountNumber(num) - } - if txbldrAccSeq == 0 { - txBldr = txBldr.WithSequence(seq) - } - } - - return txBldr, nil -}