Skip to content

Commit

Permalink
a bit refactor and more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ws4charlie committed Mar 15, 2024
1 parent 03581b2 commit 1321dcc
Show file tree
Hide file tree
Showing 12 changed files with 340 additions and 106 deletions.
4 changes: 2 additions & 2 deletions cmd/zetaclientd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ func CreateChainClientMap(
clientMap[evmConfig.Chain.ChainId] = co
}
// BTC client
btcChain, _, enabled := appContext.GetBTCChainAndConfig()
btcChain, btcConfig, enabled := appContext.GetBTCChainAndConfig()
if enabled {
co, err := bitcoin.NewBitcoinClient(appContext, btcChain, bridge, tss, dbpath, loggers, ts)
co, err := bitcoin.NewBitcoinClient(appContext, btcChain, bridge, tss, dbpath, loggers, btcConfig, ts)
if err != nil {
loggers.Std.Error().Err(err).Msgf("NewBitcoinClient error for chain %s", btcChain.String())

Expand Down
2 changes: 1 addition & 1 deletion zetaclient/bitcoin/bitcoin_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ func NewBitcoinClient(
tss interfaces.TSSSigner,
dbpath string,
loggers clientcommon.ClientLogger,
btcCfg config.BTCConfig,
ts *metrics.TelemetryServer,
) (*BTCChainClient, error) {
ob := BTCChainClient{
Expand Down Expand Up @@ -174,7 +175,6 @@ func NewBitcoinClient(
}
ob.params = *chainParams
// initialize the Client
btcCfg := appcontext.Config().BitcoinConfig
ob.logger.ChainLogger.Info().Msgf("Chain %s endpoint %s", ob.chain.String(), btcCfg.RPCHost)
connCfg := &rpcclient.ConnConfig{
Host: btcCfg.RPCHost,
Expand Down
3 changes: 2 additions & 1 deletion zetaclient/bitcoin/bitcoin_client_rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ func (suite *BitcoinClientTestSuite) SetupTest() {
PrivKey: privateKey,
}
appContext := appcontext.NewAppContext(&corecontext.ZetaCoreContext{}, config.Config{})
client, err := NewBitcoinClient(appContext, common.BtcRegtestChain(), nil, tss, "/tmp", clientcommon.DefaultLoggers(), nil)
client, err := NewBitcoinClient(appContext, common.BtcRegtestChain(), nil, tss, "/tmp",
clientcommon.DefaultLoggers(), config.BTCConfig{}, nil)
suite.Require().NoError(err)
suite.BitcoinChainClient = client
skBytes, err := hex.DecodeString(skHex)
Expand Down
20 changes: 20 additions & 0 deletions zetaclient/bitcoin/bitcoin_signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"math/rand"
"time"

ethcommon "github.com/ethereum/go-ethereum/common"
clientcommon "github.com/zeta-chain/zetacore/zetaclient/common"
"github.com/zeta-chain/zetacore/zetaclient/interfaces"
"github.com/zeta-chain/zetacore/zetaclient/metrics"
Expand All @@ -34,6 +35,7 @@ const (
outTxBytesMax = uint64(1531) // 1531v == EstimateSegWitTxSize(21, 3)
)

// BTCSigner deals with signing BTC transactions and implements the ChainSigner interface
type BTCSigner struct {
tssSigner interfaces.TSSSigner
rpcClient interfaces.BTCRPCClient
Expand Down Expand Up @@ -71,6 +73,24 @@ func NewBTCSigner(
}, nil
}

// SetZetaConnectorAddress does nothing for BTC
func (signer *BTCSigner) SetZetaConnectorAddress(_ ethcommon.Address) {
}

// SetERC20CustodyAddress does nothing for BTC
func (signer *BTCSigner) SetERC20CustodyAddress(_ ethcommon.Address) {
}

// GetZetaConnectorAddress returns dummy address
func (signer *BTCSigner) GetZetaConnectorAddress() ethcommon.Address {
return ethcommon.Address{}
}

// GetERC20CustodyAddress returns dummy address
func (signer *BTCSigner) GetERC20CustodyAddress() ethcommon.Address {
return ethcommon.Address{}
}

// SignWithdrawTx receives utxos sorted by value, amount in BTC, feeRate in BTC per Kb
func (signer *BTCSigner) SignWithdrawTx(
to *btcutil.AddressWitnessPubKeyHash,
Expand Down
7 changes: 4 additions & 3 deletions zetaclient/evm/evm_signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
zbridge "github.com/zeta-chain/zetacore/zetaclient/zetabridge"
)

// Signer deals with the signing EVM transactions and implements the ChainSigner interface
type Signer struct {
client interfaces.EVMRPCClient
chain *common.Chain
Expand All @@ -56,7 +57,7 @@ func NewEVMSigner(
endpoint string,
tssSigner interfaces.TSSSigner,
zetaConnectorABI string,
erc20CustodABI string,
erc20CustodyABI string,
zetaConnectorAddress ethcommon.Address,
erc20CustodyAddress ethcommon.Address,
loggers clientcommon.ClientLogger,
Expand All @@ -70,7 +71,7 @@ func NewEVMSigner(
if err != nil {
return nil, err
}
erc20CustodyABI, err := abi.JSON(strings.NewReader(erc20CustodABI))
custodyABI, err := abi.JSON(strings.NewReader(erc20CustodyABI))
if err != nil {
return nil, err
}
Expand All @@ -81,7 +82,7 @@ func NewEVMSigner(
tssSigner: tssSigner,
ethSigner: ethSigner,
zetaConnectorABI: connectorABI,
erc20CustodyABI: erc20CustodyABI,
erc20CustodyABI: custodyABI,
zetaConnectorAddress: zetaConnectorAddress,
er20CustodyAddress: erc20CustodyAddress,
logger: clientcommon.ClientLogger{
Expand Down
24 changes: 24 additions & 0 deletions zetaclient/evm/evm_signer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,30 @@ func getInvalidCCTX() (*types.CrossChainTx, error) {
return &cctx, err
}

func TestSigner_SetGetConnectorAddress(t *testing.T) {
evmSigner, err := getNewEvmSigner()
require.NoError(t, err)
// Get and compare
require.Equal(t, ConnectorAddress, evmSigner.GetZetaConnectorAddress())

// Update and get again
newConnector := sample.EthAddress()
evmSigner.SetZetaConnectorAddress(newConnector)
require.Equal(t, newConnector, evmSigner.GetZetaConnectorAddress())
}

func TestSigner_SetGetERC20CustodyAddress(t *testing.T) {
evmSigner, err := getNewEvmSigner()
require.NoError(t, err)
// Get and compare
require.Equal(t, ERC20CustodyAddress, evmSigner.GetERC20CustodyAddress())

// Update and get again
newCustody := sample.EthAddress()
evmSigner.SetERC20CustodyAddress(newCustody)
require.Equal(t, newCustody, evmSigner.GetERC20CustodyAddress())
}

func TestSigner_TryProcessOutTx(t *testing.T) {
evmSigner, err := getNewEvmSigner()
require.NoError(t, err)
Expand Down
4 changes: 4 additions & 0 deletions zetaclient/interfaces/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ type ChainSigner interface {
zetaBridge ZetaCoreBridger,
height uint64,
)
SetZetaConnectorAddress(address ethcommon.Address)
SetERC20CustodyAddress(address ethcommon.Address)
GetZetaConnectorAddress() ethcommon.Address
GetERC20CustodyAddress() ethcommon.Address
}

// ZetaCoreBridger is the interface to interact with ZetaCore
Expand Down
1 change: 1 addition & 0 deletions zetaclient/testutils/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const (
// some other addresses
OtherAddress1 = "0x21248Decd0B7EcB0F30186297766b8AB6496265b"
OtherAddress2 = "0x33A351C90aF486AebC35042Bb0544123cAed26AB"
OtherAddress3 = "0x86B77E4fBd07CFdCc486cAe4F2787fB5C5a62cd3"
)

// ConnectorAddresses contains constants ERC20 connector addresses for testing
Expand Down
56 changes: 52 additions & 4 deletions zetaclient/testutils/stub/chain_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@ import (
"github.com/zeta-chain/zetacore/zetaclient/interfaces"
)

// ----------------------------------------------------------------------------
// EVMClient
// ----------------------------------------------------------------------------
var _ interfaces.ChainClient = (*EVMClient)(nil)

// EVMClient is a mock of evm chain client for testing
type EVMClient struct {
ChainParams observertypes.ChainParams
}

func NewEVMClient() *EVMClient {
return &EVMClient{}
func NewEVMClient(chainParams *observertypes.ChainParams) *EVMClient {
return &EVMClient{
ChainParams: *chainParams,
}
}

func (s *EVMClient) Start() {
Expand All @@ -27,11 +33,12 @@ func (s *EVMClient) IsSendOutTxProcessed(_ *crosschaintypes.CrossChainTx, _ zero
return false, false, nil
}

func (s *EVMClient) SetChainParams(observertypes.ChainParams) {
func (s *EVMClient) SetChainParams(chainParams observertypes.ChainParams) {
s.ChainParams = chainParams
}

func (s *EVMClient) GetChainParams() observertypes.ChainParams {
return observertypes.ChainParams{}
return s.ChainParams
}

func (s *EVMClient) GetTxID(_ uint64) string {
Expand All @@ -40,3 +47,44 @@ func (s *EVMClient) GetTxID(_ uint64) string {

func (s *EVMClient) ExternalChainWatcherForNewInboundTrackerSuggestions() {
}

// ----------------------------------------------------------------------------
// BTCClient
// ----------------------------------------------------------------------------
var _ interfaces.ChainClient = (*BTCClient)(nil)

// BTCClient is a mock of btc chain client for testing
type BTCClient struct {
ChainParams observertypes.ChainParams
}

func NewBTCClient(chainParams *observertypes.ChainParams) *BTCClient {
return &BTCClient{
ChainParams: *chainParams,
}
}

func (s *BTCClient) Start() {
}

func (s *BTCClient) Stop() {
}

func (s *BTCClient) IsSendOutTxProcessed(_ *crosschaintypes.CrossChainTx, _ zerolog.Logger) (bool, bool, error) {
return false, false, nil
}

func (s *BTCClient) SetChainParams(chainParams observertypes.ChainParams) {
s.ChainParams = chainParams
}

func (s *BTCClient) GetChainParams() observertypes.ChainParams {
return s.ChainParams
}

func (s *BTCClient) GetTxID(_ uint64) string {
return ""
}

func (s *BTCClient) ExternalChainWatcherForNewInboundTrackerSuggestions() {
}
96 changes: 96 additions & 0 deletions zetaclient/testutils/stub/chain_signer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package stub

import (
ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/zeta-chain/zetacore/common"
crosschaintypes "github.com/zeta-chain/zetacore/x/crosschain/types"
"github.com/zeta-chain/zetacore/zetaclient/interfaces"
"github.com/zeta-chain/zetacore/zetaclient/outtxprocessor"
)

// ----------------------------------------------------------------------------
// EVMSigner
// ----------------------------------------------------------------------------
var _ interfaces.ChainSigner = (*EVMSigner)(nil)

// EVMSigner is a mock of evm chain signer for testing
type EVMSigner struct {
Chain common.Chain
ZetaConnectorAddress ethcommon.Address
ERC20CustodyAddress ethcommon.Address
}

func NewEVMSigner(
chain common.Chain,
zetaConnectorAddress ethcommon.Address,
erc20CustodyAddress ethcommon.Address,
) *EVMSigner {
return &EVMSigner{
Chain: chain,
ZetaConnectorAddress: zetaConnectorAddress,
ERC20CustodyAddress: erc20CustodyAddress,
}
}

func (s *EVMSigner) TryProcessOutTx(
_ *crosschaintypes.CrossChainTx,
_ *outtxprocessor.Processor,
_ string,
_ interfaces.ChainClient,
_ interfaces.ZetaCoreBridger,
_ uint64,
) {
}

func (s *EVMSigner) SetZetaConnectorAddress(address ethcommon.Address) {
s.ZetaConnectorAddress = address
}

func (s *EVMSigner) SetERC20CustodyAddress(address ethcommon.Address) {
s.ERC20CustodyAddress = address
}

func (s *EVMSigner) GetZetaConnectorAddress() ethcommon.Address {
return s.ZetaConnectorAddress
}

func (s *EVMSigner) GetERC20CustodyAddress() ethcommon.Address {
return s.ERC20CustodyAddress
}

// ----------------------------------------------------------------------------
// BTCSigner
// ----------------------------------------------------------------------------
var _ interfaces.ChainSigner = (*BTCSigner)(nil)

// BTCSigner is a mock of bitcoin chain signer for testing
type BTCSigner struct {
}

func NewBTCSigner() *BTCSigner {
return &BTCSigner{}
}

func (s *BTCSigner) TryProcessOutTx(
_ *crosschaintypes.CrossChainTx,
_ *outtxprocessor.Processor,
_ string,
_ interfaces.ChainClient,
_ interfaces.ZetaCoreBridger,
_ uint64,
) {
}

func (s *BTCSigner) SetZetaConnectorAddress(_ ethcommon.Address) {
}

func (s *BTCSigner) SetERC20CustodyAddress(_ ethcommon.Address) {
}

func (s *BTCSigner) GetZetaConnectorAddress() ethcommon.Address {
return ethcommon.Address{}
}

func (s *BTCSigner) GetERC20CustodyAddress() ethcommon.Address {
return ethcommon.Address{}
}
Loading

0 comments on commit 1321dcc

Please sign in to comment.