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(storev2): change error namespace #19698

Merged
merged 2 commits into from
Mar 8, 2024
Merged
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
37 changes: 19 additions & 18 deletions store/db/goleveldb.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import (

"github.com/spf13/cast"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/errors"
dberrors "github.com/syndtr/goleveldb/leveldb/errors"
"github.com/syndtr/goleveldb/leveldb/filter"
"github.com/syndtr/goleveldb/leveldb/iterator"
"github.com/syndtr/goleveldb/leveldb/opt"
"github.com/syndtr/goleveldb/leveldb/util"

corestore "cosmossdk.io/core/store"
"cosmossdk.io/store/v2"
"cosmossdk.io/store/v2/errors"
)

// GoLevelDB implements RawDB using github.com/syndtr/goleveldb/leveldb.
Expand Down Expand Up @@ -54,11 +55,11 @@ func NewGoLevelDBWithOpts(name, dir string, o *opt.Options) (*GoLevelDB, error)
// Get implements RawDB.
func (db *GoLevelDB) Get(key []byte) ([]byte, error) {
if len(key) == 0 {
return nil, store.ErrKeyEmpty
return nil, errors.ErrKeyEmpty
}
res, err := db.db.Get(key, nil)
if err != nil {
if err == errors.ErrNotFound {
if err == dberrors.ErrNotFound {
return nil, nil
}
return nil, err
Expand All @@ -78,10 +79,10 @@ func (db *GoLevelDB) Has(key []byte) (bool, error) {
// Set implements RawDB.
func (db *GoLevelDB) Set(key, value []byte) error {
if len(key) == 0 {
return store.ErrKeyEmpty
return errors.ErrKeyEmpty
}
if value == nil {
return store.ErrValueNil
return errors.ErrValueNil
}
if err := db.db.Put(key, value, nil); err != nil {
return err
Expand All @@ -92,10 +93,10 @@ func (db *GoLevelDB) Set(key, value []byte) error {
// SetSync implements RawDB.
func (db *GoLevelDB) SetSync(key, value []byte) error {
if len(key) == 0 {
return store.ErrKeyEmpty
return errors.ErrKeyEmpty
}
if value == nil {
return store.ErrValueNil
return errors.ErrValueNil
}
if err := db.db.Put(key, value, &opt.WriteOptions{Sync: true}); err != nil {
return err
Expand All @@ -106,7 +107,7 @@ func (db *GoLevelDB) SetSync(key, value []byte) error {
// Delete implements RawDB.
func (db *GoLevelDB) Delete(key []byte) error {
if len(key) == 0 {
return store.ErrKeyEmpty
return errors.ErrKeyEmpty
}
if err := db.db.Delete(key, nil); err != nil {
return err
Expand All @@ -117,7 +118,7 @@ func (db *GoLevelDB) Delete(key []byte) error {
// DeleteSync implements RawDB.
func (db *GoLevelDB) DeleteSync(key []byte) error {
if len(key) == 0 {
return store.ErrKeyEmpty
return errors.ErrKeyEmpty
}
err := db.db.Delete(key, &opt.WriteOptions{Sync: true})
if err != nil {
Expand Down Expand Up @@ -195,7 +196,7 @@ func (db *GoLevelDB) NewBatchWithSize(size int) store.RawBatch {
// Iterator implements RawDB.
func (db *GoLevelDB) Iterator(start, end []byte) (corestore.Iterator, error) {
if (start != nil && len(start) == 0) || (end != nil && len(end) == 0) {
return nil, store.ErrKeyEmpty
return nil, errors.ErrKeyEmpty
}
itr := db.db.NewIterator(&util.Range{Start: start, Limit: end}, nil)
return newGoLevelDBIterator(itr, start, end, false), nil
Expand All @@ -204,7 +205,7 @@ func (db *GoLevelDB) Iterator(start, end []byte) (corestore.Iterator, error) {
// ReverseIterator implements RawDB.
func (db *GoLevelDB) ReverseIterator(start, end []byte) (corestore.Iterator, error) {
if (start != nil && len(start) == 0) || (end != nil && len(end) == 0) {
return nil, store.ErrKeyEmpty
return nil, errors.ErrKeyEmpty
}
itr := db.db.NewIterator(&util.Range{Start: start, Limit: end}, nil)
return newGoLevelDBIterator(itr, start, end, true), nil
Expand Down Expand Up @@ -363,13 +364,13 @@ func newGoLevelDBBatchWithSize(db *GoLevelDB, size int) *goLevelDBBatch {
// Set implements RawBatch.
func (b *goLevelDBBatch) Set(key, value []byte) error {
if len(key) == 0 {
return store.ErrKeyEmpty
return errors.ErrKeyEmpty
}
if value == nil {
return store.ErrValueNil
return errors.ErrValueNil
}
if b.batch == nil {
return store.ErrBatchClosed
return errors.ErrBatchClosed
}
b.batch.Put(key, value)
return nil
Expand All @@ -378,10 +379,10 @@ func (b *goLevelDBBatch) Set(key, value []byte) error {
// Delete implements RawBatch.
func (b *goLevelDBBatch) Delete(key []byte) error {
if len(key) == 0 {
return store.ErrKeyEmpty
return errors.ErrKeyEmpty
}
if b.batch == nil {
return store.ErrBatchClosed
return errors.ErrBatchClosed
}
b.batch.Delete(key)
return nil
Expand All @@ -399,7 +400,7 @@ func (b *goLevelDBBatch) WriteSync() error {

func (b *goLevelDBBatch) write(sync bool) error {
if b.batch == nil {
return store.ErrBatchClosed
return errors.ErrBatchClosed
}
err := b.db.db.Write(b.batch, &opt.WriteOptions{Sync: sync})
if err != nil {
Expand All @@ -421,7 +422,7 @@ func (b *goLevelDBBatch) Close() error {
// GetByteSize implements RawBatch
func (b *goLevelDBBatch) GetByteSize() (int, error) {
if b.batch == nil {
return 0, store.ErrBatchClosed
return 0, errors.ErrBatchClosed
}
return len(b.batch.Dump()), nil
}
33 changes: 17 additions & 16 deletions store/db/memdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

corestore "cosmossdk.io/core/store"
"cosmossdk.io/store/v2"
"cosmossdk.io/store/v2/errors"
)

const (
Expand Down Expand Up @@ -64,7 +65,7 @@ func NewMemDB() *MemDB {
// Get implements DB.
func (db *MemDB) Get(key []byte) ([]byte, error) {
if len(key) == 0 {
return nil, store.ErrKeyEmpty
return nil, errors.ErrKeyEmpty
}
db.mtx.RLock()
defer db.mtx.RUnlock()
Expand All @@ -79,7 +80,7 @@ func (db *MemDB) Get(key []byte) ([]byte, error) {
// Has implements DB.
func (db *MemDB) Has(key []byte) (bool, error) {
if len(key) == 0 {
return false, store.ErrKeyEmpty
return false, errors.ErrKeyEmpty
}
db.mtx.RLock()
defer db.mtx.RUnlock()
Expand All @@ -90,10 +91,10 @@ func (db *MemDB) Has(key []byte) (bool, error) {
// Set implements DB.
func (db *MemDB) Set(key, value []byte) error {
if len(key) == 0 {
return store.ErrKeyEmpty
return errors.ErrKeyEmpty
}
if value == nil {
return store.ErrValueNil
return errors.ErrValueNil
}
db.mtx.Lock()
defer db.mtx.Unlock()
Expand All @@ -115,7 +116,7 @@ func (db *MemDB) SetSync(key, value []byte) error {
// Delete implements DB.
func (db *MemDB) Delete(key []byte) error {
if len(key) == 0 {
return store.ErrKeyEmpty
return errors.ErrKeyEmpty
}
db.mtx.Lock()
defer db.mtx.Unlock()
Expand Down Expand Up @@ -181,7 +182,7 @@ func (db *MemDB) NewBatchWithSize(size int) store.RawBatch {
// Takes out a read-lock on the database until the iterator is closed.
func (db *MemDB) Iterator(start, end []byte) (corestore.Iterator, error) {
if (start != nil && len(start) == 0) || (end != nil && len(end) == 0) {
return nil, store.ErrKeyEmpty
return nil, errors.ErrKeyEmpty
}
return newMemDBIterator(db, start, end, false), nil
}
Expand All @@ -190,23 +191,23 @@ func (db *MemDB) Iterator(start, end []byte) (corestore.Iterator, error) {
// Takes out a read-lock on the database until the iterator is closed.
func (db *MemDB) ReverseIterator(start, end []byte) (corestore.Iterator, error) {
if (start != nil && len(start) == 0) || (end != nil && len(end) == 0) {
return nil, store.ErrKeyEmpty
return nil, errors.ErrKeyEmpty
}
return newMemDBIterator(db, start, end, true), nil
}

// IteratorNoMtx makes an iterator with no mutex.
func (db *MemDB) IteratorNoMtx(start, end []byte) (corestore.Iterator, error) {
if (start != nil && len(start) == 0) || (end != nil && len(end) == 0) {
return nil, store.ErrKeyEmpty
return nil, errors.ErrKeyEmpty
}
return newMemDBIteratorMtxChoice(db, start, end, false, false), nil
}

// ReverseIteratorNoMtx makes an iterator with no mutex.
func (db *MemDB) ReverseIteratorNoMtx(start, end []byte) (corestore.Iterator, error) {
if (start != nil && len(start) == 0) || (end != nil && len(end) == 0) {
return nil, store.ErrKeyEmpty
return nil, errors.ErrKeyEmpty
}
return newMemDBIteratorMtxChoice(db, start, end, true, false), nil
}
Expand Down Expand Up @@ -395,13 +396,13 @@ func newMemDBBatch(db *MemDB) *memDBBatch {
// Set implements Batch.
func (b *memDBBatch) Set(key, value []byte) error {
if len(key) == 0 {
return store.ErrKeyEmpty
return errors.ErrKeyEmpty
}
if value == nil {
return store.ErrValueNil
return errors.ErrValueNil
}
if b.ops == nil {
return store.ErrBatchClosed
return errors.ErrBatchClosed
}
b.size += len(key) + len(value)
b.ops = append(b.ops, operation{opTypeSet, key, value})
Expand All @@ -411,10 +412,10 @@ func (b *memDBBatch) Set(key, value []byte) error {
// Delete implements Batch.
func (b *memDBBatch) Delete(key []byte) error {
if len(key) == 0 {
return store.ErrKeyEmpty
return errors.ErrKeyEmpty
}
if b.ops == nil {
return store.ErrBatchClosed
return errors.ErrBatchClosed
}
b.size += len(key)
b.ops = append(b.ops, operation{opTypeDelete, key, nil})
Expand All @@ -424,7 +425,7 @@ func (b *memDBBatch) Delete(key []byte) error {
// Write implements Batch.
func (b *memDBBatch) Write() error {
if b.ops == nil {
return store.ErrBatchClosed
return errors.ErrBatchClosed
}
b.db.mtx.Lock()
defer b.db.mtx.Unlock()
Expand Down Expand Up @@ -459,7 +460,7 @@ func (b *memDBBatch) Close() error {
// GetByteSize implements Batch
func (b *memDBBatch) GetByteSize() (int, error) {
if b.ops == nil {
return 0, store.ErrBatchClosed
return 0, errors.ErrBatchClosed
}
return b.size, nil
}
17 changes: 9 additions & 8 deletions store/db/prefixdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

corestore "cosmossdk.io/core/store"
"cosmossdk.io/store/v2"
"cosmossdk.io/store/v2/errors"
)

// PrefixDB wraps a namespace of another database as a logical database.
Expand All @@ -29,7 +30,7 @@ func NewPrefixDB(db store.RawDB, prefix []byte) *PrefixDB {
// Get implements RawDB.
func (pdb *PrefixDB) Get(key []byte) ([]byte, error) {
if len(key) == 0 {
return nil, store.ErrKeyEmpty
return nil, errors.ErrKeyEmpty
}

pkey := pdb.prefixed(key)
Expand All @@ -43,7 +44,7 @@ func (pdb *PrefixDB) Get(key []byte) ([]byte, error) {
// Has implements RawDB.
func (pdb *PrefixDB) Has(key []byte) (bool, error) {
if len(key) == 0 {
return false, store.ErrKeyEmpty
return false, errors.ErrKeyEmpty
}

ok, err := pdb.db.Has(pdb.prefixed(key))
Expand All @@ -57,7 +58,7 @@ func (pdb *PrefixDB) Has(key []byte) (bool, error) {
// Iterator implements RawDB.
func (pdb *PrefixDB) Iterator(start, end []byte) (corestore.Iterator, error) {
if (start != nil && len(start) == 0) || (end != nil && len(end) == 0) {
return nil, store.ErrKeyEmpty
return nil, errors.ErrKeyEmpty
}

var pstart, pend []byte
Expand All @@ -78,7 +79,7 @@ func (pdb *PrefixDB) Iterator(start, end []byte) (corestore.Iterator, error) {
// ReverseIterator implements RawDB.
func (pdb *PrefixDB) ReverseIterator(start, end []byte) (corestore.Iterator, error) {
if (start != nil && len(start) == 0) || (end != nil && len(end) == 0) {
return nil, store.ErrKeyEmpty
return nil, errors.ErrKeyEmpty
}

var pstart, pend []byte
Expand Down Expand Up @@ -277,10 +278,10 @@ func newPrefixBatch(prefix []byte, source store.RawBatch) prefixDBBatch {
// Set implements RawBatch.
func (pb prefixDBBatch) Set(key, value []byte) error {
if len(key) == 0 {
return store.ErrKeyEmpty
return errors.ErrKeyEmpty
}
if value == nil {
return store.ErrValueNil
return errors.ErrValueNil
}
pkey := append(cp(pb.prefix), key...)
return pb.source.Set(pkey, value)
Expand All @@ -289,7 +290,7 @@ func (pb prefixDBBatch) Set(key, value []byte) error {
// Delete implements RawBatch.
func (pb prefixDBBatch) Delete(key []byte) error {
if len(key) == 0 {
return store.ErrKeyEmpty
return errors.ErrKeyEmpty
}
pkey := append(cp(pb.prefix), key...)
return pb.source.Delete(pkey)
Expand All @@ -313,7 +314,7 @@ func (pb prefixDBBatch) Close() error {
// GetByteSize implements RawBatch
func (pb prefixDBBatch) GetByteSize() (int, error) {
if pb.source == nil {
return 0, store.ErrBatchClosed
return 0, errors.ErrBatchClosed
}
return pb.source.GetByteSize()
}
Expand Down
7 changes: 5 additions & 2 deletions store/errors.go → store/errors/errors.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package store
package errors

import (
"fmt"
Expand All @@ -7,9 +7,12 @@ import (
)

// StoreCodespace defines the store package's unique error code space.
const StoreCodespace = "store"
const StoreCodespace = "storev2"

var (
// ErrInvalidProof is returned when a proof is invalid
ErrInvalidProof = errors.Register(StoreCodespace, 2, "invalid proof")

// ErrTxDecode is returned if we cannot parse a transaction
ErrTxDecode = errors.Register(StoreCodespace, 3, "tx parse error")

Expand Down
Loading
Loading