diff --git a/config/monitor.go b/config/monitor.go index d5a603a..4332fa7 100644 --- a/config/monitor.go +++ b/config/monitor.go @@ -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 diff --git a/config/reporter.go b/config/reporter.go index 292bf21..a883a11 100644 --- a/config/reporter.go +++ b/config/reporter.go @@ -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 } diff --git a/monitor/btcscanner/block_handler.go b/monitor/btcscanner/block_handler.go index 0884dba..e9ccea4 100644 --- a/monitor/btcscanner/block_handler.go +++ b/monitor/btcscanner/block_handler.go @@ -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")) } diff --git a/monitor/btcscanner/btc_scanner_test.go b/monitor/btcscanner/btc_scanner_test.go index fb5c065..800d2c9 100644 --- a/monitor/btcscanner/btc_scanner_test.go +++ b/monitor/btcscanner/btc_scanner_test.go @@ -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) diff --git a/reporter/bootstrapping.go b/reporter/bootstrapping.go index 58bed29..f2bfc0c 100644 --- a/reporter/bootstrapping.go +++ b/reporter/bootstrapping.go @@ -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) } diff --git a/types/btccache.go b/types/btccache.go index 3f8b81a..973c793 100644 --- a/types/btccache.go +++ b/types/btccache.go @@ -13,7 +13,7 @@ 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 @@ -21,7 +21,7 @@ func NewBTCCache(maxEntries uint64) (*BTCCache, error) { return &BTCCache{ blocks: make([]*IndexedBlock, 0, maxEntries), - maxEntries: uint32(maxEntries), + maxEntries: maxEntries, }, nil } @@ -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 @@ -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 @@ -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 { @@ -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 } @@ -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 } diff --git a/types/btccache_test.go b/types/btccache_test.go index 1622bd7..c218e06 100644 --- a/types/btccache_test.go +++ b/types/btccache_test.go @@ -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) @@ -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)) @@ -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 @@ -118,7 +118,7 @@ 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++ { @@ -126,7 +126,7 @@ func FuzzBtcCache(f *testing.F) { 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) }) }