Skip to content

Commit

Permalink
fix types
Browse files Browse the repository at this point in the history
  • Loading branch information
Lazar955 committed Nov 4, 2024
1 parent 2805163 commit 6827c6a
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 20 deletions.
2 changes: 1 addition & 1 deletion config/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type MonitorConfig struct {
// Max number of BTC blocks in the buffer
BtcBlockBufferSize uint64 `mapstructure:"btc-block-buffer-size"`
// Max number of BTC blocks in the cache
BtcCacheSize uint64 `mapstructure:"btc-cache-size"`
BtcCacheSize uint32 `mapstructure:"btc-cache-size"`
// Intervals between each liveness check in seconds
LivenessCheckIntervalSeconds uint64 `mapstructure:"liveness-check-interval-seconds"`
// Max lasting BTC heights that a checkpoint is not reported before an alarm is sent
Expand Down
2 changes: 1 addition & 1 deletion config/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const (
// ReporterConfig defines configuration for the reporter.
type ReporterConfig struct {
NetParams string `mapstructure:"netparams"` // should be mainnet|testnet|simnet|signet
BTCCacheSize uint64 `mapstructure:"btc_cache_size"` // size of the BTC cache
BTCCacheSize uint32 `mapstructure:"btc_cache_size"` // size of the BTC cache
MaxHeadersInMsg uint32 `mapstructure:"max_headers_in_msg"` // maximum number of headers in a MsgInsertHeaders message
}

Expand Down
2 changes: 1 addition & 1 deletion monitor/btcscanner/block_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (bs *BtcScanner) handleNewBlock(height uint32, header *wire.BlockHeader) er
// otherwise, add the block to the cache
bs.unconfirmedBlockCache.Add(ib)

if bs.unconfirmedBlockCache.Size() > math.MaxInt64 {
if bs.unconfirmedBlockCache.Size() > math.MaxInt32 {
panic(fmt.Errorf("unconfirmedBlockCache exceeds uint32"))
}

Expand Down
2 changes: 1 addition & 1 deletion monitor/btcscanner/btc_scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func FuzzBootStrap(f *testing.F) {
Return(chainIndexedBlocks[i], nil, nil).AnyTimes()
}

cache, err := types.NewBTCCache(numBlocks)
cache, err := types.NewBTCCache(uint32(numBlocks))
require.NoError(t, err)
var btcScanner btcscanner.BtcScanner
btcScanner.SetBtcClient(mockBtcClient)
Expand Down
2 changes: 1 addition & 1 deletion reporter/bootstrapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (r *Reporter) bootstrap() error {

// trim cache to the latest k+w blocks on BTC (which are same as in BBN)
maxEntries := r.btcConfirmationDepth + r.checkpointFinalizationTimeout
if err = r.btcCache.Resize(uint64(maxEntries)); err != nil {
if err = r.btcCache.Resize(maxEntries); err != nil {
r.logger.Errorf("Failed to resize BTC cache: %v", err)
panic(err)
}
Expand Down
20 changes: 10 additions & 10 deletions types/btccache.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ type BTCCache struct {
sync.RWMutex
}

func NewBTCCache(maxEntries uint64) (*BTCCache, error) {
func NewBTCCache(maxEntries uint32) (*BTCCache, error) {
// if maxEntries is 0, it means that the cache is disabled
if maxEntries == 0 {
return nil, ErrInvalidMaxEntries
}

return &BTCCache{
blocks: make([]*IndexedBlock, 0, maxEntries),
maxEntries: uint32(maxEntries),
maxEntries: maxEntries,
}, nil
}

Expand Down Expand Up @@ -58,10 +58,10 @@ func (b *BTCCache) Add(ib *IndexedBlock) {

// Thread-unsafe version of Add
func (b *BTCCache) add(ib *IndexedBlock) {
if b.size() > b.maxEntries {
if b.size() > int(b.maxEntries) {
panic(ErrTooManyEntries)
}
if b.size() == b.maxEntries {
if b.size() == int(b.maxEntries) {
// dereference the 0-th block to ensure it will be garbage-collected
// see https://stackoverflow.com/questions/55045402/memory-leak-in-golang-slice
b.blocks[0] = nil
Expand Down Expand Up @@ -117,11 +117,11 @@ func (b *BTCCache) RemoveAll() {
}

// Size returns the size of the cache. Thread-safe.
func (b *BTCCache) Size() uint64 {
func (b *BTCCache) Size() int {
b.RLock()
defer b.RUnlock()

return uint64(b.size())
return b.size()
}

// thread-unsafe version of Size
Expand Down Expand Up @@ -188,7 +188,7 @@ func (b *BTCCache) FindBlock(blockHeight uint32) *IndexedBlock {
return nil
}

leftBound := uint32(0)
leftBound := 0
rightBound := b.size() - 1

for leftBound <= rightBound {
Expand All @@ -208,14 +208,14 @@ func (b *BTCCache) FindBlock(blockHeight uint32) *IndexedBlock {
return nil
}

func (b *BTCCache) Resize(maxEntries uint64) error {
func (b *BTCCache) Resize(maxEntries uint32) error {
b.Lock()
defer b.Unlock()

if maxEntries == 0 {
return ErrInvalidMaxEntries
}
b.maxEntries = uint32(maxEntries)
b.maxEntries = maxEntries
return nil
}

Expand All @@ -225,7 +225,7 @@ func (b *BTCCache) Trim() {
defer b.Unlock()

// cache size is smaller than maxEntries, can't trim
if b.size() < b.maxEntries {
if b.size() < int(b.maxEntries) {
return
}

Expand Down
10 changes: 5 additions & 5 deletions types/btccache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func FuzzBtcCache(f *testing.F) {
invalidMaxEntries = true
}

cache, err := types.NewBTCCache(maxEntries)
cache, err := types.NewBTCCache(uint32(maxEntries))
if err != nil {
if !invalidMaxEntries {
t.Fatalf("NewBTCCache returned error %s", err)
Expand Down Expand Up @@ -64,7 +64,7 @@ func FuzzBtcCache(f *testing.F) {
t.Skip("Skipping test with invalid numBlocks")
}

require.Equal(t, numBlocks, cache.Size())
require.Equal(t, numBlocks, uint64(cache.Size()))

// Find a random block in the cache
randIdx := datagen.RandomInt(r, int(numBlocks))
Expand All @@ -82,7 +82,7 @@ func FuzzBtcCache(f *testing.F) {
for _, ib := range blocksToAdd {
cache.Add(ib)
}
require.Equal(t, prevCacheHeight+int32(addCount), cache.Tip().Height)
require.Equal(t, prevCacheHeight+int32(addCount), int32(cache.Tip().Height))
require.Equal(t, blocksToAdd[addCount-1], cache.Tip())

// ensure block heights in cache are in increasing order
Expand Down Expand Up @@ -118,15 +118,15 @@ func FuzzBtcCache(f *testing.F) {
}

// Remove random number of blocks from the cache
prevSize := cache.Size()
prevSize := uint64(cache.Size())
deleteCount := datagen.RandomInt(r, int(prevSize))
cacheBlocksBeforeDeletion := cache.GetAllBlocks()
for i := 0; i < int(deleteCount); i++ {
err = cache.RemoveLast()
require.NoError(t, err)
}
cacheBlocksAfterDeletion := cache.GetAllBlocks()
require.Equal(t, prevSize-deleteCount, cache.Size())
require.Equal(t, prevSize-deleteCount, uint64(cache.Size()))
require.Equal(t, cacheBlocksBeforeDeletion[:len(cacheBlocksBeforeDeletion)-int(deleteCount)], cacheBlocksAfterDeletion)
})
}

0 comments on commit 6827c6a

Please sign in to comment.