x/evm: fix EndBlock consensus failure (#334)

* add test for sending tx w/ 21000 gas

* improve rpc transfer test

* use ctx in EndBlock

* UpdateAccounts and ClearStateObjects with passed in context

* log ethereum address on error

Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>
Co-authored-by: Federico Kunze <federico.kunze94@gmail.com>
This commit is contained in:
noot 2020-06-22 12:07:35 -04:00 committed by GitHub
parent 0921c863e7
commit 28e28f2a7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 62 additions and 10 deletions

View File

@ -14,6 +14,7 @@ import (
emint "github.com/cosmos/ethermint/types" emint "github.com/cosmos/ethermint/types"
evmtypes "github.com/cosmos/ethermint/x/evm/types" evmtypes "github.com/cosmos/ethermint/x/evm/types"
"github.com/ethereum/go-ethereum/common"
ethcore "github.com/ethereum/go-ethereum/core" ethcore "github.com/ethereum/go-ethereum/core"
) )
@ -180,7 +181,7 @@ func (avd AccountVerificationDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, s
acc := avd.ak.GetAccount(ctx, address) acc := avd.ak.GetAccount(ctx, address)
if acc == nil { if acc == nil {
return ctx, fmt.Errorf("account %s is nil", address) return ctx, fmt.Errorf("account %s (%s) is nil", common.BytesToAddress(address.Bytes()), address)
} }
// on InitChain make sure account number == 0 // on InitChain make sure account number == 0
@ -232,7 +233,7 @@ func (nvd NonceVerificationDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, sim
acc := nvd.ak.GetAccount(ctx, address) acc := nvd.ak.GetAccount(ctx, address)
if acc == nil { if acc == nil {
return ctx, fmt.Errorf("account %s is nil", address) return ctx, fmt.Errorf("account %s (%s) is nil", common.BytesToAddress(address.Bytes()), address)
} }
seq := acc.GetSequence() seq := acc.GetSequence()
@ -287,7 +288,7 @@ func (egcd EthGasConsumeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simula
} }
if senderAcc == nil { if senderAcc == nil {
return ctx, fmt.Errorf("sender account %s is nil", address) return ctx, fmt.Errorf("sender account %s (%s) is nil", common.BytesToAddress(address.Bytes()), address)
} }
gasLimit := msgEthTx.GetGas() gasLimit := msgEthTx.GetGas()

View File

@ -221,7 +221,29 @@ func getAddress(t *testing.T) []byte {
return res[0] return res[0]
} }
func TestEth_SendTransaction(t *testing.T) { func TestEth_SendTransaction_Transfer(t *testing.T) {
from := getAddress(t)
param := make([]map[string]string, 1)
param[0] = make(map[string]string)
param[0]["from"] = "0x" + fmt.Sprintf("%x", from)
param[0]["to"] = "0x0000000000000000000000000000000012341234"
param[0]["value"] = "0x16345785d8a0000"
param[0]["gasLimit"] = "0x5208"
param[0]["gasPrice"] = "0x55ae82600"
rpcRes := call(t, "eth_sendTransaction", param)
var hash hexutil.Bytes
err := json.Unmarshal(rpcRes.Result, &hash)
require.NoError(t, err)
receipt := waitForReceipt(t, hash)
require.NotNil(t, receipt)
require.Equal(t, "0x1", receipt["status"].(string))
}
func TestEth_SendTransaction_ContractDeploy(t *testing.T) {
from := getAddress(t) from := getAddress(t)
param := make([]map[string]string, 1) param := make([]map[string]string, 1)

View File

@ -31,16 +31,16 @@ func EndBlock(k Keeper, ctx sdk.Context, req abci.RequestEndBlock) []abci.Valida
ctx = ctx.WithBlockGasMeter(sdk.NewInfiniteGasMeter()) ctx = ctx.WithBlockGasMeter(sdk.NewInfiniteGasMeter())
// Update account balances before committing other parts of state // Update account balances before committing other parts of state
k.CommitStateDB.UpdateAccounts() k.UpdateAccounts(ctx)
// Commit state objects to KV store // Commit state objects to KV store
_, err := k.CommitStateDB.WithContext(ctx).Commit(true) _, err := k.Commit(ctx, true)
if err != nil { if err != nil {
panic(err) panic(err)
} }
// Clear accounts cache after account data has been committed // Clear accounts cache after account data has been committed
k.CommitStateDB.ClearStateObjects() k.ClearStateObjects(ctx)
bloom := ethtypes.BytesToBloom(k.Bloom.Bytes()) bloom := ethtypes.BytesToBloom(k.Bloom.Bytes())
k.SetBlockBloom(ctx, ctx.BlockHeight(), bloom) k.SetBlockBloom(ctx, ctx.BlockHeight(), bloom)

View File

@ -1,6 +1,7 @@
package evm_test package evm_test
import ( import (
"crypto/ecdsa"
"fmt" "fmt"
"math/big" "math/big"
"testing" "testing"
@ -10,6 +11,7 @@ import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
ethcmn "github.com/ethereum/go-ethereum/common" ethcmn "github.com/ethereum/go-ethereum/common"
ethcrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
@ -258,9 +260,6 @@ func (suite *EvmTestSuite) TestQueryTxLogs() {
err = tx.Sign(big.NewInt(3), priv.ToECDSA()) err = tx.Sign(big.NewInt(3), priv.ToECDSA())
suite.Require().NoError(err) suite.Require().NoError(err)
// result, err := evm.HandleEthTxMsg(suite.ctx, suite.app.EvmKeeper, tx)
// suite.Require().NoError(err, "failed to handle eth tx msg")
result, err := suite.handler(suite.ctx, tx) result, err := suite.handler(suite.ctx, tx)
suite.Require().NoError(err) suite.Require().NoError(err)
suite.Require().NotNil(result) suite.Require().NotNil(result)
@ -291,3 +290,23 @@ func (suite *EvmTestSuite) TestQueryTxLogs() {
resultData.Logs[0].Data = []byte{} resultData.Logs[0].Data = []byte{}
suite.Require().Equal(txLogs.Logs[0], resultData.Logs[0]) suite.Require().Equal(txLogs.Logs[0], resultData.Logs[0])
} }
func (suite *EvmTestSuite) TestSendTransaction() {
gasLimit := uint64(21000)
gasPrice := big.NewInt(1)
priv, err := crypto.GenerateKey()
suite.Require().NoError(err, "failed to create key")
pub := priv.ToECDSA().Public().(*ecdsa.PublicKey)
suite.app.EvmKeeper.SetBalance(suite.ctx, ethcrypto.PubkeyToAddress(*pub), big.NewInt(100))
// send simple value transfer with gasLimit=21000
tx := types.NewMsgEthereumTx(1, &ethcmn.Address{0x1}, big.NewInt(1), gasLimit, gasPrice, nil)
err = tx.Sign(big.NewInt(3), priv.ToECDSA())
suite.Require().NoError(err)
result, err := suite.handler(suite.ctx, tx)
suite.Require().NoError(err)
suite.Require().NotNil(result)
}

View File

@ -228,6 +228,16 @@ func (k *Keeper) CreateAccount(ctx sdk.Context, addr ethcmn.Address) {
k.CommitStateDB.WithContext(ctx).CreateAccount(addr) k.CommitStateDB.WithContext(ctx).CreateAccount(addr)
} }
// UpdateAccounts calls CommitStateDB.UpdateAccounts using the passed in context
func (k *Keeper) UpdateAccounts(ctx sdk.Context) {
k.CommitStateDB.WithContext(ctx).UpdateAccounts()
}
// ClearStateObjects calls CommitStateDB.ClearStateObjects using the passed in context
func (k *Keeper) ClearStateObjects(ctx sdk.Context) {
k.CommitStateDB.WithContext(ctx).ClearStateObjects()
}
// Copy calls CommitStateDB.Copy using the passed in context // Copy calls CommitStateDB.Copy using the passed in context
func (k *Keeper) Copy(ctx sdk.Context) ethvm.StateDB { func (k *Keeper) Copy(ctx sdk.Context) ethvm.StateDB {
return k.CommitStateDB.WithContext(ctx).Copy() return k.CommitStateDB.WithContext(ctx).Copy()