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: cherry pick v10.1.x hotfix (minRelayTxFee and bitcoin mainnet/testnet address format) #1367

Merged
merged 10 commits into from
Nov 13, 2023
5 changes: 1 addition & 4 deletions zetaclient/bitcoin_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ const (
minConfirmations = 0
maxHeightDiff = 10000
btcBlocksPerDay = 144
bytesPerKB = 1000
)

func (ob *BitcoinChainClient) WithZetaClient(bridge *ZetaCoreBridge) {
Expand Down Expand Up @@ -543,9 +542,7 @@ func (ob *BitcoinChainClient) PostGasPrice() error {
if *feeResult.FeeRate > math.MaxInt64 {
return fmt.Errorf("gas price is too large: %f", *feeResult.FeeRate)
}
// #nosec G701 always in range
feeRate := new(big.Int).SetInt64(int64(*feeResult.FeeRate * 1e8))
feeRatePerByte := new(big.Int).Div(feeRate, big.NewInt(bytesPerKB))
feeRatePerByte := feeRateToSatPerByte(*feeResult.FeeRate)
bn, err := ob.rpcClient.GetBlockCount()
if err != nil {
return err
Expand Down
15 changes: 14 additions & 1 deletion zetaclient/btc_signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,18 +294,31 @@ func (signer *BTCSigner) TryProcessOutTx(
return
}

// FIXME: config chain params
// Check receiver P2WPKH address
addr, err := btcutil.DecodeAddress(params.Receiver, config.BitconNetParams)
if err != nil {
logger.Error().Err(err).Msgf("cannot decode address %s ", params.Receiver)
return
}
if !addr.IsForNet(config.BitconNetParams) {
logger.Error().Msgf("address %s is not for network %s", params.Receiver, config.BitconNetParams.Name)
return
}
to, ok := addr.(*btcutil.AddressWitnessPubKeyHash)
if err != nil || !ok {
logger.Error().Err(err).Msgf("cannot convert address %s to P2WPKH address", params.Receiver)
return
}

// Add 1 satoshi/byte to gasPrice to avoid minRelayTxFee issue
networkInfo, err := signer.rpcClient.GetNetworkInfo()
if err != nil {
logger.Error().Err(err).Msgf("cannot get bitcoin network info")
return
}
satPerByte := feeRateToSatPerByte(networkInfo.RelayFee)
gasprice.Add(gasprice, satPerByte)
lumtis marked this conversation as resolved.
Show resolved Hide resolved

logger.Info().Msgf("SignWithdrawTx: to %s, value %d sats", addr.EncodeAddress(), params.Amount.Uint64())
logger.Info().Msgf("using utxos: %v", btcClient.utxos)

Expand Down
8 changes: 4 additions & 4 deletions zetaclient/evm_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,9 +551,9 @@ func (ob *EVMChainClient) IsSendOutTxProcessed(sendHash string, nonce uint64, co

// The lowest nonce we observe outTx for each chain
var lowestOutTxNonceToObserve = map[int64]uint64{
5: 70000, // Goerli
97: 95000, // BSC testnet
80001: 120000, // Mumbai
5: 113000, // Goerli
97: 102600, // BSC testnet
80001: 154500, // Mumbai
}

// FIXME: there's a chance that a txhash in OutTxChan may not deliver when Stop() is called
Expand Down Expand Up @@ -643,7 +643,7 @@ func (ob *EVMChainClient) queryTxByHash(txHash string, nonce uint64) (*ethtypes.
receipt, err := ob.evmClient.TransactionReceipt(ctxt, ethcommon.HexToHash(txHash))
if err != nil {
if err != ethereum.NotFound {
logger.Warn().Err(err).Msg("TransactionReceipt/TransactionByHash error")
logger.Warn().Err(err).Msgf("TransactionReceipt/TransactionByHash error, txHash %s", txHash)
}
return nil, nil, err
}
Expand Down
1 change: 1 addition & 0 deletions zetaclient/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ type ZetaCoreBridger interface {

// BTCRPCClient is the interface for BTC RPC client
type BTCRPCClient interface {
GetNetworkInfo() (*btcjson.GetNetworkInfoResult, error)
SendRawTransaction(tx *wire.MsgTx, allowHighFees bool) (*chainhash.Hash, error)
ListUnspentMinMaxAddresses(minConf int, maxConf int, addrs []btcutil.Address) ([]btcjson.ListUnspentResult, error)
EstimateSmartFee(confTarget int64, mode *btcjson.EstimateSmartFeeMode) (*btcjson.EstimateSmartFeeResult, error)
Expand Down
8 changes: 8 additions & 0 deletions zetaclient/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,16 @@ import (

const (
satoshiPerBitcoin = 1e8
bytesPerKB = 1000
)

// feeRateToSatPerByte converts a fee rate in BTC/KB to sat/byte.
func feeRateToSatPerByte(rate float64) *big.Int {
// #nosec G701 always in range
satPerKB := new(big.Int).SetInt64(int64(rate * satoshiPerBitcoin))
return new(big.Int).Div(satPerKB, big.NewInt(bytesPerKB))
}

func getSatoshis(btc float64) (int64, error) {
// The amount is only considered invalid if it cannot be represented
// as an integer type. This may happen if f is NaN or +-Infinity.
Expand Down
Loading