-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add GetTxHash() in evm module (#138)
* evm: add GetTxHash() * optimize code * optimize code * optimize code
- Loading branch information
1 parent
06a2cfe
commit 87a4af6
Showing
5 changed files
with
100 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |