Skip to content

Commit

Permalink
chore: update to use uint32 in btc blocks and conversion of BLS publi…
Browse files Browse the repository at this point in the history
…c key responses
  • Loading branch information
RafilxTenfen committed Oct 16, 2024
1 parent fe9d7dc commit 155d8e4
Show file tree
Hide file tree
Showing 29 changed files with 160 additions and 105 deletions.
6 changes: 3 additions & 3 deletions btcclient/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import (
type BTCClient interface {
Stop()
WaitForShutdown()
GetBestBlock() (uint64, error)
GetBestBlock() (uint32, error)
GetBlockByHash(blockHash *chainhash.Hash) (*types.IndexedBlock, *wire.MsgBlock, error)
FindTailBlocksByHeight(height uint64) ([]*types.IndexedBlock, error)
GetBlockByHeight(height uint64) (*types.IndexedBlock, *wire.MsgBlock, error)
FindTailBlocksByHeight(height uint32) ([]*types.IndexedBlock, error)
GetBlockByHeight(height uint32) (*types.IndexedBlock, *wire.MsgBlock, error)
GetTxOut(txHash *chainhash.Hash, index uint32, mempool bool) (*btcjson.GetTxOutResult, error)
SendRawTransaction(tx *wire.MsgTx, allowHighFees bool) (*chainhash.Hash, error)
GetTransaction(txHash *chainhash.Hash) (*btcjson.GetTransactionResult, error)
Expand Down
18 changes: 9 additions & 9 deletions btcclient/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import (
)

// GetBestBlock returns the height of the best block
func (c *Client) GetBestBlock() (uint64, error) {
func (c *Client) GetBestBlock() (uint32, error) {
height, err := c.getBlockCountWithRetry()
if err != nil {
return 0, err
}

return uint64(height), nil
return uint32(height), nil
}

func (c *Client) GetBlockByHash(blockHash *chainhash.Hash) (*types.IndexedBlock, *wire.MsgBlock, error) {
Expand All @@ -38,7 +38,7 @@ func (c *Client) GetBlockByHash(blockHash *chainhash.Hash) (*types.IndexedBlock,
}

// GetBlockByHeight returns a block with the given height
func (c *Client) GetBlockByHeight(height uint64) (*types.IndexedBlock, *wire.MsgBlock, error) {
func (c *Client) GetBlockByHeight(height uint32) (*types.IndexedBlock, *wire.MsgBlock, error) {
blockHash, err := c.getBlockHashWithRetry(height)
if err != nil {
return nil, nil, fmt.Errorf("failed to get block by height %d: %w", height, err)
Expand Down Expand Up @@ -78,7 +78,7 @@ func (c *Client) getBestBlockHashWithRetry() (*chainhash.Hash, error) {
return blockHash, nil
}

func (c *Client) getBlockHashWithRetry(height uint64) (*chainhash.Hash, error) {
func (c *Client) getBlockHashWithRetry(height uint32) (*chainhash.Hash, error) {
var (
blockHash *chainhash.Hash
err error
Expand All @@ -96,7 +96,7 @@ func (c *Client) getBlockHashWithRetry(height uint64) (*chainhash.Hash, error) {
retry.Attempts(c.maxRetryTimes),
); err != nil {
c.logger.Debug(
"failed to query the block hash", zap.Uint64("height", height), zap.Error(err))
"failed to query the block hash", zap.Uint32("height", height), zap.Error(err))
}

return blockHash, nil
Expand Down Expand Up @@ -152,8 +152,8 @@ func (c *Client) getBlockVerboseWithRetry(hash *chainhash.Hash) (*btcjson.GetBlo

// getChainBlocks returns a chain of indexed blocks from the block at baseHeight to the tipBlock
// note: the caller needs to ensure that tipBlock is on the blockchain
func (c *Client) getChainBlocks(baseHeight uint64, tipBlock *types.IndexedBlock) ([]*types.IndexedBlock, error) {
tipHeight := uint64(tipBlock.Height)
func (c *Client) getChainBlocks(baseHeight uint32, tipBlock *types.IndexedBlock) ([]*types.IndexedBlock, error) {
tipHeight := uint32(tipBlock.Height)
if tipHeight < baseHeight {
return nil, fmt.Errorf("the tip block height %v is less than the base height %v", tipHeight, baseHeight)
}
Expand Down Expand Up @@ -195,13 +195,13 @@ func (c *Client) getBestIndexedBlock() (*types.IndexedBlock, error) {
}

// FindTailBlocksByHeight returns the chain of blocks from the block at baseHeight to the tip
func (c *Client) FindTailBlocksByHeight(baseHeight uint64) ([]*types.IndexedBlock, error) {
func (c *Client) FindTailBlocksByHeight(baseHeight uint32) ([]*types.IndexedBlock, error) {
tipIb, err := c.getBestIndexedBlock()
if err != nil {
return nil, err
}

if baseHeight > uint64(tipIb.Height) {
if baseHeight > uint32(tipIb.Height) {
return nil, fmt.Errorf("invalid base height %d, should not be higher than tip block %d", baseHeight, tipIb.Height)
}

Expand Down
2 changes: 2 additions & 0 deletions btcstaking-tracker/btcslasher/bootstrapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ func FuzzSlasher_Bootstrapping(f *testing.F) {
covPks,
bsParams.Params.CovenantQuorum,
slashingPkScript,
1000,
100,
1100,
delAmount,
Expand Down Expand Up @@ -150,6 +151,7 @@ func FuzzSlasher_Bootstrapping(f *testing.F) {
covPks,
bsParams.Params.CovenantQuorum,
slashingPkScript,
1000,
100,
1100,
delAmount,
Expand Down
7 changes: 4 additions & 3 deletions btcstaking-tracker/btcslasher/slasher.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package btcslasher
import (
"context"
"fmt"
"sync"
"time"

"github.com/babylonlabs-io/vigilante/types"
"github.com/decred/dcrd/dcrec/secp256k1/v4"
"golang.org/x/sync/semaphore"
"sync"
"time"

bbn "github.com/babylonlabs-io/babylon/types"
bstypes "github.com/babylonlabs-io/babylon/x/btcstaking/types"
Expand Down Expand Up @@ -35,7 +36,7 @@ type BTCSlasher struct {

// parameters
netParams *chaincfg.Params
btcFinalizationTimeout uint64
btcFinalizationTimeout uint32
retrySleepTime time.Duration
maxRetrySleepTime time.Duration
maxRetryTimes uint
Expand Down
3 changes: 3 additions & 0 deletions btcstaking-tracker/btcslasher/slasher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ func FuzzSlasher(f *testing.F) {
covenantBtcPks,
bsParams.Params.CovenantQuorum,
slashingPkScript,
999,
100,
1099,
delAmount,
Expand Down Expand Up @@ -142,6 +143,7 @@ func FuzzSlasher(f *testing.F) {
covenantBtcPks,
bsParams.Params.CovenantQuorum,
slashingPkScript,
1000,
100,
1100,
delAmount,
Expand Down Expand Up @@ -169,6 +171,7 @@ func FuzzSlasher(f *testing.F) {
covenantBtcPks,
bsParams.Params.CovenantQuorum,
slashingPkScript,
1000,
100,
1100,
delAmount,
Expand Down
2 changes: 2 additions & 0 deletions btcstaking-tracker/btcslasher/slasher_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ func BuildUnbondingSlashingTxWithWitness(
0,
delSlashingSig,
covAdaptorSigs,
bsParams.CovenantQuorum,
slashingSpendInfo,
)
if err != nil {
Expand Down Expand Up @@ -364,6 +365,7 @@ func BuildSlashingTxWithWitness(
d.StakingOutputIdx,
delSigSlash,
covAdaptorSigs,
bsParams.CovenantQuorum,
slashingSpendInfo,
)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package stakingeventwatcher
import (
"context"
"fmt"

btcctypes "github.com/babylonlabs-io/babylon/x/btccheckpoint/types"

"cosmossdk.io/errors"
Expand All @@ -18,7 +19,7 @@ import (
type Delegation struct {
StakingTx *wire.MsgTx
StakingOutputIdx uint32
DelegationStartHeight uint64
DelegationStartHeight uint32
UnbondingOutput *wire.TxOut
HasProof bool
}
Expand Down
9 changes: 5 additions & 4 deletions btcstaking-tracker/stakingeventwatcher/stakingeventwatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import (
"context"
"errors"
"fmt"
"sync"
"sync/atomic"
"time"

btcctypes "github.com/babylonlabs-io/babylon/x/btccheckpoint/types"
btcstakingtypes "github.com/babylonlabs-io/babylon/x/btcstaking/types"
"github.com/babylonlabs-io/vigilante/btcclient"
"github.com/babylonlabs-io/vigilante/types"
"sync"
"sync/atomic"
"time"

"github.com/avast/retry-go/v4"
"github.com/babylonlabs-io/vigilante/config"
Expand Down Expand Up @@ -50,7 +51,7 @@ type newDelegation struct {
stakingTxHash chainhash.Hash
stakingTx *wire.MsgTx
stakingOutputIdx uint32
delegationStartHeight uint64
delegationStartHeight uint32
unbondingOutput *wire.TxOut
}

Expand Down
4 changes: 2 additions & 2 deletions btcstaking-tracker/stakingeventwatcher/tracked_delegations.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type TrackedDelegation struct {
StakingTx *wire.MsgTx
StakingOutputIdx uint32
UnbondingOutput *wire.TxOut
DelegationStartHeight uint64
DelegationStartHeight uint32
}

type TrackedDelegations struct {
Expand Down Expand Up @@ -61,7 +61,7 @@ func (td *TrackedDelegations) AddDelegation(
StakingTx *wire.MsgTx,
StakingOutputIdx uint32,
UnbondingOutput *wire.TxOut,
delegationStartHeight uint64,
delegationStartHeight uint32,
shouldUpdate bool,
) (*TrackedDelegation, error) {
delegation := &TrackedDelegation{
Expand Down
11 changes: 6 additions & 5 deletions e2etest/test_manager_btcstaking.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ package e2etest
import (
"bytes"
"context"
sdkmath "cosmossdk.io/math"
"encoding/hex"
"fmt"
"math/rand"
"testing"
"time"

sdkmath "cosmossdk.io/math"
"github.com/avast/retry-go/v4"
"github.com/babylonlabs-io/babylon/btcstaking"
txformat "github.com/babylonlabs-io/babylon/btctxformatter"
Expand All @@ -29,9 +33,6 @@ import (
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/cosmos/relayer/v2/relayer/provider"
"github.com/stretchr/testify/require"
"math/rand"
"testing"
"time"
)

var (
Expand All @@ -43,7 +44,7 @@ var (
)
)

func (tm *TestManager) getBTCUnbondingTime(t *testing.T) uint64 {
func (tm *TestManager) getBTCUnbondingTime(t *testing.T) uint32 {
bsParams, err := tm.BabylonClient.BTCStakingParams()
require.NoError(t, err)
btccParams, err := tm.BabylonClient.BTCCheckpointParams()
Expand Down
3 changes: 2 additions & 1 deletion monitor/btcscanner/block_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package btcscanner
import (
"errors"
"fmt"

"github.com/btcsuite/btcd/wire"
"github.com/lightningnetwork/lnd/chainntnfs"
)
Expand Down Expand Up @@ -91,7 +92,7 @@ func (bs *BtcScanner) handleNewBlock(height int32, header *wire.BlockHeader) err
bs.unconfirmedBlockCache.Add(ib)

// still unconfirmed
if bs.unconfirmedBlockCache.Size() <= bs.k {
if uint32(bs.unconfirmedBlockCache.Size()) <= bs.k {
return nil
}

Expand Down
31 changes: 16 additions & 15 deletions monitor/btcscanner/btc_scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package btcscanner

import (
"fmt"
notifier "github.com/lightningnetwork/lnd/chainntnfs"
"sync"

notifier "github.com/lightningnetwork/lnd/chainntnfs"

"github.com/babylonlabs-io/babylon/btctxformatter"
ckpttypes "github.com/babylonlabs-io/babylon/x/checkpointing/types"
"github.com/btcsuite/btcd/wire"
Expand All @@ -19,12 +20,12 @@ import (
var _ Scanner = (*BtcScanner)(nil)

type Scanner interface {
Start(startHeight uint64)
Start(startHeight uint32)
Stop()

GetCheckpointsChan() chan *types.CheckpointRecord
GetConfirmedBlocksChan() chan *types.IndexedBlock
GetBaseHeight() uint64
GetBaseHeight() uint32
}

type BtcScanner struct {
Expand All @@ -36,9 +37,9 @@ type BtcScanner struct {
btcNotifier notifier.ChainNotifier

// the BTC height the scanner starts
baseHeight uint64
baseHeight uint32
// the BTC confirmation depth
k uint64
k uint32

confirmedTipBlock *types.IndexedBlock
confirmedBlocksChan chan *types.IndexedBlock
Expand Down Expand Up @@ -77,7 +78,7 @@ func New(
logger: parentLogger.With(zap.String("module", "btcscanner")).Sugar(),
btcClient: btcClient,
btcNotifier: btcNotifier,
k: monitorCfg.BtcConfirmationDepth,
k: uint32(monitorCfg.BtcConfirmationDepth),
ckptCache: ckptCache,
unconfirmedBlockCache: unconfirmedBlockCache,
confirmedBlocksChan: confirmedBlocksChan,
Expand All @@ -89,7 +90,7 @@ func New(
}

// Start starts the scanning process from curBTCHeight to tipHeight
func (bs *BtcScanner) Start(startHeight uint64) {
func (bs *BtcScanner) Start(startHeight uint32) {
if bs.started.Load() {
bs.logger.Info("the BTC scanner is already started")
return
Expand Down Expand Up @@ -136,15 +137,15 @@ func (bs *BtcScanner) Start(startHeight uint64) {
// Bootstrap syncs with BTC by getting the confirmed blocks and the caching the unconfirmed blocks
func (bs *BtcScanner) Bootstrap() {
var (
firstUnconfirmedHeight uint64
firstUnconfirmedHeight uint32
confirmedBlock *types.IndexedBlock
err error
)

if bs.confirmedTipBlock != nil {
firstUnconfirmedHeight = uint64(bs.confirmedTipBlock.Height + 1)
firstUnconfirmedHeight = uint32(bs.confirmedTipBlock.Height + 1)
} else {
firstUnconfirmedHeight = bs.GetBaseHeight()
firstUnconfirmedHeight = uint32(bs.GetBaseHeight())
}

bs.logger.Infof("the bootstrapping starts at %d", firstUnconfirmedHeight)
Expand Down Expand Up @@ -250,7 +251,7 @@ func (bs *BtcScanner) matchAndPop() (*types.CheckpointRecord, error) {

return &types.CheckpointRecord{
RawCheckpoint: rawCheckpoint,
FirstSeenBtcHeight: uint64(ckptSegments.Segments[0].AssocBlock.Height),
FirstSeenBtcHeight: uint32(ckptSegments.Segments[0].AssocBlock.Height),
}, nil
}

Expand Down Expand Up @@ -283,13 +284,13 @@ func (bs *BtcScanner) Stop() {
close(bs.quit)
}

func (bs *BtcScanner) GetBaseHeight() uint64 {
func (bs *BtcScanner) GetBaseHeight() uint32 {
bs.mu.Lock()
defer bs.mu.Unlock()
return bs.baseHeight
}

func (bs *BtcScanner) SetBaseHeight(h uint64) {
func (bs *BtcScanner) SetBaseHeight(h uint32) {
bs.mu.Lock()
defer bs.mu.Unlock()
bs.baseHeight = h
Expand All @@ -306,12 +307,12 @@ func (bs *BtcScanner) SetBtcClient(c btcclient.BTCClient) {
}

// GetK returns the value of k - confirmation depth
func (bs *BtcScanner) GetK() uint64 {
func (bs *BtcScanner) GetK() uint32 {
return bs.k
}

// SetK sets the value of k - confirmation depth
func (bs *BtcScanner) SetK(k uint64) {
func (bs *BtcScanner) SetK(k uint32) {
bs.k = k
}

Expand Down
Loading

0 comments on commit 155d8e4

Please sign in to comment.