Skip to content

Commit

Permalink
Merge branch 'on/evm-query-tests' of https://github.com/NibiruChain/n…
Browse files Browse the repository at this point in the history
…ibiru into on/evm-query-tests
  • Loading branch information
Unique-Divine committed May 22, 2024
2 parents 48d32e2 + e38b9a5 commit 9001ade
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 162 deletions.
8 changes: 8 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,14 @@ func NewNibiruApp(
app.SetProcessProposal(handler.ProcessProposalHandler())
})

baseAppOptions = append(baseAppOptions, func(app *baseapp.BaseApp) {
mp := mempool.NoOpMempool{}
app.SetMempool(mp)
handler := baseapp.NewDefaultProposalHandler(mp, app)
app.SetPrepareProposal(handler.PrepareProposalHandler())
app.SetProcessProposal(handler.ProcessProposalHandler())
})

bApp := baseapp.NewBaseApp(
appName, logger, db, encodingConfig.TxConfig.TxDecoder(), baseAppOptions...)
bApp.SetCommitMultiStoreTracer(traceStore)
Expand Down
12 changes: 12 additions & 0 deletions app/appconst/appconst.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ var (
GoArch = ""
)

// EVM Chain ID Map
var EVMChainIDs = map[string]*big.Int{
"cataclysm-1": big.NewInt(100),
"nibiru-localnet-0": big.NewInt(1000),
"nibiru-devnet-1": big.NewInt(2000),
"nibiru-devnet-2": big.NewInt(3000),
"nibiru-testnet-1": big.NewInt(3000),
"nibiru-testnet-2": big.NewInt(4000),
// other test chains will default to 10000
}
var DefaultEVMChainID = big.NewInt(10000)

func init() {
if len(AppVersion) == 0 {
AppVersion = "dev"
Expand Down
20 changes: 2 additions & 18 deletions app/server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import (
"os"
"os/signal"
"path/filepath"
"regexp"
"runtime/pprof"
"strings"
"syscall"
"time"

Expand Down Expand Up @@ -403,7 +401,7 @@ func startInProcess(ctx *server.Context, clientCtx client.Context, opts StartOpt

clientCtx = clientCtx.
WithHomeDir(home).
WithChainID(hackChainID(genDoc.ChainID))
WithChainID(genDoc.ChainID)

// Set `GRPCClient` to `clientCtx` to enjoy concurrent grpc query.
// only use it if gRPC server is enabled.
Expand Down Expand Up @@ -506,7 +504,7 @@ func startInProcess(ctx *server.Context, clientCtx client.Context, opts StartOpt
return err
}

clientCtx := clientCtx.WithChainID(hackChainID(genDoc.ChainID))
clientCtx.WithChainID(genDoc.ChainID)

tmEndpoint := "/websocket"
tmRPCAddr := cfg.RPC.ListenAddress
Expand Down Expand Up @@ -659,17 +657,3 @@ func wrapCPUProfile(ctx *server.Context, callback func() error) error {

return WaitForQuitSignals()
}

// hackChainID replaces nibiru-localnet-0 with nibirulocalnet-9000-1 which matches the standard
func hackChainID(chainID string) string {
re := regexp.MustCompile(`-\d+$`)
lastNumber := re.FindString(chainID)
trimmedInput := strings.TrimSuffix(chainID, lastNumber)
if lastNumber == "-0" {
lastNumber = "-1"
}
trimmedInput = strings.ReplaceAll(trimmedInput, "-", "")
result := trimmedInput + "_9000" + lastNumber

return result
}
51 changes: 6 additions & 45 deletions eth/chain_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,15 @@
package eth

import (
"fmt"
"math/big"
"regexp"
"strings"

"github.com/NibiruChain/nibiru/app/appconst"
)

var (
// one of any lower case letter from "a"-"z"
regexChainID = `[a-z]{1,}`
// one of either "_" or "-"
regexEIP155Separator = `[_-]{1}`
// one of "_"
// regexEIP155Separator = `_{1}`
regexEIP155 = `[1-9][0-9]*`
regexEpochSeparator = `-{1}`
regexEpoch = `[1-9][0-9]*`
nibiruEvmChainId = regexp.MustCompile(fmt.Sprintf(`^(%s)%s(%s)%s(%s)$`,
regexChainID,
regexEIP155Separator,
regexEIP155,
regexEpochSeparator,
regexEpoch))
)

// IsValidChainID returns false if the given chain identifier is incorrectly
// formatted.
func IsValidChainID(chainID string) bool {
if len(chainID) > 48 {
return false
}

return nibiruEvmChainId.MatchString(chainID)
return len(chainID) <= 48
}

// ParseEthChainID parses a string chain identifier's epoch to an
Expand All @@ -52,24 +27,10 @@ func ParseEthChainID(chainID string) (*big.Int, error) {
// Ethereum-compatible chain-id in *big.Int format. The function returns an error
// if the chain-id has an invalid format
func ParseEthChainIDStrict(chainID string) (*big.Int, error) {
chainID = strings.TrimSpace(chainID)
if len(chainID) > 48 {
return nil, ErrInvalidChainID.Wrapf(
`chain-id input "%s" cannot exceed 48 chars`, chainID)
evmChainID, exists := appconst.EVMChainIDs[chainID]
if exists {
return evmChainID, nil
} else {
return appconst.DefaultEVMChainID, nil
}

matches := nibiruEvmChainId.FindStringSubmatch(chainID)
if matches == nil || len(matches) != 4 || matches[1] == "" {
return nil, ErrInvalidChainID.Wrapf(
`chain-id input "%s", matches "%v"`, chainID, matches)
}

// verify that the chain-id entered is a base 10 integer
chainIDInt, ok := new(big.Int).SetString(matches[2], 10)
if !ok {
return nil, ErrInvalidChainID.Wrapf(
`epoch "%s" must be base-10 integer format`, matches[2])
}

return chainIDInt, nil
}
102 changes: 8 additions & 94 deletions eth/chain_id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package eth

import (
"math/big"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -16,24 +15,18 @@ func TestParseChainID_Happy(t *testing.T) {
expInt *big.Int
}{
{
chainID: "nibiru_1-1",
name: "valid chain-id, single digit",
expInt: big.NewInt(1),
chainID: "cataclysm-1",
expInt: big.NewInt(100),
},
{
chainID: "cataclysm_256-1",
name: "valid chain-id, multiple digits",
expInt: big.NewInt(256),
chainID: "nibiru-localnet-0",
name: "valid nibiru-localnet-0",
expInt: big.NewInt(1000),
},
{
chainID: "cataclysm-4-20",
name: "valid chain-id, dashed, multiple digits",
expInt: big.NewInt(4),
},
{
chainID: "chain-1-1",
name: "valid chain-id, double dash",
expInt: big.NewInt(1),
chainID: "otherchain",
name: "other chain, default id",
expInt: big.NewInt(10000),
},
}

Expand All @@ -49,82 +42,3 @@ func TestParseChainID_Happy(t *testing.T) {
require.True(t, IsValidChainID(tc.chainID))
}
}

func TestParseChainID_Sad(t *testing.T) {
testCases := []struct {
name string
chainID string
}{
{
chainID: "chain_1_1",
name: "invalid chain-id, double underscore",
},
{
chainID: "-",
name: "invalid chain-id, dash only",
},
{
chainID: "-1",
name: "invalid chain-id, undefined identifier and EIP155",
},
{
chainID: "_1-1",
name: "invalid chain-id, undefined identifier",
},
{
chainID: "NIBIRU_1-1",
name: "invalid chain-id, uppercases",
},
{
chainID: "Nibiru_1-1",
name: "invalid chain-id, mixed cases",
},
{
chainID: "$&*#!_1-1",
name: "invalid chain-id, special chars",
},
{
chainID: "nibiru_001-1",
name: "invalid eip155 chain-id, cannot start with 0",
},
{
chainID: "nibiru_0x212-1",
name: "invalid eip155 chain-id, cannot invalid base",
},
{
chainID: "nibiru_1-0x212",
name: "invalid eip155 chain-id, cannot invalid base",
},
{
chainID: "nibiru_nibiru_9000-1",
name: "invalid eip155 chain-id, non-integer",
},
{
chainID: "nibiru_-",
name: "invalid epoch, undefined",
},
{
chainID: " ",
name: "blank chain ID",
},
{
chainID: "",
name: "empty chain ID",
},
{
chainID: "_-",
name: "empty content for chain id, eip155 and epoch numbers",
},
{
chainID: "nibiru_" + strings.Repeat("1", 45) + "-1",
name: "long chain-id",
},
}

for _, tc := range testCases {
chainIDEpoch, err := ParseEthChainIDStrict(tc.chainID)
require.Error(t, err, tc.name)
require.Nil(t, chainIDEpoch)
require.False(t, IsValidChainID(tc.chainID), tc.name)
}
}
30 changes: 30 additions & 0 deletions x/evm/keeper/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package keeper

import (
"math/big"

"github.com/NibiruChain/nibiru/x/evm/statedb"

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// EVMConfig creates the EVMConfig based on current state
func (k *Keeper) EVMConfig(ctx sdk.Context, proposerAddress sdk.ConsAddress, chainID *big.Int) (*statedb.EVMConfig, error) {
params := k.GetParams(ctx)
ethCfg := params.ChainConfig.EthereumConfig(chainID)

// get the coinbase address from the block proposer
coinbase, err := k.GetCoinbaseAddress(ctx, proposerAddress)
if err != nil {
return nil, errorsmod.Wrap(err, "failed to obtain coinbase address")
}

baseFee := k.GetBaseFee(ctx, ethCfg)
return &statedb.EVMConfig{
Params: params,
ChainConfig: ethCfg,
CoinBase: coinbase,
BaseFee: baseFee,
}, nil
}
13 changes: 8 additions & 5 deletions x/evm/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@ import (
"errors"
"fmt"
"math/big"

"time"

"github.com/ethereum/go-ethereum/common/hexutil"

"github.com/NibiruChain/nibiru/eth"
"github.com/NibiruChain/nibiru/x/evm/statedb"

grpccodes "google.golang.org/grpc/codes"
grpcstatus "google.golang.org/grpc/status"

Expand All @@ -17,19 +23,16 @@ import (
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/NibiruChain/nibiru/eth"
"github.com/NibiruChain/nibiru/x/evm"
"github.com/NibiruChain/nibiru/x/evm/statedb"

gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core"
gethcore "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/eth/tracers"
"github.com/ethereum/go-ethereum/eth/tracers/logger"
gethparams "github.com/ethereum/go-ethereum/params"

"github.com/NibiruChain/nibiru/x/evm"

cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
)

Expand Down

0 comments on commit 9001ade

Please sign in to comment.