Skip to content

Commit

Permalink
Merge branch 'develop' into intx-filter
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinssgh authored Mar 19, 2024
2 parents 9d9fefa + 337e485 commit dc24fe9
Show file tree
Hide file tree
Showing 15 changed files with 546 additions and 220 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,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 @@ 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
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 @@ 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
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 @@ func NewEVMSigner(
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 @@ 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
}

// 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 @@ 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 +189,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 +215,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 +228,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 +583,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 +616,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)
if err != nil {
return nil, fmt.Errorf("Sign error: %w", err)
}
Expand All @@ -612,25 +639,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.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
}
ethSigner := ethtypes.LatestSignerForChainID(chainID)
return client, chainID, ethSigner, nil
return client, ethSigner, nil
}

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

0 comments on commit dc24fe9

Please sign in to comment.