Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for EVM jsonrpc #32

Merged
merged 5 commits into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions app/ante/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,24 @@ import (
"github.com/cosmos/cosmos-sdk/types/tx/signing"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
)

// HandlerOptions are the options required for constructing a default SDK AnteHandler.
type HandlerOptions struct {
AccountKeeper ante.AccountKeeper
BankKeeper authtypes.BankKeeper
ExtensionOptionChecker ante.ExtensionOptionChecker
FeegrantKeeper ante.FeegrantKeeper
SignModeHandler authsigning.SignModeHandler
TxFeeChecker ante.TxFeeChecker
GashubKeeper ante.GashubKeeper
}

// NewAnteHandler returns an AnteHandler that checks and increments sequence
// numbers, checks signatures & account numbers, and deducts fees from the first
// signer.
func NewAnteHandler(options ante.HandlerOptions) (sdk.AnteHandler, error) {
func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
if options.AccountKeeper == nil {
return nil, errors.Wrap(sdkerrors.ErrLogic, "account keeper is required for ante builder")
}
Expand All @@ -33,11 +45,11 @@ func NewAnteHandler(options ante.HandlerOptions) (sdk.AnteHandler, error) {
ante.NewValidateBasicDecorator(),
ante.NewTxTimeoutHeightDecorator(),
ante.NewValidateMemoDecorator(options.AccountKeeper),
ante.NewConsumeGasForTxSizeDecorator(options.AccountKeeper),
ante.NewValidateTxSizeDecorator(options.AccountKeeper, options.GashubKeeper),
ante.NewConsumeMsgGasDecorator(options.AccountKeeper, options.GashubKeeper),
ante.NewDeductFeeDecorator(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper, options.TxFeeChecker),
ante.NewSetPubKeyDecorator(options.AccountKeeper), // SetPubKeyDecorator must be called before all signature verification decorators
ante.NewValidateSigCountDecorator(options.AccountKeeper),
ante.NewSigGasConsumeDecorator(options.AccountKeeper, options.SigGasConsumer),
ante.NewSigVerificationDecorator(options.AccountKeeper, options.SignModeHandler),
ante.NewIncrementSequenceDecorator(options.AccountKeeper),
}
Expand Down
51 changes: 25 additions & 26 deletions app/ante/ante_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ package ante_test

import (
"encoding/hex"
"time"

"fmt"
"time"

sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -69,30 +68,6 @@ func (suite *AnteTestSuite) TestAnteHandler() {
return txBuilder.GetTx()
}, false, false, true,
},
{
"success- DeliverTx EIP712 create validator",
func() sdk.Tx {
from := acc.GetAddress()
coinAmount := sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(20))
amount := sdk.NewCoins(coinAmount)
gas := uint64(200000)
txBuilder := suite.CreateTestEIP712MsgCreateValidator(from, privKey, "greenfield_9000-1", gas, amount)
return txBuilder.GetTx()
}, false, false, true,
},
{
"success- DeliverTx EIP712 MsgSubmitProposal",
func() sdk.Tx {
from := acc.GetAddress()
coinAmount := sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(20))
gasAmount := sdk.NewCoins(coinAmount)
gas := uint64(200000)
// reusing the gasAmount for deposit
deposit := sdk.NewCoins(coinAmount)
txBuilder := suite.CreateTestEIP712SubmitProposal(from, privKey, "greenfield_9000-1", gas, gasAmount, deposit)
return txBuilder.GetTx()
}, false, false, true,
},
{
"success- DeliverTx EIP712 MsgGrant",
func() sdk.Tx {
Expand Down Expand Up @@ -165,6 +140,30 @@ func (suite *AnteTestSuite) TestAnteHandler() {
return txBuilder.GetTx()
}, false, false, true,
},
{
"fails- DeliverTx EIP712 create validator",
func() sdk.Tx {
from := acc.GetAddress()
coinAmount := sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(20))
amount := sdk.NewCoins(coinAmount)
gas := uint64(200000)
txBuilder := suite.CreateTestEIP712MsgCreateValidator(from, privKey, "greenfield_9000-1", gas, amount)
return txBuilder.GetTx()
}, false, false, false,
},
{
"fails- DeliverTx EIP712 MsgSubmitProposal",
func() sdk.Tx {
from := acc.GetAddress()
coinAmount := sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(20))
gasAmount := sdk.NewCoins(coinAmount)
gas := uint64(200000)
// reusing the gasAmount for deposit
deposit := sdk.NewCoins(coinAmount)
txBuilder := suite.CreateTestEIP712SubmitProposal(from, privKey, "greenfield_9000-1", gas, gasAmount, deposit)
return txBuilder.GetTx()
}, false, false, false,
},
{
"fails - DeliverTx EIP712 signed Cosmos Tx with wrong Chain ID",
func() sdk.Tx {
Expand Down
37 changes: 23 additions & 14 deletions app/ante/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
signingtypes "github.com/cosmos/cosmos-sdk/types/tx/signing"
authante "github.com/cosmos/cosmos-sdk/x/auth/ante"
sdkante "github.com/cosmos/cosmos-sdk/x/auth/ante"
"github.com/cosmos/cosmos-sdk/x/auth/signing"
"github.com/cosmos/cosmos-sdk/x/auth/tx"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/authz"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
evtypes "github.com/cosmos/cosmos-sdk/x/evidence/types"
"github.com/cosmos/cosmos-sdk/x/feegrant"
Expand All @@ -35,6 +35,7 @@ import (
"github.com/evmos/ethermint/crypto/ethsecp256k1"
"github.com/evmos/ethermint/tests"
evmtypes "github.com/evmos/ethermint/x/evm/types"
"github.com/prysmaticlabs/prysm/crypto/bls"
"github.com/stretchr/testify/suite"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/log"
Expand All @@ -45,7 +46,6 @@ import (
"github.com/bnb-chain/greenfield/app"
"github.com/bnb-chain/greenfield/app/ante"
"github.com/bnb-chain/greenfield/app/params"
"github.com/cosmos/cosmos-sdk/x/authz"
)

const genesisAccountPrivateKeyForTest = "02DCA3F2C6CDF541934FA043A0ADBD891968EC7B948691ABA0C3CACA59A5DAC753"
Expand Down Expand Up @@ -85,12 +85,12 @@ func (suite *AnteTestSuite) SetupTest() {

suite.clientCtx = client.Context{}.WithTxConfig(encCfg.TxConfig)

anteHandler, _ := ante.NewAnteHandler(sdkante.HandlerOptions{
anteHandler, _ := ante.NewAnteHandler(ante.HandlerOptions{
AccountKeeper: suite.app.AccountKeeper,
BankKeeper: suite.app.BankKeeper,
FeegrantKeeper: suite.app.FeeGrantKeeper,
SignModeHandler: encCfg.TxConfig.SignModeHandler(),
SigGasConsumer: sdkante.DefaultSigVerificationGasConsumer,
GashubKeeper: suite.app.GashubKeeper,
})
suite.anteHandler = anteHandler
}
Expand All @@ -114,13 +114,20 @@ func (suite *AnteTestSuite) CreateTestEIP712MsgCreateValidator(from sdk.AccAddre
// Build MsgCreateValidator
valAddr := sdk.AccAddress(from.Bytes())
privEd := ed25519.GenPrivKey()
addr1 := sdk.AccAddress(from.Bytes())
blsSecretKey, _ := bls.RandKey()
blsPk := hex.EncodeToString(blsSecretKey.PublicKey().Marshal())
msgCreate, err := stakingtypes.NewMsgCreateValidator(
valAddr,
privEd.PubKey(),
sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(20)),
stakingtypes.NewDescription("moniker", "indentity", "website", "security_contract", "details"),
stakingtypes.NewCommissionRates(sdk.OneDec(), sdk.OneDec(), sdk.OneDec()),
sdk.OneInt(), valAddr, valAddr, valAddr, "test",
sdk.OneInt(),
addr1,
addr1,
addr1,
blsPk,
)
suite.Require().NoError(err)
return suite.CreateTestEIP712CosmosTxBuilder(from, priv, chainId, gas, gasAmount, msgCreate)
Expand Down Expand Up @@ -150,13 +157,14 @@ func (suite *AnteTestSuite) CreateTestEIP712GrantAllowance(from sdk.AccAddress,

func (suite *AnteTestSuite) CreateTestEIP712MsgEditValidator(from sdk.AccAddress, priv cryptotypes.PrivKey, chainId string, gas uint64, gasAmount sdk.Coins) client.TxBuilder {
valAddr := sdk.AccAddress(from.Bytes())
blsSecretKey, _ := bls.RandKey()
blsPk := hex.EncodeToString(blsSecretKey.PublicKey().Marshal())
msgEdit := stakingtypes.NewMsgEditValidator(
valAddr,
stakingtypes.NewDescription("moniker", "identity", "website", "security_contract", "details"),
nil,
nil,
valAddr,
"test",
sdk.AccAddress(priv.PubKey().Address()), blsPk,
)
return suite.CreateTestEIP712CosmosTxBuilder(from, priv, chainId, gas, gasAmount, msgEdit)
}
Expand Down Expand Up @@ -222,7 +230,7 @@ func (suite *AnteTestSuite) CreateTestEIP712CosmosTxBuilder(

txBuilder := suite.clientCtx.TxConfig.NewTxBuilder()

// txBuilder.SetFeeAmount(gasAmount)
txBuilder.SetFeeAmount(gasAmount)
txBuilder.SetGasLimit(gas)

err = txBuilder.SetMsgs(msg)
Expand All @@ -236,13 +244,14 @@ func (suite *AnteTestSuite) CreateTestEIP712CosmosTxBuilder(
PubKey: acc.GetPubKey(),
}

msgTypes, _, err := tx.GetMsgTypes(signerData, txBuilder.GetTx(), big.NewInt(9000))
msgTypes, signDoc, err := tx.GetMsgTypes(signerData, txBuilder.GetTx(), big.NewInt(9000))
suite.Require().NoError(err)

typedData, err := tx.WrapTxToTypedData(9000, signDoc, msgTypes)
suite.Require().NoError(err)

msgTypesJson, _ := json.MarshalIndent(msgTypes, "", " ")
fmt.Println("Msg Types:\n", string(msgTypesJson))
msgJson, _ := json.MarshalIndent(txBuilder.GetTx().GetMsgs()[0], "", " ")
fmt.Println("Msg:\n", string(msgJson))
typedDataJson, _ := json.MarshalIndent(typedData, "", " ")
fmt.Println("Typed data:\n", string(typedDataJson))

sigHash, err := suite.clientCtx.TxConfig.SignModeHandler().GetSignBytes(signingtypes.SignMode_SIGN_MODE_EIP_712, signerData, txBuilder.GetTx())
suite.Require().NoError(err)
Expand Down Expand Up @@ -368,7 +377,7 @@ func NewApp(options ...func(baseApp *baseapp.BaseApp)) (*app.App, params.Encodin
encCfg := app.MakeEncodingConfig()

nApp := app.New(
logger, db, nil, true, app.DefaultNodeHome, 0, encCfg, app.NewDefaultAppConfig(), simapp.EmptyAppOptions{}, options...)
logger, db, nil, true, app.DefaultNodeHome, 0, encCfg, &app.AppConfig{CrossChain: app.NewDefaultAppConfig().CrossChain}, simapp.EmptyAppOptions{}, options...)

genesisState := app.NewDefaultGenesisState(encCfg.Marshaler)
genesisState, _ = genesisStateWithValSet(nApp, genesisState, valSet, []authtypes.GenesisAccount{acc}, balance)
Expand Down
32 changes: 24 additions & 8 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
authsims "github.com/cosmos/cosmos-sdk/x/auth/simulation"
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
Expand All @@ -32,7 +31,6 @@ import (
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"

"github.com/cosmos/cosmos-sdk/x/crosschain"
crosschainkeeper "github.com/cosmos/cosmos-sdk/x/crosschain/keeper"
crosschaintypes "github.com/cosmos/cosmos-sdk/x/crosschain/types"
Expand All @@ -44,6 +42,9 @@ import (
"github.com/cosmos/cosmos-sdk/x/feegrant"
feegrantkeeper "github.com/cosmos/cosmos-sdk/x/feegrant/keeper"
feegrantmodule "github.com/cosmos/cosmos-sdk/x/feegrant/module"
"github.com/cosmos/cosmos-sdk/x/gashub"
gashubkeeper "github.com/cosmos/cosmos-sdk/x/gashub/keeper"
gashubtypes "github.com/cosmos/cosmos-sdk/x/gashub/types"
"github.com/cosmos/cosmos-sdk/x/genutil"
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
"github.com/cosmos/cosmos-sdk/x/gov"
Expand Down Expand Up @@ -83,17 +84,16 @@ import (
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
dbm "github.com/tendermint/tm-db"

"github.com/bnb-chain/greenfield/app/ante"
appparams "github.com/bnb-chain/greenfield/app/params"
"github.com/bnb-chain/greenfield/docs"
"github.com/bnb-chain/greenfield/version"
greenfieldmodule "github.com/bnb-chain/greenfield/x/greenfield"
greenfieldmodulekeeper "github.com/bnb-chain/greenfield/x/greenfield/keeper"
greenfieldmoduletypes "github.com/bnb-chain/greenfield/x/greenfield/types"

bridgemodule "github.com/bnb-chain/greenfield/x/bridge"
bridgemodulekeeper "github.com/bnb-chain/greenfield/x/bridge/keeper"
bridgemoduletypes "github.com/bnb-chain/greenfield/x/bridge/types"
// this line is used by starport scaffolding # stargate/app/moduleImport
greenfieldmodule "github.com/bnb-chain/greenfield/x/greenfield"
greenfieldmodulekeeper "github.com/bnb-chain/greenfield/x/greenfield/keeper"
greenfieldmoduletypes "github.com/bnb-chain/greenfield/x/greenfield/types"
)

const (
Expand Down Expand Up @@ -144,6 +144,7 @@ var (
crosschain.AppModuleBasic{},
oracle.AppModuleBasic{},
bridgemodule.AppModuleBasic{},
gashub.AppModuleBasic{},
// this line is used by starport scaffolding # stargate/app/moduleBasic
)

Expand Down Expand Up @@ -204,6 +205,7 @@ type App struct {
FeeGrantKeeper feegrantkeeper.Keeper
CrossChainKeeper crosschainkeeper.Keeper
OracleKeeper oraclekeeper.Keeper
GashubKeeper gashubkeeper.Keeper

GreenfieldKeeper greenfieldmodulekeeper.Keeper

Expand Down Expand Up @@ -260,6 +262,7 @@ func New(
crosschaintypes.StoreKey,
oracletypes.StoreKey,
bridgemoduletypes.StoreKey,
gashubtypes.StoreKey,
// this line is used by starport scaffolding # stargate/app/storeKey
)
tkeys := sdk.NewTransientStoreKeys(paramstypes.TStoreKey)
Expand Down Expand Up @@ -418,6 +421,13 @@ func New(
)
bridgeModule := bridgemodule.NewAppModule(appCodec, app.BridgeKeeper, app.AccountKeeper, app.BankKeeper)

app.GashubKeeper = gashubkeeper.NewGashubKeeper(
appCodec,
keys[gashubtypes.StoreKey],
app.GetSubspace(gashubtypes.ModuleName),
)
gashubModule := gashub.NewAppModule(appCodec, app.GashubKeeper)

// this line is used by starport scaffolding # stargate/app/keeperDefinition

/**** Module Options ****/
Expand Down Expand Up @@ -447,6 +457,7 @@ func New(
oracle.NewAppModule(app.OracleKeeper),
greenfieldModule,
bridgeModule,
gashubModule,
// this line is used by starport scaffolding # stargate/app/appModule
)

Expand All @@ -471,6 +482,7 @@ func New(
crosschaintypes.ModuleName,
oracletypes.ModuleName,
bridgemoduletypes.ModuleName,
gashubtypes.ModuleName,
// this line is used by starport scaffolding # stargate/app/beginBlockers
)

Expand All @@ -490,6 +502,7 @@ func New(
crosschaintypes.ModuleName,
oracletypes.ModuleName,
bridgemoduletypes.ModuleName,
gashubtypes.ModuleName,
// this line is used by starport scaffolding # stargate/app/endBlockers
)

Expand All @@ -514,6 +527,7 @@ func New(
crosschaintypes.ModuleName,
oracletypes.ModuleName,
bridgemoduletypes.ModuleName,
gashubtypes.ModuleName,
// this line is used by starport scaffolding # stargate/app/initGenesis
)

Expand All @@ -540,6 +554,7 @@ func New(
oracle.NewAppModule(app.OracleKeeper),
greenfieldModule,
bridgeModule,
gashubModule,
// this line is used by starport scaffolding # stargate/app/appModule
)
app.sm.RegisterStoreDecoders()
Expand All @@ -559,7 +574,7 @@ func New(
BankKeeper: app.BankKeeper,
SignModeHandler: encodingConfig.TxConfig.SignModeHandler(),
FeegrantKeeper: app.FeeGrantKeeper,
SigGasConsumer: ante.DefaultSigVerificationGasConsumer,
GashubKeeper: app.GashubKeeper,
},
)
if err != nil {
Expand Down Expand Up @@ -768,6 +783,7 @@ func initParamsKeeper(appCodec codec.BinaryCodec, legacyAmino *codec.LegacyAmino
paramsKeeper.Subspace(crosschaintypes.ModuleName)
paramsKeeper.Subspace(oracletypes.ModuleName)
paramsKeeper.Subspace(bridgemoduletypes.ModuleName)
paramsKeeper.Subspace(gashubtypes.ModuleName)
// this line is used by starport scaffolding # stargate/app/paramSubspace

return paramsKeeper
Expand Down
2 changes: 2 additions & 0 deletions deployment/localup/.env
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ VALIDATOR_P2P_PORT_START=27750
VALIDATOR_GRPC_PORT_START=9090
VALIDATOR_GRPC_WEB_PORT_START=9190
VALIDATOR_RPC_PORT_START=26750
VALIDATOR_JSONRPC_PORT_START=8545
DATASEED_ADDRESS_PORT_START=28850
DATASEED_P2P_PORT_START=27850
DATASEED_GRPC_PORT_START=9890
DATASEED_GRPC_WEB_PORT_START=9990
DATASEED_RPC_PORT_START=26850
DATASEED_JSONRPC_PORT_START=8565
Loading