diff --git a/ethereum/rpc/types/utils.go b/ethereum/rpc/types/utils.go index da7933cef..6fc89e71e 100644 --- a/ethereum/rpc/types/utils.go +++ b/ethereum/rpc/types/utils.go @@ -79,7 +79,7 @@ func NewTransaction(tx *ethtypes.Transaction, blockHash common.Hash, blockNumber From: from, Gas: hexutil.Uint64(tx.Gas()), 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()), Nonce: hexutil.Uint64(tx.Nonce()), To: tx.To(), diff --git a/x/evm/keeper/statedb.go b/x/evm/keeper/statedb.go index d1f7ce7c3..ae021c1ec 100644 --- a/x/evm/keeper/statedb.go +++ b/x/evm/keeper/statedb.go @@ -10,8 +10,11 @@ import ( "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/crypto" + tmtypes "github.com/tendermint/tendermint/types" + "github.com/cosmos/cosmos-sdk/store/prefix" sdk "github.com/cosmos/cosmos-sdk/types" + ethermint "github.com/tharsis/ethermint/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 // 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. // 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 // to store. func (k *Keeper) AddLog(log *ethtypes.Log) { + + key := log.TxHash + if len(k.ctx.TxBytes()) > 0 { tx := ðtypes.Transaction{} if err := tx.UnmarshalBinary(k.ctx.TxBytes()); err != nil { @@ -575,6 +581,10 @@ func (k *Keeper) AddLog(log *ethtypes.Log) { 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() } @@ -585,11 +595,13 @@ func (k *Keeper) AddLog(log *ethtypes.Log) { log.Index = uint(len(logs)) logs = append(logs, log) - k.SetLogs(log.TxHash, logs) + + k.SetLogs(key, logs) k.Logger(k.ctx).Debug( "log added", - "tx-hash", log.TxHash.Hex(), + "tx-hash-tendermint", key.Hex(), + "tx-hash-ethereum", log.TxHash.Hex(), "log-index", int(log.Index), ) } diff --git a/x/evm/keeper/statedb_test.go b/x/evm/keeper/statedb_test.go index 29426128e..4553c2121 100644 --- a/x/evm/keeper/statedb_test.go +++ b/x/evm/keeper/statedb_test.go @@ -6,6 +6,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + tmtypes "github.com/tendermint/tendermint/types" "github.com/ethereum/go-ethereum/common" ethtypes "github.com/ethereum/go-ethereum/core/types" @@ -459,14 +460,17 @@ func (suite *KeeperTestSuite) TestAddLog() { txBz, err := tx.MarshalBinary() suite.Require().NoError(err) txHash := tx.Hash() + tmHash := common.BytesToHash(tmtypes.Tx(txBz).Hash()) testCases := []struct { name string + hash common.Hash log, expLog *ethtypes.Log // pre and post populating log fields malleate func() }{ { "block hash not found", + common.Hash{}, ðtypes.Log{ Address: addr, }, @@ -477,6 +481,7 @@ func (suite *KeeperTestSuite) TestAddLog() { }, { "tx hash from message", + tmHash, ðtypes.Log{ Address: addr, }, @@ -494,11 +499,11 @@ func (suite *KeeperTestSuite) TestAddLog() { suite.Run(tc.name, func() { tc.malleate() - prev := suite.app.EvmKeeper.GetTxLogs(tc.expLog.TxHash) + prev := suite.app.EvmKeeper.GetTxLogs(tc.hash) 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().NotNil(post[len(post)-1]) suite.Require().Equal(tc.log, post[len(post)-1])