Skip to content

Commit

Permalink
complete evm_signer test cases and remove hardcoded private key
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinssgh committed Feb 29, 2024
1 parent 53e87b9 commit 95b7f2f
Show file tree
Hide file tree
Showing 3 changed files with 254 additions and 11 deletions.
10 changes: 10 additions & 0 deletions zetaclient/evm/evm_signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -766,9 +766,19 @@ func (signer *Signer) SignWhitelistTx(
return tx, nil
}

// Exported for unit tests

func (signer *Signer) GetReportedTxList() *map[string]bool {
return &signer.outTxHashBeingReported
}
func (signer *Signer) EvmClient() interfaces.EVMRPCClient {
return signer.client
}
func (signer *Signer) EvmSigner() ethtypes.Signer {
return signer.ethSigner
}

// ________________________

func getEVMRPC(endpoint string) (interfaces.EVMRPCClient, *big.Int, ethtypes.Signer, error) {
if endpoint == mock.EVMRPCEnabled {
Expand Down
238 changes: 237 additions & 1 deletion zetaclient/evm/evm_signer_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package evm

import (
sdktypes "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/crypto"
"math/big"
"path"
"testing"

Expand Down Expand Up @@ -71,7 +74,7 @@ func TestSigner_TryProcessOutTx(t *testing.T) {

evmSigner.TryProcessOutTx(cctx, processorManager, "123", mockChainClient, mock.NewZetaCoreBridge(), 123)

//Check if cctx was processed
//Check if cctx was signed and broadcasted
list := evmSigner.GetReportedTxList()
found := false
for range *list {
Expand All @@ -81,28 +84,261 @@ func TestSigner_TryProcessOutTx(t *testing.T) {
}

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

// Setup txData struct
txData := TransactionData{}
cctx, err := getCCTX()
require.NoError(t, err)
mockChainClient, err := getNewEvmChainClient()
require.NoError(t, err)
skip, err := SetTransactionData(cctx, mockChainClient, evmSigner.EvmClient(), zerolog.Logger{}, mock.NewZetaCoreBridge(), &txData)
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)
require.NoError(t, err)

// Verify Signature
tss := mock.NewTSSMainnet()
_, r, s := tx.RawSignatureValues()
signature := append(r.Bytes(), s.Bytes()...)
hash := evmSigner.EvmSigner().Hash(tx)

verified := crypto.VerifySignature(tss.Pubkey(), hash.Bytes(), signature)
require.True(t, verified)
})
}

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

// Setup txData struct
txData := TransactionData{}
cctx, err := getCCTX()
require.NoError(t, err)
mockChainClient, err := getNewEvmChainClient()
require.NoError(t, err)
skip, err := SetTransactionData(cctx, mockChainClient, evmSigner.EvmClient(), zerolog.Logger{}, mock.NewZetaCoreBridge(), &txData)
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)
require.NoError(t, err)

// Verify Signature
tss := mock.NewTSSMainnet()
_, r, s := tx.RawSignatureValues()
signature := append(r.Bytes(), s.Bytes()...)
hash := evmSigner.EvmSigner().Hash(tx)

verified := crypto.VerifySignature(tss.Pubkey(), hash.Bytes(), signature)
require.True(t, verified)
})
}

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

// Setup txData struct
txData := TransactionData{}
cctx, err := getCCTX()
require.NoError(t, err)
mockChainClient, err := getNewEvmChainClient()
require.NoError(t, err)
skip, err := SetTransactionData(cctx, mockChainClient, evmSigner.EvmClient(), zerolog.Logger{}, mock.NewZetaCoreBridge(), &txData)
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)
require.NoError(t, err)

// Verify Signature
tss := mock.NewTSSMainnet()
_, r, s := tx.RawSignatureValues()
signature := append(r.Bytes(), s.Bytes()...)
hash := evmSigner.EvmSigner().Hash(tx)

verified := crypto.VerifySignature(tss.Pubkey(), hash.Bytes(), signature)
require.True(t, verified)
})
}

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

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

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

// Verify Signature
tss := mock.NewTSSMainnet()
_, r, s := tx.RawSignatureValues()
signature := append(r.Bytes(), s.Bytes()...)
hash := evmSigner.EvmSigner().Hash(tx)

verified := crypto.VerifySignature(tss.Pubkey(), hash.Bytes(), signature)
require.True(t, verified)
})
}

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

// Setup txData struct
txData := TransactionData{}
cctx, err := getCCTX()
require.NoError(t, err)
mockChainClient, err := getNewEvmChainClient()
require.NoError(t, err)
skip, err := SetTransactionData(cctx, mockChainClient, evmSigner.EvmClient(), zerolog.Logger{}, mock.NewZetaCoreBridge(), &txData)
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)
require.NoError(t, err)

// Verify Signature
tss := mock.NewTSSMainnet()
_, r, s := tx.RawSignatureValues()
signature := append(r.Bytes(), s.Bytes()...)
hash := evmSigner.EvmSigner().Hash(tx)

verified := crypto.VerifySignature(tss.Pubkey(), hash.Bytes(), signature)
require.True(t, verified)
})
}

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

// Setup txData struct
txData := TransactionData{}
cctx, err := getCCTX()
require.NoError(t, err)
mockChainClient, err := getNewEvmChainClient()
require.NoError(t, err)
skip, err := SetTransactionData(cctx, mockChainClient, evmSigner.EvmClient(), zerolog.Logger{}, mock.NewZetaCoreBridge(), &txData)
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)
require.NoError(t, err)

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

//Check if cctx was signed and broadcasted
list := evmSigner.GetReportedTxList()
found := false
for range *list {
found = true
}
require.True(t, found)
})
}

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

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

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

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 := SetChainAndSender(cctx, logger, txData)

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 := SetChainAndSender(cctx, logger, txData)
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 := &TransactionData{}
logger := zerolog.Logger{}

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

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

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

// Setup txData struct
txData := TransactionData{}
cctx, err := getCCTX()
require.NoError(t, err)
mockChainClient, err := getNewEvmChainClient()
require.NoError(t, err)
skip, err := SetTransactionData(cctx, mockChainClient, evmSigner.EvmClient(), zerolog.Logger{}, mock.NewZetaCoreBridge(), &txData)
require.False(t, skip)
require.NoError(t, err)
}
17 changes: 7 additions & 10 deletions zetaclient/testutils/mock/mock_tss_signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ import (
"github.com/zeta-chain/zetacore/zetaclient/testutils"
)

const (
TestPrivateKey = "401c22d98e2b63138afba629f70bc7dea9f299f973b257b484d1ae4c73bf54e4"
)
var TestPrivateKey *ecdsa.PrivateKey

var _ interfaces.TSSSigner = (*TSS)(nil)

func init() {
TestPrivateKey, _ = crypto.GenerateKey()
}

// TSS is a mock of TSS signer for testing
type TSS struct {
evmAddress string
Expand All @@ -41,11 +43,7 @@ func NewTSSAthens3() *TSS {

// Sign uses test key unrelated to any tss key in production
func (s *TSS) Sign(data []byte, _ uint64, _ uint64, _ *common.Chain, _ string) ([65]byte, error) {
privateKey, err := crypto.HexToECDSA(TestPrivateKey)
if err != nil {
return [65]byte{}, err
}
signature, err := crypto.Sign(data, privateKey)
signature, err := crypto.Sign(data, TestPrivateKey)
if err != nil {
return [65]byte{}, err
}
Expand All @@ -57,8 +55,7 @@ func (s *TSS) Sign(data []byte, _ uint64, _ uint64, _ *common.Chain, _ string) (

// Pubkey uses the hardcoded private test key to generate the public key in bytes
func (s *TSS) Pubkey() []byte {
privateKey, _ := crypto.HexToECDSA(TestPrivateKey)
publicKey := privateKey.Public()
publicKey := TestPrivateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
fmt.Println("error casting public key to ECDSA")
Expand Down

0 comments on commit 95b7f2f

Please sign in to comment.