refactor: remove reliance on sdk/types/errors from store (#14090)

* Move snapshotstore under store

* add changelog entry

* errors store

* remove dep on sdk/types/errors

* clean up

* fix var

* undo some changes, clean up fmt.Errorf usage

* remove pkg/errors
This commit is contained in:
Marko 2022-12-03 13:11:33 +00:00 committed by GitHub
parent 25449b5812
commit 390262f56b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 129 additions and 95 deletions

View File

@ -13,12 +13,12 @@ import (
tmcrypto "github.com/tendermint/tendermint/proto/tendermint/crypto"
dbm "github.com/tendermint/tm-db"
sdkerrors "cosmossdk.io/errors"
"github.com/cosmos/cosmos-sdk/store/cachekv"
pruningtypes "github.com/cosmos/cosmos-sdk/store/pruning/types"
"github.com/cosmos/cosmos-sdk/store/tracekv"
"github.com/cosmos/cosmos-sdk/store/types"
"github.com/cosmos/cosmos-sdk/telemetry"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/kv"
)
@ -111,7 +111,7 @@ func UnsafeNewStore(tree *iavl.MutableTree) *Store {
// Any mutable operations executed will result in a panic.
func (st *Store) GetImmutable(version int64) (*Store, error) {
if !st.VersionExists(version) {
return nil, fmt.Errorf("version mismatch on immutable IAVL tree; version does not exist. Version has either been pruned, or is for a future block height")
return nil, errors.New("version mismatch on immutable IAVL tree; version does not exist. Version has either been pruned, or is for a future block height")
}
iTree, err := st.tree.GetImmutable(version)
@ -267,7 +267,7 @@ func (st *Store) SetInitialVersion(version int64) {
func (st *Store) Export(version int64) (*iavl.Exporter, error) {
istore, err := st.GetImmutable(version)
if err != nil {
return nil, fmt.Errorf("iavl export failed for version %v: %w", version, err)
return nil, sdkerrors.Wrapf(err, "iavl export failed for version %v", version)
}
tree, ok := istore.tree.(*immutableTree)
if !ok || tree == nil {
@ -310,7 +310,7 @@ func (st *Store) Query(req abci.RequestQuery) (res abci.ResponseQuery) {
defer telemetry.MeasureSince(time.Now(), "store", "iavl", "query")
if len(req.Data) == 0 {
return sdkerrors.QueryResult(sdkerrors.Wrap(sdkerrors.ErrTxDecode, "query cannot be zero length"), false)
return types.QueryResult(sdkerrors.Wrap(types.ErrTxDecode, "query cannot be zero length"), false)
}
tree := st.tree
@ -375,7 +375,7 @@ func (st *Store) Query(req abci.RequestQuery) (res abci.ResponseQuery) {
res.Value = bz
default:
return sdkerrors.QueryResult(sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unexpected query path: %v", req.Path), false)
return types.QueryResult(sdkerrors.Wrapf(types.ErrUnknownRequest, "unexpected query path: %v", req.Path), false)
}
return res

View File

@ -2,7 +2,6 @@ package proofs
import (
"errors"
"fmt"
"sort"
ics23 "github.com/confio/ics23/go"
@ -45,7 +44,7 @@ func CreateNonMembershipProof(data map[string][]byte, key []byte) (*ics23.Commit
}
// ensure this key is not in the store
if _, ok := data[string(key)]; ok {
return nil, fmt.Errorf("cannot create non-membership proof if key is in map")
return nil, errors.New("cannot create non-membership proof if key is in map")
}
keys := SortedKeys(data)
@ -91,13 +90,13 @@ func createExistenceProof(data map[string][]byte, key []byte) (*ics23.ExistenceP
}
value, ok := data[string(key)]
if !ok {
return nil, fmt.Errorf("cannot make existence proof if key is not in map")
return nil, errors.New("cannot make existence proof if key is not in map")
}
_, proofs, _ := sdkmaps.ProofsFromMap(data)
proof := proofs[string(key)]
if proof == nil {
return nil, fmt.Errorf("returned no proof for key")
return nil, errors.New("returned no proof for key")
}
return ConvertExistenceProof(proof, key, value)

View File

@ -1,6 +1,7 @@
package rootmulti
import (
"errors"
"fmt"
"io"
"math"
@ -11,11 +12,11 @@ import (
protoio "github.com/cosmos/gogoproto/io"
gogotypes "github.com/cosmos/gogoproto/types"
iavltree "github.com/cosmos/iavl"
"github.com/pkg/errors"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/log"
dbm "github.com/tendermint/tm-db"
sdkerrors "cosmossdk.io/errors"
"github.com/cosmos/cosmos-sdk/store/cachemulti"
"github.com/cosmos/cosmos-sdk/store/dbadapter"
"github.com/cosmos/cosmos-sdk/store/iavl"
@ -27,7 +28,6 @@ import (
"github.com/cosmos/cosmos-sdk/store/tracekv"
"github.com/cosmos/cosmos-sdk/store/transient"
"github.com/cosmos/cosmos-sdk/store/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
const (
@ -250,15 +250,15 @@ func (rs *Store) loadVersion(ver int64, upgrades *types.StoreUpgrades) error {
store, err := rs.loadCommitStoreFromParams(key, commitID, storeParams)
if err != nil {
return errors.Wrap(err, "failed to load store")
return sdkerrors.Wrap(err, "failed to load store")
}
newStores[key] = store
// If it was deleted, remove all data
if upgrades.IsDeleted(key.Name()) {
if err := deleteKVStore(types.KVStore(store)); err != nil {
return errors.Wrapf(err, "failed to delete store %s", key.Name())
if err := deleteKVStore(store.(types.KVStore)); err != nil {
return sdkerrors.Wrapf(err, "failed to delete store %s", key.Name())
}
rs.removalMap[key] = true
} else if oldName := upgrades.RenamedFrom(key.Name()); oldName != "" {
@ -271,12 +271,12 @@ func (rs *Store) loadVersion(ver int64, upgrades *types.StoreUpgrades) error {
// load from the old name
oldStore, err := rs.loadCommitStoreFromParams(oldKey, rs.getCommitID(infos, oldName), oldParams)
if err != nil {
return errors.Wrapf(err, "failed to load old store %s", oldName)
return sdkerrors.Wrapf(err, "failed to load old store %s", oldName)
}
// move all data
if err := moveKVStoreData(types.KVStore(oldStore), types.KVStore(store)); err != nil {
return errors.Wrapf(err, "failed to move store %s -> %s", oldName, key.Name())
if err := moveKVStoreData(oldStore.(types.KVStore), store.(types.KVStore)); err != nil {
return sdkerrors.Wrapf(err, "failed to move store %s -> %s", oldName, key.Name())
}
// add the old key so its deletion is committed
@ -614,7 +614,7 @@ func (rs *Store) PruneStores(clearPruningManager bool, pruningHeights []int64) (
continue
}
if errCause := errors.Cause(err); errCause != nil && errCause != iavltree.ErrVersionDoesNotExist {
if errors.Is(err, iavltree.ErrVersionDoesNotExist) && err != nil {
return err
}
}
@ -642,17 +642,17 @@ func (rs *Store) Query(req abci.RequestQuery) abci.ResponseQuery {
path := req.Path
storeName, subpath, err := parsePath(path)
if err != nil {
return sdkerrors.QueryResult(err, false)
return types.QueryResult(err, false)
}
store := rs.GetStoreByName(storeName)
if store == nil {
return sdkerrors.QueryResult(sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "no such store: %s", storeName), false)
return types.QueryResult(sdkerrors.Wrapf(types.ErrUnknownRequest, "no such store: %s", storeName), false)
}
queryable, ok := store.(types.Queryable)
if !ok {
return sdkerrors.QueryResult(sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "store %s (type %T) doesn't support queries", storeName, store), false)
return types.QueryResult(sdkerrors.Wrapf(types.ErrUnknownRequest, "store %s (type %T) doesn't support queries", storeName, store), false)
}
// trim the path and make the query
@ -664,7 +664,7 @@ func (rs *Store) Query(req abci.RequestQuery) abci.ResponseQuery {
}
if res.ProofOps == nil || len(res.ProofOps.Ops) == 0 {
return sdkerrors.QueryResult(sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "proof is unexpectedly empty; ensure height has not been pruned"), false)
return types.QueryResult(sdkerrors.Wrap(types.ErrInvalidRequest, "proof is unexpectedly empty; ensure height has not been pruned"), false)
}
// If the request's height is the latest height we've committed, then utilize
@ -677,7 +677,7 @@ func (rs *Store) Query(req abci.RequestQuery) abci.ResponseQuery {
} else {
commitInfo, err = getCommitInfo(rs.db, res.Height)
if err != nil {
return sdkerrors.QueryResult(err, false)
return types.QueryResult(err, false)
}
}
@ -711,7 +711,7 @@ func (rs *Store) SetInitialVersion(version int64) error {
// Returns error if it doesn't start with /
func parsePath(path string) (storeName string, subpath string, err error) {
if !strings.HasPrefix(path, "/") {
return storeName, subpath, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "invalid path: %s", path)
return storeName, subpath, sdkerrors.Wrapf(types.ErrUnknownRequest, "invalid path: %s", path)
}
paths := strings.SplitN(path[1:], "/", 2)
@ -732,10 +732,10 @@ func parsePath(path string) (storeName string, subpath string, err error) {
// TestMultistoreSnapshot_Checksum test.
func (rs *Store) Snapshot(height uint64, protoWriter protoio.Writer) error {
if height == 0 {
return sdkerrors.Wrap(sdkerrors.ErrLogic, "cannot snapshot height 0")
return sdkerrors.Wrap(types.ErrLogic, "cannot snapshot height 0")
}
if height > uint64(GetLatestVersion(rs.db)) {
return sdkerrors.Wrapf(sdkerrors.ErrLogic, "cannot snapshot future height %v", height)
return sdkerrors.Wrapf(types.ErrLogic, "cannot snapshot future height %v", height)
}
// Collect stores to snapshot (only IAVL stores are supported)
@ -753,7 +753,7 @@ func (rs *Store) Snapshot(height uint64, protoWriter protoio.Writer) error {
// Non-persisted stores shouldn't be snapshotted
continue
default:
return sdkerrors.Wrapf(sdkerrors.ErrLogic,
return sdkerrors.Wrapf(types.ErrLogic,
"don't know how to snapshot store %q of type %T", key.Name(), store)
}
}
@ -849,7 +849,7 @@ loop:
}
store, ok := rs.GetStoreByName(item.Store.Name).(*iavl.Store)
if !ok || store == nil {
return snapshottypes.SnapshotItem{}, sdkerrors.Wrapf(sdkerrors.ErrLogic, "cannot import into non-IAVL store %q", item.Store.Name)
return snapshottypes.SnapshotItem{}, sdkerrors.Wrapf(types.ErrLogic, "cannot import into non-IAVL store %q", item.Store.Name)
}
importer, err = store.Import(int64(height))
if err != nil {
@ -859,10 +859,10 @@ loop:
case *snapshottypes.SnapshotItem_IAVL:
if importer == nil {
return snapshottypes.SnapshotItem{}, sdkerrors.Wrap(sdkerrors.ErrLogic, "received IAVL node item before store item")
return snapshottypes.SnapshotItem{}, sdkerrors.Wrap(types.ErrLogic, "received IAVL node item before store item")
}
if item.IAVL.Height > math.MaxInt8 {
return snapshottypes.SnapshotItem{}, sdkerrors.Wrapf(sdkerrors.ErrLogic, "node height %v cannot exceed %v",
return snapshottypes.SnapshotItem{}, sdkerrors.Wrapf(types.ErrLogic, "node height %v cannot exceed %v",
item.IAVL.Height, math.MaxInt8)
}
node := &iavltree.ExportNode{
@ -1089,14 +1089,14 @@ func getCommitInfo(db dbm.DB, ver int64) (*types.CommitInfo, error) {
bz, err := db.Get([]byte(cInfoKey))
if err != nil {
return nil, errors.Wrap(err, "failed to get commit info")
return nil, sdkerrors.Wrap(err, "failed to get commit info")
} else if bz == nil {
return nil, errors.New("no commit info found")
}
cInfo := &types.CommitInfo{}
if err = cInfo.Unmarshal(bz); err != nil {
return nil, errors.Wrap(err, "failed unmarshal commit info")
return nil, sdkerrors.Wrap(err, "failed unmarshal commit info")
}
return cInfo, nil

View File

@ -19,7 +19,6 @@ import (
"github.com/cosmos/cosmos-sdk/store/listenkv"
pruningtypes "github.com/cosmos/cosmos-sdk/store/pruning/types"
"github.com/cosmos/cosmos-sdk/store/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
func TestStoreType(t *testing.T) {
@ -433,19 +432,19 @@ func TestMultiStoreQuery(t *testing.T) {
// Test bad path.
query := abci.RequestQuery{Path: "/key", Data: k, Height: ver}
qres := multi.Query(query)
require.EqualValues(t, sdkerrors.ErrUnknownRequest.ABCICode(), qres.Code)
require.EqualValues(t, sdkerrors.ErrUnknownRequest.Codespace(), qres.Codespace)
require.EqualValues(t, types.ErrUnknownRequest.ABCICode(), qres.Code)
require.EqualValues(t, types.ErrUnknownRequest.Codespace(), qres.Codespace)
query.Path = "h897fy32890rf63296r92"
qres = multi.Query(query)
require.EqualValues(t, sdkerrors.ErrUnknownRequest.ABCICode(), qres.Code)
require.EqualValues(t, sdkerrors.ErrUnknownRequest.Codespace(), qres.Codespace)
require.EqualValues(t, types.ErrUnknownRequest.ABCICode(), qres.Code)
require.EqualValues(t, types.ErrUnknownRequest.Codespace(), qres.Codespace)
// Test invalid store name.
query.Path = "/garbage/key"
qres = multi.Query(query)
require.EqualValues(t, sdkerrors.ErrUnknownRequest.ABCICode(), qres.Code)
require.EqualValues(t, sdkerrors.ErrUnknownRequest.Codespace(), qres.Codespace)
require.EqualValues(t, types.ErrUnknownRequest.ABCICode(), qres.Code)
require.EqualValues(t, types.ErrUnknownRequest.Codespace(), qres.Codespace)
// Test valid query with data.
query.Path = "/store1/key"

View File

@ -4,8 +4,9 @@ import (
"io"
"math"
sdkerrors "cosmossdk.io/errors"
snapshottypes "github.com/cosmos/cosmos-sdk/store/snapshots/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
)
// ChunkWriter reads an input stream, splits it into fixed-size chunks, and writes them to a
@ -69,7 +70,7 @@ func (w *ChunkWriter) CloseWithError(err error) {
// Write implements io.Writer.
func (w *ChunkWriter) Write(data []byte) (int, error) {
if w.closed {
return 0, sdkerrors.Wrap(sdkerrors.ErrLogic, "cannot write to closed ChunkWriter")
return 0, sdkerrors.Wrap(storetypes.ErrLogic, "cannot write to closed ChunkWriter")
}
nTotal := 0
for len(data) > 0 {
@ -171,7 +172,7 @@ func ValidRestoreHeight(format uint32, height uint64) error {
}
if height == 0 {
return sdkerrors.Wrap(sdkerrors.ErrLogic, "cannot restore snapshot at height 0")
return sdkerrors.Wrap(storetypes.ErrLogic, "cannot restore snapshot at height 0")
}
if height > uint64(math.MaxInt64) {
return sdkerrors.Wrapf(snapshottypes.ErrInvalidMetadata,

View File

@ -15,11 +15,11 @@ import (
"github.com/tendermint/tendermint/libs/log"
db "github.com/tendermint/tm-db"
sdkerrors "cosmossdk.io/errors"
"github.com/cosmos/cosmos-sdk/store/snapshots"
snapshottypes "github.com/cosmos/cosmos-sdk/store/snapshots/types"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
func checksums(slice [][]byte) [][]byte {

View File

@ -10,10 +10,11 @@ import (
"sort"
"sync"
sdkerrors "cosmossdk.io/errors"
"github.com/tendermint/tendermint/libs/log"
"github.com/cosmos/cosmos-sdk/store/snapshots/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
)
// Manager manages snapshot and restore operations for an app, making sure only a single
@ -110,10 +111,10 @@ func (m *Manager) begin(op operation) error {
// beginLocked begins an operation while already holding the mutex.
func (m *Manager) beginLocked(op operation) error {
if op == opNone {
return sdkerrors.Wrap(sdkerrors.ErrLogic, "can't begin a none operation")
return sdkerrors.Wrap(storetypes.ErrLogic, "can't begin a none operation")
}
if m.operation != opNone {
return sdkerrors.Wrapf(sdkerrors.ErrConflict, "a %v operation is in progress", m.operation)
return sdkerrors.Wrapf(storetypes.ErrConflict, "a %v operation is in progress", m.operation)
}
m.operation = op
return nil
@ -159,7 +160,7 @@ func (m *Manager) GetSnapshotBlockRetentionHeights() int64 {
// Create creates a snapshot and returns its metadata.
func (m *Manager) Create(height uint64) (*types.Snapshot, error) {
if m == nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrLogic, "no snapshot store configured")
return nil, sdkerrors.Wrap(storetypes.ErrLogic, "no snapshot store configured")
}
defer m.multistore.PruneSnapshotHeight(int64(height))
@ -175,7 +176,7 @@ func (m *Manager) Create(height uint64) (*types.Snapshot, error) {
return nil, sdkerrors.Wrap(err, "failed to examine latest snapshot")
}
if latest != nil && latest.Height >= height {
return nil, sdkerrors.Wrapf(sdkerrors.ErrConflict,
return nil, sdkerrors.Wrapf(storetypes.ErrConflict,
"a more recent snapshot already exists at height %v", latest.Height)
}
@ -277,7 +278,7 @@ func (m *Manager) Restore(snapshot types.Snapshot) error {
return sdkerrors.Wrapf(types.ErrUnknownFormat, "snapshot format %v", snapshot.Format)
}
if snapshot.Height == 0 {
return sdkerrors.Wrap(sdkerrors.ErrLogic, "cannot restore snapshot at height 0")
return sdkerrors.Wrap(storetypes.ErrLogic, "cannot restore snapshot at height 0")
}
if snapshot.Height > uint64(math.MaxInt64) {
return sdkerrors.Wrapf(types.ErrInvalidMetadata,
@ -344,11 +345,11 @@ func (m *Manager) restoreSnapshot(snapshot types.Snapshot, chChunks <-chan io.Re
}
metadata := nextItem.GetExtension()
if metadata == nil {
return sdkerrors.Wrapf(sdkerrors.ErrLogic, "unknown snapshot item %T", nextItem.Item)
return sdkerrors.Wrapf(storetypes.ErrLogic, "unknown snapshot item %T", nextItem.Item)
}
extension, ok := m.extensions[metadata.Name]
if !ok {
return sdkerrors.Wrapf(sdkerrors.ErrLogic, "unknown extension snapshotter %s", metadata.Name)
return sdkerrors.Wrapf(storetypes.ErrLogic, "unknown extension snapshotter %s", metadata.Name)
}
if !IsFormatSupported(extension, metadata.Format) {
return sdkerrors.Wrapf(types.ErrUnknownFormat, "format %v for extension %s", metadata.Format, metadata.Name)
@ -371,11 +372,11 @@ func (m *Manager) RestoreChunk(chunk []byte) (bool, error) {
m.mtx.Lock()
defer m.mtx.Unlock()
if m.operation != opRestore {
return false, sdkerrors.Wrap(sdkerrors.ErrLogic, "no restore operation in progress")
return false, sdkerrors.Wrap(storetypes.ErrLogic, "no restore operation in progress")
}
if int(m.restoreChunkIndex) >= len(m.restoreChunkHashes) {
return false, sdkerrors.Wrap(sdkerrors.ErrLogic, "received unexpected chunk")
return false, sdkerrors.Wrap(storetypes.ErrLogic, "received unexpected chunk")
}
// Check if any errors have occurred yet.
@ -385,7 +386,7 @@ func (m *Manager) RestoreChunk(chunk []byte) (bool, error) {
if done.err != nil {
return false, done.err
}
return false, sdkerrors.Wrap(sdkerrors.ErrLogic, "restore ended unexpectedly")
return false, sdkerrors.Wrap(storetypes.ErrLogic, "restore ended unexpectedly")
default:
}
@ -410,7 +411,7 @@ func (m *Manager) RestoreChunk(chunk []byte) (bool, error) {
return false, done.err
}
if !done.complete {
return false, sdkerrors.Wrap(sdkerrors.ErrLogic, "restore ended prematurely")
return false, sdkerrors.Wrap(storetypes.ErrLogic, "restore ended prematurely")
}
return true, nil
}

View File

@ -10,11 +10,12 @@ import (
"strconv"
"sync"
"cosmossdk.io/errors"
"github.com/cosmos/gogoproto/proto"
db "github.com/tendermint/tm-db"
"github.com/cosmos/cosmos-sdk/store/snapshots/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
)
const (
@ -34,11 +35,11 @@ type Store struct {
// NewStore creates a new snapshot store.
func NewStore(db db.DB, dir string) (*Store, error) {
if dir == "" {
return nil, sdkerrors.Wrap(sdkerrors.ErrLogic, "snapshot directory not given")
return nil, errors.Wrap(storetypes.ErrLogic, "snapshot directory not given")
}
err := os.MkdirAll(dir, 0o755)
if err != nil {
return nil, sdkerrors.Wrapf(err, "failed to create snapshot directory %q", dir)
return nil, errors.Wrapf(err, "failed to create snapshot directory %q", dir)
}
return &Store{
@ -54,16 +55,16 @@ func (s *Store) Delete(height uint64, format uint32) error {
saving := s.saving[height]
s.mtx.Unlock()
if saving {
return sdkerrors.Wrapf(sdkerrors.ErrConflict,
return errors.Wrapf(storetypes.ErrConflict,
"snapshot for height %v format %v is currently being saved", height, format)
}
err := s.db.DeleteSync(encodeKey(height, format))
if err != nil {
return sdkerrors.Wrapf(err, "failed to delete snapshot for height %v format %v",
return errors.Wrapf(err, "failed to delete snapshot for height %v format %v",
height, format)
}
err = os.RemoveAll(s.pathSnapshot(height, format))
return sdkerrors.Wrapf(err, "failed to delete snapshot chunks for height %v format %v",
return errors.Wrapf(err, "failed to delete snapshot chunks for height %v format %v",
height, format)
}
@ -71,7 +72,7 @@ func (s *Store) Delete(height uint64, format uint32) error {
func (s *Store) Get(height uint64, format uint32) (*types.Snapshot, error) {
bytes, err := s.db.Get(encodeKey(height, format))
if err != nil {
return nil, sdkerrors.Wrapf(err, "failed to fetch snapshot metadata for height %v format %v",
return nil, errors.Wrapf(err, "failed to fetch snapshot metadata for height %v format %v",
height, format)
}
if bytes == nil {
@ -80,7 +81,7 @@ func (s *Store) Get(height uint64, format uint32) (*types.Snapshot, error) {
snapshot := &types.Snapshot{}
err = proto.Unmarshal(bytes, snapshot)
if err != nil {
return nil, sdkerrors.Wrapf(err, "failed to decode snapshot metadata for height %v format %v",
return nil, errors.Wrapf(err, "failed to decode snapshot metadata for height %v format %v",
height, format)
}
if snapshot.Metadata.ChunkHashes == nil {
@ -93,7 +94,7 @@ func (s *Store) Get(height uint64, format uint32) (*types.Snapshot, error) {
func (s *Store) GetLatest() (*types.Snapshot, error) {
iter, err := s.db.ReverseIterator(encodeKey(0, 0), encodeKey(uint64(math.MaxUint64), math.MaxUint32))
if err != nil {
return nil, sdkerrors.Wrap(err, "failed to find latest snapshot")
return nil, errors.Wrap(err, "failed to find latest snapshot")
}
defer iter.Close()
@ -102,18 +103,18 @@ func (s *Store) GetLatest() (*types.Snapshot, error) {
snapshot = &types.Snapshot{}
err := proto.Unmarshal(iter.Value(), snapshot)
if err != nil {
return nil, sdkerrors.Wrap(err, "failed to decode latest snapshot")
return nil, errors.Wrap(err, "failed to decode latest snapshot")
}
}
err = iter.Error()
return snapshot, sdkerrors.Wrap(err, "failed to find latest snapshot")
return snapshot, errors.Wrap(err, "failed to find latest snapshot")
}
// List lists snapshots, in reverse order (newest first).
func (s *Store) List() ([]*types.Snapshot, error) {
iter, err := s.db.ReverseIterator(encodeKey(0, 0), encodeKey(uint64(math.MaxUint64), math.MaxUint32))
if err != nil {
return nil, sdkerrors.Wrap(err, "failed to list snapshots")
return nil, errors.Wrap(err, "failed to list snapshots")
}
defer iter.Close()
@ -122,7 +123,7 @@ func (s *Store) List() ([]*types.Snapshot, error) {
snapshot := &types.Snapshot{}
err := proto.Unmarshal(iter.Value(), snapshot)
if err != nil {
return nil, sdkerrors.Wrap(err, "failed to decode snapshot info")
return nil, errors.Wrap(err, "failed to decode snapshot info")
}
snapshots = append(snapshots, snapshot)
}
@ -183,7 +184,7 @@ func (s *Store) loadChunkFile(height uint64, format uint32, chunk uint32) (io.Re
func (s *Store) Prune(retain uint32) (uint64, error) {
iter, err := s.db.ReverseIterator(encodeKey(0, 0), encodeKey(uint64(math.MaxUint64), math.MaxUint32))
if err != nil {
return 0, sdkerrors.Wrap(err, "failed to prune snapshots")
return 0, errors.Wrap(err, "failed to prune snapshots")
}
defer iter.Close()
@ -193,7 +194,7 @@ func (s *Store) Prune(retain uint32) (uint64, error) {
for ; iter.Valid(); iter.Next() {
height, format, err := decodeKey(iter.Key())
if err != nil {
return 0, sdkerrors.Wrap(err, "failed to prune snapshots")
return 0, errors.Wrap(err, "failed to prune snapshots")
}
if skip[height] || uint32(len(skip)) < retain {
skip[height] = true
@ -201,7 +202,7 @@ func (s *Store) Prune(retain uint32) (uint64, error) {
}
err = s.Delete(height, format)
if err != nil {
return 0, sdkerrors.Wrap(err, "failed to prune snapshots")
return 0, errors.Wrap(err, "failed to prune snapshots")
}
pruned++
prunedHeights[height] = true
@ -212,7 +213,7 @@ func (s *Store) Prune(retain uint32) (uint64, error) {
if ok {
err = os.Remove(s.pathHeight(height))
if err != nil {
return 0, sdkerrors.Wrapf(err, "failed to remove snapshot directory for height %v", height)
return 0, errors.Wrapf(err, "failed to remove snapshot directory for height %v", height)
}
}
}
@ -225,7 +226,7 @@ func (s *Store) Save(
) (*types.Snapshot, error) {
defer DrainChunks(chunks)
if height == 0 {
return nil, sdkerrors.Wrap(sdkerrors.ErrLogic, "snapshot height cannot be 0")
return nil, errors.Wrap(storetypes.ErrLogic, "snapshot height cannot be 0")
}
s.mtx.Lock()
@ -233,7 +234,7 @@ func (s *Store) Save(
s.saving[height] = true
s.mtx.Unlock()
if saving {
return nil, sdkerrors.Wrapf(sdkerrors.ErrConflict,
return nil, errors.Wrapf(storetypes.ErrConflict,
"a snapshot for height %v is already being saved", height)
}
defer func() {
@ -247,7 +248,7 @@ func (s *Store) Save(
return nil, err
}
if exists {
return nil, sdkerrors.Wrapf(sdkerrors.ErrConflict,
return nil, errors.Wrapf(storetypes.ErrConflict,
"snapshot already exists for height %v format %v", height, format)
}
@ -263,27 +264,27 @@ func (s *Store) Save(
dir := s.pathSnapshot(height, format)
err = os.MkdirAll(dir, 0o755)
if err != nil {
return nil, sdkerrors.Wrapf(err, "failed to create snapshot directory %q", dir)
return nil, errors.Wrapf(err, "failed to create snapshot directory %q", dir)
}
path := s.pathChunk(height, format, index)
file, err := os.Create(path)
if err != nil {
return nil, sdkerrors.Wrapf(err, "failed to create snapshot chunk file %q", path)
return nil, errors.Wrapf(err, "failed to create snapshot chunk file %q", path)
}
defer file.Close() //nolint:staticcheck
chunkHasher.Reset()
_, err = io.Copy(io.MultiWriter(file, chunkHasher, snapshotHasher), chunkBody)
if err != nil {
return nil, sdkerrors.Wrapf(err, "failed to generate snapshot chunk %v", index)
return nil, errors.Wrapf(err, "failed to generate snapshot chunk %v", index)
}
err = file.Close()
if err != nil {
return nil, sdkerrors.Wrapf(err, "failed to close snapshot chunk %v", index)
return nil, errors.Wrapf(err, "failed to close snapshot chunk %v", index)
}
err = chunkBody.Close()
if err != nil {
return nil, sdkerrors.Wrapf(err, "failed to close snapshot chunk %v", index)
return nil, errors.Wrapf(err, "failed to close snapshot chunk %v", index)
}
snapshot.Metadata.ChunkHashes = append(snapshot.Metadata.ChunkHashes, chunkHasher.Sum(nil))
index++
@ -297,10 +298,10 @@ func (s *Store) Save(
func (s *Store) saveSnapshot(snapshot *types.Snapshot) error {
value, err := proto.Marshal(snapshot)
if err != nil {
return sdkerrors.Wrap(err, "failed to encode snapshot metadata")
return errors.Wrap(err, "failed to encode snapshot metadata")
}
err = s.db.SetSync(encodeKey(snapshot.Height, snapshot.Format), value)
return sdkerrors.Wrap(err, "failed to store snapshot")
return errors.Wrap(err, "failed to store snapshot")
}
// pathHeight generates the path to a height, containing multiple snapshot formats.
@ -321,10 +322,10 @@ func (s *Store) pathChunk(height uint64, format uint32, chunk uint32) string {
// decodeKey decodes a snapshot key.
func decodeKey(k []byte) (uint64, uint32, error) {
if len(k) != 13 {
return 0, 0, sdkerrors.Wrapf(sdkerrors.ErrLogic, "invalid snapshot key with length %v", len(k))
return 0, 0, errors.Wrapf(storetypes.ErrLogic, "invalid snapshot key with length %v", len(k))
}
if k[0] != keyPrefixSnapshot {
return 0, 0, sdkerrors.Wrapf(sdkerrors.ErrLogic, "invalid snapshot key prefix %x", k[0])
return 0, 0, errors.Wrapf(storetypes.ErrLogic, "invalid snapshot key prefix %x", k[0])
}
height := binary.BigEndian.Uint64(k[1:9])
format := binary.BigEndian.Uint32(k[9:13])

View File

@ -5,10 +5,10 @@ import (
"compress/zlib"
"io"
"cosmossdk.io/errors"
protoio "github.com/cosmos/gogoproto/io"
"github.com/cosmos/gogoproto/proto"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
const (
@ -34,7 +34,7 @@ func NewStreamWriter(ch chan<- io.ReadCloser) *StreamWriter {
bufWriter := bufio.NewWriterSize(chunkWriter, snapshotBufferSize)
zWriter, err := zlib.NewWriterLevel(bufWriter, snapshotCompressionLevel)
if err != nil {
chunkWriter.CloseWithError(sdkerrors.Wrap(err, "zlib failure"))
chunkWriter.CloseWithError(errors.Wrap(err, "zlib failure"))
return nil
}
protoWriter := protoio.NewDelimitedWriter(zWriter)
@ -82,7 +82,7 @@ func NewStreamReader(chunks <-chan io.ReadCloser) (*StreamReader, error) {
chunkReader := NewChunkReader(chunks)
zReader, err := zlib.NewReader(chunkReader)
if err != nil {
return nil, sdkerrors.Wrap(err, "zlib failure")
return nil, errors.Wrap(err, "zlib failure")
}
protoReader := protoio.NewDelimitedReader(zReader, snapshotMaxItemSize)
return &StreamReader{

View File

@ -4,7 +4,7 @@ import (
proto "github.com/cosmos/gogoproto/proto"
abci "github.com/tendermint/tendermint/abci/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"cosmossdk.io/errors"
)
// Converts an ABCI snapshot to a snapshot. Mainly to decode the SDK metadata.
@ -17,7 +17,7 @@ func SnapshotFromABCI(in *abci.Snapshot) (Snapshot, error) {
}
err := proto.Unmarshal(in.Metadata, &snapshot.Metadata)
if err != nil {
return Snapshot{}, sdkerrors.Wrap(err, "failed to unmarshal snapshot metadata")
return Snapshot{}, errors.Wrap(err, "failed to unmarshal snapshot metadata")
}
return snapshot, nil
}
@ -33,7 +33,7 @@ func (s Snapshot) ToABCI() (abci.Snapshot, error) {
var err error
out.Metadata, err = proto.Marshal(&s.Metadata)
if err != nil {
return abci.Snapshot{}, sdkerrors.Wrap(err, "failed to marshal snapshot metadata")
return abci.Snapshot{}, errors.Wrap(err, "failed to marshal snapshot metadata")
}
return out, nil
}

View File

@ -5,8 +5,8 @@ import (
"encoding/json"
"io"
"cosmossdk.io/errors"
"github.com/cosmos/cosmos-sdk/store/types"
"github.com/cosmos/cosmos-sdk/types/errors"
)
const (

View File

@ -1,9 +1,42 @@
package types
import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
sdkerrors "cosmossdk.io/errors"
abci "github.com/tendermint/tendermint/abci/types"
)
const StoreCodespace = "store"
var ErrInvalidProof = sdkerrors.Register(StoreCodespace, 2, "invalid proof")
var (
// ErrInvalidProof is returned when a proof is invalid
ErrInvalidProof = sdkerrors.Register(StoreCodespace, 2, "invalid proof")
// ErrTxDecode is returned if we cannot parse a transaction
ErrTxDecode = sdkerrors.Register(StoreCodespace, 3, "tx parse error")
// ErrUnknownRequest to doc
ErrUnknownRequest = sdkerrors.Register(StoreCodespace, 4, "unknown request")
// ErrLogic defines an internal logic error, e.g. an invariant or assertion
// that is violated. It is a programmer error, not a user-facing error.
ErrLogic = sdkerrors.Register(StoreCodespace, 5, "internal logic error")
// ErrConflict defines a conflict error, e.g. when two goroutines try to access
// the same resource and one of them fails.
ErrConflict = sdkerrors.Register(StoreCodespace, 6, "conflict")
// ErrInvalidRequest defines an ABCI typed error where the request contains
// invalid data.
ErrInvalidRequest = sdkerrors.Register(StoreCodespace, 7, "invalid request")
)
// ABCI QueryResult
// QueryResult returns a ResponseQuery from an error. It will try to parse ABCI
// info from the error.
func QueryResult(err error, debug bool) abci.ResponseQuery {
space, code, log := sdkerrors.ABCIInfo(err, debug)
return abci.ResponseQuery{
Codespace: space,
Code: code,
Log: log,
}
}

View File

@ -7,9 +7,9 @@ import (
"github.com/tendermint/tendermint/crypto/merkle"
tmmerkle "github.com/tendermint/tendermint/proto/tendermint/crypto"
sdkerrors "cosmossdk.io/errors"
sdkmaps "github.com/cosmos/cosmos-sdk/store/internal/maps"
sdkproofs "github.com/cosmos/cosmos-sdk/store/internal/proofs"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
const (