Skip to content

Commit

Permalink
fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinssgh committed Mar 4, 2024
1 parent e69928e commit 8ab5ef0
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 87 deletions.
85 changes: 85 additions & 0 deletions zetaclient/evm/base_transaction_data_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package evm

import (
"math/big"
"testing"

ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/rs/zerolog"
"github.com/stretchr/testify/require"
corecommon "github.com/zeta-chain/zetacore/common"
"github.com/zeta-chain/zetacore/x/crosschain/types"
)

func TestSigner_SetChainAndSender(t *testing.T) {
// setup inputs
cctx, err := getCCTX()
require.NoError(t, err)

txData := &BaseTransactionData{}
logger := zerolog.Logger{}

t.Run("SetChainAndSender PendingRevert", func(t *testing.T) {
cctx.CctxStatus.Status = types.CctxStatus_PendingRevert
skipTx := txData.SetChainAndSender(cctx, logger)

require.False(t, skipTx)
require.Equal(t, ethcommon.HexToAddress(cctx.InboundTxParams.Sender), txData.to)
require.Equal(t, big.NewInt(cctx.InboundTxParams.SenderChainId), txData.toChainID)
})

t.Run("SetChainAndSender PendingOutBound", func(t *testing.T) {
cctx.CctxStatus.Status = types.CctxStatus_PendingOutbound
skipTx := txData.SetChainAndSender(cctx, logger)

require.False(t, skipTx)
require.Equal(t, ethcommon.HexToAddress(cctx.GetCurrentOutTxParam().Receiver), txData.to)
require.Equal(t, big.NewInt(cctx.GetCurrentOutTxParam().ReceiverChainId), txData.toChainID)
})

t.Run("SetChainAndSender Should skip cctx", func(t *testing.T) {
cctx.CctxStatus.Status = types.CctxStatus_PendingInbound
skipTx := txData.SetChainAndSender(cctx, logger)
require.True(t, skipTx)
})
}

func TestSigner_SetupGas(t *testing.T) {
cctx, err := getCCTX()
require.NoError(t, err)

evmSigner, err := getNewEvmSigner()
require.NoError(t, err)

txData := &BaseTransactionData{}
logger := zerolog.Logger{}

t.Run("SetupGas_success", func(t *testing.T) {
chain := corecommon.BscMainnetChain()
err := txData.SetupGas(cctx, logger, evmSigner.EvmClient(), &chain)
require.NoError(t, err)
})

t.Run("SetupGas_error", func(t *testing.T) {
cctx.GetCurrentOutTxParam().OutboundTxGasPrice = "invalidGasPrice"
chain := corecommon.BtcMainnetChain()
err := txData.SetupGas(cctx, logger, evmSigner.EvmClient(), &chain)
require.Error(t, err)
})
}

func TestSigner_SetTransactionData(t *testing.T) {
// Setup evm signer
evmSigner, err := getNewEvmSigner()
require.NoError(t, err)

// Setup txData struct
txData := BaseTransactionData{}
cctx, err := getCCTX()
require.NoError(t, err)
mockChainClient, err := getNewEvmChainClient()
require.NoError(t, err)
skip, err := txData.SetTransactionData(cctx, mockChainClient, evmSigner.EvmClient(), zerolog.Logger{})
require.False(t, skip)
require.NoError(t, err)
}
96 changes: 9 additions & 87 deletions zetaclient/evm/evm_signer_test.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
package evm

import (
"math/big"
"path"
"testing"

"github.com/zeta-chain/zetacore/testutil/sample"
"github.com/zeta-chain/zetacore/zetaclient/testutils/stub"

appcontext "github.com/zeta-chain/zetacore/zetaclient/app_context"
corecontext "github.com/zeta-chain/zetacore/zetaclient/core_context"

sdktypes "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/crypto"

ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/rs/zerolog"
"github.com/stretchr/testify/require"
corecommon "github.com/zeta-chain/zetacore/common"
"github.com/zeta-chain/zetacore/testutil/sample"
"github.com/zeta-chain/zetacore/x/crosschain/types"
crosschaintypes "github.com/zeta-chain/zetacore/x/crosschain/types"
appcontext "github.com/zeta-chain/zetacore/zetaclient/app_context"
"github.com/zeta-chain/zetacore/zetaclient/common"
"github.com/zeta-chain/zetacore/zetaclient/config"
corecontext "github.com/zeta-chain/zetacore/zetaclient/core_context"
"github.com/zeta-chain/zetacore/zetaclient/metrics"
"github.com/zeta-chain/zetacore/zetaclient/outtxprocessor"
"github.com/zeta-chain/zetacore/zetaclient/testutils"
"github.com/zeta-chain/zetacore/zetaclient/testutils/stub"
)

var (
Expand Down Expand Up @@ -204,10 +199,10 @@ func TestSigner_SignCommandTx(t *testing.T) {
require.NoError(t, err)

t.Run("SignCommandTx CmdWhitelistERC20", func(t *testing.T) {
txData.cmd = corecommon.CmdWhitelistERC20
txData.params = ConnectorAddress.Hex()
cmd := corecommon.CmdWhitelistERC20
params := ConnectorAddress.Hex()
// Call SignCommandTx
tx, err := evmSigner.SignCommandTx(&txData)
tx, err := evmSigner.SignCommandTx(&txData, cmd, params)
require.NoError(t, err)

// Verify Signature
Expand All @@ -221,9 +216,9 @@ func TestSigner_SignCommandTx(t *testing.T) {
})

t.Run("SignCommandTx CmdMigrateTssFunds", func(t *testing.T) {
txData.cmd = corecommon.CmdMigrateTssFunds
cmd := corecommon.CmdMigrateTssFunds
// Call SignCommandTx
tx, err := evmSigner.SignCommandTx(&txData)
tx, err := evmSigner.SignCommandTx(&txData, cmd, "")
require.NoError(t, err)

// Verify Signature
Expand Down Expand Up @@ -299,76 +294,3 @@ func TestSigner_BroadcastOutTx(t *testing.T) {
require.True(t, found)
})
}

func TestSigner_SetChainAndSender(t *testing.T) {
// setup inputs
cctx, err := getCCTX()
require.NoError(t, err)

txData := &BaseTransactionData{}
logger := zerolog.Logger{}

t.Run("SetChainAndSender PendingRevert", func(t *testing.T) {
cctx.CctxStatus.Status = types.CctxStatus_PendingRevert
skipTx := txData.SetChainAndSender(cctx, logger)

require.False(t, skipTx)
require.Equal(t, ethcommon.HexToAddress(cctx.InboundTxParams.Sender), txData.to)
require.Equal(t, big.NewInt(cctx.InboundTxParams.SenderChainId), txData.toChainID)
})

t.Run("SetChainAndSender PendingOutBound", func(t *testing.T) {
cctx.CctxStatus.Status = types.CctxStatus_PendingOutbound
skipTx := txData.SetChainAndSender(cctx, logger)

require.False(t, skipTx)
require.Equal(t, ethcommon.HexToAddress(cctx.GetCurrentOutTxParam().Receiver), txData.to)
require.Equal(t, big.NewInt(cctx.GetCurrentOutTxParam().ReceiverChainId), txData.toChainID)
})

t.Run("SetChainAndSender Should skip cctx", func(t *testing.T) {
cctx.CctxStatus.Status = types.CctxStatus_PendingInbound
skipTx := txData.SetChainAndSender(cctx, logger)
require.True(t, skipTx)
})
}

func TestSigner_SetupGas(t *testing.T) {
cctx, err := getCCTX()
require.NoError(t, err)

evmSigner, err := getNewEvmSigner()
require.NoError(t, err)

txData := &BaseTransactionData{}
logger := zerolog.Logger{}

t.Run("SetupGas_success", func(t *testing.T) {
chain := corecommon.BscMainnetChain()
err := txData.SetupGas(cctx, logger, evmSigner.EvmClient(), &chain)
require.NoError(t, err)
})

t.Run("SetupGas_error", func(t *testing.T) {
cctx.GetCurrentOutTxParam().OutboundTxGasPrice = "invalidGasPrice"
chain := corecommon.BtcMainnetChain()
err := txData.SetupGas(cctx, logger, evmSigner.EvmClient(), &chain)
require.Error(t, err)
})
}

func TestSigner_SetTransactionData(t *testing.T) {
// Setup evm signer
evmSigner, err := getNewEvmSigner()
require.NoError(t, err)

// Setup txData struct
txData := BaseTransactionData{}
cctx, err := getCCTX()
require.NoError(t, err)
mockChainClient, err := getNewEvmChainClient()
require.NoError(t, err)
skip, err := txData.SetTransactionData(cctx, mockChainClient, evmSigner.EvmClient(), zerolog.Logger{})
require.False(t, skip)
require.NoError(t, err)
}

0 comments on commit 8ab5ef0

Please sign in to comment.