Skip to content

Commit

Permalink
refactor bitcoin error
Browse files Browse the repository at this point in the history
  • Loading branch information
lumtis committed Aug 29, 2024
1 parent 4327710 commit 93a7f83
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
6 changes: 6 additions & 0 deletions zetaclient/chains/bitcoin/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package bitcoin

import "errors"

// ErrBitcoinNotEnabled is the error returned when bitcoin is not enabled
var ErrBitcoinNotEnabled = errors.New("bitcoin is not enabled")
29 changes: 19 additions & 10 deletions zetaclient/chains/bitcoin/observer/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/hex"
"fmt"
"math/big"
"strings"

cosmosmath "cosmossdk.io/math"
"github.com/btcsuite/btcd/btcjson"
Expand Down Expand Up @@ -57,9 +56,13 @@ func (ob *Observer) WatchInbound(ctx context.Context) error {
}
err := ob.ObserveInbound(ctx)
if err != nil {
// skip showing log for block number 0 as it means Bitcoin is not being observed
if !strings.Contains(err.Error(), "current block number 0 is too low") {
// skip showing log for block number 0 as it means Bitcoin node is not enabled
// TODO: prevent this routine from running if Bitcoin node is not enabled
// https://github.com/zeta-chain/node/issues/2790
if !errors.Is(err, bitcoin.ErrBitcoinNotEnabled) {
ob.logger.Inbound.Error().Err(err).Msg("WatchInbound error observing in tx")
} else {
ob.logger.Inbound.Debug().Err(err).Msg("WatchInbound: Bitcoin node is not enabled")

Check warning on line 65 in zetaclient/chains/bitcoin/observer/inbound.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/observer/inbound.go#L62-L65

Added lines #L62 - L65 were not covered by tests
}
}
ticker.UpdateInterval(ob.GetChainParams().InboundTicker, ob.logger.Inbound)
Expand All @@ -76,28 +79,34 @@ func (ob *Observer) ObserveInbound(ctx context.Context) error {
zetaCoreClient := ob.ZetacoreClient()

// get and update latest block height
cnt, err := ob.btcClient.GetBlockCount()
currentBlock, err := ob.btcClient.GetBlockCount()

Check warning on line 82 in zetaclient/chains/bitcoin/observer/inbound.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/observer/inbound.go#L82

Added line #L82 was not covered by tests
if err != nil {
return fmt.Errorf("observeInboundBTC: error getting block number: %s", err)
}
if cnt < 0 {
return fmt.Errorf("observeInboundBTC: block number is negative: %d", cnt)
if currentBlock < 0 {
return fmt.Errorf("observeInboundBTC: block number is negative: %d", currentBlock)

Check warning on line 87 in zetaclient/chains/bitcoin/observer/inbound.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/observer/inbound.go#L86-L87

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

// 0 will be returned if the node is not synced
if currentBlock == 0 {
return errors.Wrap(bitcoin.ErrBitcoinNotEnabled, "observeInboundBTC: current block number 0 is too low")

Check warning on line 92 in zetaclient/chains/bitcoin/observer/inbound.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/observer/inbound.go#L91-L92

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

// #nosec G115 checked positive
lastBlock := uint64(cnt)
lastBlock := uint64(currentBlock)

Check warning on line 96 in zetaclient/chains/bitcoin/observer/inbound.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/observer/inbound.go#L96

Added line #L96 was not covered by tests

if lastBlock < ob.LastBlock() {
return fmt.Errorf(
"observeInboundBTC: block number should not decrease: current %d last %d",
cnt,
currentBlock,

Check warning on line 101 in zetaclient/chains/bitcoin/observer/inbound.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/observer/inbound.go#L101

Added line #L101 was not covered by tests
ob.LastBlock(),
)
}
ob.WithLastBlock(lastBlock)

// skip if current height is too low
if lastBlock < ob.GetChainParams().ConfirmationCount {
return fmt.Errorf("observeInboundBTC: skipping observer, current block number %d is too low", cnt)
return fmt.Errorf("observeInboundBTC: skipping observer, current block number %d is too low", currentBlock)

Check warning on line 109 in zetaclient/chains/bitcoin/observer/inbound.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/observer/inbound.go#L109

Added line #L109 was not covered by tests
}

// skip if no new block is confirmed
Expand All @@ -115,7 +124,7 @@ func (ob *Observer) ObserveInbound(ctx context.Context) error {
return err
}
ob.logger.Inbound.Info().Msgf("observeInboundBTC: block %d has %d txs, current block %d, last block %d",
blockNumber, len(res.Block.Tx), cnt, lastScanned)
blockNumber, len(res.Block.Tx), currentBlock, lastScanned)

Check warning on line 127 in zetaclient/chains/bitcoin/observer/inbound.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/observer/inbound.go#L127

Added line #L127 was not covered by tests

// add block header to zetacore
if len(res.Block.Tx) > 1 {
Expand Down
4 changes: 3 additions & 1 deletion zetaclient/chains/bitcoin/observer/observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,10 +458,12 @@ func (ob *Observer) WatchUTXOs(ctx context.Context) error {
if err != nil {
// log debug log if the error if no wallet is loaded
// this is to prevent extensive logging in localnet when the wallet is not loaded for non-Bitcoin test
// TODO: prevent this routine from running if Bitcoin node is not enabled
// https://github.com/zeta-chain/node/issues/2790
if !strings.Contains(err.Error(), "No wallet is loaded") {
ob.logger.UTXOs.Error().Err(err).Msg("error fetching btc utxos")
} else {
ob.logger.UTXOs.Debug().Msg("No wallet is loaded; retrying...")
ob.logger.UTXOs.Debug().Err(err).Msg("No wallet is loaded")

Check warning on line 466 in zetaclient/chains/bitcoin/observer/observer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/observer/observer.go#L463-L466

Added lines #L463 - L466 were not covered by tests
}
}
ticker.UpdateInterval(ob.GetChainParams().WatchUtxoTicker, ob.logger.UTXOs)
Expand Down

0 comments on commit 93a7f83

Please sign in to comment.