Skip to content

Commit

Permalink
zetaclient should pick up new connector and erc20 custody addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
ws4charlie committed Mar 13, 2024
1 parent 810c7c1 commit 364b817
Show file tree
Hide file tree
Showing 10 changed files with 296 additions and 206 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
### Fixes

* [1861](https://github.com/zeta-chain/node/pull/1861) - fix `ObserverSlashAmount` invalid read
* [1633](https://github.com/zeta-chain/node/issues/1633) - zetaclient should be able to pick up new connector and erc20Custody addresses

### Chores

Expand Down
17 changes: 8 additions & 9 deletions cmd/zetaclientd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
sdk "github.com/cosmos/cosmos-sdk/types"
ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/zeta-chain/zetacore/common"
"github.com/zeta-chain/zetacore/common/cosmos"
appcontext "github.com/zeta-chain/zetacore/zetaclient/app_context"
"github.com/zeta-chain/zetacore/zetaclient/authz"
Expand Down Expand Up @@ -55,8 +54,8 @@ func CreateSignerMap(
tss interfaces.TSSSigner,
loggers clientcommon.ClientLogger,
ts *metrics.TelemetryServer,
) (map[common.Chain]interfaces.ChainSigner, error) {
signerMap := make(map[common.Chain]interfaces.ChainSigner)
) (map[int64]interfaces.ChainSigner, error) {
signerMap := make(map[int64]interfaces.ChainSigner)
// EVM signers
for _, evmConfig := range appContext.Config().GetAllEVMConfigs() {
if evmConfig.Chain.IsZetaChain() {
Expand All @@ -77,7 +76,7 @@ func CreateSignerMap(
loggers.Std.Error().Err(err).Msgf("NewEVMSigner error for chain %s", evmConfig.Chain.String())
continue
}
signerMap[evmConfig.Chain] = signer
signerMap[evmConfig.Chain.ChainId] = signer
}
// BTC signer
btcChain, btcConfig, enabled := appContext.GetBTCChainAndConfig()
Expand All @@ -86,7 +85,7 @@ func CreateSignerMap(
if err != nil {
loggers.Std.Error().Err(err).Msgf("NewBTCSigner error for chain %s", btcChain.String())
} else {
signerMap[btcChain] = signer
signerMap[btcChain.ChainId] = signer
}
}

Expand All @@ -100,8 +99,8 @@ func CreateChainClientMap(
dbpath string,
loggers clientcommon.ClientLogger,
ts *metrics.TelemetryServer,
) (map[common.Chain]interfaces.ChainClient, error) {
clientMap := make(map[common.Chain]interfaces.ChainClient)
) (map[int64]interfaces.ChainClient, error) {
clientMap := make(map[int64]interfaces.ChainClient)
// EVM clients
for _, evmConfig := range appContext.Config().GetAllEVMConfigs() {
if evmConfig.Chain.IsZetaChain() {
Expand All @@ -120,7 +119,7 @@ func CreateChainClientMap(
loggers.Std.Error().Err(err).Msgf("NewEVMChainClient error for chain %s", evmConfig.Chain.String())
continue
}
clientMap[evmConfig.Chain] = co
clientMap[evmConfig.Chain.ChainId] = co
}
// BTC client
btcChain, _, enabled := appContext.GetBTCChainAndConfig()
Expand All @@ -130,7 +129,7 @@ func CreateChainClientMap(
loggers.Std.Error().Err(err).Msgf("NewBitcoinClient error for chain %s", btcChain.String())

} else {
clientMap[btcChain] = co
clientMap[btcChain.ChainId] = co
}
}

Expand Down
4 changes: 2 additions & 2 deletions zetaclient/evm/evm_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ type Log struct {
Compliance zerolog.Logger // Compliance logger
}

var _ interfaces.ChainClient = &ChainClient{}

// ChainClient represents the chain configuration for an EVM chain
// Filled with above constants depending on chain
type ChainClient struct {
Expand All @@ -81,7 +83,6 @@ type ChainClient struct {
outTxPendingTransactions map[string]*ethtypes.Transaction
outTXConfirmedReceipts map[string]*ethtypes.Receipt
outTXConfirmedTransactions map[string]*ethtypes.Transaction
OutTxChan chan OutTx // send to this channel if you want something back!
stop chan struct{}
logger Log
coreContext *corecontext.ZetaCoreContext
Expand Down Expand Up @@ -128,7 +129,6 @@ func NewEVMChainClient(
ob.outTxPendingTransactions = make(map[string]*ethtypes.Transaction)
ob.outTXConfirmedReceipts = make(map[string]*ethtypes.Receipt)
ob.outTXConfirmedTransactions = make(map[string]*ethtypes.Transaction)
ob.OutTxChan = make(chan OutTx, 100)

ob.logger.ChainLogger.Info().Msgf("Chain %s endpoint %s", ob.chain.ChainName.String(), evmCfg.Endpoint)
client, err := ethclient.Dial(evmCfg.Endpoint)
Expand Down
106 changes: 66 additions & 40 deletions zetaclient/evm/evm_signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,19 @@ import (
)

type Signer struct {
client interfaces.EVMRPCClient
chain *common.Chain
chainID *big.Int
tssSigner interfaces.TSSSigner
ethSigner ethtypes.Signer
abi abi.ABI
erc20CustodyABI abi.ABI
metaContractAddress ethcommon.Address
erc20CustodyContractAddress ethcommon.Address
logger clientcommon.ClientLogger
ts *metrics.TelemetryServer

// for outTx tracker report only
client interfaces.EVMRPCClient
chain *common.Chain
tssSigner interfaces.TSSSigner
ethSigner ethtypes.Signer
logger clientcommon.ClientLogger
ts *metrics.TelemetryServer

// mu protects below fields from concurrent access
mu *sync.Mutex
zetaConnectorABI abi.ABI
erc20CustodyABI abi.ABI
zetaConnectorAddress ethcommon.Address
er20CustodyAddress ethcommon.Address
outTxHashBeingReported map[string]bool
}

Expand All @@ -56,36 +55,35 @@ func NewEVMSigner(
chain common.Chain,
endpoint string,
tssSigner interfaces.TSSSigner,
abiString string,
erc20CustodyABIString string,
metaContract ethcommon.Address,
erc20CustodyContract ethcommon.Address,
zetaConnectorABI string,
erc20CustodABI string,
zetaConnectorAddress ethcommon.Address,
erc20CustodyAddress ethcommon.Address,
loggers clientcommon.ClientLogger,
ts *metrics.TelemetryServer,
) (*Signer, error) {
client, chainID, ethSigner, err := getEVMRPC(endpoint)
client, ethSigner, err := getEVMRPC(endpoint)
if err != nil {
return nil, err
}
connectorABI, err := abi.JSON(strings.NewReader(abiString))
connectorABI, err := abi.JSON(strings.NewReader(zetaConnectorABI))
if err != nil {
return nil, err
}
erc20CustodyABI, err := abi.JSON(strings.NewReader(erc20CustodyABIString))
erc20CustodyABI, err := abi.JSON(strings.NewReader(erc20CustodABI))
if err != nil {
return nil, err
}

return &Signer{
client: client,
chain: &chain,
tssSigner: tssSigner,
chainID: chainID,
ethSigner: ethSigner,
abi: connectorABI,
erc20CustodyABI: erc20CustodyABI,
metaContractAddress: metaContract,
erc20CustodyContractAddress: erc20CustodyContract,
client: client,
chain: &chain,
tssSigner: tssSigner,
ethSigner: ethSigner,
zetaConnectorABI: connectorABI,
erc20CustodyABI: erc20CustodyABI,
zetaConnectorAddress: zetaConnectorAddress,
er20CustodyAddress: erc20CustodyAddress,
logger: clientcommon.ClientLogger{
Std: loggers.Std.With().Str("chain", chain.ChainName.String()).Str("module", "EVMSigner").Logger(),
Compliance: loggers.Compliance,
Expand All @@ -96,6 +94,34 @@ func NewEVMSigner(
}, nil
}

// SetZetaConnectorAddress sets the zeta connector address
func (signer *Signer) SetZetaConnectorAddress(addr ethcommon.Address) {
signer.mu.Lock()
defer signer.mu.Unlock()
signer.zetaConnectorAddress = addr

Check warning on line 101 in zetaclient/evm/evm_signer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/evm/evm_signer.go#L98-L101

Added lines #L98 - L101 were not covered by tests
}

// SetERC20CustodyAddress sets the erc20 custody address
func (signer *Signer) SetERC20CustodyAddress(addr ethcommon.Address) {
signer.mu.Lock()
defer signer.mu.Unlock()
signer.er20CustodyAddress = addr

Check warning on line 108 in zetaclient/evm/evm_signer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/evm/evm_signer.go#L105-L108

Added lines #L105 - L108 were not covered by tests
}

// GetZetaConnectorAddress returns the zeta connector address
func (signer *Signer) GetZetaConnectorAddress() ethcommon.Address {
signer.mu.Lock()
defer signer.mu.Unlock()
return signer.zetaConnectorAddress

Check warning on line 115 in zetaclient/evm/evm_signer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/evm/evm_signer.go#L112-L115

Added lines #L112 - L115 were not covered by tests
}

// GetERC20CustodyAddress returns the erc20 custody address
func (signer *Signer) GetERC20CustodyAddress() ethcommon.Address {
signer.mu.Lock()
defer signer.mu.Unlock()
return signer.er20CustodyAddress

Check warning on line 122 in zetaclient/evm/evm_signer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/evm/evm_signer.go#L119-L122

Added lines #L119 - L122 were not covered by tests
}

// Sign given data, and metadata (gas, nonce, etc)
// returns a signed transaction, sig bytes, hash bytes, and error
func (signer *Signer) Sign(
Expand Down Expand Up @@ -150,7 +176,7 @@ func (signer *Signer) SignOutboundTx(txData *OutBoundTransactionData) (*ethtypes
var data []byte
var err error

data, err = signer.abi.Pack("onReceive",
data, err = signer.zetaConnectorABI.Pack("onReceive",
txData.sender.Bytes(),
txData.srcChainID,
txData.to,
Expand All @@ -162,7 +188,7 @@ func (signer *Signer) SignOutboundTx(txData *OutBoundTransactionData) (*ethtypes
}

tx, _, _, err := signer.Sign(data,
signer.metaContractAddress,
signer.zetaConnectorAddress,
txData.gasLimit,
txData.gasPrice,
txData.nonce,
Expand All @@ -188,7 +214,7 @@ func (signer *Signer) SignRevertTx(txData *OutBoundTransactionData) (*ethtypes.T
var data []byte
var err error

data, err = signer.abi.Pack("onRevert",
data, err = signer.zetaConnectorABI.Pack("onRevert",
txData.sender,
txData.srcChainID,
txData.to.Bytes(),
Expand All @@ -201,7 +227,7 @@ func (signer *Signer) SignRevertTx(txData *OutBoundTransactionData) (*ethtypes.T
}

tx, _, _, err := signer.Sign(data,
signer.metaContractAddress,
signer.zetaConnectorAddress,
txData.gasLimit,
txData.gasPrice,
txData.nonce,
Expand Down Expand Up @@ -556,7 +582,7 @@ func (signer *Signer) SignERC20WithdrawTx(txData *OutBoundTransactionData) (*eth
return nil, fmt.Errorf("pack error: %w", err)
}

tx, _, _, err := signer.Sign(data, signer.erc20CustodyContractAddress, txData.gasLimit, txData.gasPrice, txData.nonce, txData.height)
tx, _, _, err := signer.Sign(data, signer.er20CustodyAddress, txData.gasLimit, txData.gasPrice, txData.nonce, txData.height)
if err != nil {
return nil, fmt.Errorf("sign error: %w", err)
}
Expand Down Expand Up @@ -589,7 +615,7 @@ func (signer *Signer) SignWhitelistTx(
return nil, fmt.Errorf("pack error: %w", err)
}

tx, _, _, err := signer.Sign(data, signer.erc20CustodyContractAddress, gasLimit, gasPrice, nonce, height)
tx, _, _, err := signer.Sign(data, signer.er20CustodyAddress, gasLimit, gasPrice, nonce, height)

Check warning on line 618 in zetaclient/evm/evm_signer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/evm/evm_signer.go#L618

Added line #L618 was not covered by tests
if err != nil {
return nil, fmt.Errorf("Sign error: %w", err)
}
Expand All @@ -612,25 +638,25 @@ func (signer *Signer) EvmSigner() ethtypes.Signer {
// ________________________

// getEVMRPC is a helper function to set up the client and signer, also initializes a mock client for unit tests
func getEVMRPC(endpoint string) (interfaces.EVMRPCClient, *big.Int, ethtypes.Signer, error) {
func getEVMRPC(endpoint string) (interfaces.EVMRPCClient, ethtypes.Signer, error) {
if endpoint == stub.EVMRPCEnabled {
chainID := big.NewInt(common.BscMainnetChain().ChainId)
ethSigner := ethtypes.NewEIP155Signer(chainID)
client := stub.EvmClient{}
return client, chainID, ethSigner, nil
return client, ethSigner, nil
}

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

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

Check warning on line 656 in zetaclient/evm/evm_signer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/evm/evm_signer.go#L656

Added line #L656 was not covered by tests
}
ethSigner := ethtypes.LatestSignerForChainID(chainID)
return client, chainID, ethSigner, nil
return client, ethSigner, nil

Check warning on line 659 in zetaclient/evm/evm_signer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/evm/evm_signer.go#L659

Added line #L659 was not covered by tests
}

func roundUpToNearestGwei(gasPrice *big.Int) *big.Int {
Expand Down
3 changes: 1 addition & 2 deletions zetaclient/evm/evm_signer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,8 @@ func TestSigner_BroadcastOutTx(t *testing.T) {

func TestSigner_getEVMRPC(t *testing.T) {
t.Run("getEVMRPC error dialing", func(t *testing.T) {
client, chainId, signer, err := getEVMRPC("invalidEndpoint")
client, signer, err := getEVMRPC("invalidEndpoint")
require.Nil(t, client)
require.Nil(t, chainId)
require.Nil(t, signer)
require.Error(t, err)
})
Expand Down
6 changes: 3 additions & 3 deletions zetaclient/evm/inbounds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func TestEVM_CheckAndVoteInboundTokenGas(t *testing.T) {
})
t.Run("should not act if receiver is not TSS", func(t *testing.T) {
tx, receipt, _ := testutils.LoadEVMIntxNReceiptNCctx(t, chainID, intxHash, common.CoinType_Gas)
tx.To = testutils.OtherAddress // use other address
tx.To = testutils.OtherAddress1 // use other address
require.NoError(t, evm.ValidateEvmTransaction(tx))
lastBlock := receipt.BlockNumber.Uint64() + confirmation

Expand Down Expand Up @@ -320,9 +320,9 @@ func TestEVM_BuildInboundVoteMsgForTokenSentToTSS(t *testing.T) {
t.Run("should return nil msg if receiver is restricted", func(t *testing.T) {
txCopy := &ethrpc.Transaction{}
*txCopy = *tx
message := hex.EncodeToString(ethcommon.HexToAddress(testutils.OtherAddress).Bytes())
message := hex.EncodeToString(ethcommon.HexToAddress(testutils.OtherAddress1).Bytes())
txCopy.Input = message // use other address as receiver
cfg.ComplianceConfig.RestrictedAddresses = []string{testutils.OtherAddress}
cfg.ComplianceConfig.RestrictedAddresses = []string{testutils.OtherAddress1}
config.LoadComplianceConfig(cfg)
msg := ob.BuildInboundVoteMsgForTokenSentToTSS(txCopy, ethcommon.HexToAddress(txCopy.From), receipt.BlockNumber.Uint64())
require.Nil(t, msg)
Expand Down
7 changes: 5 additions & 2 deletions zetaclient/testutils/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ const (
// tss addresses
TSSAddressEVMMainnet = "0x70e967acFcC17c3941E87562161406d41676FD83"
TSSAddressBTCMainnet = "bc1qm24wp577nk8aacckv8np465z3dvmu7ry45el6y"
TssPubkeyEVMMainnet = "zetapub1addwnpepqtadxdyt037h86z60nl98t6zk56mw5zpnm79tsmvspln3hgt5phdc79kvfc"

TSSAddressEVMAthens3 = "0x8531a5aB847ff5B22D855633C25ED1DA3255247e"
TSSAddressBTCAthens3 = "tb1qy9pqmk2pd9sv63g27jt8r657wy0d9ueeh0nqur"
TssPubkeyEVMAthens3 = "zetapub1addwnpepq28c57cvcs0a2htsem5zxr6qnlvq9mzhmm76z3jncsnzz32rclangr2g35p"

// some other address
OtherAddress = "0x21248Decd0B7EcB0F30186297766b8AB6496265b"
// some other addresses
OtherAddress1 = "0x21248Decd0B7EcB0F30186297766b8AB6496265b"
OtherAddress2 = "0x33A351C90aF486AebC35042Bb0544123cAed26AB"
)

// ConnectorAddresses contains constants ERC20 connector addresses for testing
Expand Down
42 changes: 42 additions & 0 deletions zetaclient/testutils/stub/chain_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package stub

import (
"github.com/rs/zerolog"
crosschaintypes "github.com/zeta-chain/zetacore/x/crosschain/types"
observertypes "github.com/zeta-chain/zetacore/x/observer/types"
"github.com/zeta-chain/zetacore/zetaclient/interfaces"
)

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

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

func NewEVMClient() *EVMClient {
return &EVMClient{}
}

func (s *EVMClient) Start() {
}

func (s *EVMClient) Stop() {
}

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

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

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

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

func (s *EVMClient) ExternalChainWatcherForNewInboundTrackerSuggestions() {
}
Loading

0 comments on commit 364b817

Please sign in to comment.