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 all hotfix from v10.0.x (zero-amount, precision, etc.) #1235

Merged
merged 8 commits into from
Oct 12, 2023
34 changes: 19 additions & 15 deletions zetaclient/bitcoin_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,12 @@ func (ob *BitcoinChainClient) observeInTx() error {

for _, inTx := range inTxs {
ob.logger.WatchInTx.Debug().Msgf("Processing inTx: %s", inTx.TxHash)
amount := big.NewFloat(inTx.Value)
amount = amount.Mul(amount, big.NewFloat(1e8))
amountInt, _ := amount.Int(nil)
sats, err := getSatoshis(inTx.Value)
if err != nil {
ob.logger.WatchInTx.Error().Err(err).Msgf("getSatoshis error: %s", err)
continue
}
amountInt := big.NewInt(sats)
message := hex.EncodeToString(inTx.MemoBytes)
zetaHash, err := ob.zetaClient.PostSend(
inTx.FromAddress,
Expand Down Expand Up @@ -353,15 +356,15 @@ func (ob *BitcoinChainClient) IsSendOutTxProcessed(sendHash string, nonce uint64
// Get original cctx parameters
params, err := ob.GetPendingCctxParams(nonce)
if err != nil {
ob.logger.ObserveOutTx.Info().Msgf("IsSendOutTxProcessed: can't find pending cctx for nonce %d", nonce)
return false, false, nil
ob.logger.ObserveOutTx.Warn().Msgf("IsSendOutTxProcessed: can't find pending cctx for nonce %d", nonce)
return false, false, err
}

// Try including this outTx broadcasted by myself
inMempool, err := ob.checkNSaveIncludedTx(txnHash, params)
if err != nil {
ob.logger.ObserveOutTx.Error().Err(err).Msg("IsSendOutTxProcessed: checkNSaveIncludedTx failed")
return false, false, nil
return false, false, err
}
if inMempool { // to avoid unnecessary Tss keysign
ob.logger.ObserveOutTx.Info().Msgf("IsSendOutTxProcessed: outTx %s is still in mempool", outTxID)
Expand All @@ -379,17 +382,19 @@ func (ob *BitcoinChainClient) IsSendOutTxProcessed(sendHash string, nonce uint64
}

var amount float64
if res.Amount > 0 {
ob.logger.ObserveOutTx.Warn().Msg("IsSendOutTxProcessed: res.Amount > 0")
if res.Amount >= 0 {
ob.logger.ObserveOutTx.Warn().Msgf("IsSendOutTxProcessed: res.Amount >= 0")
amount = res.Amount
} else if res.Amount == 0 {
ob.logger.ObserveOutTx.Error().Msg("IsSendOutTxProcessed: res.Amount == 0")
return false, false, nil
} else {
amount = -res.Amount
}

amountInSat, _ := big.NewFloat(amount * 1e8).Int(nil)
sats, err := getSatoshis(amount)
if err != nil {
ob.logger.ObserveOutTx.Error().Msgf("IsSendOutTxProcessed: getSatoshis error: %s", err)
return false, false, err
}
amountInSat := big.NewInt(sats)
if res.Confirmations < ob.ConfirmationsThreshold(amountInSat) {
return true, false, nil
}
Expand Down Expand Up @@ -662,10 +667,9 @@ func (ob *BitcoinChainClient) refreshPendingNonce() {
pendingNonce := ob.pendingNonce
ob.mu.Unlock()

// #nosec G701 always positive
// #nosec G701 always non-negative
nonceLow := uint64(p.NonceLow)

if nonceLow > 0 && nonceLow >= pendingNonce {
if nonceLow > pendingNonce {
// get the last included outTx hash
txid, err := ob.getOutTxidByNonce(nonceLow-1, false)
if err != nil {
Expand Down
7 changes: 6 additions & 1 deletion zetaclient/zetacore_observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,12 @@ func (co *CoreObserver) startSendScheduler() {
outTxID := fmt.Sprintf("%s-%d-%d", cctx.Index, params.ReceiverChainId, nonce) // would outTxID a better ID?

// Process Bitcoin OutTx
if common.IsBitcoinChain(c.ChainId) && !outTxMan.IsOutTxActive(outTxID) {
if common.IsBitcoinChain(c.ChainId) {
if outTxMan.IsOutTxActive(outTxID) {
// bitcoun outTx is processed sequencially by nonce
// if the current outTx is being processed, there is no need to process outTx with future nonces
break
}
// #nosec G701 positive
if stop := co.processBitcoinOutTx(outTxMan, uint64(idx), cctx, signer, ob, currentHeight); stop {
break
Expand Down