Skip to content

Commit

Permalink
feat(eth-rpc): Conversion types and functions between Ethereum txs an…
Browse files Browse the repository at this point in the history
…d blocks and Tendermint ones. (#1856)

* feat(eth-pubsub): Implement in-memory EventBus for real-time topic management and event distribution

* feat(eth-rpc): Conversion types and functions between Ethereum txs and blocks and Tendermint ones.

* chore: linter

* test(eth-rpc): more tests for types dir

* ci: add CODECOV_TOKEN env var to secrets

* test,refactor(eth): remove unnecessary nesting + more tests

* refactor(eth): rpc/types -> rpc

* refactor(eth): evm/types -> evm

* refactor(eth): ethtypes -> eth

* test(eth): eip712 more tests

* test(eth): more tests

* test(eth-rpc): more tests
  • Loading branch information
Unique-Divine authored May 5, 2024
1 parent ffcea96 commit 2bd7a14
Show file tree
Hide file tree
Showing 90 changed files with 4,248 additions and 852 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [#1838](https://github.com/NibiruChain/nibiru/pull/1838) - feat(eth): Go-ethereum, crypto, encoding, and unit tests for evm/types
- [#1841](https://github.com/NibiruChain/nibiru/pull/1841) - feat(eth): Collections encoders for bytes, Ethereum addresses, and Ethereum hashes
- [#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.

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

Expand Down
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
14 changes: 7 additions & 7 deletions eth/types/account.pb.go → eth/account.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion eth/types/assert.go → eth/assert.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2023-2024 Nibi, Inc.
package types
package eth

import (
"bytes"
Expand Down
145 changes: 145 additions & 0 deletions eth/assert_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package eth_test

import (
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"

"github.com/NibiruChain/nibiru/eth"
"github.com/NibiruChain/nibiru/x/common/testutil"
)

func TestIsEmptyHash(t *testing.T) {
testCases := []struct {
name string
hash string
expEmpty bool
}{
{
"empty string", "", true,
},
{
"zero hash", common.Hash{}.String(), true,
},

{
"non-empty hash", common.BytesToHash([]byte{1, 2, 3, 4}).String(), false,
},
}

for _, tc := range testCases {
require.Equal(t, tc.expEmpty, eth.IsEmptyHash(tc.hash), tc.name)
}
}

func TestIsZeroAddress(t *testing.T) {
testCases := []struct {
name string
address string
expEmpty bool
}{
{
"empty string", "", true,
},
{
"zero address", common.Address{}.String(), true,
},

{
"non-empty address", common.BytesToAddress([]byte{1, 2, 3, 4}).String(), false,
},
}

for _, tc := range testCases {
require.Equal(t, tc.expEmpty, eth.IsZeroAddress(tc.address), tc.name)
}
}

func TestValidateAddress(t *testing.T) {
testCases := []struct {
name string
address string
expError bool
}{
{
"empty string", "", true,
},
{
"invalid address", "0x", true,
},
{
"zero address", common.Address{}.String(), false,
},
{
"valid address", testutil.NewEthAddr().Hex(), false,
},
}

for _, tc := range testCases {
err := eth.ValidateAddress(tc.address)

if tc.expError {
require.Error(t, err, tc.name)
} else {
require.NoError(t, err, tc.name)
}
}
}

func TestValidateNonZeroAddress(t *testing.T) {
testCases := []struct {
name string
address string
expError bool
}{
{
"empty string", "", true,
},
{
"invalid address", "0x", true,
},
{
"zero address", common.Address{}.String(), true,
},
{
"valid address", testutil.NewEthAddr().Hex(), false,
},
}

for _, tc := range testCases {
err := eth.ValidateNonZeroAddress(tc.address)

if tc.expError {
require.Error(t, err, tc.name)
} else {
require.NoError(t, err, tc.name)
}
}
}

func TestSafeInt64(t *testing.T) {
testCases := []struct {
name string
value uint64
expError bool
}{
{
"no overflow", 10, false,
},
{
"overflow", 18446744073709551615, true,
},
}

for _, tc := range testCases {
value, err := eth.SafeInt64(tc.value)
if tc.expError {
require.Error(t, err, tc.name)
continue
}

require.NoError(t, err, tc.name)
require.Equal(t, int64(tc.value), value, tc.name)
}
}
2 changes: 1 addition & 1 deletion eth/types/chain_id.go → eth/chain_id.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2023-2024 Nibi, Inc.
package types
package eth

import (
"fmt"
Expand Down
5 changes: 4 additions & 1 deletion eth/types/chain_id_test.go → eth/chain_id_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package types
package eth

import (
"math/big"
Expand Down Expand Up @@ -51,6 +51,9 @@ func TestParseChainID(t *testing.T) {
{
"invalid eip155 chain-id, cannot invalid base", "nibiru_0x212-1", true, nil,
},
{
"invalid eip155 chain-id, cannot invalid base", "nibiru_1-0x212", true, nil,
},
{
"invalid eip155 chain-id, non-integer", "nibiru_nibiru_9000-1", true, nil,
},
Expand Down
19 changes: 16 additions & 3 deletions eth/types/codec.go → eth/codec.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
// Copyright (c) 2023-2024 Nibi, Inc.
package types
package eth

import (
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/types/tx"
sdktx "github.com/cosmos/cosmos-sdk/types/tx"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
)

// RegisterInterfaces registers the tendermint concrete client-related
// implementations and interfaces.
func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
// proto name: "cosmos.auth.v1beta1.AccountI"
registry.RegisterImplementations(
(*authtypes.AccountI)(nil),
&EthAccount{},
// Also impl by: [
// &authtypes.BaseAccount{},
// &authtypes.ModuleAccount{},
// ]
)

// proto name: "cosmos.auth.v1beta1.GenesisAccount"
registry.RegisterImplementations(
(*authtypes.GenesisAccount)(nil),
&EthAccount{},
// Also impl by: [
// &authtypes.BaseAccount{},
// &authtypes.ModuleAccount{},
// ]
)

// proto name: "cosmos.tx.v1beta1.TxExtensionOptionI"
registry.RegisterImplementations(
(*tx.TxExtensionOptionI)(nil),
(*sdktx.TxExtensionOptionI)(nil),
&ExtensionOptionsWeb3Tx{},
&ExtensionOptionDynamicFeeTx{},
)
Expand Down
Loading

0 comments on commit 2bd7a14

Please sign in to comment.