Skip to content

Commit

Permalink
multi: enforce new linter rules
Browse files Browse the repository at this point in the history
  • Loading branch information
guggero committed Apr 25, 2022
1 parent b88e11b commit 9130a5e
Show file tree
Hide file tree
Showing 20 changed files with 80 additions and 68 deletions.
30 changes: 16 additions & 14 deletions blockmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,6 @@ type blockManager struct { // nolint:maligned
// newBlockManager returns a new bitcoin block manager. Use Start to begin
// processing asynchronous block and inv updates.
func newBlockManager(cfg *blockManagerCfg) (*blockManager, error) {

targetTimespan := int64(cfg.ChainParams.TargetTimespan / time.Second)
targetTimePerBlock := int64(cfg.ChainParams.TargetTimePerBlock / time.Second)
adjustmentFactor := cfg.ChainParams.RetargetAdjustmentFactor
Expand Down Expand Up @@ -509,8 +508,8 @@ waitForHeaders:
case <-b.quit:
b.newHeadersSignal.L.Unlock()
return
default:

default:
}

// Re-acquire the lock in order to check for the filter header
Expand Down Expand Up @@ -547,7 +546,6 @@ waitForHeaders:
// from all peers. We can go on and just request the cfheaders.
var goodCheckpoints []*chainhash.Hash
for len(goodCheckpoints) == 0 && lastHeight >= wire.CFCheckptInterval {

// Quit if requested.
select {
case <-b.quit:
Expand Down Expand Up @@ -1272,10 +1270,10 @@ func (b *blockManager) writeCFHeadersMsg(msg *wire.MsgCFHeaders,

// rollBackToHeight rolls back all blocks until it hits the specified height.
// It sends notifications along the way.
func (b *blockManager) rollBackToHeight(height uint32) (*headerfs.BlockStamp, error) {
func (b *blockManager) rollBackToHeight(height uint32) error {
header, headerHeight, err := b.cfg.BlockHeaders.ChainTip()
if err != nil {
return nil, err
return err
}
bs := &headerfs.BlockStamp{
Height: int32(headerHeight),
Expand All @@ -1285,13 +1283,13 @@ func (b *blockManager) rollBackToHeight(height uint32) (*headerfs.BlockStamp, er

_, regHeight, err := b.cfg.RegFilterHeaders.ChainTip()
if err != nil {
return nil, err
return err
}

for uint32(bs.Height) > height {
header, headerHeight, err := b.cfg.BlockHeaders.FetchHeader(&bs.Hash)
if err != nil {
return nil, err
return err
}

newTip := &header.PrevBlock
Expand All @@ -1300,14 +1298,14 @@ func (b *blockManager) rollBackToHeight(height uint32) (*headerfs.BlockStamp, er
if uint32(bs.Height) <= regHeight {
newFilterTip, err := b.cfg.RegFilterHeaders.RollbackLastBlock(newTip)
if err != nil {
return nil, err
return err
}
regHeight = uint32(newFilterTip.Height)
}

bs, err = b.cfg.BlockHeaders.RollbackLastBlock()
if err != nil {
return nil, err
return err
}

// Notifications are asynchronous, so we include the previous
Expand All @@ -1316,15 +1314,16 @@ func (b *blockManager) rollBackToHeight(height uint32) (*headerfs.BlockStamp, er
// can't read it before it's deleted from the store.
prevHeader, _, err := b.cfg.BlockHeaders.FetchHeader(newTip)
if err != nil {
return nil, err
return err
}

// Now we send the block disconnected notifications.
b.onBlockDisconnected(
*header, headerHeight, *prevHeader,
)
}
return bs, nil

return nil
}

// minCheckpointHeight returns the height of the last filter checkpoint for the
Expand Down Expand Up @@ -1858,6 +1857,7 @@ func (b *blockManager) fetchFilterFromAllPeers(
// Ignore this message.
if blockHash != response.BlockHash ||
filterType != response.FilterType {

return
}

Expand Down Expand Up @@ -1903,6 +1903,7 @@ func (b *blockManager) getCheckpts(lastHash *chainhash.Hash,
if isCheckpoint {
if m.FilterType == fType &&
m.StopHash == *lastHash {

checkpoints[sp.Addr()] = m.FilterHeaders
close(peerQuit)
}
Expand Down Expand Up @@ -2592,7 +2593,7 @@ func (b *blockManager) handleHeadersMsg(hmsg *headersMsg) {
b.syncPeerMutex.Lock()
b.syncPeer = hmsg.peer
b.syncPeerMutex.Unlock()
_, err = b.rollBackToHeight(backHeight)
err = b.rollBackToHeight(backHeight)
if err != nil {
panic(fmt.Sprintf("Rollback failed: %s", err))
// Should we panic here?
Expand Down Expand Up @@ -2644,7 +2645,7 @@ func (b *blockManager) handleHeadersMsg(hmsg *headersMsg) {
prevCheckpoint.Height,
prevCheckpoint.Hash)

_, err := b.rollBackToHeight(uint32(
err := b.rollBackToHeight(uint32(
prevCheckpoint.Height),
)
if err != nil {
Expand Down Expand Up @@ -2708,6 +2709,7 @@ func (b *blockManager) handleHeadersMsg(hmsg *headersMsg) {
// checkHeaderSanity checks the PoW, and timestamp of a block header.
func (b *blockManager) checkHeaderSanity(blockHeader *wire.BlockHeader,
maxTimestamp time.Time, reorgAttempt bool) error {

diff, err := b.calcNextRequiredDifficulty(
blockHeader.Timestamp, reorgAttempt)
if err != nil {
Expand Down Expand Up @@ -2848,7 +2850,7 @@ func (b *blockManager) findPrevTestNetDifficulty(hList headerlist.Chain) (uint32
iterNode := &startNode.Header
iterHeight := startNode.Height
for iterNode != nil && iterHeight%b.blocksPerRetarget != 0 &&
iterNode.Bits == b.cfg.ChainParams.PowLimitBits {
iterNode.Bits == b.cfg.ChainParams.PowLimitBits { // nolint

// Get the previous block node. This function is used over
// simply accessing iterNode.parent directly as it will
Expand Down
4 changes: 1 addition & 3 deletions blockmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,6 @@ func generateResponses(msgs []wire.Message,

var prevFilterHeader chainhash.Hash
switch q.StartHeight {

// If the start height is 1 the prevFilterHeader is set to the
// genesis header.
case 1:
Expand Down Expand Up @@ -426,7 +425,6 @@ func TestBlockManagerInitialInterval(t *testing.T) {
"%v", index, testDesc)
return
}

}
errChan <- nil
}()
Expand Down Expand Up @@ -657,7 +655,6 @@ func TestBlockManagerInvalidInterval(t *testing.T) {

errChan := make(chan error, 1)
go func() {

// Check that the success of the callback match what we
// expect.
for i := range responses {
Expand Down Expand Up @@ -726,6 +723,7 @@ func buildNonPushScriptFilter(block *wire.MsgBlock) (*gcs.Filter, error) {
// had a push-only script.
if txOut.PkScript[0] == txscript.OP_RETURN &&
txscript.IsPushOnlyScript(txOut.PkScript[1:]) {

continue
}

Expand Down
9 changes: 4 additions & 5 deletions headerfs/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ func newHeaderIndex(db walletdb.DB, indexType HeaderType) (*headerIndex, error)
err := walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
_, err := tx.CreateTopLevelBucket(indexBucket)
return err

})
if err != nil && err != walletdb.ErrBucketExists {
return nil, err
Expand Down Expand Up @@ -285,8 +284,8 @@ func (h *headerIndex) chainTipWithTx(tx walletdb.ReadTx) (*chainhash.Hash,
// truncateIndex truncates the index for a particular header type by a single
// header entry. The passed newTip pointer should point to the hash of the new
// chain tip. Optionally, if the entry is to be deleted as well, then the
// delete flag should be set to true.
func (h *headerIndex) truncateIndex(newTip *chainhash.Hash, delete bool) error {
// remove flag should be set to true.
func (h *headerIndex) truncateIndex(newTip *chainhash.Hash, remove bool) error {
return walletdb.Update(h.db, func(tx walletdb.ReadWriteTx) error {
rootBucket := tx.ReadWriteBucket(indexBucket)

Expand All @@ -304,10 +303,10 @@ func (h *headerIndex) truncateIndex(newTip *chainhash.Hash, delete bool) error {
return fmt.Errorf("unknown index type: %v", h.indexType)
}

// If the delete flag is set, then we'll also delete this entry
// If the remove flag is set, then we'll also delete this entry
// from the database as the primary index (block headers) is
// being rolled back.
if delete {
if remove {
prevTipHash := rootBucket.Get(tipKey)
err := delHeaderEntry(rootBucket, prevTipHash)
if err != nil {
Expand Down
16 changes: 8 additions & 8 deletions headerfs/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
_ "github.com/btcsuite/btcwallet/walletdb/bdb"
)

func createTestIndex() (func(), *headerIndex, error) {
func createTestIndex(t testing.TB) (func(), *headerIndex, error) {
tempDir, err := ioutil.TempDir("", "neutrino")
if err != nil {
return nil, nil, err
Expand All @@ -31,7 +31,7 @@ func createTestIndex() (func(), *headerIndex, error) {
cleanUp := func() {
_ = db.Close()
fi, _ := os.Stat(tempDir + "/test.db")
fmt.Printf("DB file size at cleanup: %d bytes\n", fi.Size())
t.Logf("DB file size at cleanup: %d bytes\n", fi.Size())
_ = os.RemoveAll(tempDir)
}

Expand All @@ -44,7 +44,7 @@ func createTestIndex() (func(), *headerIndex, error) {
}

func TestAddHeadersIndexRetrieve(t *testing.T) {
cleanUp, hIndex, err := createTestIndex()
cleanUp, hIndex, err := createTestIndex(t)
defer cleanUp()
if err != nil {
t.Fatalf("unable to create test db: %v", err)
Expand Down Expand Up @@ -115,7 +115,7 @@ func TestAddHeadersIndexRetrieve(t *testing.T) {
// location in the bbolt database for reduced memory consumption don't impact
// existing users that already have entries in their database.
func TestHeaderStorageFallback(t *testing.T) {
cleanUp, hIndex, err := createTestIndex()
cleanUp, hIndex, err := createTestIndex(t)
if err != nil {
t.Fatalf("unable to create test db: %v", err)
}
Expand Down Expand Up @@ -238,7 +238,7 @@ func BenchmarkWriteHeadersSmallBatch(b *testing.B) {
numBatches = 5000
)
for n := 0; n < b.N; n++ {
cleanUp, hIndex, err := createTestIndex()
cleanUp, hIndex, err := createTestIndex(b)
if err != nil {
b.Fatalf("unable to create test db: %v", err)
}
Expand All @@ -262,7 +262,7 @@ func BenchmarkWriteHeadersMediumBatch(b *testing.B) {
numBatches = 250
)
for n := 0; n < b.N; n++ {
cleanUp, hIndex, err := createTestIndex()
cleanUp, hIndex, err := createTestIndex(b)
if err != nil {
b.Fatalf("unable to create test db: %v", err)
}
Expand All @@ -286,7 +286,7 @@ func BenchmarkWriteHeadersLargeBatch(b *testing.B) {
numBatches = 50
)
for n := 0; n < b.N; n++ {
cleanUp, hIndex, err := createTestIndex()
cleanUp, hIndex, err := createTestIndex(b)
if err != nil {
b.Fatalf("unable to create test db: %v", err)
}
Expand All @@ -306,7 +306,7 @@ func BenchmarkWriteHeadersLargeBatch(b *testing.B) {
// index with a hash.
func BenchmarkHeightLookupLatency(b *testing.B) {
// Start by creating an index with 10k headers.
cleanUp, hIndex, err := createTestIndex()
cleanUp, hIndex, err := createTestIndex(b)
if err != nil {
b.Fatalf("unable to create test db: %v", err)
}
Expand Down
1 change: 0 additions & 1 deletion headerfs/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,6 @@ func NewFilterHeaderStore(filePath string, db walletdb.DB,
// If the size of the file is zero, then this means that we haven't yet
// written the initial genesis header to disk, so we'll do so now.
if fileInfo.Size() == 0 {

var genesisFilterHash chainhash.Hash
switch filterType {
case RegularFilter:
Expand Down
1 change: 1 addition & 0 deletions headerfs/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

func createTestBlockHeaderStore() (func(), walletdb.DB, string,
*blockHeaderStore, error) {

tempDir, err := ioutil.TempDir("", "store_test")
if err != nil {
return nil, nil, "", nil, err
Expand Down
1 change: 0 additions & 1 deletion headerlist/bounded_header_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ func TestBoundedMemoryChainSizeLimit(t *testing.T) {
}
}
}

}

// TestBoundedMemoryChainPrevIteration tests that once we insert elements, we
Expand Down
6 changes: 4 additions & 2 deletions mock_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ func newMockBlockHeaderStore() *mockBlockHeaderStore {

func (m *mockBlockHeaderStore) ChainTip() (*wire.BlockHeader,
uint32, error) {
return nil, 0, nil

return nil, 0, nil
}
func (m *mockBlockHeaderStore) LatestBlockLocator() (
blockchain.BlockLocator, error) {

return nil, nil
}

Expand All @@ -58,15 +59,16 @@ func (m *mockBlockHeaderStore) FetchHeaderAncestors(uint32,
}
func (m *mockBlockHeaderStore) HeightFromHash(*chainhash.Hash) (uint32, error) {
return 0, nil

}
func (m *mockBlockHeaderStore) RollbackLastBlock() (*headerfs.BlockStamp,
error) {

return nil, nil
}

func (m *mockBlockHeaderStore) FetchHeader(h *chainhash.Hash) (
*wire.BlockHeader, uint32, error) {

if header, ok := m.headers[*h]; ok {
return &header, 0, nil
}
Expand Down
Loading

0 comments on commit 9130a5e

Please sign in to comment.