Skip to content

Commit

Permalink
Merge PR: add ut related to txhash (#1418)
Browse files Browse the repository at this point in the history
* add ut related to txhash

* func UnittestOnlySetMilestoneVenusHeight(height int64)

* fix goimports

* Update Makefile

Co-authored-by: KamiD <[email protected]>
Co-authored-by: zhongqiuwood <[email protected]>
Co-authored-by: Zhong Qiu <[email protected]>
  • Loading branch information
4 people authored Jan 12, 2022
1 parent e6fecd1 commit 64832fc
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 6 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export GO111MODULE=on
GithubTop=github.com



Version=v1.1.3
CosmosSDK=v0.39.2
Tendermint=v0.33.9
Expand Down
13 changes: 9 additions & 4 deletions libs/cosmos-sdk/x/auth/client/utils/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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()

Expand Down
12 changes: 12 additions & 0 deletions libs/tendermint/types/milestone.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
24 changes: 22 additions & 2 deletions libs/tendermint/types/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
37 changes: 37 additions & 0 deletions x/evm/types/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 64832fc

Please sign in to comment.