evm: update log tx hash key (#182)

* evm: update log tx hash key

* update test
This commit is contained in:
Federico Kunze 2021-06-24 12:05:45 -04:00 committed by GitHub
parent 0f3a346cdc
commit 365c96acfa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 7 deletions

View File

@ -79,7 +79,7 @@ func NewTransaction(tx *ethtypes.Transaction, blockHash common.Hash, blockNumber
From: from, From: from,
Gas: hexutil.Uint64(tx.Gas()), Gas: hexutil.Uint64(tx.Gas()),
GasPrice: (*hexutil.Big)(tx.GasPrice()), GasPrice: (*hexutil.Big)(tx.GasPrice()),
Hash: tx.Hash(), Hash: tx.Hash(), // NOTE: transaction hash here uses the ethereum format for compatibility
Input: hexutil.Bytes(tx.Data()), Input: hexutil.Bytes(tx.Data()),
Nonce: hexutil.Uint64(tx.Nonce()), Nonce: hexutil.Uint64(tx.Nonce()),
To: tx.To(), To: tx.To(),

View File

@ -10,8 +10,11 @@ import (
"github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
tmtypes "github.com/tendermint/tendermint/types"
"github.com/cosmos/cosmos-sdk/store/prefix" "github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
ethermint "github.com/tharsis/ethermint/types" ethermint "github.com/tharsis/ethermint/types"
"github.com/tharsis/ethermint/x/evm/types" "github.com/tharsis/ethermint/x/evm/types"
) )
@ -285,7 +288,7 @@ func (k *Keeper) GetCodeSize(addr common.Address) int {
// NOTE: gas refunded needs to be tracked and stored in a separate variable in // NOTE: gas refunded needs to be tracked and stored in a separate variable in
// order to add it subtract/add it from/to the gas used value after the EVM // order to add it subtract/add it from/to the gas used value after the EVM
// execution has finalised. The refund value is cleared on every transaction and // execution has finalized. The refund value is cleared on every transaction and
// at the end of every block. // at the end of every block.
// AddRefund adds the given amount of gas to the refund cached value. // AddRefund adds the given amount of gas to the refund cached value.
@ -565,6 +568,9 @@ func (k *Keeper) RevertToSnapshot(_ int) {}
// context. This function also fills in the tx hash, block hash, tx index and log index fields before setting the log // context. This function also fills in the tx hash, block hash, tx index and log index fields before setting the log
// to store. // to store.
func (k *Keeper) AddLog(log *ethtypes.Log) { func (k *Keeper) AddLog(log *ethtypes.Log) {
key := log.TxHash
if len(k.ctx.TxBytes()) > 0 { if len(k.ctx.TxBytes()) > 0 {
tx := &ethtypes.Transaction{} tx := &ethtypes.Transaction{}
if err := tx.UnmarshalBinary(k.ctx.TxBytes()); err != nil { if err := tx.UnmarshalBinary(k.ctx.TxBytes()); err != nil {
@ -575,6 +581,10 @@ func (k *Keeper) AddLog(log *ethtypes.Log) {
return return
} }
// NOTE: we set up the transaction hash from tendermint as it is the format expected by the application:
// Remove once hashing is fixed on Tendermint. See https://github.com/tendermint/tendermint/issues/6539
key = common.BytesToHash(tmtypes.Tx(k.ctx.TxBytes()).Hash())
log.TxHash = tx.Hash() log.TxHash = tx.Hash()
} }
@ -585,11 +595,13 @@ func (k *Keeper) AddLog(log *ethtypes.Log) {
log.Index = uint(len(logs)) log.Index = uint(len(logs))
logs = append(logs, log) logs = append(logs, log)
k.SetLogs(log.TxHash, logs)
k.SetLogs(key, logs)
k.Logger(k.ctx).Debug( k.Logger(k.ctx).Debug(
"log added", "log added",
"tx-hash", log.TxHash.Hex(), "tx-hash-tendermint", key.Hex(),
"tx-hash-ethereum", log.TxHash.Hex(),
"log-index", int(log.Index), "log-index", int(log.Index),
) )
} }

View File

@ -6,6 +6,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
tmtypes "github.com/tendermint/tendermint/types"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types" ethtypes "github.com/ethereum/go-ethereum/core/types"
@ -459,14 +460,17 @@ func (suite *KeeperTestSuite) TestAddLog() {
txBz, err := tx.MarshalBinary() txBz, err := tx.MarshalBinary()
suite.Require().NoError(err) suite.Require().NoError(err)
txHash := tx.Hash() txHash := tx.Hash()
tmHash := common.BytesToHash(tmtypes.Tx(txBz).Hash())
testCases := []struct { testCases := []struct {
name string name string
hash common.Hash
log, expLog *ethtypes.Log // pre and post populating log fields log, expLog *ethtypes.Log // pre and post populating log fields
malleate func() malleate func()
}{ }{
{ {
"block hash not found", "block hash not found",
common.Hash{},
&ethtypes.Log{ &ethtypes.Log{
Address: addr, Address: addr,
}, },
@ -477,6 +481,7 @@ func (suite *KeeperTestSuite) TestAddLog() {
}, },
{ {
"tx hash from message", "tx hash from message",
tmHash,
&ethtypes.Log{ &ethtypes.Log{
Address: addr, Address: addr,
}, },
@ -494,11 +499,11 @@ func (suite *KeeperTestSuite) TestAddLog() {
suite.Run(tc.name, func() { suite.Run(tc.name, func() {
tc.malleate() tc.malleate()
prev := suite.app.EvmKeeper.GetTxLogs(tc.expLog.TxHash) prev := suite.app.EvmKeeper.GetTxLogs(tc.hash)
suite.app.EvmKeeper.AddLog(tc.log) suite.app.EvmKeeper.AddLog(tc.log)
post := suite.app.EvmKeeper.GetTxLogs(tc.expLog.TxHash) post := suite.app.EvmKeeper.GetTxLogs(tc.hash)
suite.Require().NotZero(len(post), tc.expLog.TxHash.Hex()) suite.Require().NotZero(len(post), tc.hash.Hex())
suite.Require().Equal(len(prev)+1, len(post)) suite.Require().Equal(len(prev)+1, len(post))
suite.Require().NotNil(post[len(post)-1]) suite.Require().NotNil(post[len(post)-1])
suite.Require().Equal(tc.log, post[len(post)-1]) suite.Require().Equal(tc.log, post[len(post)-1])