Skip to content

Commit

Permalink
update mocks and test files
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinssgh committed Feb 28, 2024
1 parent 2a440b8 commit 32e218d
Show file tree
Hide file tree
Showing 9 changed files with 496 additions and 90 deletions.
6 changes: 4 additions & 2 deletions zetaclient/bitcoin/bitcoin_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"sync"
"testing"

"github.com/zeta-chain/zetacore/zetaclient/testutils/mock"

"github.com/btcsuite/btcd/blockchain"
"github.com/btcsuite/btcd/btcjson"
"github.com/btcsuite/btcd/chaincfg"
Expand All @@ -24,8 +26,8 @@ import (
func MockBTCClientMainnet() *BTCChainClient {
return &BTCChainClient{
chain: common.BtcMainnetChain(),
zetaClient: testutils.MockCoreBridge(),
Tss: testutils.NewMockTSSMainnet(),
zetaClient: mock.NewZetaCoreBridge(),
Tss: mock.NewTSSMainnet(),
}
}

Expand Down
47 changes: 32 additions & 15 deletions zetaclient/evm/evm_signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"sync"
"time"

"github.com/zeta-chain/zetacore/zetaclient/testutils/mock"

sdk "github.com/cosmos/cosmos-sdk/types"

clientcommon "github.com/zeta-chain/zetacore/zetaclient/common"
Expand Down Expand Up @@ -92,16 +94,10 @@ func NewEVMSigner(
loggers clientcommon.ClientLogger,
ts *metrics.TelemetryServer,
) (*Signer, error) {
client, err := ethclient.Dial(endpoint)
client, chainID, ethSigner, err := getEVMRPC(endpoint)
if err != nil {
return nil, err
}

chainID, err := client.ChainID(context.TODO())
if err != nil {
return nil, err
}
ethSigner := ethtypes.LatestSignerForChainID(chainID)
connectorABI, err := abi.JSON(strings.NewReader(abiString))
if err != nil {
return nil, err
Expand Down Expand Up @@ -339,7 +335,7 @@ func (signer *Signer) SignCommandTx(txData *TransactionData) (*ethtypes.Transact
return nil, fmt.Errorf("SignCommandTx: unknown command %s", txData.cmd)
}

func setChainAndSender(cctx *types.CrossChainTx, logger zerolog.Logger, txData *TransactionData) bool {
func SetChainAndSender(cctx *types.CrossChainTx, logger zerolog.Logger, txData *TransactionData) bool {
if cctx.CctxStatus.Status == types.CctxStatus_PendingRevert {
txData.to = ethcommon.HexToAddress(cctx.InboundTxParams.Sender)
txData.toChainID = big.NewInt(cctx.InboundTxParams.SenderChainId) //common.GetChainFromChainID(cctx.InboundTxParams.SenderChainId)
Expand All @@ -354,7 +350,7 @@ func setChainAndSender(cctx *types.CrossChainTx, logger zerolog.Logger, txData *
return false
}

func setupGas(
func SetupGas(
cctx *types.CrossChainTx,
logger zerolog.Logger,
client interfaces.EVMRPCClient,
Expand Down Expand Up @@ -392,7 +388,7 @@ func setupGas(
return nil
}

func setTransactionData(
func SetTransactionData(
cctx *types.CrossChainTx,
evmClient *ChainClient,
evmRPC interfaces.EVMRPCClient,
Expand All @@ -407,7 +403,7 @@ func setTransactionData(
txData.srcChainID = big.NewInt(cctx.InboundTxParams.SenderChainId)
txData.asset = ethcommon.HexToAddress(cctx.InboundTxParams.Asset)

skipTx := setChainAndSender(cctx, logger, txData)
skipTx := SetChainAndSender(cctx, logger, txData)
if skipTx {
return true, nil
}
Expand All @@ -429,7 +425,7 @@ func setTransactionData(
}

// Set up gas limit and gas price
err = setupGas(cctx, logger, evmRPC, toChain, txData)
err = SetupGas(cctx, logger, evmRPC, toChain, txData)
if err != nil {
return true, err
}
Expand Down Expand Up @@ -499,7 +495,7 @@ func (signer *Signer) TryProcessOutTx(
// Setup Transaction input
txData := TransactionData{}
txData.height = height
skipTx, err := setTransactionData(cctx, evmClient, signer.client, logger, zetaBridge, &txData)
skipTx, err := SetTransactionData(cctx, evmClient, signer.client, logger, zetaBridge, &txData)
if err != nil {
logger.Error().Msg(err.Error())
return
Expand Down Expand Up @@ -577,10 +573,10 @@ func (signer *Signer) TryProcessOutTx(
}

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

func (signer *Signer) broadcastOutTx(
func (signer *Signer) BroadcastOutTx(
tx *ethtypes.Transaction,
cctx *types.CrossChainTx,
logger zerolog.Logger,
Expand Down Expand Up @@ -770,6 +766,27 @@ func (signer *Signer) SignWhitelistTx(
return tx, nil
}

func getEVMRPC(endpoint string) (interfaces.EVMRPCClient, *big.Int, ethtypes.Signer, error) {
if endpoint == mock.EVMRPCEnabled {
chainID := big.NewInt(common.BscMainnetChain().ChainId)
ethSigner := ethtypes.NewEIP155Signer(chainID)
client := mock.EvmClient{}
return client, chainID, ethSigner, nil
}

client, err := ethclient.Dial(endpoint)
if err != nil {
return nil, nil, nil, err
}

chainID, err := client.ChainID(context.TODO())
if err != nil {
return nil, nil, nil, err
}
ethSigner := ethtypes.LatestSignerForChainID(chainID)
return client, chainID, ethSigner, nil
}

func roundUpToNearestGwei(gasPrice *big.Int) *big.Int {
oneGwei := big.NewInt(1_000_000_000) // 1 Gwei
mod := new(big.Int)
Expand Down
97 changes: 97 additions & 0 deletions zetaclient/evm/evm_signer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package evm

import (
"path"
"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"
crosschaintypes "github.com/zeta-chain/zetacore/x/crosschain/types"
"github.com/zeta-chain/zetacore/zetaclient/common"
"github.com/zeta-chain/zetacore/zetaclient/config"
"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/mock"
)

const (
// Dummy addresses as they are just used as transaction data to be signed
ConnectorAddress = "0x00000000219ab540356cbb839cbe05303d7705fa"
ERC20CustodyAddress = "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"
)

func getNewEvmSigner() (*Signer, error) {
mpiAddress := ethcommon.HexToAddress(ConnectorAddress)
erc20CustodyAddress := ethcommon.HexToAddress(ERC20CustodyAddress)
logger := common.ClientLogger{}
ts := &metrics.TelemetryServer{}
return NewEVMSigner(
corecommon.BscMainnetChain(),
mock.EVMRPCEnabled,
mock.NewTSSMainnet(),
config.GetConnectorABI(),
config.GetERC20CustodyABI(),
mpiAddress,
erc20CustodyAddress,
logger,
ts)
}

func getNewOutTxProcessor() *outtxprocessor.Processor {
logger := zerolog.Logger{}
return outtxprocessor.NewOutTxProcessorManager(logger)
}

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

func TestSigner_TryProcessOutTx(t *testing.T) {
evmSigner, err := getNewEvmSigner()
require.NoError(t, err)

cctx, err := getCCTX()
require.NoError(t, err)

processorManager := getNewOutTxProcessor()

mockChainClient := &ChainClient{
chain: corecommon.BscMainnetChain(),
zetaClient: mock.NewZetaCoreBridge(),
Tss: mock.NewTSSMainnet(),
}
evmSigner.TryProcessOutTx(cctx, processorManager, "123", mockChainClient, mock.NewZetaCoreBridge(), 123)
}

func TestSigner_SignOutboundTx(t *testing.T) {
}

func TestSigner_SignRevertTx(t *testing.T) {
}

func TestSigner_SignWithdrawTx(t *testing.T) {
}

func TestSigner_SignCommandTx(t *testing.T) {
}

func TestSigner_SignERC20WithdrawTx(t *testing.T) {
}

func TestSigner_BroadcastOutTx(t *testing.T) {
}

func TestSigner_SetChainAndSender(t *testing.T) {
}

func TestSigner_SetupGas(t *testing.T) {
}

func TestSigner_SetTransactionData(t *testing.T) {
}
4 changes: 3 additions & 1 deletion zetaclient/evm/inbounds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"path"
"testing"

"github.com/zeta-chain/zetacore/zetaclient/testutils/mock"

ethcommon "github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
Expand All @@ -18,7 +20,7 @@ import (
func MockEVMClient(chain common.Chain) *ChainClient {
return &ChainClient{
chain: chain,
zetaClient: testutils.MockCoreBridge(),
zetaClient: mock.NewZetaCoreBridge(),
}
}

Expand Down
48 changes: 48 additions & 0 deletions zetaclient/testdata/cctx/cctx_56_68270.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"CrossChainTx": [
{
"creator": "",
"index": "0x541b570182950809f9b9077861a0fc7038af9a14ce8af4e151a83adfa308c7a9",
"zeta_fees": "0",
"relayed_message": "",
"cctx_status": {
"status": "PendingOutbound",
"status_message": "",
"lastUpdate_timestamp": "1709145057"
},
"inbound_tx_params": {
"sender": "0xd91b507F2A3e2D4A32d0C86Ac19FEAD2D461008D",
"sender_chain_id": "7000",
"tx_origin": "0xb0C04e07A301927672A8A7a874DB6930576C90B8",
"coin_type": "Gas",
"asset": "",
"amount": "657177295293237048",
"inbound_tx_observed_hash": "0x093f4ca4c1884df0fd9dd59b75979342ded29d3c9b6861644287a2e1417b9a39",
"inbound_tx_observed_external_height": "1960153",
"inbound_tx_ballot_index": "0x541b570182950809f9b9077861a0fc7038af9a14ce8af4e151a83adfa308c7a9",
"inbound_tx_finalized_zeta_height": "0",
"tx_finalization_status": "NotFinalized"
},
"outbound_tx_params": [
{
"receiver": "0xb0C04e07A301927672A8A7a874DB6930576C90B8",
"receiver_chainId": "56",
"coin_type": "Gas",
"amount": "657177295293237048",
"outbound_tx_tss_nonce": "68270",
"outbound_tx_gas_limit": "21000",
"outbound_tx_gas_price": "6000000000",
"outbound_tx_hash": "",
"outbound_tx_ballot_index": "",
"outbound_tx_observed_external_height": "0",
"outbound_tx_gas_used": "0",
"outbound_tx_effective_gas_price": "0",
"outbound_tx_effective_gas_limit": "0",
"tss_pubkey": "zetapub1addwnpepqtadxdyt037h86z60nl98t6zk56mw5zpnm79tsmvspln3hgt5phdc79kvfc",
"tx_finalization_status": "NotFinalized"
}
]
}
],
"totalPending": "1"
}
72 changes: 0 additions & 72 deletions zetaclient/testutils/mock.go

This file was deleted.

Loading

0 comments on commit 32e218d

Please sign in to comment.