Skip to content
This repository has been archived by the owner on Nov 30, 2021. It is now read-only.

Commit

Permalink
evm: params (#458)
Browse files Browse the repository at this point in the history
* evm: params

* setup

* bump commit

* fixes

* increase gas usage

* tests

* evm denom param

* more config updates

* update genesis

* update ante handler

* csdb param test

* more tests and fixes

* update statedb.Copy

* lint

* additional test

* fix importer tests

* fix AnteHandler test

* minor update

* revert

* undo gas update

* stringer test

* changelog

* fix csdb index error (#493)

* attempt to fix

* cleanup

* add idx check

* update csdb.Copy

* update default hash

* update querier

* update rpc tests

* fix estimate gas test

Co-authored-by: noot <[email protected]>
Co-authored-by: noot <[email protected]>
  • Loading branch information
3 people authored Sep 2, 2020
1 parent 26816e2 commit 792c1ff
Show file tree
Hide file tree
Showing 33 changed files with 848 additions and 144 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ Ref: https://keepachangelog.com/en/1.0.0/
### Improvements

* (app) [\#471](https://github.com/ChainSafe/ethermint/pull/471) Add `x/upgrade` module for managing software updates.
* (`x/evm`) [\#458](https://github.com/ChainSafe/ethermint/pull/458) Define parameter for token denomination used for the EVM module.
* (`x/evm`) [\#443](https://github.com/ChainSafe/ethermint/issues/443) Support custom Ethereum `ChainConfig` params.
* (types) [\#434](https://github.com/ChainSafe/ethermint/issues/434) Update default denomination to Atto Photon (`aphoton`).

### Bug Fixes

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ test-race:

test-import:
@go test ./importer -v --vet=off --run=TestImportBlocks --datadir tmp \
--blockchain blockchain --timeout=10m
--blockchain blockchain
rm -rf importer/tmp

test-rpc:
Expand Down
9 changes: 4 additions & 5 deletions app/ante/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/cosmos/cosmos-sdk/x/auth"
authante "github.com/cosmos/cosmos-sdk/x/auth/ante"
"github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/bank"

"github.com/cosmos/ethermint/crypto"
evmtypes "github.com/cosmos/ethermint/x/evm/types"
Expand All @@ -29,7 +28,7 @@ const (
// Ethereum or SDK transaction to an internal ante handler for performing
// transaction-level processing (e.g. fee payment, signature verification) before
// being passed onto it's respective handler.
func NewAnteHandler(ak auth.AccountKeeper, bk bank.Keeper, sk types.SupplyKeeper) sdk.AnteHandler {
func NewAnteHandler(ak auth.AccountKeeper, evmKeeper EVMKeeper, sk types.SupplyKeeper) sdk.AnteHandler {
return func(
ctx sdk.Context, tx sdk.Tx, sim bool,
) (newCtx sdk.Context, err error) {
Expand All @@ -53,11 +52,11 @@ func NewAnteHandler(ak auth.AccountKeeper, bk bank.Keeper, sk types.SupplyKeeper
case evmtypes.MsgEthereumTx:
anteHandler = sdk.ChainAnteDecorators(
NewEthSetupContextDecorator(), // outermost AnteDecorator. EthSetUpContext must be called first
NewEthMempoolFeeDecorator(),
NewEthMempoolFeeDecorator(evmKeeper),
NewEthSigVerificationDecorator(),
NewAccountVerificationDecorator(ak, bk),
NewAccountVerificationDecorator(ak, evmKeeper),
NewNonceVerificationDecorator(ak),
NewEthGasConsumeDecorator(ak, sk),
NewEthGasConsumeDecorator(ak, sk, evmKeeper),
NewIncrementSenderSequenceDecorator(ak), // innermost AnteDecorator.
)
default:
Expand Down
3 changes: 2 additions & 1 deletion app/ante/ante_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,9 @@ func (suite *AnteTestSuite) TestEthInvalidMempoolFees() {
// setup app with checkTx = true
suite.app = app.Setup(true)
suite.ctx = suite.app.BaseApp.NewContext(true, abci.Header{Height: 1, ChainID: "3", Time: time.Now().UTC()})
suite.anteHandler = ante.NewAnteHandler(suite.app.AccountKeeper, suite.app.BankKeeper, suite.app.SupplyKeeper)
suite.app.EvmKeeper.SetParams(suite.ctx, evmtypes.DefaultParams())

suite.anteHandler = ante.NewAnteHandler(suite.app.AccountKeeper, suite.app.EvmKeeper, suite.app.SupplyKeeper)
suite.ctx = suite.ctx.WithMinGasPrices(sdk.NewDecCoins(sdk.NewDecCoin(types.DenomDefault, sdk.NewInt(500000))))
addr1, priv1 := newTestAddrKey()
addr2, _ := newTestAddrKey()
Expand Down
56 changes: 36 additions & 20 deletions app/ante/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/cosmos/cosmos-sdk/x/auth"
authante "github.com/cosmos/cosmos-sdk/x/auth/ante"
"github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/bank"

emint "github.com/cosmos/ethermint/types"
evmtypes "github.com/cosmos/ethermint/x/evm/types"
Expand All @@ -18,6 +17,11 @@ import (
ethcore "github.com/ethereum/go-ethereum/core"
)

// EVMKeeper defines the expected keeper interface used on the Eth AnteHandler
type EVMKeeper interface {
GetParams(ctx sdk.Context) evmtypes.Params
}

// EthSetupContextDecorator sets the infinite GasMeter in the Context and wraps
// the next AnteHandler with a defer clause to recover from any downstream
// OutOfGas panics in the AnteHandler chain to return an error with information
Expand Down Expand Up @@ -68,11 +72,15 @@ func (escd EthSetupContextDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simu

// EthMempoolFeeDecorator validates that sufficient fees have been provided that
// meet a minimum threshold defined by the proposer (for mempool purposes during CheckTx).
type EthMempoolFeeDecorator struct{}
type EthMempoolFeeDecorator struct {
evmKeeper EVMKeeper
}

// NewEthMempoolFeeDecorator creates a new EthMempoolFeeDecorator
func NewEthMempoolFeeDecorator() EthMempoolFeeDecorator {
return EthMempoolFeeDecorator{}
func NewEthMempoolFeeDecorator(ek EVMKeeper) EthMempoolFeeDecorator {
return EthMempoolFeeDecorator{
evmKeeper: ek,
}
}

// AnteHandle verifies that enough fees have been provided by the
Expand All @@ -90,16 +98,18 @@ func (emfd EthMempoolFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simula
return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "invalid transaction type: %T", tx)
}

evmDenom := emfd.evmKeeper.GetParams(ctx).EvmDenom

// fee = GP * GL
fee := sdk.NewInt64DecCoin(emint.DenomDefault, msgEthTx.Fee().Int64())
fee := sdk.NewInt64DecCoin(evmDenom, msgEthTx.Fee().Int64())

minGasPrices := ctx.MinGasPrices()

// check that fee provided is greater than the minimum
// NOTE: we only check if aphotons are present in min gas prices. It is up to the
// sender if they want to send additional fees in other denominations.
var hasEnoughFees bool
if fee.Amount.GTE(minGasPrices.AmountOf(emint.DenomDefault)) {
if fee.Amount.GTE(minGasPrices.AmountOf(evmDenom)) {
hasEnoughFees = true
}

Expand Down Expand Up @@ -150,15 +160,15 @@ func (esvd EthSigVerificationDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, s

// AccountVerificationDecorator validates an account balance checks
type AccountVerificationDecorator struct {
ak auth.AccountKeeper
bk bank.Keeper
ak auth.AccountKeeper
evmKeeper EVMKeeper
}

// NewAccountVerificationDecorator creates a new AccountVerificationDecorator
func NewAccountVerificationDecorator(ak auth.AccountKeeper, bk bank.Keeper) AccountVerificationDecorator {
func NewAccountVerificationDecorator(ak auth.AccountKeeper, ek EVMKeeper) AccountVerificationDecorator {
return AccountVerificationDecorator{
ak: ak,
bk: bk,
ak: ak,
evmKeeper: ek,
}
}

Expand Down Expand Up @@ -192,12 +202,14 @@ func (avd AccountVerificationDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, s
)
}

evmDenom := avd.evmKeeper.GetParams(ctx).EvmDenom

// validate sender has enough funds to pay for gas cost
balance := sdk.Coin{Denom: emint.DenomDefault, Amount: acc.GetCoins().AmountOf(emint.DenomDefault)}
if balance.Amount.BigInt().Cmp(msgEthTx.Cost()) < 0 {
balance := acc.GetCoins().AmountOf(evmDenom)
if balance.BigInt().Cmp(msgEthTx.Cost()) < 0 {
return ctx, sdkerrors.Wrapf(
sdkerrors.ErrInsufficientFunds,
"sender balance < tx gas cost (%s < %s%s)", balance.String(), msgEthTx.Cost().String(), emint.DenomDefault,
"sender balance < tx gas cost (%s%s < %s%s)", balance.String(), evmDenom, msgEthTx.Cost().String(), evmDenom,
)
}

Expand Down Expand Up @@ -250,15 +262,17 @@ func (nvd NonceVerificationDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, sim
// EthGasConsumeDecorator validates enough intrinsic gas for the transaction and
// gas consumption.
type EthGasConsumeDecorator struct {
ak auth.AccountKeeper
sk types.SupplyKeeper
ak auth.AccountKeeper
sk types.SupplyKeeper
evmKeeper EVMKeeper
}

// NewEthGasConsumeDecorator creates a new EthGasConsumeDecorator
func NewEthGasConsumeDecorator(ak auth.AccountKeeper, sk types.SupplyKeeper) EthGasConsumeDecorator {
func NewEthGasConsumeDecorator(ak auth.AccountKeeper, sk types.SupplyKeeper, ek EVMKeeper) EthGasConsumeDecorator {
return EthGasConsumeDecorator{
ak: ak,
sk: sk,
ak: ak,
sk: sk,
evmKeeper: ek,
}
}

Expand Down Expand Up @@ -307,8 +321,10 @@ func (egcd EthGasConsumeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simula
// Cost calculates the fees paid to validators based on gas limit and price
cost := new(big.Int).Mul(msgEthTx.Data.Price, new(big.Int).SetUint64(gasLimit))

evmDenom := egcd.evmKeeper.GetParams(ctx).EvmDenom

feeAmt := sdk.NewCoins(
sdk.NewCoin(emint.DenomDefault, sdk.NewIntFromBigInt(cost)),
sdk.NewCoin(evmDenom, sdk.NewIntFromBigInt(cost)),
)

err = auth.DeductFees(egcd.sk, ctx, senderAcc, feeAmt)
Expand Down
4 changes: 3 additions & 1 deletion app/ante/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ func (suite *AnteTestSuite) SetupTest() {
suite.app.Codec().RegisterConcrete(&sdk.TestMsg{}, "test/TestMsg", nil)

suite.ctx = suite.app.BaseApp.NewContext(checkTx, abci.Header{Height: 1, ChainID: "3", Time: time.Now().UTC()})
suite.anteHandler = ante.NewAnteHandler(suite.app.AccountKeeper, suite.app.BankKeeper, suite.app.SupplyKeeper)
suite.app.EvmKeeper.SetParams(suite.ctx, evmtypes.DefaultParams())

suite.anteHandler = ante.NewAnteHandler(suite.app.AccountKeeper, suite.app.EvmKeeper, suite.app.SupplyKeeper)
}

func TestAnteTestSuite(t *testing.T) {
Expand Down
7 changes: 4 additions & 3 deletions app/ethermint.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func NewEthermintApp(

cdc := ethermintcodec.MakeCodec(ModuleBasics)

// use custom Ethermint transaction decoder
// NOTE we use custom Ethermint transaction decoder that supports the sdk.Tx interface instead of sdk.StdTx
bApp := bam.NewBaseApp(appName, logger, db, evm.TxDecoder(cdc), baseAppOptions...)
bApp.SetCommitMultiStoreTracer(traceStore)
bApp.SetAppVersion(version.Version)
Expand Down Expand Up @@ -182,6 +182,7 @@ func NewEthermintApp(
app.subspaces[gov.ModuleName] = app.ParamsKeeper.Subspace(gov.DefaultParamspace).WithKeyTable(gov.ParamKeyTable())
app.subspaces[crisis.ModuleName] = app.ParamsKeeper.Subspace(crisis.DefaultParamspace)
app.subspaces[evidence.ModuleName] = app.ParamsKeeper.Subspace(evidence.DefaultParamspace)
app.subspaces[evm.ModuleName] = app.ParamsKeeper.Subspace(evm.DefaultParamspace)

// use custom Ethermint account for contracts
app.AccountKeeper = auth.NewAccountKeeper(
Expand Down Expand Up @@ -212,7 +213,7 @@ func NewEthermintApp(
)
app.UpgradeKeeper = upgrade.NewKeeper(skipUpgradeHeights, keys[upgrade.StoreKey], app.cdc)
app.EvmKeeper = evm.NewKeeper(
app.cdc, keys[evm.StoreKey], app.AccountKeeper,
app.cdc, keys[evm.StoreKey], app.subspaces[evm.ModuleName], app.AccountKeeper,
)
app.FaucetKeeper = faucet.NewKeeper(
app.cdc, keys[faucet.StoreKey], app.SupplyKeeper,
Expand Down Expand Up @@ -311,7 +312,7 @@ func NewEthermintApp(
// initialize BaseApp
app.SetInitChainer(app.InitChainer)
app.SetBeginBlocker(app.BeginBlocker)
app.SetAnteHandler(ante.NewAnteHandler(app.AccountKeeper, app.BankKeeper, app.SupplyKeeper))
app.SetAnteHandler(ante.NewAnteHandler(app.AccountKeeper, app.EvmKeeper, app.SupplyKeeper))
app.SetEndBlocker(app.EndBlocker)

if loadLatest {
Expand Down
Loading

0 comments on commit 792c1ff

Please sign in to comment.