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

refactor(zetaclient): use interfaces for used attributes #1191

Merged
merged 11 commits into from
Oct 24, 2023
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
17 changes: 15 additions & 2 deletions cmd/zetaclientd/aux.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ func CreateZetaBridge(cfg *config.Config) (*zetaclient.ZetaCoreBridge, error) {
return bridge, nil
}

func CreateSignerMap(tss zetaclient.TSSSigner, logger zerolog.Logger, cfg *config.Config, ts *zetaclient.TelemetryServer) (map[common.Chain]zetaclient.ChainSigner, error) {
func CreateSignerMap(
tss zetaclient.TSSSigner,
logger zerolog.Logger,
cfg *config.Config,
ts *zetaclient.TelemetryServer,
) (map[common.Chain]zetaclient.ChainSigner, error) {
signerMap := make(map[common.Chain]zetaclient.ChainSigner)
// EVM signers
for _, evmConfig := range cfg.GetAllEVMConfigs() {
Expand Down Expand Up @@ -63,7 +68,15 @@ func CreateSignerMap(tss zetaclient.TSSSigner, logger zerolog.Logger, cfg *confi
return signerMap, nil
}

func CreateChainClientMap(bridge *zetaclient.ZetaCoreBridge, tss zetaclient.TSSSigner, dbpath string, metrics *metrics.Metrics, logger zerolog.Logger, cfg *config.Config, ts *zetaclient.TelemetryServer) (map[common.Chain]zetaclient.ChainClient, error) {
func CreateChainClientMap(
bridge *zetaclient.ZetaCoreBridge,
tss zetaclient.TSSSigner,
dbpath string,
metrics *metrics.Metrics,
logger zerolog.Logger,
cfg *config.Config,
ts *zetaclient.TelemetryServer,
) (map[common.Chain]zetaclient.ChainClient, error) {
clientMap := make(map[common.Chain]zetaclient.ChainClient)
// EVM clients
for _, evmConfig := range cfg.GetAllEVMConfigs() {
Expand Down
2 changes: 1 addition & 1 deletion cmd/zetaclientd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func start(_ *cobra.Command, _ []string) error {
}

// CreateCoreObserver : Core observer wraps the zetacore bridge and adds the client and signer maps to it . This is the high level object used for CCTX interactions
mo1 := mc.NewCoreObserver(zetaBridge, signerMap, chainClientMap, metrics, tss, masterLogger, cfg, telemetryServer)
kevinssgh marked this conversation as resolved.
Show resolved Hide resolved
mo1 := mc.NewCoreObserver(zetaBridge, signerMap, chainClientMap, metrics, masterLogger, cfg, telemetryServer)
mo1.MonitorCore()

startLogger.Info().Msgf("awaiting the os.Interrupt, syscall.SIGTERM signals...")
Expand Down
69 changes: 38 additions & 31 deletions zetaclient/bitcoin_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,32 @@ import (
"bytes"
"encoding/hex"
"fmt"

"cosmossdk.io/math"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"github.com/pkg/errors"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"

math2 "math"
"math"
"math/big"
"os"
"sort"
"strconv"
"sync"
"sync/atomic"

cosmosmath "cosmossdk.io/math"
"github.com/btcsuite/btcd/btcjson"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/rpcclient"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
lru "github.com/hashicorp/golang-lru"
"github.com/pkg/errors"
"github.com/rs/zerolog"
"github.com/zeta-chain/zetacore/common"
"github.com/zeta-chain/zetacore/x/crosschain/types"
observertypes "github.com/zeta-chain/zetacore/x/observer/types"
"github.com/zeta-chain/zetacore/zetaclient/config"
metricsPkg "github.com/zeta-chain/zetacore/zetaclient/metrics"
clienttypes "github.com/zeta-chain/zetacore/zetaclient/types"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)

var _ ChainClient = &BitcoinChainClient{}
Expand All @@ -44,14 +42,14 @@ type BTCLog struct {
WatchGasPrice zerolog.Logger
}

// Chain configuration struct
// BitcoinChainClient represents a chain configuration for Bitcoin
// Filled with above constants depending on chain
type BitcoinChainClient struct {
*ChainMetrics

chain common.Chain
rpcClient *rpcclient.Client
zetaClient *ZetaCoreBridge
rpcClient BTCRPCClient
zetaClient ZetaCoreBridger
Tss TSSSigner
lastBlock int64
lastBlockScanned int64
Expand Down Expand Up @@ -121,8 +119,17 @@ func (ob *BitcoinChainClient) GetCoreParams() observertypes.CoreParams {
return ob.params
}

// Return configuration based on supplied target chain
func NewBitcoinClient(chain common.Chain, bridge *ZetaCoreBridge, tss TSSSigner, dbpath string, metrics *metricsPkg.Metrics, logger zerolog.Logger, btcCfg config.BTCConfig, ts *TelemetryServer) (*BitcoinChainClient, error) {
// NewBitcoinClient returns a new configuration based on supplied target chain
func NewBitcoinClient(
chain common.Chain,
bridge ZetaCoreBridger,
tss TSSSigner,
dbpath string,
metrics *metricsPkg.Metrics,
logger zerolog.Logger,
btcCfg config.BTCConfig,
ts *TelemetryServer,
) (*BitcoinChainClient, error) {
ob := BitcoinChainClient{
ChainMetrics: NewChainMetrics(chain.ChainName.String(), metrics),
ts: ts,
Expand Down Expand Up @@ -205,7 +212,7 @@ func (ob *BitcoinChainClient) SetLastBlockHeight(block int64) {
if block < 0 {
panic("lastBlock is negative")
}
if block >= math2.MaxInt64 {
if block >= math.MaxInt64 {
panic("lastBlock is too large")
}
atomic.StoreInt64(&ob.lastBlock, block)
Expand All @@ -216,7 +223,7 @@ func (ob *BitcoinChainClient) GetLastBlockHeight() int64 {
if height < 0 {
panic("lastBlock is negative")
}
if height >= math2.MaxInt64 {
if height >= math.MaxInt64 {
panic("lastBlock is too large")
}
return height
Expand All @@ -226,7 +233,7 @@ func (ob *BitcoinChainClient) SetLastBlockHeightScanned(block int64) {
if block < 0 {
panic("lastBlockScanned is negative")
}
if block >= math2.MaxInt64 {
if block >= math.MaxInt64 {
panic("lastBlockScanned is too large")
}
atomic.StoreInt64(&ob.lastBlockScanned, block)
Expand All @@ -238,7 +245,7 @@ func (ob *BitcoinChainClient) GetLastBlockHeightScanned() int64 {
if height < 0 {
panic("lastBlockScanned is negative")
}
if height >= math2.MaxInt64 {
if height >= math.MaxInt64 {
panic("lastBlockScanned is too large")
}
return height
Expand Down Expand Up @@ -281,14 +288,14 @@ func (ob *BitcoinChainClient) observeInTx() error {
if err != nil {
return fmt.Errorf("error getting block count: %s", err)
}
if cnt < 0 || cnt >= math2.MaxInt64 {
if cnt < 0 || cnt >= math.MaxInt64 {
return fmt.Errorf("block count is out of range: %d", cnt)
}

// "confirmed" current block number
// #nosec G701 always in range
confirmedBlockNum := cnt - int64(ob.GetCoreParams().ConfirmationCount)
if confirmedBlockNum < 0 || confirmedBlockNum > math2.MaxInt64 {
if confirmedBlockNum < 0 || confirmedBlockNum > math.MaxInt64 {
return fmt.Errorf("skipping observer , confirmedBlockNum is negative or too large ")
}
ob.SetLastBlockHeight(confirmedBlockNum)
Expand Down Expand Up @@ -365,15 +372,15 @@ func (ob *BitcoinChainClient) observeInTx() error {
return nil
}

// Returns number of required Bitcoin confirmations depending on sent BTC amount.
// ConfirmationsThreshold returns number of required Bitcoin confirmations depending on sent BTC amount.
func (ob *BitcoinChainClient) ConfirmationsThreshold(amount *big.Int) int64 {
if amount.Cmp(big.NewInt(200000000)) >= 0 {
return 6
}
return 2
}

// returns isIncluded(or inMempool), isConfirmed, Error
// IsSendOutTxProcessed returns isIncluded(or inMempool), isConfirmed, Error
func (ob *BitcoinChainClient) IsSendOutTxProcessed(sendHash string, nonce uint64, _ common.CoinType, logger zerolog.Logger) (bool, bool, error) {
outTxID := ob.GetTxID(nonce)
logger.Info().Msgf("IsSendOutTxProcessed %s", outTxID)
Expand Down Expand Up @@ -509,7 +516,7 @@ func (ob *BitcoinChainClient) PostGasPrice() error {
if feeResult.Errors != nil || feeResult.FeeRate == nil {
return fmt.Errorf("error getting gas price: %s", feeResult.Errors)
}
if *feeResult.FeeRate > math2.MaxInt64 {
if *feeResult.FeeRate > math.MaxInt64 {
return fmt.Errorf("gas price is too large: %f", *feeResult.FeeRate)
}
// #nosec G701 always in range
Expand Down Expand Up @@ -538,7 +545,7 @@ type BTCInTxEvnet struct {
TxHash string
}

// given txs list returned by the "getblock 2" RPC command, return the txs that are relevant to us
// FilterAndParseIncomingTx given txs list returned by the "getblock 2" RPC command, return the txs that are relevant to us
// relevant tx must have the following vouts as the first two vouts:
// vout0: p2wpkh to the TSS address (targetAddress)
// vout1: OP_RETURN memo, base64 encoded
Expand Down Expand Up @@ -572,14 +579,14 @@ func (ob *BitcoinChainClient) GetInboundVoteMessageFromBtcEvent(inTx *BTCInTxEvn
inTx.FromAddress,
inTx.FromAddress,
common.ZetaChain().ChainId,
math.NewUintFromBigInt(amountInt),
cosmosmath.NewUintFromBigInt(amountInt),
message,
inTx.TxHash,
inTx.BlockNumber,
0,
common.CoinType_Gas,
"",
ob.zetaClient.keys.GetOperatorAddress().String(),
ob.zetaClient.GetKeys().GetOperatorAddress().String(),
)
}

Expand Down Expand Up @@ -811,7 +818,7 @@ func (ob *BitcoinChainClient) findNonceMarkUTXO(nonce uint64, txid string) (int,
return -1, fmt.Errorf("findNonceMarkUTXO: cannot find nonce-mark utxo with nonce %d", nonce)
}

// Selects a sublist of utxos to be used as inputs.
// SelectUTXOs selects a sublist of utxos to be used as inputs.
//
// Parameters:
// - amount: The desired minimum total value of the selected UTXOs.
Expand Down Expand Up @@ -897,7 +904,7 @@ func (ob *BitcoinChainClient) SelectUTXOs(amount float64, utxosToSpend uint16, n
return results, total, consolidatedUtxo, consolidatedValue, nil
}

// Save successfully broadcasted transaction
// SaveBroadcastedTx saves successfully broadcasted transaction
func (ob *BitcoinChainClient) SaveBroadcastedTx(txHash string, nonce uint64) {
outTxID := ob.GetTxID(nonce)
ob.Mu.Lock()
Expand Down Expand Up @@ -1072,7 +1079,7 @@ func (ob *BitcoinChainClient) getRawTxResult(hash *chainhash.Hash, res *btcjson.
}
}

// Vin is valid if:
// checkTSSVin checks vin is valid if:
// - The first input is the nonce-mark
// - All inputs are from TSS address
func (ob *BitcoinChainClient) checkTSSVin(vins []btcjson.Vin, nonce uint64) error {
Expand Down Expand Up @@ -1104,7 +1111,7 @@ func (ob *BitcoinChainClient) checkTSSVin(vins []btcjson.Vin, nonce uint64) erro
return nil
}

// Vout is valid if:
// checkTSSVout vout is valid if:
// - The first output is the nonce-mark
// - The second output is the correct payment to recipient
// - The third output is the change to TSS (optional)
Expand Down
20 changes: 0 additions & 20 deletions zetaclient/block_height.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"

//"github.com/Meta-Protocol/zetacore/common"
"github.com/zeta-chain/zetacore/x/crosschain/types"
)

Expand All @@ -23,22 +22,3 @@ func (b *ZetaCoreBridge) GetBlockHeight() (int64, error) {
fmt.Printf("block height: %d\n", height.Height)
return height.Height, nil
}

//func (b *ZetaCoreBridge) GetLastBlockObserved(chain common.Chain) (uint64, error) {
// Client := types.NewQueryClient(b.grpcConn)
// last_obs, err := Client.LastBlockObserved(
// context.Background(),
// &types.QueryGetLastBlockObservedRequest{
// Index: chain.String(),
// },
// )
// if err != nil {
// return 0, err
// }
//
// observed := last_obs.LastBlockObserved
// fmt.Printf("last observed block height on chain %s: %d\n",
// observed.Chain,
// observed.Height)
// return observed.Height, nil
//}
42 changes: 33 additions & 9 deletions zetaclient/btc_signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/rs/zerolog"
"github.com/zeta-chain/zetacore/common"
"github.com/zeta-chain/zetacore/x/crosschain/types"
zetaObserverModuleTypes "github.com/zeta-chain/zetacore/x/observer/types"
observertypes "github.com/zeta-chain/zetacore/x/observer/types"
"github.com/zeta-chain/zetacore/zetaclient/config"
)

Expand All @@ -35,7 +35,7 @@ const (

type BTCSigner struct {
tssSigner TSSSigner
rpcClient *rpcclient.Client
rpcClient BTCRPCClient
logger zerolog.Logger
ts *TelemetryServer
}
Expand Down Expand Up @@ -67,8 +67,16 @@ func NewBTCSigner(cfg config.BTCConfig, tssSigner TSSSigner, logger zerolog.Logg
}

// SignWithdrawTx receives utxos sorted by value, amount in BTC, feeRate in BTC per Kb
func (signer *BTCSigner) SignWithdrawTx(to *btcutil.AddressWitnessPubKeyHash, amount float64, gasPrice *big.Int, sizeLimit uint64,
btcClient *BitcoinChainClient, height uint64, nonce uint64, chain *common.Chain) (*wire.MsgTx, error) {
func (signer *BTCSigner) SignWithdrawTx(
to *btcutil.AddressWitnessPubKeyHash,
amount float64,
gasPrice *big.Int,
sizeLimit uint64,
btcClient *BitcoinChainClient,
height uint64,
nonce uint64,
chain *common.Chain,
) (*wire.MsgTx, error) {
estimateFee := float64(gasPrice.Uint64()) * outTxBytesMax / 1e8
nonceMark := common.NonceMarkAmount(nonce)

Expand Down Expand Up @@ -224,7 +232,14 @@ func (signer *BTCSigner) Broadcast(signedTx *wire.MsgTx) error {
return nil
}

func (signer *BTCSigner) TryProcessOutTx(send *types.CrossChainTx, outTxMan *OutTxProcessorManager, outTxID string, chainclient ChainClient, zetaBridge *ZetaCoreBridge, height uint64) {
func (signer *BTCSigner) TryProcessOutTx(
send *types.CrossChainTx,
outTxMan *OutTxProcessorManager,
outTxID string,
chainclient ChainClient,
zetaBridge ZetaCoreBridger,
height uint64,
) {
defer func() {
outTxMan.EndTryProcess(outTxID)
if err := recover(); err != nil {
Expand Down Expand Up @@ -258,7 +273,7 @@ func (signer *BTCSigner) TryProcessOutTx(send *types.CrossChainTx, outTxMan *Out
logger.Info().Msgf("outbound is disabled")
return
}
myid := zetaBridge.keys.GetAddress()
myid := zetaBridge.GetKeys().GetAddress()
// Early return if the send is already processed
// FIXME: handle revert case
outboundTxTssNonce := params.OutboundTxTssNonce
Expand Down Expand Up @@ -293,8 +308,17 @@ func (signer *BTCSigner) TryProcessOutTx(send *types.CrossChainTx, outTxMan *Out

logger.Info().Msgf("SignWithdrawTx: to %s, value %d sats", addr.EncodeAddress(), params.Amount.Uint64())
logger.Info().Msgf("using utxos: %v", btcClient.utxos)
tx, err := signer.SignWithdrawTx(to, float64(params.Amount.Uint64())/1e8, gasprice, sizelimit, btcClient, height,
outboundTxTssNonce, &btcClient.chain)

tx, err := signer.SignWithdrawTx(
to,
float64(params.Amount.Uint64())/1e8,
gasprice,
sizelimit,
btcClient,
height,
outboundTxTssNonce,
&btcClient.chain,
)
if err != nil {
logger.Warn().Err(err).Msgf("SignOutboundTx error: nonce %d chain %d", outboundTxTssNonce, params.ReceiverChainId)
return
Expand All @@ -303,7 +327,7 @@ func (signer *BTCSigner) TryProcessOutTx(send *types.CrossChainTx, outTxMan *Out
// FIXME: add prometheus metrics
_, err = zetaBridge.GetObserverList(btcClient.chain)
if err != nil {
logger.Warn().Err(err).Msgf("unable to get observer list: chain %d observation %s", outboundTxTssNonce, zetaObserverModuleTypes.ObservationType_OutBoundTx.String())
logger.Warn().Err(err).Msgf("unable to get observer list: chain %d observation %s", outboundTxTssNonce, observertypes.ObservationType_OutBoundTx.String())
}
if tx != nil {
outTxHash := tx.TxHash().String()
Expand Down
Loading
Loading