From 64832fc9117ebb293ba42912fa785b7cb237ba12 Mon Sep 17 00:00:00 2001 From: YuanXingqiang Date: Wed, 12 Jan 2022 13:25:18 +0800 Subject: [PATCH] Merge PR: add ut related to txhash (#1418) * add ut related to txhash * func UnittestOnlySetMilestoneVenusHeight(height int64) * fix goimports * Update Makefile Co-authored-by: KamiD <44460798+KamiD@users.noreply.github.com> Co-authored-by: zhongqiuwood Co-authored-by: Zhong Qiu <36867992+zhongqiuwood@users.noreply.github.com> --- Makefile | 1 + .../cosmos-sdk/x/auth/client/utils/tx_test.go | 13 +++++-- libs/tendermint/types/milestone.go | 12 ++++++ libs/tendermint/types/tx_test.go | 24 +++++++++++- x/evm/types/utils_test.go | 37 +++++++++++++++++++ 5 files changed, 81 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 68c4070d53..f3ea3b4272 100644 --- a/Makefile +++ b/Makefile @@ -8,6 +8,7 @@ export GO111MODULE=on GithubTop=github.com + Version=v1.1.3 CosmosSDK=v0.39.2 Tendermint=v0.33.9 diff --git a/libs/cosmos-sdk/x/auth/client/utils/tx_test.go b/libs/cosmos-sdk/x/auth/client/utils/tx_test.go index f0835c1586..d677dd1aa4 100644 --- a/libs/cosmos-sdk/x/auth/client/utils/tx_test.go +++ b/libs/cosmos-sdk/x/auth/client/utils/tx_test.go @@ -7,13 +7,11 @@ import ( "os" "testing" - "github.com/stretchr/testify/require" - - "github.com/okex/exchain/libs/tendermint/crypto/ed25519" - "github.com/okex/exchain/libs/cosmos-sdk/codec" sdk "github.com/okex/exchain/libs/cosmos-sdk/types" authtypes "github.com/okex/exchain/libs/cosmos-sdk/x/auth/types" + "github.com/okex/exchain/libs/tendermint/crypto/ed25519" + "github.com/stretchr/testify/require" ) var ( @@ -98,6 +96,13 @@ func TestDefaultTxEncoder(t *testing.T) { compareEncoders(t, defaultEncoder, encoder) } +func TestEthereumTxEncoder(t *testing.T) { + ethereumTxEncoder := authtypes.EthereumTxEncoder(nil) + encoder := GetTxEncoder(nil, WithEthereumTx()) + + compareEncoders(t, ethereumTxEncoder, encoder) +} + func TestConfiguredTxEncoder(t *testing.T) { cdc := makeCodec() diff --git a/libs/tendermint/types/milestone.go b/libs/tendermint/types/milestone.go index 4afd6c8919..d802d81be7 100644 --- a/libs/tendermint/types/milestone.go +++ b/libs/tendermint/types/milestone.go @@ -58,6 +58,13 @@ func HigherThanVenus(height int64) bool { return height >= milestoneVenusHeight } + + +// GetMilestoneVenusHeight returns milestoneVenusHeight +func GetMilestoneVenusHeight() int64 { + return milestoneVenusHeight +} + // 2322600 is mainnet GenesisHeight func IsMainNet() bool { return MILESTONE_GENESIS_HEIGHT == "2322600" @@ -79,3 +86,8 @@ func GetVenusHeight() int64 { func GetMercuryHeight() int64 { return milestoneMercuryHeight } + +// can be used in unit test only +func UnittestOnlySetMilestoneVenusHeight(height int64) { + milestoneVenusHeight = height +} \ No newline at end of file diff --git a/libs/tendermint/types/tx_test.go b/libs/tendermint/types/tx_test.go index e0846bbb6e..3f5fc94714 100644 --- a/libs/tendermint/types/tx_test.go +++ b/libs/tendermint/types/tx_test.go @@ -4,10 +4,11 @@ import ( "bytes" "testing" - "github.com/stretchr/testify/assert" - + "github.com/okex/exchain/libs/tendermint/crypto/etherhash" + "github.com/okex/exchain/libs/tendermint/crypto/tmhash" tmrand "github.com/okex/exchain/libs/tendermint/libs/rand" ctest "github.com/okex/exchain/libs/tendermint/libs/test" + "github.com/stretchr/testify/assert" ) func makeTxs(cnt, size int) Txs { @@ -23,6 +24,25 @@ func randInt(low, high int) int { return low + off } +func TestTx_Hash(t *testing.T) { + tx := Tx("Hello, world!") + oldHeight := GetMilestoneVenusHeight() + defer UnittestOnlySetMilestoneVenusHeight(oldHeight) + for _, c := range []struct { + curHeight int64 + venusHeight int64 + expected []byte + }{ + {999, 0, tmhash.Sum(tx)}, + {999, 1000, tmhash.Sum(tx)}, + {1000, 1000, etherhash.Sum(tx)}, + {1500, 1000, etherhash.Sum(tx)}, + } { + UnittestOnlySetMilestoneVenusHeight(c.venusHeight) + assert.Equal(t, c.expected, tx.Hash(c.curHeight)) + } +} + func TestTxIndex(t *testing.T) { for i := 0; i < 20; i++ { txs := makeTxs(15, 60) diff --git a/x/evm/types/utils_test.go b/x/evm/types/utils_test.go index d50d126dfd..dbffb80f9c 100644 --- a/x/evm/types/utils_test.go +++ b/x/evm/types/utils_test.go @@ -9,8 +9,11 @@ import ( ethcmn "github.com/ethereum/go-ethereum/common" ethtypes "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/rlp" "github.com/okex/exchain/libs/cosmos-sdk/codec" sdk "github.com/okex/exchain/libs/cosmos-sdk/types" + "github.com/okex/exchain/libs/tendermint/global" + "github.com/okex/exchain/libs/tendermint/types" "github.com/stretchr/testify/require" ) @@ -130,6 +133,40 @@ func TestTxDecoder(t *testing.T) { _, err = txDecoder(txbytes[1:]) require.Error(t, err) + + oldHeight := types.GetMilestoneVenusHeight() + defer types.UnittestOnlySetMilestoneVenusHeight(oldHeight) + rlpBytes, err := rlp.EncodeToBytes(&expectedEthMsg) + require.Nil(t, err) + + for _, c := range []struct { + curHeight int64 + venusHeight int64 + enableAminoDecoder bool + enableRLPDecoder bool + }{ + {999, 0, true, false}, + {999, 1000, true, false}, + {1000, 1000, false, true}, + {1500, 1000, false, true}, + } { + types.UnittestOnlySetMilestoneVenusHeight(c.venusHeight) + _, err = TxDecoder(cdc)(txbytes, c.curHeight) + require.Equal(t, c.enableAminoDecoder, err == nil) + _, err = TxDecoder(cdc)(rlpBytes, c.curHeight) + require.Equal(t, c.enableRLPDecoder, err == nil) + + // use global height when height is not pass through parameters. + global.SetGlobalHeight(c.curHeight) + _, err = TxDecoder(cdc)(txbytes) + require.Equal(t, c.enableAminoDecoder, err == nil) + _, err = TxDecoder(cdc)(rlpBytes) + require.Equal(t, c.enableRLPDecoder, err == nil) + } + // only one height parameter is allowed. + tx, err = TxDecoder(cdc)(txbytes, 0, 999) + require.NotNil(t, err) + } func TestEthLogAmino(t *testing.T) {