Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: zetaclient should pick up new connector and erc20 custody addresses #1890

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,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
21 changes: 10 additions & 11 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,17 +119,17 @@ 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()
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())

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

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 @@
"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 @@
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 @@
}, nil
}

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

Check warning on line 77 in zetaclient/bitcoin/bitcoin_signer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/bitcoin/bitcoin_signer.go#L77

Added line #L77 was not covered by tests
}

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

Check warning on line 81 in zetaclient/bitcoin/bitcoin_signer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/bitcoin/bitcoin_signer.go#L81

Added line #L81 was not covered by tests
}

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

Check warning on line 86 in zetaclient/bitcoin/bitcoin_signer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/bitcoin/bitcoin_signer.go#L85-L86

Added lines #L85 - L86 were not covered by tests
}

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

Check warning on line 91 in zetaclient/bitcoin/bitcoin_signer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/bitcoin/bitcoin_signer.go#L90-L91

Added lines #L90 - L91 were not covered by tests
}

// SignWithdrawTx receives utxos sorted by value, amount in BTC, feeRate in BTC per Kb
func (signer *BTCSigner) SignWithdrawTx(
to *btcutil.AddressWitnessPubKeyHash,
Expand Down
2 changes: 2 additions & 0 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 Down
107 changes: 67 additions & 40 deletions zetaclient/evm/evm_signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,21 @@
zbridge "github.com/zeta-chain/zetacore/zetaclient/zetabridge"
)

// Signer deals with the signing EVM transactions and implements the ChainSigner interface
type Signer struct {
ws4charlie marked this conversation as resolved.
Show resolved Hide resolved
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 +56,35 @@
chain common.Chain,
endpoint string,
tssSigner interfaces.TSSSigner,
abiString string,
erc20CustodyABIString string,
metaContract ethcommon.Address,
erc20CustodyContract ethcommon.Address,
zetaConnectorABI string,
erc20CustodyABI 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))
custodyABI, err := abi.JSON(strings.NewReader(erc20CustodyABI))
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: custodyABI,
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 +95,34 @@
}, nil
}

// SetZetaConnectorAddress sets the zeta connector address
func (signer *Signer) SetZetaConnectorAddress(addr ethcommon.Address) {
ws4charlie marked this conversation as resolved.
Show resolved Hide resolved
signer.mu.Lock()
defer signer.mu.Unlock()
signer.zetaConnectorAddress = addr
}

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

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

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

// 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 +177,7 @@
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 +189,7 @@
}

tx, _, _, err := signer.Sign(data,
signer.metaContractAddress,
signer.zetaConnectorAddress,
txData.gasLimit,
txData.gasPrice,
txData.nonce,
Expand All @@ -188,7 +215,7 @@
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 +228,7 @@
}

tx, _, _, err := signer.Sign(data,
signer.metaContractAddress,
signer.zetaConnectorAddress,
txData.gasLimit,
txData.gasPrice,
txData.nonce,
Expand Down Expand Up @@ -556,7 +583,7 @@
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 +616,7 @@
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 619 in zetaclient/evm/evm_signer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/evm/evm_signer.go#L619

Added line #L619 was not covered by tests
if err != nil {
return nil, fmt.Errorf("Sign error: %w", err)
}
Expand All @@ -612,25 +639,25 @@
// ________________________

// 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.MockEvmClient{}
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 657 in zetaclient/evm/evm_signer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/evm/evm_signer.go#L657

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

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

View check run for this annotation

Codecov / codecov/patch

zetaclient/evm/evm_signer.go#L660

Added line #L660 was not covered by tests
}

func roundUpToNearestGwei(gasPrice *big.Int) *big.Int {
Expand Down
Loading
Loading