fix: return ethereum-formatted tx hash to client (#202)

* return eth tx hash to client

Closes #67

Update ethereum/rpc/namespaces/eth/api.go

Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com>

Update ethereum/rpc/namespaces/eth/api.go

Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com>

change GetTxByEthHash to method

add entry to changelog

* use eth tx hash internally
This commit is contained in:
yihuang
2021-06-30 05:35:11 -04:00
committed by GitHub
co-authored by Federico Kunze Küllmer
parent 5ba8ffe669
commit 0113b4d2c0
9 changed files with 69 additions and 75 deletions
+21 -6
View File
@@ -21,8 +21,7 @@ import (
// to the StateDB interface.
type Keeper struct {
// Protobuf codec
cdc codec.BinaryCodec
txDecoder sdk.TxDecoder
cdc codec.BinaryCodec
// Store key required for the EVM Prefix KVStore. It is required by:
// - storing Account's Storage State
// - storing Account's Code
@@ -47,7 +46,7 @@ type Keeper struct {
// NewKeeper generates new evm module keeper
func NewKeeper(
cdc codec.BinaryCodec, txDecoder sdk.TxDecoder,
cdc codec.BinaryCodec,
storeKey, transientKey sdk.StoreKey, paramSpace paramtypes.Subspace,
ak types.AccountKeeper, bankKeeper types.BankKeeper, sk types.StakingKeeper,
debug bool,
@@ -66,7 +65,6 @@ func NewKeeper(
// NOTE: we pass in the parameter space to the CommitStateDB in order to use custom denominations for the EVM operations
return &Keeper{
cdc: cdc,
txDecoder: txDecoder,
paramSpace: paramSpace,
accountKeeper: ak,
bankKeeper: bankKeeper,
@@ -152,10 +150,27 @@ func (k Keeper) SetBlockBloomTransient(bloom *big.Int) {
// Tx
// ----------------------------------------------------------------------------
// GetTxHashTransient returns the hash of current processing transaction
func (k Keeper) GetTxHashTransient() common.Hash {
store := k.ctx.TransientStore(k.transientKey)
bz := store.Get(types.KeyPrefixTransientTxHash)
if len(bz) == 0 {
return common.Hash{}
}
return common.BytesToHash(bz)
}
// SetTxHashTransient set the hash of processing transaction
func (k Keeper) SetTxHashTransient(hash common.Hash) {
store := k.ctx.TransientStore(k.transientKey)
store.Set(types.KeyPrefixTransientTxHash, hash.Bytes())
}
// GetTxIndexTransient returns EVM transaction index on the current block.
func (k Keeper) GetTxIndexTransient() uint64 {
store := k.ctx.TransientStore(k.transientKey)
bz := store.Get(types.KeyPrefixTransientBloom)
bz := store.Get(types.KeyPrefixTransientTxIndex)
if len(bz) == 0 {
return 0
}
@@ -168,7 +183,7 @@ func (k Keeper) GetTxIndexTransient() uint64 {
func (k Keeper) IncreaseTxIndexTransient() {
txIndex := k.GetTxIndexTransient()
store := k.ctx.TransientStore(k.transientKey)
store.Set(types.KeyPrefixTransientBloom, sdk.Uint64ToBigEndian(txIndex+1))
store.Set(types.KeyPrefixTransientTxIndex, sdk.Uint64ToBigEndian(txIndex+1))
}
// ResetRefundTransient resets the available refund amount to 0
+2 -3
View File
@@ -128,6 +128,7 @@ func (k *Keeper) ApplyTransaction(tx *ethtypes.Transaction) (*types.MsgEthereumT
evm := k.NewEVM(msg, ethCfg)
k.SetTxHashTransient(tx.Hash())
k.IncreaseTxIndexTransient()
// create an ethereum StateTransition instance and run TransitionDb
@@ -136,9 +137,7 @@ func (k *Keeper) ApplyTransaction(tx *ethtypes.Transaction) (*types.MsgEthereumT
return nil, stacktrace.Propagate(err, "failed to apply ethereum core message")
}
// 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
txHash := common.BytesToHash(tmtypes.Tx(k.ctx.TxBytes()).Hash())
txHash := tx.Hash()
res.Hash = txHash.Hex()
logs := k.GetTxLogs(txHash)
+3 -21
View File
@@ -10,8 +10,6 @@ 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"
@@ -588,35 +586,19 @@ 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) {
tx, err := k.txDecoder(k.ctx.TxBytes())
if err != nil {
// safety check, should be checked when processing the tx
panic(err)
}
// NOTE: tx length checked on AnteHandler
ethTx, ok := tx.GetMsgs()[0].(*types.MsgEthereumTx)
if !ok {
panic("invalid ethereum tx")
}
// 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 = common.HexToHash(ethTx.Hash)
log.BlockHash = common.BytesToHash(k.ctx.HeaderHash())
log.TxIndex = uint(k.GetTxIndexTransient())
log.TxHash = k.GetTxHashTransient()
logs := k.GetTxLogs(key)
logs := k.GetTxLogs(log.TxHash)
log.Index = uint(len(logs))
logs = append(logs, log)
k.SetLogs(key, logs)
k.SetLogs(log.TxHash, logs)
k.Logger(k.ctx).Debug(
"log added",
"tx-hash-tendermint", key.Hex(),
"tx-hash-ethereum", log.TxHash.Hex(),
"log-index", int(log.Index),
)
+3 -10
View File
@@ -10,7 +10,6 @@ import (
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
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"
@@ -483,15 +482,8 @@ func (suite *KeeperTestSuite) TestAddLog() {
msg.From = addr.Hex()
tx := suite.CreateTestTx(msg, privKey)
txBz, err := suite.clientCtx.TxConfig.TxEncoder()(tx)
suite.Require().NoError(err)
tmHash := common.BytesToHash(tmtypes.Tx(txBz).Hash())
msg, _ = tx.GetMsgs()[0].(*evmtypes.MsgEthereumTx)
ethTx := msg.AsTransaction()
txHash := ethTx.Hash()
suite.app.EvmKeeper.WithContext(suite.ctx.WithTxBytes(txBz))
txHash := msg.AsTransaction().Hash()
testCases := []struct {
name string
@@ -501,7 +493,7 @@ func (suite *KeeperTestSuite) TestAddLog() {
}{
{
"tx hash from message",
tmHash,
txHash,
&ethtypes.Log{
Address: addr,
},
@@ -518,6 +510,7 @@ func (suite *KeeperTestSuite) TestAddLog() {
tc.malleate()
prev := suite.app.EvmKeeper.GetTxLogs(tc.hash)
suite.app.EvmKeeper.SetTxHashTransient(tc.hash)
suite.app.EvmKeeper.AddLog(tc.log)
post := suite.app.EvmKeeper.GetTxLogs(tc.hash)
+2
View File
@@ -42,6 +42,7 @@ const (
prefixTransientRefund
prefixTransientAccessListAddress
prefixTransientAccessListSlot
prefixTransientTxHash
)
// KVStore key prefixes
@@ -63,6 +64,7 @@ var (
KeyPrefixTransientRefund = []byte{prefixTransientRefund}
KeyPrefixTransientAccessListAddress = []byte{prefixTransientAccessListAddress}
KeyPrefixTransientAccessListSlot = []byte{prefixTransientAccessListSlot}
KeyPrefixTransientTxHash = []byte{prefixTransientTxHash}
)
// BloomKey defines the store key for a block Bloom