Skip to content
This repository was archived by the owner on Nov 30, 2021. It is now read-only.

Commit 1d490ba

Browse files
David Anserminoaustinabell
authored andcommitted
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
1 parent ba203d3 commit 1d490ba

File tree

8 files changed

+123
-27
lines changed

8 files changed

+123
-27
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@
1414
.glide/
1515
vendor/
1616
build/
17+
18+
# Goland
19+
.idea/

app/ethermint.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package app
22

33
import (
4+
"github.com/cosmos/ethermint/x/evm"
45
"os"
56

67
bam "github.com/cosmos/cosmos-sdk/baseapp"
@@ -164,7 +165,7 @@ func (app *EthermintApp) initChainer(
164165
_ sdk.Context, req abci.RequestInitChain,
165166
) abci.ResponseInitChain {
166167

167-
var genesisState GenesisState
168+
var genesisState evm.GenesisState
168169
stateJSON := req.AppStateBytes
169170

170171
err := app.cdc.UnmarshalJSON(stateJSON, &genesisState)

app/genesis.go

Lines changed: 0 additions & 22 deletions
This file was deleted.

x/evm/genesis.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package evm
2+
3+
import (
4+
"fmt"
5+
sdk "github.com/cosmos/cosmos-sdk/types"
6+
"github.com/cosmos/ethermint/types"
7+
)
8+
9+
type (
10+
// GenesisState defines the application's genesis state. It contains all the
11+
// information required and accounts to initialize the blockchain.
12+
GenesisState struct {
13+
Accounts []GenesisAccount `json:"accounts"`
14+
}
15+
16+
// GenesisAccount defines an account to be initialized in the genesis state.
17+
GenesisAccount struct {
18+
Address sdk.AccAddress `json:"address"`
19+
Coins sdk.Coins `json:"coins"`
20+
Code []byte `json:"code,omitempty"`
21+
Storage types.Storage `json:"storage,omitempty"`
22+
}
23+
)
24+
25+
func ValidateGenesis(data GenesisState) error {
26+
for _, acct := range data.Accounts {
27+
if acct.Address == nil {
28+
return fmt.Errorf("Invalid GenesisAccount Error: Missing Address")
29+
}
30+
if acct.Coins == nil {
31+
return fmt.Errorf("Invalid GenesisAccount Error: Missing Coins")
32+
}
33+
}
34+
return nil
35+
}
36+
37+
func DefaultGenesisState() GenesisState {
38+
return GenesisState{
39+
Accounts: []GenesisAccount{},
40+
}
41+
}
42+
43+
// TODO: Implement these once keeper is established
44+
//func InitGenesis(ctx sdk.Context, keeper Keeper, data GenesisState) []abci.ValidatorUpdate {
45+
// for _, record := range data.Accounts {
46+
// // TODO: Add to keeper
47+
// }
48+
// return []abci.ValidatorUpdate{}
49+
//}
50+
//
51+
//func ExportGenesis(ctx sdk.Context, k Keeper) GenesisState {
52+
// return GenesisState{Accounts: nil}
53+
//}

x/evm/module.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package evm
2+
3+
import (
4+
"encoding/json"
5+
"github.com/cosmos/cosmos-sdk/client/context"
6+
"github.com/cosmos/cosmos-sdk/codec"
7+
"github.com/cosmos/ethermint/x/evm/types"
8+
"github.com/gorilla/mux"
9+
"github.com/spf13/cobra"
10+
)
11+
12+
// app module Basics object
13+
type AppModuleBasic struct{}
14+
15+
func (AppModuleBasic) Name() string {
16+
return types.ModuleName
17+
}
18+
19+
func (AppModuleBasic) RegisterCodec(cdc *codec.Codec) {
20+
types.RegisterCodec(cdc)
21+
}
22+
23+
func (AppModuleBasic) DefaultGenesis() json.RawMessage {
24+
return types.ModuleCdc.MustMarshalJSON(DefaultGenesisState())
25+
}
26+
27+
// Validation check of the Genesis
28+
func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error {
29+
var data GenesisState
30+
err := types.ModuleCdc.UnmarshalJSON(bz, &data)
31+
if err != nil {
32+
return err
33+
}
34+
// Once json successfully marshalled, passes along to genesis.go
35+
return ValidateGenesis(data)
36+
}
37+
38+
// Register rest routes
39+
func (AppModuleBasic) RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Router) {
40+
//rpc.RegisterRoutes(ctx, rtr, StoreKey)
41+
}
42+
43+
// Get the root query command of this module
44+
func (AppModuleBasic) GetQueryCmd(cdc *codec.Codec) *cobra.Command {
45+
return nil // cli.GetQueryCmd(StoreKey, cdc)
46+
}
47+
48+
// Get the root tx command of this module
49+
func (AppModuleBasic) GetTxCmd(cdc *codec.Codec) *cobra.Command {
50+
return nil // cli.GetTxCmd(StoreKey, cdc)
51+
}

x/evm/types/codec.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ package types
22

33
import "github.com/cosmos/cosmos-sdk/codec"
44

5-
var msgCodec = codec.New()
5+
var ModuleCdc = codec.New()
66

77
func init() {
88
cdc := codec.New()
99

1010
RegisterCodec(cdc)
1111
codec.RegisterCrypto(cdc)
1212

13-
msgCodec = cdc.Seal()
13+
ModuleCdc = cdc.Seal()
1414
}
1515

1616
// RegisterCodec registers concrete types and interfaces on the given codec.

x/evm/types/key.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package types
2+
3+
const (
4+
// module name
5+
ModuleName = "ethermint"
6+
7+
// TODO: Use this
8+
// StoreKey to be used when creating the KVStore
9+
StoreKey = ModuleName
10+
)

x/evm/types/msg_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,12 @@ func TestMsgEthereumTxAmino(t *testing.T) {
124124
addr := GenerateEthAddress()
125125
msg := NewEthereumTxMsg(0, addr, nil, 100000, nil, []byte("test"))
126126

127-
raw, err := msgCodec.MarshalBinaryBare(msg)
127+
raw, err := ModuleCdc.MarshalBinaryBare(msg)
128128
require.NoError(t, err)
129129

130130
var msg2 EthereumTxMsg
131131

132-
err = msgCodec.UnmarshalBinaryBare(raw, &msg2)
132+
err = ModuleCdc.UnmarshalBinaryBare(raw, &msg2)
133133
require.NoError(t, err)
134134
require.Equal(t, msg.Data, msg2.Data)
135135
}

0 commit comments

Comments
 (0)