Skip to content

Commit

Permalink
test(evm): QueryEthAccount tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Unique-Divine committed May 15, 2024
1 parent 7d96d1c commit f5089d8
Show file tree
Hide file tree
Showing 60 changed files with 957 additions and 2,317 deletions.
2 changes: 0 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [#1855](https://github.com/NibiruChain/nibiru/pull/1855) - feat(eth-pubsub): Implement in-memory EventBus for real-time topic management and event distribution
- [#1856](https://github.com/NibiruChain/nibiru/pull/1856) - feat(eth-rpc): Conversion types and functions between Ethereum txs and blocks and Tendermint ones.
- [#1861](https://github.com/NibiruChain/nibiru/pull/1861) - feat(eth-rpc): RPC backend, Ethereum tracer, KV indexer, and RPC APIs
<<<<<<< HEAD
- [#1869](https://github.com/NibiruChain/nibiru/pull/1869) - feat(eth): Module and start of keeper tests
- [#1873](https://github.com/NibiruChain/nibiru/pull/1873) - feat(evm): keeper grpc query impls and collection start
- [#1869](https://github.com/NibiruChain/nibiru/pull/1869) - feat(eth): Module and start of keeper tests

#### Dapp modules: perp, spot, oracle, etc

Expand Down
47 changes: 27 additions & 20 deletions app/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,8 @@ type AnteHandlerOptions struct {
// numbers, checks signatures and account numbers, and deducts fees from the
// first signer.
func NewAnteHandler(options AnteHandlerOptions) (sdk.AnteHandler, error) {
if options.AccountKeeper == nil {
return nil, AnteHandlerError("account keeper")
}
if options.BankKeeper == nil {
return nil, AnteHandlerError("bank keeper")
}
if options.SignModeHandler == nil {
return nil, AnteHandlerError("sign mode handler")
}
if options.SigGasConsumer == nil {
options.SigGasConsumer = sdkante.DefaultSigVerificationGasConsumer
}
if options.WasmConfig == nil {
return nil, AnteHandlerError("wasm config")
}
if options.DevGasKeeper == nil {
return nil, AnteHandlerError("devgas keeper")
}
if options.IBCKeeper == nil {
return nil, AnteHandlerError("ibc keeper")
if err := options.ValidateAndClean(); err != nil {
return nil, err
}

anteDecorators := []sdk.AnteDecorator{
Expand Down Expand Up @@ -80,6 +62,31 @@ func NewAnteHandler(options AnteHandlerOptions) (sdk.AnteHandler, error) {
return sdk.ChainAnteDecorators(anteDecorators...), nil
}

func (opts *AnteHandlerOptions) ValidateAndClean() error {
if opts.AccountKeeper == nil {
return AnteHandlerError("account keeper")
}
if opts.BankKeeper == nil {
return AnteHandlerError("bank keeper")
}
if opts.SignModeHandler == nil {
return AnteHandlerError("sign mode handler")
}
if opts.SigGasConsumer == nil {
opts.SigGasConsumer = sdkante.DefaultSigVerificationGasConsumer
}
if opts.WasmConfig == nil {
return AnteHandlerError("wasm config")
}
if opts.DevGasKeeper == nil {
return AnteHandlerError("devgas keeper")
}
if opts.IBCKeeper == nil {
return AnteHandlerError("ibc keeper")
}
return nil
}

func AnteHandlerError(shortDesc string) error {
return sdkerrors.Wrapf(errors.ErrLogic, "%s is required for AnteHandler", shortDesc)
}
2 changes: 1 addition & 1 deletion app/ante/testutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (suite *AnteTestSuite) SetupTest() {
// Set up base app and ctx
testapp.EnsureNibiruPrefix()
encodingConfig := app.MakeEncodingConfig()
suite.app = testapp.NewNibiruTestApp(app.NewDefaultGenesisState(encodingConfig.Marshaler))
suite.app = testapp.NewNibiruTestApp(app.NewDefaultGenesisState(encodingConfig.Codec))
chainId := "test-chain-id"
ctx := suite.app.NewContext(true, tmproto.Header{
Height: 1,
Expand Down
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func NewNibiruApp(
appOpts servertypes.AppOptions,
baseAppOptions ...func(*baseapp.BaseApp),
) *NibiruApp {
appCodec := encodingConfig.Marshaler
appCodec := encodingConfig.Codec
legacyAmino := encodingConfig.Amino
interfaceRegistry := encodingConfig.InterfaceRegistry
txConfig := encodingConfig.TxConfig
Expand Down
4 changes: 2 additions & 2 deletions app/codec/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
// This is provided for compatibility between protobuf and amino implementations.
type EncodingConfig struct {
InterfaceRegistry types.InterfaceRegistry
Marshaler codec.Codec
Codec codec.Codec
TxConfig client.TxConfig
Amino *codec.LegacyAmino
}
Expand All @@ -25,7 +25,7 @@ func MakeEncodingConfig() EncodingConfig {

return EncodingConfig{
InterfaceRegistry: interfaceRegistry,
Marshaler: marshaler,
Codec: marshaler,
TxConfig: txCfg,
Amino: amino,
}
Expand Down
2 changes: 1 addition & 1 deletion app/ibc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func SetupNibiruTestingApp() (

// Create genesis state
encCdc := app.MakeEncodingConfig()
genesisState := app.NewDefaultGenesisState(encCdc.Marshaler)
genesisState := app.NewDefaultGenesisState(encCdc.Codec)
testapp.SetDefaultSudoGenesis(genesisState)

return nibiruApp, genesisState
Expand Down
8 changes: 6 additions & 2 deletions app/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ import (
// ---------------------------------------------------------------
// Nibiru Custom Modules

"github.com/NibiruChain/nibiru/eth"
"github.com/NibiruChain/nibiru/x/common"
"github.com/NibiruChain/nibiru/x/devgas/v1"
devgaskeeper "github.com/NibiruChain/nibiru/x/devgas/v1/keeper"
Expand Down Expand Up @@ -280,11 +281,12 @@ func (app *NibiruApp) InitKeepers(
// seal capability keeper after scoping modules
// app.capabilityKeeper.Seal()

// add keepers
// TODO: chore(upgrade): Potential breaking change on AccountKeeper dur
// to ProtoBaseAccount replacement.

Check failure on line 285 in app/keepers.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed with -local github.com/NibiruChain/nibiru (goimports)
app.AccountKeeper = authkeeper.NewAccountKeeper(
appCodec,
keys[authtypes.StoreKey],
authtypes.ProtoBaseAccount,
eth.ProtoBaseAccount,
maccPerms,
sdk.GetConfig().GetBech32AccountAddrPrefix(),
govModuleAddr,
Expand Down Expand Up @@ -396,6 +398,8 @@ func (app *NibiruApp) InitKeepers(
keys[evm.StoreKey],
tkeys[evm.TransientKey],
authtypes.NewModuleAddress(govtypes.ModuleName),
app.AccountKeeper,
app.BankKeeper,
)

// ---------------------------------- IBC keepers
Expand Down
10 changes: 5 additions & 5 deletions app/modules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ func (s *TestSuite) SetupSuite() {
}

func (s *TestSuite) DefaultGenesisCopy() app.GenesisState {
return app.NewDefaultGenesisState(s.encCfg.Marshaler)
return app.NewDefaultGenesisState(s.encCfg.Codec)
}

func (s *TestSuite) TestGenesis() {
getDefaultStakingGenesis := func() *stakingtypes.GenesisState {
genStaking := new(stakingtypes.GenesisState)
s.encCfg.Marshaler.MustUnmarshalJSON(
app.StakingModule{}.DefaultGenesis(s.encCfg.Marshaler),
s.encCfg.Codec.MustUnmarshalJSON(
app.StakingModule{}.DefaultGenesis(s.encCfg.Codec),
genStaking,
)
return genStaking
Expand Down Expand Up @@ -61,9 +61,9 @@ func (s *TestSuite) TestGenesis() {
},
} {
s.T().Run(tc.name, func(t *testing.T) {
genStakingJson := s.encCfg.Marshaler.MustMarshalJSON(tc.gen)
genStakingJson := s.encCfg.Codec.MustMarshalJSON(tc.gen)
err := app.StakingModule{}.ValidateGenesis(
s.encCfg.Marshaler,
s.encCfg.Codec,
s.encCfg.TxConfig,
genStakingJson,
)
Expand Down
2 changes: 1 addition & 1 deletion cmd/nibid/cmd/decode_base64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestBase64Decode(t *testing.T) {
cfg, err := genutiltest.CreateDefaultTendermintConfig(home)
require.NoError(t, err)

appCodec := app.MakeEncodingConfig().Marshaler
appCodec := app.MakeEncodingConfig().Codec
err = genutiltest.ExecInitCmd(
testModuleBasicManager, home, appCodec)
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/nibid/cmd/genaccounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestAddGenesisAccountCmd(t *testing.T) {
cfg, err := genutiltest.CreateDefaultTendermintConfig(home)
require.NoError(t, err)

appCodec := app.MakeEncodingConfig().Marshaler
appCodec := app.MakeEncodingConfig().Codec
err = genutiltest.ExecInitCmd(
testModuleBasicManager, home, appCodec)
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/nibid/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func NewRootCmd() (*cobra.Command, app.EncodingConfig) {
app.SetPrefixes(appconst.AccountAddressPrefix)

initClientCtx := client.Context{}.
WithCodec(encodingConfig.Marshaler).
WithCodec(encodingConfig.Codec).
WithInterfaceRegistry(encodingConfig.InterfaceRegistry).
WithTxConfig(encodingConfig.TxConfig).
WithLegacyAmino(encodingConfig.Amino).
Expand Down
6 changes: 3 additions & 3 deletions cmd/nibid/cmd/testnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ func Test_TestnetCmd(t *testing.T) {
cfg, err := genutiltest.CreateDefaultTendermintConfig(home)
require.NoError(t, err)

err = genutiltest.ExecInitCmd(app.ModuleBasics, home, encodingConfig.Marshaler)
err = genutiltest.ExecInitCmd(app.ModuleBasics, home, encodingConfig.Codec)
require.NoError(t, err)

serverCtx := server.NewContext(viper.New(), cfg, logger)
clientCtx := client.Context{}.
WithCodec(encodingConfig.Marshaler).
WithCodec(encodingConfig.Codec).
WithHomeDir(home).
WithTxConfig(encodingConfig.TxConfig)

Expand All @@ -49,6 +49,6 @@ func Test_TestnetCmd(t *testing.T) {
appState, _, err := genutiltypes.GenesisStateFromGenFile(genFile)
require.NoError(t, err)

bankGenState := banktypes.GetGenesisStateFromAppState(encodingConfig.Marshaler, appState)
bankGenState := banktypes.GetGenesisStateFromAppState(encodingConfig.Codec, appState)
require.NotEmpty(t, bankGenState.Supply.String())
}
6 changes: 6 additions & 0 deletions eth/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ const (
ProtocolVersion = 65
)

// Transaction extension protobuf type URLs
const (
TYPE_URL_WEB3_TX = "/eth.types.v1.ExtensionOptionsWeb3Tx"
TYPE_URL_DYNAMIC_FEE_TX = "/eth.types.v1.ExtensionOptionDynamicFeeTx"
)

// RegisterInterfaces registers the tendermint concrete client-related
// implementations and interfaces.
func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
Expand Down
4 changes: 2 additions & 2 deletions eth/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ func (suite *CodecTestSuite) TestRegisterInterfaces() {
ProtoName: "cosmos.tx.v1beta1.TxExtensionOptionI",
Interface: new(sdktx.TxExtensionOptionI),
WantImpls: []string{
"/eth.types.v1.ExtensionOptionsWeb3Tx",
"/eth.types.v1.ExtensionOptionDynamicFeeTx",
TYPE_URL_WEB3_TX,
TYPE_URL_DYNAMIC_FEE_TX,
},
},
}
Expand Down
2 changes: 1 addition & 1 deletion eth/encoding/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ func MakeConfig(mb module.BasicManager) params.EncodingConfig {
}

enccodec.RegisterLegacyAminoCodec(encodingConfig.Amino)
mb.RegisterLegacyAminoCodec(encodingConfig.Amino)
enccodec.RegisterInterfaces(encodingConfig.InterfaceRegistry)
mb.RegisterLegacyAminoCodec(encodingConfig.Amino)
mb.RegisterInterfaces(encodingConfig.InterfaceRegistry)
return encodingConfig
}
84 changes: 84 additions & 0 deletions eth/eth_account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright (c) 2023-2024 Nibi, Inc.
package eth

import (
"bytes"

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
)

var (
_ authtypes.AccountI = (*EthAccount)(nil)
_ EthAccountI = (*EthAccount)(nil)
_ authtypes.GenesisAccount = (*EthAccount)(nil)
_ codectypes.UnpackInterfacesMessage = (*EthAccount)(nil)
)

// EthAccType: Enum for Ethereum account types.
type EthAccType = int8

const (
// EthAccType_EOA: For externally owned accounts (EOAs)
EthAccType_EOA EthAccType = iota + 1
// EthAccType_Contract: For smart contracts accounts.
EthAccType_Contract
)

// EthAccountI represents the interface of an EVM compatible account
type EthAccountI interface { //revive:disable-line:exported
authtypes.AccountI
// EthAddress returns the ethereum Address representation of the AccAddress
EthAddress() common.Address
// CodeHash is the keccak256 hash of the contract code (if any)
GetCodeHash() common.Hash
// SetCodeHash sets the code hash to the account fields
SetCodeHash(code common.Hash) error
// Type returns the type of Ethereum Account (EOA or Contract)
Type() EthAccType
}

func (acc EthAccount) GetBaseAccount() *authtypes.BaseAccount {
return acc.BaseAccount
}

// EthAddress returns the account address ethereum format.
func (acc EthAccount) EthAddress() common.Address {
return common.BytesToAddress(acc.GetAddress().Bytes())
}

func (acc EthAccount) GetCodeHash() common.Hash {
return common.HexToHash(acc.CodeHash)
}

func (acc *EthAccount) SetCodeHash(codeHash common.Hash) error {
acc.CodeHash = codeHash.Hex()
return nil
}

// Type returns the type of Ethereum Account (EOA or Contract)
func (acc EthAccount) Type() EthAccType {
if bytes.Equal(
emptyCodeHash, common.HexToHash(acc.CodeHash).Bytes(),
) {
return EthAccType_EOA
}
return EthAccType_Contract
}

var emptyCodeHash = crypto.Keccak256(nil)

// ProtoBaseAccount: Implementation of `BaseAccount` for the `AccountI` interface
// used in the AccountKeeper from the Auth Module. [ProtoBaseAccount] is a
// drop-in replacement for the `auth.ProtoBaseAccount` from
// "cosmos-sdk/auth/types" extended to fit the the `EthAccountI` interface for
// Ethereum accounts.
func ProtoBaseAccount() authtypes.AccountI {
return &EthAccount{
BaseAccount: &authtypes.BaseAccount{},
CodeHash: common.BytesToHash(emptyCodeHash).String(),
}
}
3 changes: 2 additions & 1 deletion eth/indexer/kv_indexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ func TestKVIndexer(t *testing.T) {
require.NoError(t, tx.Sign(ethSigner, signer))
txHash := tx.AsTransaction().Hash()

encCfg := MakeEncodingConfig()
// encCfg := MakeEncodingConfig()
encCfg := app.MakeEncodingConfig()
eth.RegisterInterfaces(encCfg.InterfaceRegistry)
evm.RegisterInterfaces(encCfg.InterfaceRegistry)
clientCtx := client.Context{}.
Expand Down
4 changes: 2 additions & 2 deletions eth/rpc/backend/account_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ func (b *Backend) GetProof(
}

// query EVM account
req := &evm.QueryAccountRequest{
req := &evm.QueryEthAccountRequest{
Address: address.String(),
}

res, err := b.queryClient.Account(ctx, req)
res, err := b.queryClient.EthAccount(ctx, req)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions eth/rpc/backend/evm_query_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,8 @@ func RegisterStorageAtError(
func RegisterAccount(
queryClient *mocks.EVMQueryClient, addr common.Address, height int64,
) {
queryClient.On("Account", rpc.NewContextWithHeight(height), &evm.QueryAccountRequest{Address: addr.String()}).
Return(&evm.QueryAccountResponse{
queryClient.On("EthAccount", rpc.NewContextWithHeight(height), &evm.QueryEthAccountRequest{Address: addr.String()}).
Return(&evm.QueryEthAccountResponse{
Balance: "0",
CodeHash: "",
Nonce: 0,
Expand Down
Loading

0 comments on commit f5089d8

Please sign in to comment.