From c967d67295c7ad91f95534da7be2d0756fc5516b Mon Sep 17 00:00:00 2001 From: David Hill Date: Sun, 11 Oct 2020 13:53:42 -0400 Subject: [PATCH] blockchain: fix errorlint warnings --- blockchain/chain.go | 2 -- blockchain/common_test.go | 8 +++--- blockchain/fullblocks_test.go | 8 +++--- blockchain/indexers/common.go | 6 ++--- .../stake/internal/ticketdb/chainio_test.go | 17 ++++++------ blockchain/stake/staketx_test.go | 27 ++++++++++--------- blockchain/stake/tickets_test.go | 22 +++++++-------- blockchain/treasury.go | 23 +++++++++------- blockchain/validate.go | 13 ++++----- 9 files changed, 63 insertions(+), 63 deletions(-) diff --git a/blockchain/chain.go b/blockchain/chain.go index 3c157851ea..079180e469 100644 --- a/blockchain/chain.go +++ b/blockchain/chain.go @@ -1151,8 +1151,6 @@ func (b *BlockChain) reorganizeChainInternal(targetTip *blockNode) error { if err != nil { return err } - - tip = n } return nil diff --git a/blockchain/common_test.go b/blockchain/common_test.go index f2302b9cce..6b1c6a15cf 100644 --- a/blockchain/common_test.go +++ b/blockchain/common_test.go @@ -65,7 +65,7 @@ func chainSetup(dbName string, params *chaincfg.Params) (*BlockChain, func(), er if testDbType == "memdb" { ndb, err := database.Create(testDbType) if err != nil { - return nil, nil, fmt.Errorf("error creating db: %v", err) + return nil, nil, fmt.Errorf("error creating db: %w", err) } db = ndb @@ -78,7 +78,7 @@ func chainSetup(dbName string, params *chaincfg.Params) (*BlockChain, func(), er // Create the directory for test database. dbPath, err := ioutil.TempDir("", dbName) if err != nil { - err := fmt.Errorf("unable to create test db path: %v", + err := fmt.Errorf("unable to create test db path: %w", err) return nil, nil, err } @@ -87,7 +87,7 @@ func chainSetup(dbName string, params *chaincfg.Params) (*BlockChain, func(), er ndb, err := database.Create(testDbType, dbPath, blockDataNet) if err != nil { os.RemoveAll(dbPath) - return nil, nil, fmt.Errorf("error creating db: %v", err) + return nil, nil, fmt.Errorf("error creating db: %w", err) } db = ndb @@ -120,7 +120,7 @@ func chainSetup(dbName string, params *chaincfg.Params) (*BlockChain, func(), er if err != nil { teardown() - err := fmt.Errorf("failed to create chain instance: %v", err) + err := fmt.Errorf("failed to create chain instance: %w", err) return nil, nil, err } diff --git a/blockchain/fullblocks_test.go b/blockchain/fullblocks_test.go index efd04812be..105e01d013 100644 --- a/blockchain/fullblocks_test.go +++ b/blockchain/fullblocks_test.go @@ -60,7 +60,7 @@ func chainSetup(dbName string, params *chaincfg.Params) (*blockchain.BlockChain, if testDbType == "memdb" { ndb, err := database.Create(testDbType) if err != nil { - return nil, nil, fmt.Errorf("error creating db: %v", err) + return nil, nil, fmt.Errorf("error creating db: %w", err) } db = ndb @@ -73,7 +73,7 @@ func chainSetup(dbName string, params *chaincfg.Params) (*blockchain.BlockChain, // Create the directory for test database. dbPath, err := ioutil.TempDir("", dbName) if err != nil { - err := fmt.Errorf("unable to create test db path: %v", + err := fmt.Errorf("unable to create test db path: %w", err) return nil, nil, err } @@ -82,7 +82,7 @@ func chainSetup(dbName string, params *chaincfg.Params) (*blockchain.BlockChain, ndb, err := database.Create(testDbType, dbPath, blockDataNet) if err != nil { os.RemoveAll(dbPath) - return nil, nil, fmt.Errorf("error creating db: %v", err) + return nil, nil, fmt.Errorf("error creating db: %w", err) } db = ndb @@ -115,7 +115,7 @@ func chainSetup(dbName string, params *chaincfg.Params) (*blockchain.BlockChain, if err != nil { teardown() - err := fmt.Errorf("failed to create chain instance: %v", err) + err := fmt.Errorf("failed to create chain instance: %w", err) return nil, nil, err } diff --git a/blockchain/indexers/common.go b/blockchain/indexers/common.go index b366149de4..b73b1a85bf 100644 --- a/blockchain/indexers/common.go +++ b/blockchain/indexers/common.go @@ -1,5 +1,5 @@ // Copyright (c) 2016 The btcsuite developers -// Copyright (c) 2016-2019 The Decred developers +// Copyright (c) 2016-2020 The Decred developers // Use of this source code is governed by an ISC // license that can be found in the LICENSE file. @@ -149,8 +149,8 @@ func (e errDeserialize) Error() string { // isDeserializeErr returns whether or not the passed error is an errDeserialize // error. func isDeserializeErr(err error) bool { - _, ok := err.(errDeserialize) - return ok + var derr errDeserialize + return errors.As(err, &derr) } // internalBucket is an abstraction over a database bucket. It is used to make diff --git a/blockchain/stake/internal/ticketdb/chainio_test.go b/blockchain/stake/internal/ticketdb/chainio_test.go index 15c3754f02..4beeb79941 100644 --- a/blockchain/stake/internal/ticketdb/chainio_test.go +++ b/blockchain/stake/internal/ticketdb/chainio_test.go @@ -7,6 +7,7 @@ package ticketdb import ( "bytes" "encoding/hex" + "errors" "io/ioutil" "os" "reflect" @@ -115,11 +116,11 @@ func TestDbInfoDeserializeErrors(t *testing.T) { }, } + var ticketDBErr DBError for _, test := range tests { // Ensure the expected error type is returned. _, err := deserializeDatabaseInfo(test.serialized) - ticketDBErr, ok := err.(DBError) - if !ok { + if !errors.As(err, &ticketDBErr) { t.Errorf("couldn't convert deserializeDatabaseInfo error "+ "to ticket db error (err: %v)", err) continue @@ -208,11 +209,11 @@ func TestBestChainStateDeserializeErrors(t *testing.T) { }, } + var ticketDBErr DBError for _, test := range tests { // Ensure the expected error type is returned. _, err := deserializeBestChainState(test.serialized) - ticketDBErr, ok := err.(DBError) - if !ok { + if !errors.As(err, &ticketDBErr) { t.Errorf("couldn't convert deserializeBestChainState error "+ "to ticket db error (err: %v)", err) continue @@ -306,11 +307,11 @@ func TestBlockUndoDataDeserializingErrors(t *testing.T) { }, } + var ticketDBErr DBError for _, test := range tests { // Ensure the expected error type is returned. _, err := deserializeBlockUndoData(test.serialized) - ticketDBErr, ok := err.(DBError) - if !ok { + if !errors.As(err, &ticketDBErr) { t.Errorf("couldn't convert deserializeBlockUndoData error "+ "to ticket db error (err: %v)", err) continue @@ -395,11 +396,11 @@ func TestTicketHashesDeserializingErrors(t *testing.T) { }, } + var ticketDBErr DBError for _, test := range tests { // Ensure the expected error type is returned. _, err := deserializeTicketHashes(test.serialized) - ticketDBErr, ok := err.(DBError) - if !ok { + if !errors.As(err, &ticketDBErr) { t.Errorf("couldn't convert deserializeTicketHashes error "+ "to ticket db error (err: %v)", err) continue diff --git a/blockchain/stake/staketx_test.go b/blockchain/stake/staketx_test.go index a4b0a5f747..9c262773f9 100644 --- a/blockchain/stake/staketx_test.go +++ b/blockchain/stake/staketx_test.go @@ -665,7 +665,7 @@ func TestSSGenErrors(t *testing.T) { ssgenNoDiscriminator.SetIndex(0) err = stake.CheckSSGen(ssgenNoDiscriminator.MsgTx(), withTreasury) - if err.(stake.RuleError).GetCode() != + if !errors.As(err, &serr) || serr.GetCode() != stake.ErrSSGenInvalidDiscriminatorLength { t.Errorf("CheckSSGen should have returned %v but instead returned %v", stake.ErrSSGenInvalidDiscriminatorLength, err) @@ -680,7 +680,7 @@ func TestSSGenErrors(t *testing.T) { ssgenInvalidDiscriminator.SetIndex(0) err = stake.CheckSSGen(ssgenInvalidDiscriminator.MsgTx(), withTreasury) - if err.(stake.RuleError).GetCode() != + if !errors.As(err, &serr) || serr.GetCode() != stake.ErrSSGenInvalidDiscriminatorLength { t.Errorf("CheckSSGen should have returned %v but instead returned %v", stake.ErrSSGenInvalidDiscriminatorLength, err) @@ -695,7 +695,7 @@ func TestSSGenErrors(t *testing.T) { ssgenInvalidDiscriminator2.SetIndex(0) err = stake.CheckSSGen(ssgenInvalidDiscriminator2.MsgTx(), withTreasury) - if err.(stake.RuleError).GetCode() != + if !errors.As(err, &serr) || serr.GetCode() != stake.ErrSSGenUnknownDiscriminator { t.Errorf("CheckSSGen should have returned %v but instead returned %v", stake.ErrSSGenUnknownDiscriminator, err) @@ -710,7 +710,7 @@ func TestSSGenErrors(t *testing.T) { ssgenInvalidDiscriminator3.SetIndex(0) err = stake.CheckSSGen(ssgenInvalidDiscriminator3.MsgTx(), withTreasury) - if err.(stake.RuleError).GetCode() != + if !errors.As(err, &serr) || serr.GetCode() != stake.ErrSSGenBadGenOuts { t.Errorf("CheckSSGen should have returned %v but instead returned %v", stake.ErrSSGenBadGenOuts, err) @@ -720,7 +720,8 @@ func TestSSGenErrors(t *testing.T) { } // Verify we don't crash in this case as well. _, err = stake.GetSSGenTreasuryVotes(ssgenInvalidDiscriminator3.MsgTx().TxOut[4].PkScript) - if err.(stake.RuleError).GetCode() != stake.ErrSSGenInvalidNullScript { + if !errors.As(err, &serr) || serr.GetCode() != + stake.ErrSSGenInvalidNullScript { t.Error(err) } @@ -730,7 +731,7 @@ func TestSSGenErrors(t *testing.T) { ssgenInvalidTVNoVote.SetIndex(0) err = stake.CheckSSGen(ssgenInvalidTVNoVote.MsgTx(), withTreasury) - if err.(stake.RuleError).GetCode() != + if !errors.As(err, &serr) || serr.GetCode() != stake.ErrSSGenInvalidTVLength { t.Errorf("CheckSSGen should have returned %v but instead returned %v", stake.ErrSSGenInvalidTVLength, err) @@ -745,7 +746,7 @@ func TestSSGenErrors(t *testing.T) { ssgenInvalidTVNoVote2.SetIndex(0) err = stake.CheckSSGen(ssgenInvalidTVNoVote2.MsgTx(), withTreasury) - if err.(stake.RuleError).GetCode() != + if !errors.As(err, &serr) || serr.GetCode() != stake.ErrSSGenInvalidTVLength { t.Errorf("CheckSSGen should have returned %v but instead returned %v", stake.ErrSSGenInvalidTVLength, err) @@ -761,7 +762,7 @@ func TestSSGenErrors(t *testing.T) { ssgenInvalidTVNoVote3.SetIndex(0) err = stake.CheckSSGen(ssgenInvalidTVNoVote3.MsgTx(), withTreasury) - if err.(stake.RuleError).GetCode() != + if !errors.As(err, &serr) || serr.GetCode() != stake.ErrSSGenInvalidTVLength { t.Errorf("CheckSSGen should have returned %v but instead returned %v", stake.ErrSSGenInvalidTVLength, err) @@ -777,7 +778,7 @@ func TestSSGenErrors(t *testing.T) { ssgenInvalidTVNoVote4.SetIndex(0) err = stake.CheckSSGen(ssgenInvalidTVNoVote4.MsgTx(), withTreasury) - if err.(stake.RuleError).GetCode() != + if !errors.As(err, &serr) || serr.GetCode() != stake.ErrSSGenInvalidTVLength { t.Errorf("CheckSSGen should have returned %v but instead returned %v", stake.ErrSSGenInvalidTVLength, err) @@ -793,7 +794,7 @@ func TestSSGenErrors(t *testing.T) { ssgenInvalidTVNoVote5.SetIndex(0) err = stake.CheckSSGen(ssgenInvalidTVNoVote5.MsgTx(), withTreasury) - if err.(stake.RuleError).GetCode() != + if !errors.As(err, &serr) || serr.GetCode() != stake.ErrSSGenInvalidDiscriminatorLength { t.Errorf("CheckSSGen should have returned %v but instead returned %v", stake.ErrSSGenInvalidDiscriminatorLength, err) @@ -808,7 +809,7 @@ func TestSSGenErrors(t *testing.T) { ssgenInvalidTVote.SetIndex(0) err = stake.CheckSSGen(ssgenInvalidTVote.MsgTx(), withTreasury) - if err.(stake.RuleError).GetCode() != + if !errors.As(err, &serr) || serr.GetCode() != stake.ErrSSGenInvalidTreasuryVote { t.Errorf("CheckSSGen should have returned %v but instead returned %v", stake.ErrSSGenInvalidTreasuryVote, err) @@ -823,7 +824,7 @@ func TestSSGenErrors(t *testing.T) { ssgenInvalidTVote2.SetIndex(0) err = stake.CheckSSGen(ssgenInvalidTVote2.MsgTx(), withTreasury) - if err.(stake.RuleError).GetCode() != + if !errors.As(err, &serr) || serr.GetCode() != stake.ErrSSGenInvalidTreasuryVote { t.Errorf("CheckSSGen should have returned %v but instead returned %v", stake.ErrSSGenInvalidTreasuryVote, err) @@ -838,7 +839,7 @@ func TestSSGenErrors(t *testing.T) { ssgenInvalidTVote3.SetIndex(0) err = stake.CheckSSGen(ssgenInvalidTVote3.MsgTx(), withTreasury) - if err.(stake.RuleError).GetCode() != + if !errors.As(err, &serr) || serr.GetCode() != stake.ErrSSGenDuplicateTreasuryVote { t.Errorf("CheckSSGen should have returned %v but instead returned %v", stake.ErrSSGenDuplicateTreasuryVote, err) diff --git a/blockchain/stake/tickets_test.go b/blockchain/stake/tickets_test.go index 215edbcba1..200ca215d9 100644 --- a/blockchain/stake/tickets_test.go +++ b/blockchain/stake/tickets_test.go @@ -719,13 +719,13 @@ func TestTicketDBGeneral(t *testing.T) { // In memory addition test. lotteryIV, err := calcHash256PRNGIVFromHeader(&header) if err != nil { - return fmt.Errorf("failed to calc lottery IV: %v", err) + return fmt.Errorf("failed to calc lottery IV: %w", err) } bestNode, err = bestNode.ConnectNode(lotteryIV, ticketsSpentInBlock(block), revokedTicketsInBlock(block), ticketsToAdd) if err != nil { - return fmt.Errorf("couldn't connect node: %v", err.Error()) + return fmt.Errorf("couldn't connect node: %w", err) } // Write the new node to db. @@ -733,8 +733,8 @@ func TestTicketDBGeneral(t *testing.T) { blockHash := block.Hash() err = WriteConnectedBestNode(dbTx, bestNode, *blockHash) if err != nil { - return fmt.Errorf("failure writing the best node: %v", - err.Error()) + return fmt.Errorf("failure writing the best node: %w", + err) } // Reload the node from DB and make sure it's the same. @@ -742,8 +742,8 @@ func TestTicketDBGeneral(t *testing.T) { loadedNode, err := LoadBestNode(dbTx, bestNode.Height(), *blockHash, header, params) if err != nil { - return fmt.Errorf("failed to load the best node: %v", - err.Error()) + return fmt.Errorf("failed to load the best node: %w", + err) } err = nodesEqual(loadedNode, bestNode) if err != nil { @@ -795,7 +795,7 @@ func TestTicketDBGeneral(t *testing.T) { // Negative test. lotteryIV, err := calcHash256PRNGIVFromHeader(&header) if err != nil { - return fmt.Errorf("failed to calc lottery IV: %v", err) + return fmt.Errorf("failed to calc lottery IV: %w", err) } bestNodeUsingDB, err = formerBestNode.DisconnectNode(lotteryIV, nil, nil, nil) @@ -826,8 +826,8 @@ func TestTicketDBGeneral(t *testing.T) { err := WriteDisconnectedBestNode(dbTx, bestNode, *parentBlockHash, formerBestNode.UndoData()) if err != nil { - return fmt.Errorf("failure writing the best node: %v", - err.Error()) + return fmt.Errorf("failure writing the best node: %w", + err) } return nil @@ -843,8 +843,8 @@ func TestTicketDBGeneral(t *testing.T) { loadedNode, err := LoadBestNode(dbTx, bestNode.Height(), *parentBlockHash, header, params) if err != nil { - return fmt.Errorf("failed to load the best node: %v", - err.Error()) + return fmt.Errorf("failed to load the best node: %w", + err) } err = nodesEqual(loadedNode, bestNode) if err != nil { diff --git a/blockchain/treasury.go b/blockchain/treasury.go index 93707dbd17..ed0db2083a 100644 --- a/blockchain/treasury.go +++ b/blockchain/treasury.go @@ -289,14 +289,14 @@ func deserializeTSpend(data []byte) ([]chainhash.Hash, error) { var count int64 err := binary.Read(buf, byteOrder, &count) if err != nil { - return nil, fmt.Errorf("count %v", err) + return nil, fmt.Errorf("failed to read count: %w", err) } hashes := make([]chainhash.Hash, count) for i := int64(0); i < count; i++ { err := binary.Read(buf, byteOrder, &hashes[i]) if err != nil { return nil, - fmt.Errorf("values read %v error %v", i, err) + fmt.Errorf("failed to read idx %v: %w", i, err) } } @@ -348,12 +348,12 @@ func dbFetchTSpend(dbTx database.Tx, tx chainhash.Hash) ([]chainhash.Hash, error // deduplicate. This is ok because in practice a TX cannot appear in the same // block more than once. func dbUpdateTSpend(dbTx database.Tx, tx, block chainhash.Hash) error { + var derr errDbTSpend hashes, err := dbFetchTSpend(dbTx, tx) - if _, ok := err.(errDbTSpend); ok { - // Record doesn't exist. - } else if err != nil { + if err != nil && !errors.As(err, &derr) { return err } + hashes = append(hashes, block) return dbPutTSpend(dbTx, tx, hashes) } @@ -569,19 +569,19 @@ func verifyTSpendSignature(msgTx *wire.MsgTx, signature, pubKey []byte) error { sigHash, err := txscript.CalcSignatureHash(nil, txscript.SigHashAll, msgTx, 0, nil) if err != nil { - return fmt.Errorf("CalcSignatureHash: %v", err) + return fmt.Errorf("CalcSignatureHash: %w", err) } // Lift Signature from bytes. sig, err := schnorr.ParseSignature(signature) if err != nil { - return fmt.Errorf("ParseSignature: %v", err) + return fmt.Errorf("ParseSignature: %w", err) } // Lift public PI key from bytes. pk, err := schnorr.ParsePubKey(pubKey) if err != nil { - return fmt.Errorf("ParsePubKey: %v", err) + return fmt.Errorf("ParsePubKey: %w", err) } // Verify transaction was properly signed. @@ -610,9 +610,10 @@ func VerifyTSpendSignature(msgTx *wire.MsgTx, signature, pubKey []byte) error { func (b *BlockChain) sumPastTreasuryExpenditure(preTVINode *blockNode, nbBlocks uint64) (int64, *blockNode, error) { node := preTVINode var total int64 + var derr errDbTreasury for i := uint64(0); i < nbBlocks && node != nil; i++ { ts, err := b.dbFetchTreasurySingle(node.hash) - if _, ok := err.(errDbTreasury); ok { + if errors.As(err, &derr) { // Record doesn't exist. Means we reached the end of // when treasury records are available. node = nil @@ -796,8 +797,10 @@ func (b *BlockChain) checkTSpendsExpenditure(preTVINode *blockNode, totalTSpendA // block on the chain of prevNode. func (b *BlockChain) checkTSpendExists(prevNode *blockNode, tspend chainhash.Hash) error { trsyLog.Tracef(" checkTSpendExists: tspend %v", tspend) + + var derr errDbTSpend blocks, err := b.FetchTSpend(tspend) - if _, ok := err.(errDbTSpend); ok { + if errors.As(err, &derr) { // Record does not exist. return nil } else if err != nil { diff --git a/blockchain/validate.go b/blockchain/validate.go index f66474b779..802eb51b02 100644 --- a/blockchain/validate.go +++ b/blockchain/validate.go @@ -535,14 +535,11 @@ func CheckProofOfStake(block *dcrutil.Block, posLimit int64) error { // error kinds, or nil. func standaloneToChainRuleError(err error) error { // Convert standalone package rule errors to blockchain rule errors. - var rErr standalone.RuleError - if errors.As(err, &rErr) { - switch rErr.Err { - case standalone.ErrUnexpectedDifficulty: - return ruleError(ErrUnexpectedDifficulty, rErr.Description) - case standalone.ErrHighHash: - return ruleError(ErrHighHash, rErr.Description) - } + switch { + case errors.Is(err, standalone.ErrUnexpectedDifficulty): + return ruleError(ErrUnexpectedDifficulty, err.Error()) + case errors.Is(err, standalone.ErrHighHash): + return ruleError(ErrHighHash, err.Error()) } return err