Skip to content

Commit

Permalink
added test cases and converted SetTransactionData into constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinssgh committed Mar 6, 2024
1 parent f8f83c5 commit d1f1430
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 52 deletions.
22 changes: 10 additions & 12 deletions zetaclient/evm/evm_signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,7 @@ func (signer *Signer) TryProcessOutTx(
}

// Setup Transaction input
txData := OutBoundTransactionData{}
txData.height = height
skipTx, err := txData.SetTransactionData(cctx, evmClient, signer.client, logger)
txData, skipTx, err := NewOutBoundTransactionData(cctx, evmClient, signer.client, logger, height)
if err != nil {
logger.Err(err).Msg("error setting up transaction input fields")
return
Expand Down Expand Up @@ -355,7 +353,7 @@ func (signer *Signer) TryProcessOutTx(
// params field is used to pass input parameters for command requests, currently it is used to pass the ERC20
// contract address when a whitelist command is requested
params := msg[1]
tx, err = signer.SignCommandTx(&txData, cmd, params)
tx, err = signer.SignCommandTx(txData, cmd, params)
if err != nil {
logger.Warn().Err(err).Msg(SignerErrorMsg(cctx))
return
Expand All @@ -364,13 +362,13 @@ func (signer *Signer) TryProcessOutTx(
switch cctx.GetCurrentOutTxParam().CoinType {
case common.CoinType_Gas:
logger.Info().Msgf("SignWithdrawTx: %d => %s, nonce %d, gasPrice %d", cctx.InboundTxParams.SenderChainId, toChain, cctx.GetCurrentOutTxParam().OutboundTxTssNonce, txData.gasPrice)
tx, err = signer.SignWithdrawTx(&txData)
tx, err = signer.SignWithdrawTx(txData)
case common.CoinType_ERC20:
logger.Info().Msgf("SignERC20WithdrawTx: %d => %s, nonce %d, gasPrice %d", cctx.InboundTxParams.SenderChainId, toChain, cctx.GetCurrentOutTxParam().OutboundTxTssNonce, txData.gasPrice)
tx, err = signer.SignERC20WithdrawTx(&txData)
tx, err = signer.SignERC20WithdrawTx(txData)
case common.CoinType_Zeta:
logger.Info().Msgf("SignOutboundTx: %d => %s, nonce %d, gasPrice %d", cctx.InboundTxParams.SenderChainId, toChain, cctx.GetCurrentOutTxParam().OutboundTxTssNonce, txData.gasPrice)
tx, err = signer.SignOutboundTx(&txData)
tx, err = signer.SignOutboundTx(txData)
}
if err != nil {
logger.Warn().Err(err).Msg(SignerErrorMsg(cctx))
Expand All @@ -380,10 +378,10 @@ func (signer *Signer) TryProcessOutTx(
switch cctx.GetCurrentOutTxParam().CoinType {
case common.CoinType_Gas:
logger.Info().Msgf("SignWithdrawTx: %d => %s, nonce %d, gasPrice %d", cctx.InboundTxParams.SenderChainId, toChain, cctx.GetCurrentOutTxParam().OutboundTxTssNonce, txData.gasPrice)
tx, err = signer.SignWithdrawTx(&txData)
tx, err = signer.SignWithdrawTx(txData)
case common.CoinType_ERC20:
logger.Info().Msgf("SignERC20WithdrawTx: %d => %s, nonce %d, gasPrice %d", cctx.InboundTxParams.SenderChainId, toChain, cctx.GetCurrentOutTxParam().OutboundTxTssNonce, txData.gasPrice)
tx, err = signer.SignERC20WithdrawTx(&txData)
tx, err = signer.SignERC20WithdrawTx(txData)
}
if err != nil {
logger.Warn().Err(err).Msg(SignerErrorMsg(cctx))
Expand All @@ -394,14 +392,14 @@ func (signer *Signer) TryProcessOutTx(
txData.srcChainID = big.NewInt(cctx.OutboundTxParams[0].ReceiverChainId)
txData.toChainID = big.NewInt(cctx.GetCurrentOutTxParam().ReceiverChainId)

tx, err = signer.SignRevertTx(&txData)
tx, err = signer.SignRevertTx(txData)
if err != nil {
logger.Warn().Err(err).Msg(SignerErrorMsg(cctx))
return
}
} else if cctx.CctxStatus.Status == types.CctxStatus_PendingOutbound {
logger.Info().Msgf("SignOutboundTx: %d => %s, nonce %d, gasPrice %d", cctx.InboundTxParams.SenderChainId, toChain, cctx.GetCurrentOutTxParam().OutboundTxTssNonce, txData.gasPrice)
tx, err = signer.SignOutboundTx(&txData)
tx, err = signer.SignOutboundTx(txData)
if err != nil {
logger.Warn().Err(err).Msg(SignerErrorMsg(cctx))
return
Expand All @@ -411,7 +409,7 @@ func (signer *Signer) TryProcessOutTx(
logger.Info().Msgf("Key-sign success: %d => %s, nonce %d", cctx.InboundTxParams.SenderChainId, toChain, cctx.GetCurrentOutTxParam().OutboundTxTssNonce)

// Broadcast Signed Tx
signer.BroadcastOutTx(tx, cctx, logger, myID, zetaBridge, &txData)
signer.BroadcastOutTx(tx, cctx, logger, myID, zetaBridge, txData)
}

// BroadcastOutTx signed transaction through evm rpc client
Expand Down
41 changes: 21 additions & 20 deletions zetaclient/evm/evm_signer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ func getCCTX() (*types.CrossChainTx, error) {
return &cctx, err
}

func getInvalidCCTX() (*types.CrossChainTx, error) {
var cctx crosschaintypes.CrossChainTx
err := testutils.LoadObjectFromJSONFile(&cctx, path.Join("../", testutils.TestDataPathCctx, "cctx_56_68270_invalidChainID.json"))
return &cctx, err
}

func TestSigner_TryProcessOutTx(t *testing.T) {
evmSigner, err := getNewEvmSigner()
require.NoError(t, err)
Expand All @@ -92,18 +98,18 @@ func TestSigner_SignOutboundTx(t *testing.T) {
require.NoError(t, err)

// Setup txData struct
txData := OutBoundTransactionData{}

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

t.Run("SignOutboundTx - should successfully sign", func(t *testing.T) {
// Call SignOutboundTx
tx, err := evmSigner.SignOutboundTx(&txData)
tx, err := evmSigner.SignOutboundTx(txData)
require.NoError(t, err)

// Verify Signature
Expand All @@ -123,18 +129,17 @@ func TestSigner_SignRevertTx(t *testing.T) {
require.NoError(t, err)

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

t.Run("SignRevertTx - should successfully sign", func(t *testing.T) {
// Call SignRevertTx
tx, err := evmSigner.SignRevertTx(&txData)
tx, err := evmSigner.SignRevertTx(txData)
require.NoError(t, err)

// Verify Signature
Expand All @@ -154,18 +159,17 @@ func TestSigner_SignWithdrawTx(t *testing.T) {
require.NoError(t, err)

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

t.Run("SignWithdrawTx - should successfully sign", func(t *testing.T) {
// Call SignWithdrawTx
tx, err := evmSigner.SignWithdrawTx(&txData)
tx, err := evmSigner.SignWithdrawTx(txData)
require.NoError(t, err)

// Verify Signature
Expand All @@ -185,20 +189,19 @@ func TestSigner_SignCommandTx(t *testing.T) {
require.NoError(t, err)

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

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

// Verify Signature
Expand All @@ -214,7 +217,7 @@ func TestSigner_SignCommandTx(t *testing.T) {
t.Run("SignCommandTx CmdMigrateTssFunds", func(t *testing.T) {
cmd := corecommon.CmdMigrateTssFunds
// Call SignCommandTx
tx, err := evmSigner.SignCommandTx(&txData, cmd, "")
tx, err := evmSigner.SignCommandTx(txData, cmd, "")
require.NoError(t, err)

// Verify Signature
Expand All @@ -234,18 +237,17 @@ func TestSigner_SignERC20WithdrawTx(t *testing.T) {
require.NoError(t, err)

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

t.Run("SignERC20WithdrawTx - should successfully sign", func(t *testing.T) {
// Call SignERC20WithdrawTx
tx, err := evmSigner.SignERC20WithdrawTx(&txData)
tx, err := evmSigner.SignERC20WithdrawTx(txData)
require.NoError(t, err)

// Verify Signature
Expand All @@ -265,21 +267,20 @@ func TestSigner_BroadcastOutTx(t *testing.T) {
require.NoError(t, err)

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

t.Run("BroadcastOutTx - should successfully broadcast", func(t *testing.T) {
// Call SignERC20WithdrawTx
tx, err := evmSigner.SignERC20WithdrawTx(&txData)
tx, err := evmSigner.SignERC20WithdrawTx(txData)
require.NoError(t, err)

evmSigner.BroadcastOutTx(tx, cctx, zerolog.Logger{}, sdktypes.AccAddress{}, stub.NewZetaCoreBridge(), &txData)
evmSigner.BroadcastOutTx(tx, cctx, zerolog.Logger{}, sdktypes.AccAddress{}, stub.NewZetaCoreBridge(), txData)

//Check if cctx was signed and broadcasted
list := evmSigner.GetReportedTxList()
Expand Down
26 changes: 14 additions & 12 deletions zetaclient/evm/outbound_transaction_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,59 +100,61 @@ func (txData *OutBoundTransactionData) SetupGas(
return nil
}

// SetTransactionData populates transaction input fields parsed from the cctx and other parameters
// NewOutBoundTransactionData populates transaction input fields parsed from the cctx and other parameters
// returns
// bool (skipTx) - if the transaction doesn't qualify to be processed the function will return true, meaning that this
//
// cctx will be skipped and false otherwise.
//
// error
func (txData *OutBoundTransactionData) SetTransactionData(
func NewOutBoundTransactionData(
cctx *types.CrossChainTx,
evmClient *ChainClient,
evmRPC interfaces.EVMRPCClient,
logger zerolog.Logger,
) (bool, error) {

height uint64,
) (*OutBoundTransactionData, bool, error) {
txData := OutBoundTransactionData{}
txData.outboundParams = cctx.GetCurrentOutTxParam()
txData.amount = cctx.GetCurrentOutTxParam().Amount.BigInt()
txData.nonce = cctx.GetCurrentOutTxParam().OutboundTxTssNonce
txData.sender = ethcommon.HexToAddress(cctx.InboundTxParams.Sender)
txData.srcChainID = big.NewInt(cctx.InboundTxParams.SenderChainId)
txData.asset = ethcommon.HexToAddress(cctx.InboundTxParams.Asset)
txData.height = height

skipTx := txData.SetChainAndSender(cctx, logger)
if skipTx {
return true, nil
return nil, true, nil
}

toChain := common.GetChainFromChainID(txData.toChainID.Int64())
if toChain == nil {
return true, fmt.Errorf("unknown chain: %d", txData.toChainID.Int64())
return nil, true, fmt.Errorf("unknown chain: %d", txData.toChainID.Int64())
}

// Get nonce, Early return if the cctx is already processed
nonce := cctx.GetCurrentOutTxParam().OutboundTxTssNonce
included, confirmed, err := evmClient.IsSendOutTxProcessed(cctx, logger)
if err != nil {
return true, errors.New("IsSendOutTxProcessed failed")
return nil, true, errors.New("IsSendOutTxProcessed failed")
}
if included || confirmed {
logger.Info().Msgf("CCTX already processed; exit signer")
return true, nil
return nil, true, nil
}

// Set up gas limit and gas price
err = txData.SetupGas(cctx, logger, evmRPC, toChain)
if err != nil {
return true, err
return nil, true, err
}

// Get sendHash
logger.Info().Msgf("chain %s minting %d to %s, nonce %d, finalized zeta bn %d", toChain, cctx.InboundTxParams.Amount, txData.to.Hex(), nonce, cctx.InboundTxParams.InboundTxFinalizedZetaHeight)
sendHash, err := hex.DecodeString(cctx.Index[2:]) // remove the leading 0x
if err != nil || len(sendHash) != 32 {
return true, fmt.Errorf("decode CCTX %s error", cctx.Index)
return nil, true, fmt.Errorf("decode CCTX %s error", cctx.Index)
}
copy(txData.sendHash[:32], sendHash[:32])

Expand All @@ -163,7 +165,7 @@ func (txData *OutBoundTransactionData) SetTransactionData(
logger.Info().Msgf("replace pending outTx %s nonce %d using gas price %d", pendingTx.Hash().Hex(), nonce, txData.gasPrice)
} else {
logger.Info().Msgf("please wait for pending outTx %s nonce %d to be included", pendingTx.Hash().Hex(), nonce)
return true, nil
return nil, true, nil
}
}

Expand All @@ -175,5 +177,5 @@ func (txData *OutBoundTransactionData) SetTransactionData(
}
}

return false, nil
return &txData, false, nil
}
43 changes: 35 additions & 8 deletions zetaclient/evm/outbound_transaction_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,45 @@ func TestSigner_SetupGas(t *testing.T) {
})
}

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

// Setup txData struct
txData := OutBoundTransactionData{}
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)

t.Run("NewOutBoundTransactionData success", func(t *testing.T) {
cctx, err := getCCTX()
require.NoError(t, err)
_, skip, err := NewOutBoundTransactionData(cctx, mockChainClient, evmSigner.EvmClient(), zerolog.Logger{}, 123)
require.False(t, skip)
require.NoError(t, err)
})

t.Run("NewOutBoundTransactionData skip", func(t *testing.T) {
cctx, err := getCCTX()
require.NoError(t, err)
cctx.CctxStatus.Status = types.CctxStatus_Aborted
_, skip, err := NewOutBoundTransactionData(cctx, mockChainClient, evmSigner.EvmClient(), zerolog.Logger{}, 123)
require.NoError(t, err)
require.True(t, skip)
})

t.Run("NewOutBoundTransactionData unknown chain", func(t *testing.T) {
cctx, err := getInvalidCCTX()
require.NoError(t, err)
_, skip, err := NewOutBoundTransactionData(cctx, mockChainClient, evmSigner.EvmClient(), zerolog.Logger{}, 123)
require.ErrorContains(t, err, "unknown chain")
require.True(t, skip)
})

t.Run("NewOutBoundTransactionData setup gas error", func(t *testing.T) {
cctx, err := getCCTX()
require.NoError(t, err)
cctx.GetCurrentOutTxParam().OutboundTxGasPrice = "invalidGasPrice"
_, skip, err := NewOutBoundTransactionData(cctx, mockChainClient, evmSigner.EvmClient(), zerolog.Logger{}, 123)
require.True(t, skip)
require.ErrorContains(t, err, "cannot convert gas price")
})
}
Loading

0 comments on commit d1f1430

Please sign in to comment.