Skip to content

Commit

Permalink
test(eth-rpc): more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Unique-Divine committed May 5, 2024
1 parent 14f8a2c commit db03210
Show file tree
Hide file tree
Showing 12 changed files with 227 additions and 46 deletions.
13 changes: 7 additions & 6 deletions cmd/nibid/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/client/pruning"
"github.com/cosmos/cosmos-sdk/client/rpc"
"github.com/cosmos/cosmos-sdk/server"
sdkserver "github.com/cosmos/cosmos-sdk/server"
serverconfig "github.com/cosmos/cosmos-sdk/server/config"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
Expand Down Expand Up @@ -48,8 +48,9 @@ func NewRootCmd() (*cobra.Command, app.EncodingConfig) {
WithViper("") // In simapp, we don't use any prefix for env variables.

rootCmd := &cobra.Command{
Use: "nibid",
Short: "Nibiru app",
Use: "nibid",
Short: "Nibiru blockchain node CLI",
Aliases: []string{"nibiru"},
PersistentPreRunE: func(cmd *cobra.Command, _ []string) error {
cmd.SetOut(cmd.OutOrStdout())
cmd.SetErr(cmd.ErrOrStderr())
Expand All @@ -74,7 +75,7 @@ func NewRootCmd() (*cobra.Command, app.EncodingConfig) {
customAppTemplate, customAppConfig := initAppConfig()
tmCfg := customTendermintConfig()

return server.InterceptConfigsPreRunHandler(
return sdkserver.InterceptConfigsPreRunHandler(
cmd,
customAppTemplate,
customAppConfig,
Expand Down Expand Up @@ -147,7 +148,7 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig app.EncodingConfig) {
pruning.PruningCmd(a.newApp),
)

server.AddCommands(rootCmd, app.DefaultNodeHome, a.newApp, a.appExport, addModuleInitFlags)
sdkserver.AddCommands(rootCmd, app.DefaultNodeHome, a.newApp, a.appExport, addModuleInitFlags)

// add keybase, auxiliary RPC, query, and tx child commands
rootCmd.AddCommand(
Expand Down Expand Up @@ -241,7 +242,7 @@ type appCreator struct {

// newApp is an appCreator
func (a appCreator) newApp(logger log.Logger, db dbm.DB, traceStore io.Writer, appOpts servertypes.AppOptions) servertypes.Application {
baseappOptions := server.DefaultBaseappOptions(appOpts)
baseappOptions := sdkserver.DefaultBaseappOptions(appOpts)

return app.NewNibiruApp(
logger, db, traceStore, true,
Expand Down
4 changes: 3 additions & 1 deletion cmd/nibid/cmd/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ var (
)

// get cmd to initialize all files for tendermint testnet and application
func testnetCmd(mbm module.BasicManager, genBalIterator banktypes.GenesisBalancesIterator) *cobra.Command {
func testnetCmd(
mbm module.BasicManager, genBalIterator banktypes.GenesisBalancesIterator,
) *cobra.Command {
cmd := &cobra.Command{
Use: "testnet",
Short: "Initialize files for a simapp testnet",
Expand Down
5 changes: 2 additions & 3 deletions eth/rpc/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ const ErrExceedBlockGasLimit = "out of gas in location: block gas meter; gasWant

// ErrStateDBCommit defines the error message when commit after executing EVM
// transaction, for example transfer native token to a distribution module
// account 0x93354845030274cD4bf1686Abd60AB28EC52e1a7 using an evm transaction.
// Note, the transfer amount cannot be set to 0, otherwise this problem will not
// be triggered.
// account using an evm transaction. Note, the transfer amount cannot be set to
// 0, otherwise this problem will not be triggered.
const ErrStateDBCommit = "failed to commit stateDB"

// RawTxToEthTx returns a evm MsgEthereum transaction from raw tx bytes.
Expand Down
73 changes: 73 additions & 0 deletions eth/rpc/rpc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright (c) 2023-2024 Nibi, Inc.
package rpc_test

import (
"testing"

sdkmath "cosmossdk.io/math"
cmt "github.com/cometbft/cometbft/types"
"github.com/cosmos/cosmos-sdk/client"
gethcore "github.com/ethereum/go-ethereum/core/types"
"github.com/stretchr/testify/suite"

"github.com/NibiruChain/nibiru/eth/rpc"
"github.com/NibiruChain/nibiru/x/evm/evmtest"
)

type SuiteRPC struct {
suite.Suite
}

func TestSuiteRPC(t *testing.T) {
suite.Run(t, new(SuiteRPC))
}

func (s *SuiteRPC) TestRawTxToEthTx() {
type TestCase struct {
tx cmt.Tx
clientCtx client.Context
wantErr string
}
type TestCaseSetupFn = func() TestCase

for _, tcSetup := range []TestCaseSetupFn{
func() TestCase {
_, _, clientCtx := evmtest.NewEthTxMsgAsCmt(s.T())
txBz := []byte("tx")
return TestCase{
tx: txBz, // invalid bytes
clientCtx: clientCtx, // valid clientCtx
wantErr: "failed to unmarshal JSON",
}
},
func() TestCase {
txBz, _, clientCtx := evmtest.NewEthTxMsgAsCmt(s.T())
return TestCase{
tx: txBz, // valid bytes
clientCtx: clientCtx, // valid clientCtx
wantErr: "", // happy
}
},
} {
tc := tcSetup()
ethTxs, err := rpc.RawTxToEthTx(tc.clientCtx, tc.tx)
if tc.wantErr != "" {
s.Require().ErrorContains(err, tc.wantErr, "ethTxs: %s", ethTxs)
continue
}
s.Require().NoError(err, "ethTxs: %s", ethTxs)
}
}

func (s *SuiteRPC) TestEthHeaderFromTendermint() {
for _, block := range []*cmt.Block{
// Some happy path test cases for good measure
cmt.MakeBlock(1, []cmt.Tx{}, nil, nil),
cmt.MakeBlock(420, []cmt.Tx{}, nil, nil),
} {
ethHeader := rpc.EthHeaderFromTendermint(
block.Header, gethcore.Bloom{}, sdkmath.NewInt(1).BigInt())
s.NotNil(ethHeader)
s.Equal(block.Header.Height, ethHeader.Number.Int64())
}
}
2 changes: 1 addition & 1 deletion eth/state_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func BytesToHex(bz []byte) string {
type EthAddr = ethcommon.Address

// EthHash: (alias) 32 byte Keccak256 hash of arbitrary data.
type EthHash = ethcommon.Hash
type EthHash = ethcommon.Hash //revive:disable-line:exported

var (
// Implements a `collections.ValueEncoder` for the `[]byte` type
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ require (
github.com/gorilla/mux v1.8.1
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1
github.com/holiman/uint256 v1.2.4
github.com/holiman/uint256 v1.2.4 // indirect
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.18.0
github.com/rakyll/statik v0.1.7
Expand Down
Loading

0 comments on commit db03210

Please sign in to comment.