From d8344fc23876fbf490b5771b16313c6c6dcffef8 Mon Sep 17 00:00:00 2001 From: ChengenH Date: Fri, 13 Dec 2024 17:48:56 +0800 Subject: [PATCH] refactor: unify the error handling methods in the crypto package that are different from the project style Signed-off-by: ChengenH --- api/admin/key_value_reader.go | 3 ++- api/keystore/keystore.go | 2 +- chains/atomic/state.go | 2 +- database/helpers.go | 2 +- database/linkeddb/linkeddb.go | 13 ++++++------ database/pebbledb/db.go | 2 +- indexer/service.go | 3 ++- node/node.go | 2 +- snow/engine/avalanche/bootstrap/queue/jobs.go | 2 +- .../engine/avalanche/bootstrap/queue/state.go | 2 +- snow/engine/snowman/block/batched_vm.go | 4 ++-- snow/engine/snowman/getter/getter.go | 3 ++- snow/uptime/manager.go | 2 +- vms/avm/state/state.go | 8 ++++---- vms/avm/vm.go | 2 +- vms/components/avax/utxo_state.go | 5 +++-- vms/components/chain/state.go | 2 +- vms/components/index/index.go | 2 +- vms/components/keystore/user.go | 5 +++-- vms/platformvm/network/warp.go | 7 ++++--- vms/platformvm/service.go | 4 ++-- vms/platformvm/state/l1_validator.go | 2 +- vms/platformvm/state/state.go | 20 +++++++++---------- .../txs/executor/staker_tx_verification.go | 4 ++-- vms/proposervm/height_indexed_vm.go | 5 +++-- vms/proposervm/state/block_state.go | 2 +- vms/proposervm/vm.go | 6 +++--- vms/rpcchainvm/vm_client.go | 2 +- wallet/chain/c/signer.go | 2 +- wallet/chain/p/signer/visitor.go | 2 +- wallet/chain/x/signer/visitor.go | 4 ++-- x/archivedb/reader.go | 7 +++++-- 32 files changed, 72 insertions(+), 61 deletions(-) diff --git a/api/admin/key_value_reader.go b/api/admin/key_value_reader.go index bfc7b2cced06..10edf45be660 100644 --- a/api/admin/key_value_reader.go +++ b/api/admin/key_value_reader.go @@ -5,6 +5,7 @@ package admin import ( "context" + "errors" "github.com/ava-labs/avalanchego/database" ) @@ -23,7 +24,7 @@ func NewKeyValueReader(client Client) *KeyValueReader { func (r *KeyValueReader) Has(key []byte) (bool, error) { _, err := r.client.DBGet(context.Background(), key) - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { return false, nil } return err == nil, err diff --git a/api/keystore/keystore.go b/api/keystore/keystore.go index ed3c9d21e57e..6d8589351eab 100644 --- a/api/keystore/keystore.go +++ b/api/keystore/keystore.go @@ -367,7 +367,7 @@ func (ks *keystore) getPassword(username string) (*password.Hash, error) { // The user is not in memory; try the database userBytes, err := ks.userDB.Get([]byte(username)) - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { // The user doesn't exist return nil, nil } diff --git a/chains/atomic/state.go b/chains/atomic/state.go index b134d2f7a5d8..d61d4e2ab2fe 100644 --- a/chains/atomic/state.go +++ b/chains/atomic/state.go @@ -147,7 +147,7 @@ func (s *state) SetValue(e *Element) error { // current engine state. func (s *state) RemoveValue(key []byte) error { value, err := s.loadValue(key) - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { // The value doesn't exist, so we should optimistically delete it dbElem := dbElement{Present: false} valueBytes, err := Codec.Marshal(CodecVersion, &dbElem) diff --git a/database/helpers.go b/database/helpers.go index db12878cb56c..7b98fed6e45d 100644 --- a/database/helpers.go +++ b/database/helpers.go @@ -146,7 +146,7 @@ func WithDefault[V any]( def V, ) (V, error) { v, err := get(db, key) - if err == ErrNotFound { + if errors.Is(err, ErrNotFound) { return def, nil } return v, err diff --git a/database/linkeddb/linkeddb.go b/database/linkeddb/linkeddb.go index 4e609d329cfe..7ecffd6ead27 100644 --- a/database/linkeddb/linkeddb.go +++ b/database/linkeddb/linkeddb.go @@ -4,6 +4,7 @@ package linkeddb import ( + "errors" "slices" "sync" @@ -142,7 +143,7 @@ func (ldb *linkedDB) Delete(key []byte) error { defer ldb.lock.Unlock() currentNode, err := ldb.getNode(key) - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { return nil } if err != nil { @@ -205,7 +206,7 @@ func (ldb *linkedDB) Delete(key []byte) error { func (ldb *linkedDB) IsEmpty() (bool, error) { _, err := ldb.HeadKey() - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { return true, nil } return false, err @@ -272,7 +273,7 @@ func (ldb *linkedDB) getHeadKey() ([]byte, error) { ldb.headKey = headKey return headKey, nil } - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { ldb.headKeyIsSynced = true ldb.headKeyExists = false return nil, database.ErrNotFound @@ -308,7 +309,7 @@ func (ldb *linkedDB) getNode(key []byte) (node, error) { } nodeBytes, err := ldb.db.Get(nodeKey(key)) - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { ldb.nodeCache.Put(keyStr, nil) return node{}, err } @@ -380,7 +381,7 @@ func (it *iterator) Next() bool { if !it.initialized { it.initialized = true headKey, err := it.ldb.getHeadKey() - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { it.exhausted = true it.key = nil it.value = nil @@ -397,7 +398,7 @@ func (it *iterator) Next() bool { } nextNode, err := it.ldb.getNode(it.nextKey) - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { it.exhausted = true it.key = nil it.value = nil diff --git a/database/pebbledb/db.go b/database/pebbledb/db.go index 8d4080bb8832..a02a5db79b53 100644 --- a/database/pebbledb/db.go +++ b/database/pebbledb/db.go @@ -139,7 +139,7 @@ func (db *Database) Has(key []byte) (bool, error) { } _, closer, err := db.pebbleDB.Get(key) - if err == pebble.ErrNotFound { + if errors.Is(err, pebble.ErrNotFound) { return false, nil } if err != nil { diff --git a/indexer/service.go b/indexer/service.go index 83f9912f2fea..9f63276f9642 100644 --- a/indexer/service.go +++ b/indexer/service.go @@ -4,6 +4,7 @@ package indexer import ( + "errors" "fmt" "net/http" "time" @@ -139,7 +140,7 @@ func (s *service) IsAccepted(_ *http.Request, args *IsAcceptedArgs, reply *IsAcc reply.IsAccepted = true return nil } - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { reply.IsAccepted = false return nil } diff --git a/node/node.go b/node/node.go index 19dfd899654d..ca458dd2d90e 100644 --- a/node/node.go +++ b/node/node.go @@ -814,7 +814,7 @@ func (n *Node) initDatabase() error { rawExpectedGenesisHash := hashing.ComputeHash256(n.Config.GenesisBytes) rawGenesisHash, err := n.DB.Get(genesisHashKey) - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { rawGenesisHash = rawExpectedGenesisHash err = n.DB.Put(genesisHashKey, rawGenesisHash) } diff --git a/snow/engine/avalanche/bootstrap/queue/jobs.go b/snow/engine/avalanche/bootstrap/queue/jobs.go index 750fe7130b0e..9857db1e5f70 100644 --- a/snow/engine/avalanche/bootstrap/queue/jobs.go +++ b/snow/engine/avalanche/bootstrap/queue/jobs.go @@ -141,7 +141,7 @@ func (j *Jobs) ExecuteAll( } job, err := j.state.RemoveRunnableJob(ctx) - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { break } if err != nil { diff --git a/snow/engine/avalanche/bootstrap/queue/state.go b/snow/engine/avalanche/bootstrap/queue/state.go index 9ad87d682453..04c673030653 100644 --- a/snow/engine/avalanche/bootstrap/queue/state.go +++ b/snow/engine/avalanche/bootstrap/queue/state.go @@ -95,7 +95,7 @@ func newState( func getNumJobs(d database.Database, jobs database.Iteratee) (uint64, error) { numJobs, err := database.GetUInt64(d, numJobsKey) - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { // If we don't have a checkpoint, we need to initialize it. count, err := database.Count(jobs) return uint64(count), err diff --git a/snow/engine/snowman/block/batched_vm.go b/snow/engine/snowman/block/batched_vm.go index ad52e3592ae6..90ea027ac4e4 100644 --- a/snow/engine/snowman/block/batched_vm.go +++ b/snow/engine/snowman/block/batched_vm.go @@ -63,7 +63,7 @@ func GetAncestors( // RemoteVM did not work, try local logic startTime := time.Now() blk, err := vm.GetBlock(ctx, blkID) - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { // Special case ErrNotFound as an empty response: this signals // the client to avoid contacting this node for further ancestors // as they may have been pruned or unavailable due to state-sync. @@ -80,7 +80,7 @@ func GetAncestors( for numFetched := 1; numFetched < maxBlocksNum && time.Since(startTime) < maxBlocksRetrivalTime; numFetched++ { parentID := blk.Parent() blk, err = vm.GetBlock(ctx, parentID) - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { // After state sync we may not have the full chain break } diff --git a/snow/engine/snowman/getter/getter.go b/snow/engine/snowman/getter/getter.go index 7af3d3a2263e..3334ecfe9d75 100644 --- a/snow/engine/snowman/getter/getter.go +++ b/snow/engine/snowman/getter/getter.go @@ -5,6 +5,7 @@ package getter import ( "context" + "errors" "time" "github.com/prometheus/client_golang/prometheus" @@ -115,7 +116,7 @@ func (gh *getter) GetAcceptedStateSummary(ctx context.Context, nodeID ids.NodeID summaryIDs := make([]ids.ID, 0, heights.Len()) for height := range heights { summary, err := gh.ssVM.GetStateSummary(ctx, height) - if err == block.ErrStateSyncableVMNotImplemented { + if errors.Is(err, block.ErrStateSyncableVMNotImplemented) { gh.log.Debug("dropping GetAcceptedStateSummary message", zap.String("reason", "state sync not supported"), zap.Stringer("nodeID", nodeID), diff --git a/snow/uptime/manager.go b/snow/uptime/manager.go index fbace19eaab8..889df779bf38 100644 --- a/snow/uptime/manager.go +++ b/snow/uptime/manager.go @@ -186,7 +186,7 @@ func (m *manager) CalculateUptimePercentFrom(nodeID ids.NodeID, startTime time.T // time that the node has been connected. func (m *manager) updateUptime(nodeID ids.NodeID) error { newDuration, newLastUpdated, err := m.CalculateUptime(nodeID) - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { // We don't track the uptimes of non-validators. return nil } diff --git a/vms/avm/state/state.go b/vms/avm/state/state.go index 4230a4f72bdf..29fc44758e59 100644 --- a/vms/avm/state/state.go +++ b/vms/avm/state/state.go @@ -244,7 +244,7 @@ func (s *state) GetTx(txID ids.ID) (*txs.Tx, error) { } txBytes, err := s.txDB.Get(txID[:]) - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { s.txCache.Put(txID, nil) return nil, database.ErrNotFound } @@ -283,7 +283,7 @@ func (s *state) GetBlockIDAtHeight(height uint64) (ids.ID, error) { heightKey := database.PackUInt64(height) blkID, err := database.GetID(s.blockIDDB, heightKey) - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { s.blockIDCache.Put(height, ids.Empty) return ids.Empty, database.ErrNotFound } @@ -308,7 +308,7 @@ func (s *state) GetBlock(blkID ids.ID) (block.Block, error) { } blkBytes, err := s.blockDB.Get(blkID[:]) - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { s.blockCache.Put(blkID, nil) return nil, database.ErrNotFound } @@ -333,7 +333,7 @@ func (s *state) AddBlock(block block.Block) { func (s *state) InitializeChainState(stopVertexID ids.ID, genesisTimestamp time.Time) error { lastAccepted, err := database.GetID(s.singletonDB, lastAcceptedKey) - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { return s.initializeChainState(stopVertexID, genesisTimestamp) } else if err != nil { return err diff --git a/vms/avm/vm.go b/vms/avm/vm.go index 1026ba7d3de7..09e35d3f71a3 100644 --- a/vms/avm/vm.go +++ b/vms/avm/vm.go @@ -653,7 +653,7 @@ func (vm *VM) onAccept(tx *txs.Tx) error { } utxo, err := vm.state.GetUTXO(utxoID.InputID()) - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { vm.ctx.Log.Debug("dropping utxo from index", zap.Stringer("txID", txID), zap.Stringer("utxoTxID", utxoID.TxID), diff --git a/vms/components/avax/utxo_state.go b/vms/components/avax/utxo_state.go index 9bc648a6e531..02f11b7beab2 100644 --- a/vms/components/avax/utxo_state.go +++ b/vms/components/avax/utxo_state.go @@ -4,6 +4,7 @@ package avax import ( + "errors" "github.com/prometheus/client_golang/prometheus" "github.com/ava-labs/avalanchego/cache" @@ -152,7 +153,7 @@ func (s *utxoState) GetUTXO(utxoID ids.ID) (*UTXO, error) { } bytes, err := s.utxoDB.Get(utxoID[:]) - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { s.utxoCache.Put(utxoID, nil) return nil, database.ErrNotFound } @@ -201,7 +202,7 @@ func (s *utxoState) PutUTXO(utxo *UTXO) error { func (s *utxoState) DeleteUTXO(utxoID ids.ID) error { utxo, err := s.GetUTXO(utxoID) - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { return nil } if err != nil { diff --git a/vms/components/chain/state.go b/vms/components/chain/state.go index 0a01f0d3779a..a5a66918c91b 100644 --- a/vms/components/chain/state.go +++ b/vms/components/chain/state.go @@ -215,7 +215,7 @@ func (s *State) GetBlock(ctx context.Context, blkID ids.ID) (snowman.Block, erro blk, err := s.getBlock(ctx, blkID) // If getBlock returns [database.ErrNotFound], State considers // this a cacheable miss. - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { s.missingBlocks.Put(blkID, struct{}{}) return nil, err } else if err != nil { diff --git a/vms/components/index/index.go b/vms/components/index/index.go index e27c1dffbc60..412db197d54f 100644 --- a/vms/components/index/index.go +++ b/vms/components/index/index.go @@ -214,7 +214,7 @@ func (i *indexer) Read(address []byte, assetID ids.ID, cursor, pageSize uint64) func checkIndexStatus(db database.KeyValueReaderWriter, enableIndexing, allowIncomplete bool) error { // verify whether the index is complete. idxComplete, err := database.GetBool(db, idxCompleteKey) - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { // We've not run before. Mark whether indexing is enabled this run. return database.PutBool(db, idxCompleteKey, enableIndexing) } else if err != nil { diff --git a/vms/components/keystore/user.go b/vms/components/keystore/user.go index 20749e5b48bf..a50af5d48b1f 100644 --- a/vms/components/keystore/user.go +++ b/vms/components/keystore/user.go @@ -4,6 +4,7 @@ package keystore import ( + "errors" "fmt" "io" @@ -63,7 +64,7 @@ func NewUserFromDB(db *encdb.Database) User { func (u *user) GetAddresses() ([]ids.ShortID, error) { // Get user's addresses addressBytes, err := u.db.Get(addressesKey) - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { // If user has no addresses, return empty list return nil, nil } @@ -171,7 +172,7 @@ func GetKeychain(u User, addresses set.Set[ids.ShortID]) (*secp256k1fx.Keychain, kc := secp256k1fx.NewKeychain() for _, addr := range addrsList { sk, err := u.GetKey(addr) - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { continue } if err != nil { diff --git a/vms/platformvm/network/warp.go b/vms/platformvm/network/warp.go index 3787d92382c6..b8097308ab34 100644 --- a/vms/platformvm/network/warp.go +++ b/vms/platformvm/network/warp.go @@ -5,6 +5,7 @@ package network import ( "context" + "errors" "fmt" "math" "sync" @@ -110,7 +111,7 @@ func (s signatureRequestVerifier) verifySubnetToL1Conversion( defer s.stateLock.Unlock() conversion, err := s.state.GetSubnetToL1Conversion(subnetID) - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { return &common.AppError{ Code: ErrConversionDoesNotExist, Message: fmt.Sprintf("subnet %q has not been converted", subnetID), @@ -172,7 +173,7 @@ func (s signatureRequestVerifier) verifyL1ValidatorRegistered( // Verify that the validator exists _, err := s.state.GetL1Validator(validationID) - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { return &common.AppError{ Code: ErrValidationDoesNotExist, Message: fmt.Sprintf("validation %q does not exist", validationID), @@ -215,7 +216,7 @@ func (s signatureRequestVerifier) verifySubnetValidatorNotCurrentlyRegistered( // Verify that the provided subnetID has been converted. _, err = s.state.GetSubnetToL1Conversion(subnetID) - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { return &common.AppError{ Code: ErrConversionDoesNotExist, Message: fmt.Sprintf("subnet %q has not been converted", subnetID), diff --git a/vms/platformvm/service.go b/vms/platformvm/service.go index c5a9df6c21a5..b83cf19db88c 100644 --- a/vms/platformvm/service.go +++ b/vms/platformvm/service.go @@ -640,7 +640,7 @@ func (s *Service) GetSubnets(_ *http.Request, args *GetSubnetsArgs, response *Ge } subnetOwner, err := s.vm.state.GetSubnetOwner(subnetID) - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { continue } if err != nil { @@ -1245,7 +1245,7 @@ func (s *Service) chainExists(ctx context.Context, blockID ids.ID, chainID ids.I } tx, _, err := state.GetTx(chainID) - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { return false, nil } if err != nil { diff --git a/vms/platformvm/state/l1_validator.go b/vms/platformvm/state/l1_validator.go index 3f728f993ce4..6e0313541eb3 100644 --- a/vms/platformvm/state/l1_validator.go +++ b/vms/platformvm/state/l1_validator.go @@ -206,7 +206,7 @@ func getL1Validator( } bytes, err := db.Get(validationID[:]) - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { cache.Put(validationID, maybe.Nothing[L1Validator]()) return L1Validator{}, database.ErrNotFound } diff --git a/vms/platformvm/state/state.go b/vms/platformvm/state/state.go index 95d973b2e4ed..d1341433433d 100644 --- a/vms/platformvm/state/state.go +++ b/vms/platformvm/state/state.go @@ -1084,13 +1084,13 @@ func (s *state) GetSubnetOwner(subnetID ids.ID) (fx.Owner, error) { }) return owner, nil } - if err != database.ErrNotFound { + if !errors.Is(err, database.ErrNotFound) { return nil, err } subnetIntf, _, err := s.GetTx(subnetID) if err != nil { - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { s.subnetOwnerCache.Put(subnetID, fxOwnerAndSize{}) } return nil, err @@ -1149,7 +1149,7 @@ func (s *state) GetSubnetTransformation(subnetID ids.ID) (*txs.Tx, error) { } transformSubnetTxID, err := database.GetID(s.transformedSubnetDB, subnetID[:]) - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { s.transformedSubnetCache.Put(subnetID, nil) return nil, database.ErrNotFound } @@ -1230,7 +1230,7 @@ func (s *state) GetTx(txID ids.ID) (*txs.Tx, status.Status, error) { return tx.tx, tx.status, nil } txBytes, err := s.txDB.Get(txID[:]) - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { s.txCache.Put(txID, nil) return nil, status.Unknown, database.ErrNotFound } else if err != nil { @@ -1390,7 +1390,7 @@ func (s *state) GetCurrentSupply(subnetID ids.ID) (uint64, error) { } supply, err := database.GetUInt64(s.supplyDB, subnetID[:]) - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { s.supplyCache.Put(subnetID, nil) return 0, database.ErrNotFound } @@ -1688,7 +1688,7 @@ func (s *state) loadMetadata() error { // Lookup the most recently indexed range on disk. If we haven't started // indexing the weights, then we keep the indexed heights as nil. indexedHeightsBytes, err := s.singletonDB.Get(HeightsIndexedKey) - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { return nil } if err != nil { @@ -2302,7 +2302,7 @@ func (s *state) GetStatelessBlock(blockID ids.ID) (block.Block, error) { } blkBytes, err := s.blockDB.Get(blockID[:]) - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { s.blockCache.Put(blockID, nil) return nil, database.ErrNotFound } @@ -2334,7 +2334,7 @@ func (s *state) GetBlockIDAtHeight(height uint64) (ids.ID, error) { heightKey := database.PackUInt64(height) blkID, err := database.GetID(s.blockIDDB, heightKey) - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { s.blockIDCache.Put(height, ids.Empty) return ids.Empty, database.ErrNotFound } @@ -2451,7 +2451,7 @@ func (s *state) updateValidatorManager(updateValidators bool) error { } priorL1Validator, err := s.getPersistedL1Validator(validationID) - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { // Deleting a non-existent validator is a noop. This can happen if // the validator was added and then immediately removed. continue @@ -3284,7 +3284,7 @@ func putFeeState(db database.KeyValueWriter, feeState gas.State) error { func getFeeState(db database.KeyValueReader) (gas.State, error) { feeStateBytes, err := db.Get(FeeStateKey) - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { return gas.State{}, nil } if err != nil { diff --git a/vms/platformvm/txs/executor/staker_tx_verification.go b/vms/platformvm/txs/executor/staker_tx_verification.go index 34a14eb5ce9c..95a882815f5a 100644 --- a/vms/platformvm/txs/executor/staker_tx_verification.go +++ b/vms/platformvm/txs/executor/staker_tx_verification.go @@ -52,7 +52,7 @@ func verifySubnetValidatorPrimaryNetworkRequirements( subnetValidator txs.Validator, ) error { primaryNetworkValidator, err := GetValidator(chainState, constants.PrimaryNetworkID, subnetValidator.NodeID) - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { return fmt.Errorf( "%s %w of the primary network", subnetValidator.NodeID, @@ -308,7 +308,7 @@ func verifyRemoveSubnetValidatorTx( isCurrentValidator := true vdr, err := chainState.GetCurrentValidator(tx.Subnet, tx.NodeID) - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { vdr, err = chainState.GetPendingValidator(tx.Subnet, tx.NodeID) isCurrentValidator = false } diff --git a/vms/proposervm/height_indexed_vm.go b/vms/proposervm/height_indexed_vm.go index 49c38a7df07f..01f77f0b1fb9 100644 --- a/vms/proposervm/height_indexed_vm.go +++ b/vms/proposervm/height_indexed_vm.go @@ -5,6 +5,7 @@ package proposervm import ( "context" + "errors" "fmt" "go.uber.org/zap" @@ -76,7 +77,7 @@ func (vm *VM) updateHeightIndex(height uint64, blkID ids.ID) error { // underflow. heightToDelete := height - vm.NumHistoricalBlocks - 1 blockToDelete, err := vm.State.GetBlockIDAtHeight(heightToDelete) - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { // Block may have already been deleted. This can happen due to a // proposervm rollback, the node having recently state-synced, or the // user reconfiguring the node to store more historical blocks than a @@ -108,7 +109,7 @@ func (vm *VM) pruneOldBlocks() error { } height, err := vm.State.GetMinimumHeight() - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { // Chain hasn't forked yet return nil } diff --git a/vms/proposervm/state/block_state.go b/vms/proposervm/state/block_state.go index 82f1cf0e5a9e..0dd79e50a561 100644 --- a/vms/proposervm/state/block_state.go +++ b/vms/proposervm/state/block_state.go @@ -91,7 +91,7 @@ func (s *blockState) GetBlock(blkID ids.ID) (block.Block, error) { } blkWrapperBytes, err := s.db.Get(blkID[:]) - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { s.blkCache.Put(blkID, nil) return nil, database.ErrNotFound } diff --git a/vms/proposervm/vm.go b/vms/proposervm/vm.go index d0c21971c80d..8a1888bb5f27 100644 --- a/vms/proposervm/vm.go +++ b/vms/proposervm/vm.go @@ -422,7 +422,7 @@ func (vm *VM) getPostDurangoSlotTime( func (vm *VM) LastAccepted(ctx context.Context) (ids.ID, error) { lastAccepted, err := vm.State.GetLastAccepted() - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { return vm.ChainVM.LastAccepted(ctx) } return lastAccepted, err @@ -438,7 +438,7 @@ func (vm *VM) repairAcceptedChainByHeight(ctx context.Context) error { return err } proLastAcceptedID, err := vm.State.GetLastAccepted() - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { // If the last accepted block isn't indexed yet, then the underlying // chain is the only chain and there is nothing to repair. return nil @@ -498,7 +498,7 @@ func (vm *VM) repairAcceptedChainByHeight(ctx context.Context) error { func (vm *VM) setLastAcceptedMetadata(ctx context.Context) error { lastAcceptedID, err := vm.GetLastAccepted() - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { // If the last accepted block wasn't a PostFork block, then we don't // initialize the metadata. vm.lastAcceptedHeight = 0 diff --git a/vms/rpcchainvm/vm_client.go b/vms/rpcchainvm/vm_client.go index 05add90700dc..8e0705129b8c 100644 --- a/vms/rpcchainvm/vm_client.go +++ b/vms/rpcchainvm/vm_client.go @@ -654,7 +654,7 @@ func (vm *VMClient) StateSyncEnabled(ctx context.Context) (bool, error) { return false, err } err = errEnumToError[resp.Err] - if err == block.ErrStateSyncableVMNotImplemented { + if errors.Is(err, block.ErrStateSyncableVMNotImplemented) { return false, nil } return resp.Enabled, err diff --git a/wallet/chain/c/signer.go b/wallet/chain/c/signer.go index 1e69db75be19..cec915c80cdd 100644 --- a/wallet/chain/c/signer.go +++ b/wallet/chain/c/signer.go @@ -102,7 +102,7 @@ func (s *txSigner) getImportSigners(ctx context.Context, sourceChainID ids.ID, i utxoID := transferInput.InputID() utxo, err := s.backend.GetUTXO(ctx, sourceChainID, utxoID) - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { // If we don't have access to the UTXO, then we can't sign this // transaction. However, we can attempt to partially sign it. continue diff --git a/wallet/chain/p/signer/visitor.go b/wallet/chain/p/signer/visitor.go index 8552194aa7df..fdd175f7b59f 100644 --- a/wallet/chain/p/signer/visitor.go +++ b/wallet/chain/p/signer/visitor.go @@ -253,7 +253,7 @@ func (s *visitor) getSigners(sourceChainID ids.ID, ins []*avax.TransferableInput utxoID := transferInput.InputID() utxo, err := s.backend.GetUTXO(s.ctx, sourceChainID, utxoID) - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { // If we don't have access to the UTXO, then we can't sign this // transaction. However, we can attempt to partially sign it. continue diff --git a/wallet/chain/x/signer/visitor.go b/wallet/chain/x/signer/visitor.go index 23a9940c6147..9c17fafca3cc 100644 --- a/wallet/chain/x/signer/visitor.go +++ b/wallet/chain/x/signer/visitor.go @@ -110,7 +110,7 @@ func (s *visitor) getSigners(ctx context.Context, sourceChainID ids.ID, ins []*a utxoID := transferInput.InputID() utxo, err := s.backend.GetUTXO(ctx, sourceChainID, utxoID) - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { // If we don't have access to the UTXO, then we can't sign this // transaction. However, we can attempt to partially sign it. continue @@ -175,7 +175,7 @@ func (s *visitor) getOpsSigners(ctx context.Context, sourceChainID ids.ID, ops [ } utxoID := op.UTXOIDs[0].InputID() utxo, err := s.backend.GetUTXO(ctx, sourceChainID, utxoID) - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { // If we don't have access to the UTXO, then we can't sign this // transaction. However, we can attempt to partially sign it. continue diff --git a/x/archivedb/reader.go b/x/archivedb/reader.go index abac3d854741..3c2a6d12e5dd 100644 --- a/x/archivedb/reader.go +++ b/x/archivedb/reader.go @@ -3,7 +3,10 @@ package archivedb -import "github.com/ava-labs/avalanchego/database" +import ( + "errors" + "github.com/ava-labs/avalanchego/database" +) var _ database.KeyValueReader = (*Reader)(nil) @@ -14,7 +17,7 @@ type Reader struct { func (r *Reader) Has(key []byte) (bool, error) { _, err := r.Get(key) - if err == database.ErrNotFound { + if errors.Is(err, database.ErrNotFound) { return false, nil } return true, err