Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: unify the error handling methods in the crypto package that are different from the project style #3592

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion api/admin/key_value_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package admin

import (
"context"
"errors"

"github.com/ava-labs/avalanchego/database"
)
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion api/keystore/keystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion chains/atomic/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion database/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 7 additions & 6 deletions database/linkeddb/linkeddb.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package linkeddb

import (
"errors"
"slices"
"sync"

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion database/pebbledb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion indexer/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package indexer

import (
"errors"
"fmt"
"net/http"
"time"
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion snow/engine/avalanche/bootstrap/queue/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion snow/engine/avalanche/bootstrap/queue/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions snow/engine/snowman/block/batched_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion snow/engine/snowman/getter/getter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package getter

import (
"context"
"errors"
"time"

"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion snow/uptime/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions vms/avm/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion vms/avm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
5 changes: 3 additions & 2 deletions vms/components/avax/utxo_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package avax

import (
"errors"
"github.com/prometheus/client_golang/prometheus"

"github.com/ava-labs/avalanchego/cache"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion vms/components/chain/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion vms/components/index/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions vms/components/keystore/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package keystore

import (
"errors"
"fmt"
"io"

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 4 additions & 3 deletions vms/platformvm/network/warp.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package network

import (
"context"
"errors"
"fmt"
"math"
"sync"
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down
4 changes: 2 additions & 2 deletions vms/platformvm/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion vms/platformvm/state/l1_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Loading