From 8ab5ef0a39c7f1194a8ce6a0768cf692b31ca927 Mon Sep 17 00:00:00 2001 From: kevinssgh Date: Mon, 4 Mar 2024 16:18:55 -0500 Subject: [PATCH] fix unit tests --- zetaclient/evm/base_transaction_data_test.go | 85 +++++++++++++++++ zetaclient/evm/evm_signer_test.go | 96 ++------------------ 2 files changed, 94 insertions(+), 87 deletions(-) create mode 100644 zetaclient/evm/base_transaction_data_test.go diff --git a/zetaclient/evm/base_transaction_data_test.go b/zetaclient/evm/base_transaction_data_test.go new file mode 100644 index 0000000000..1d3d18dde3 --- /dev/null +++ b/zetaclient/evm/base_transaction_data_test.go @@ -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) +} diff --git a/zetaclient/evm/evm_signer_test.go b/zetaclient/evm/evm_signer_test.go index c74c1602b1..5a6c970813 100644 --- a/zetaclient/evm/evm_signer_test.go +++ b/zetaclient/evm/evm_signer_test.go @@ -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 ( @@ -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 @@ -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 @@ -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) -}