Skip to content

Commit

Permalink
add GetTxHash() in evm module (#138)
Browse files Browse the repository at this point in the history
* evm: add GetTxHash()

* optimize code

* optimize code

* optimize code
  • Loading branch information
RevelationOfTuring authored Jul 2, 2021
1 parent 06a2cfe commit 87a4af6
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 1 deletion.
7 changes: 7 additions & 0 deletions exposed/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package exposed
import (
"github.com/cosmos/cosmos-sdk/crypto/keys"
sdk "github.com/cosmos/cosmos-sdk/types"
ethcmn "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
ethcore "github.com/ethereum/go-ethereum/core/types"
"github.com/okex/exchain-go-sdk/module/evm/types"
gosdktypes "github.com/okex/exchain-go-sdk/types"
rpctypes "github.com/okex/exchain/app/rpc/types"
Expand All @@ -14,6 +16,7 @@ type Evm interface {
gosdktypes.Module
EvmTx
EvmQuery
EvmUtils
web3Getter
}

Expand All @@ -33,6 +36,10 @@ type EvmQuery interface {
QueryStorageAt(contractAddrStr, keyHexStr string) (types.QueryResStorage, error)
}

type EvmUtils interface {
GetTxHash(signedTx *ethcore.Transaction) (ethcmn.Hash, error)
}

type web3Getter interface {
Web3Proxy() Web3Proxy
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/cosmos/cosmos-sdk v0.39.2
github.com/ethereum/go-ethereum v1.9.25
github.com/golang/mock v1.5.0
github.com/okex/exchain v0.18.2
github.com/okex/exchain v0.18.4
github.com/stretchr/testify v1.7.0
github.com/tendermint/tendermint v0.33.9
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,8 @@ github.com/okex/cosmos-sdk v0.39.2-exchain3 h1:1dfu855mhlS1QP9pz3A2bVAZb3KyJ7ldl
github.com/okex/cosmos-sdk v0.39.2-exchain3/go.mod h1:IvlniaZoJAtzILHgcmnfaJ5S125TYJWfJvGqY9zSXb4=
github.com/okex/exchain v0.18.2 h1:zi5wD++knq+9qv9MSfgSUfA+LhZqnAbATe7yvyUHeyk=
github.com/okex/exchain v0.18.2/go.mod h1:6fSdvquHgB8FzmxBbjMXOv0R+pIGD46jdLybaAKDKEM=
github.com/okex/exchain v0.18.4 h1:vfTigpgyH1RUhm9M2AHAAmsgYjMqE97QKO7aKnm/HwI=
github.com/okex/exchain v0.18.4/go.mod h1:FiwxFaU/uKP9tTobAywgr3gc3w1fPnAaCb9PRqcNoII=
github.com/okex/iavl v0.14.3-exchain h1:kwRIwpFD6B8mDDqoaxeUN3Pg2GW0Vr+sA+b86renWcA=
github.com/okex/iavl v0.14.3-exchain/go.mod h1:vHLYxU/zuxBmxxr1v+5Vnd/JzcIsyK17n9P9RDubPVU=
github.com/okex/tendermint v0.33.9-exchain2 h1:aI0lKffQKDHCZGxAjcBEz8Kyifn0oF4fDjcQxQ8rGvs=
Expand Down
35 changes: 35 additions & 0 deletions module/evm/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package evm

import (
authcli "github.com/cosmos/cosmos-sdk/x/auth/client/utils"
ethcmn "github.com/ethereum/go-ethereum/common"
ethcore "github.com/ethereum/go-ethereum/core/types"
evmtypes "github.com/okex/exchain/x/evm/types"
"github.com/tendermint/tendermint/crypto/tmhash"
)

// GetTxHash calculates the tx hash
func (ec evmClient) GetTxHash(signedTx *ethcore.Transaction) (txHash ethcmn.Hash, err error) {
v, r, s := signedTx.RawSignatureValues()
tx := evmtypes.MsgEthereumTx{
Data: evmtypes.TxData{
AccountNonce: signedTx.Nonce(),
Price: signedTx.GasPrice(),
GasLimit: signedTx.Gas(),
Recipient: signedTx.To(),
Amount: signedTx.Value(),
Payload: signedTx.Data(),
V: v,
R: r,
S: s,
},
}

txBytes, err := authcli.GetTxEncoder(ec.GetCodec())(tx)
if err != nil {
return
}

txHash = ethcmn.BytesToHash(tmhash.Sum(txBytes))
return
}
55 changes: 55 additions & 0 deletions module/evm/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package evm

import (
"math/big"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
ethcmn "github.com/ethereum/go-ethereum/common"
ethcore "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/golang/mock/gomock"
"github.com/okex/exchain-go-sdk/mocks"
"github.com/okex/exchain-go-sdk/module/auth"
gosdktypes "github.com/okex/exchain-go-sdk/types"
"github.com/stretchr/testify/require"
)

const (
privKeyHex = "89c81c304704e9890025a5a91898802294658d6e4034a11c6116f4b129ea12d3"

// https://www.oklink.com/okexchain-test/tx/0x8cdadf3465248ae60de749c1e96e178dd303489f2da6b4cffc0ce3f7fb5a9614
expectedTxHash = "0x8cdadf3465248ae60de749c1e96e178dd303489f2da6b4cffc0ce3f7fb5a9614"
)

func TestEvmClient_GetTxHash(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

privateKeyECDSA, err := crypto.HexToECDSA(privKeyHex)
require.NoError(t, err)

// details in okt transfer
chainID := big.NewInt(65)
toAddress := ethcmn.HexToAddress(recAddrEth)
value, err := sdk.NewDecFromStr("1")
require.NoError(t, err)
gasLimit := uint64(21000)
gasPrice, err := sdk.NewDecFromStr("0.000000001")
require.NoError(t, err)
unsignedTx := ethcore.NewTransaction(591, toAddress, value.BigInt(), gasLimit, gasPrice.BigInt(), nil)
signedTx, err := ethcore.SignTx(unsignedTx, ethcore.NewEIP155Signer(chainID), privateKeyECDSA)
require.NoError(t, err)

config, err := gosdktypes.NewClientConfig("testURL", "testchain-1", gosdktypes.BroadcastBlock, "",
200000, 1.1, "0.00000001okt")
require.NoError(t, err)
mockCli := mocks.NewMockClient(t, ctrl, config)
mockCli.RegisterModule(NewEvmClient(mockCli.MockBaseClient), auth.NewAuthClient(mockCli.MockBaseClient))
expectedCdc := mockCli.GetCodec()
mockCli.EXPECT().GetCodec().Return(expectedCdc)

txHash, err := mockCli.Evm().GetTxHash(signedTx)
require.NotNil(t, txHash)
require.Equal(t, expectedTxHash, txHash.String())
}

0 comments on commit 87a4af6

Please sign in to comment.