From 1d490ba4d9d578f56bdc526c58e4d967d0a6e016 Mon Sep 17 00:00:00 2001 From: David Ansermino Date: Mon, 8 Jul 2019 12:02:20 -0400 Subject: [PATCH] Adds AppModuleBasic and Genesis Functions (#62) * Adds AppModuleBasic implementation and genesis functions * Fixes broken links * Adds .idea/ * Adds starter for missing genesis funcs * Completes AppModuleBasic interface * Removes comment --- .gitignore | 3 +++ app/ethermint.go | 3 ++- app/genesis.go | 22 ----------------- x/evm/genesis.go | 53 +++++++++++++++++++++++++++++++++++++++++ x/evm/module.go | 51 +++++++++++++++++++++++++++++++++++++++ x/evm/types/codec.go | 4 ++-- x/evm/types/key.go | 10 ++++++++ x/evm/types/msg_test.go | 4 ++-- 8 files changed, 123 insertions(+), 27 deletions(-) delete mode 100644 app/genesis.go create mode 100644 x/evm/genesis.go create mode 100644 x/evm/module.go create mode 100644 x/evm/types/key.go diff --git a/.gitignore b/.gitignore index 17cffba8..bb3a3e0e 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,6 @@ .glide/ vendor/ build/ + +# Goland +.idea/ \ No newline at end of file diff --git a/app/ethermint.go b/app/ethermint.go index 84df14ce..7231ae0e 100644 --- a/app/ethermint.go +++ b/app/ethermint.go @@ -1,6 +1,7 @@ package app import ( + "github.com/cosmos/ethermint/x/evm" "os" bam "github.com/cosmos/cosmos-sdk/baseapp" @@ -164,7 +165,7 @@ func (app *EthermintApp) initChainer( _ sdk.Context, req abci.RequestInitChain, ) abci.ResponseInitChain { - var genesisState GenesisState + var genesisState evm.GenesisState stateJSON := req.AppStateBytes err := app.cdc.UnmarshalJSON(stateJSON, &genesisState) diff --git a/app/genesis.go b/app/genesis.go deleted file mode 100644 index 0b59fe14..00000000 --- a/app/genesis.go +++ /dev/null @@ -1,22 +0,0 @@ -package app - -import ( - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/ethermint/types" -) - -type ( - // GenesisState defines the application's genesis state. It contains all the - // information required and accounts to initialize the blockchain. - GenesisState struct { - Accounts []GenesisAccount `json:"accounts"` - } - - // GenesisAccount defines an account to be initialized in the genesis state. - GenesisAccount struct { - Address sdk.AccAddress `json:"address"` - Coins sdk.Coins `json:"coins"` - Code []byte `json:"code,omitempty"` - Storage types.Storage `json:"storage,omitempty"` - } -) diff --git a/x/evm/genesis.go b/x/evm/genesis.go new file mode 100644 index 00000000..6b0cee02 --- /dev/null +++ b/x/evm/genesis.go @@ -0,0 +1,53 @@ +package evm + +import ( + "fmt" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/ethermint/types" +) + +type ( + // GenesisState defines the application's genesis state. It contains all the + // information required and accounts to initialize the blockchain. + GenesisState struct { + Accounts []GenesisAccount `json:"accounts"` + } + + // GenesisAccount defines an account to be initialized in the genesis state. + GenesisAccount struct { + Address sdk.AccAddress `json:"address"` + Coins sdk.Coins `json:"coins"` + Code []byte `json:"code,omitempty"` + Storage types.Storage `json:"storage,omitempty"` + } +) + +func ValidateGenesis(data GenesisState) error { + for _, acct := range data.Accounts { + if acct.Address == nil { + return fmt.Errorf("Invalid GenesisAccount Error: Missing Address") + } + if acct.Coins == nil { + return fmt.Errorf("Invalid GenesisAccount Error: Missing Coins") + } + } + return nil +} + +func DefaultGenesisState() GenesisState { + return GenesisState{ + Accounts: []GenesisAccount{}, + } +} + +// TODO: Implement these once keeper is established +//func InitGenesis(ctx sdk.Context, keeper Keeper, data GenesisState) []abci.ValidatorUpdate { +// for _, record := range data.Accounts { +// // TODO: Add to keeper +// } +// return []abci.ValidatorUpdate{} +//} +// +//func ExportGenesis(ctx sdk.Context, k Keeper) GenesisState { +// return GenesisState{Accounts: nil} +//} diff --git a/x/evm/module.go b/x/evm/module.go new file mode 100644 index 00000000..c2c0141f --- /dev/null +++ b/x/evm/module.go @@ -0,0 +1,51 @@ +package evm + +import ( + "encoding/json" + "github.com/cosmos/cosmos-sdk/client/context" + "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/ethermint/x/evm/types" + "github.com/gorilla/mux" + "github.com/spf13/cobra" +) + +// app module Basics object +type AppModuleBasic struct{} + +func (AppModuleBasic) Name() string { + return types.ModuleName +} + +func (AppModuleBasic) RegisterCodec(cdc *codec.Codec) { + types.RegisterCodec(cdc) +} + +func (AppModuleBasic) DefaultGenesis() json.RawMessage { + return types.ModuleCdc.MustMarshalJSON(DefaultGenesisState()) +} + +// Validation check of the Genesis +func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { + var data GenesisState + err := types.ModuleCdc.UnmarshalJSON(bz, &data) + if err != nil { + return err + } + // Once json successfully marshalled, passes along to genesis.go + return ValidateGenesis(data) +} + +// Register rest routes +func (AppModuleBasic) RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Router) { + //rpc.RegisterRoutes(ctx, rtr, StoreKey) +} + +// Get the root query command of this module +func (AppModuleBasic) GetQueryCmd(cdc *codec.Codec) *cobra.Command { + return nil // cli.GetQueryCmd(StoreKey, cdc) +} + +// Get the root tx command of this module +func (AppModuleBasic) GetTxCmd(cdc *codec.Codec) *cobra.Command { + return nil // cli.GetTxCmd(StoreKey, cdc) +} diff --git a/x/evm/types/codec.go b/x/evm/types/codec.go index c30ae1e9..985e4def 100644 --- a/x/evm/types/codec.go +++ b/x/evm/types/codec.go @@ -2,7 +2,7 @@ package types import "github.com/cosmos/cosmos-sdk/codec" -var msgCodec = codec.New() +var ModuleCdc = codec.New() func init() { cdc := codec.New() @@ -10,7 +10,7 @@ func init() { RegisterCodec(cdc) codec.RegisterCrypto(cdc) - msgCodec = cdc.Seal() + ModuleCdc = cdc.Seal() } // RegisterCodec registers concrete types and interfaces on the given codec. diff --git a/x/evm/types/key.go b/x/evm/types/key.go new file mode 100644 index 00000000..afc45877 --- /dev/null +++ b/x/evm/types/key.go @@ -0,0 +1,10 @@ +package types + +const ( + // module name + ModuleName = "ethermint" + + // TODO: Use this + // StoreKey to be used when creating the KVStore + StoreKey = ModuleName +) \ No newline at end of file diff --git a/x/evm/types/msg_test.go b/x/evm/types/msg_test.go index 3fe43838..161b49db 100644 --- a/x/evm/types/msg_test.go +++ b/x/evm/types/msg_test.go @@ -124,12 +124,12 @@ func TestMsgEthereumTxAmino(t *testing.T) { addr := GenerateEthAddress() msg := NewEthereumTxMsg(0, addr, nil, 100000, nil, []byte("test")) - raw, err := msgCodec.MarshalBinaryBare(msg) + raw, err := ModuleCdc.MarshalBinaryBare(msg) require.NoError(t, err) var msg2 EthereumTxMsg - err = msgCodec.UnmarshalBinaryBare(raw, &msg2) + err = ModuleCdc.UnmarshalBinaryBare(raw, &msg2) require.NoError(t, err) require.Equal(t, msg.Data, msg2.Data) }