chore: clean up sdk dependencies from store package (#14245)
This commit is contained in:
parent
32bd61c9c0
commit
ce8c96bbf5
@ -2,7 +2,7 @@
|
||||
|
||||
mockgen_cmd="mockgen"
|
||||
$mockgen_cmd -source=client/account_retriever.go -package mock -destination testutil/mock/account_retriever.go
|
||||
$mockgen_cmd -package mock -destination testutil/mock/tendermint_tm_db_DB.go github.com/tendermint/tm-db DB
|
||||
$mockgen_cmd -package mock -destination store/mock/tendermint_tm_db_DB.go github.com/tendermint/tm-db DB
|
||||
$mockgen_cmd -source=types/module/module.go -package mock -destination testutil/mock/types_module_module.go
|
||||
$mockgen_cmd -source=types/module/mock_appmodule_test.go -package mock -destination testutil/mock/types_mock_appmodule.go
|
||||
$mockgen_cmd -source=types/invariant.go -package mock -destination testutil/mock/types_invariant.go
|
||||
|
||||
@ -11,8 +11,8 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/store/dbadapter"
|
||||
"github.com/cosmos/cosmos-sdk/store/mock"
|
||||
"github.com/cosmos/cosmos-sdk/store/types"
|
||||
"github.com/cosmos/cosmos-sdk/testutil/mock"
|
||||
)
|
||||
|
||||
var errFoo = errors.New("dummy")
|
||||
|
||||
@ -18,8 +18,7 @@ import (
|
||||
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"
|
||||
"github.com/cosmos/cosmos-sdk/store/types"
|
||||
)
|
||||
|
||||
func checksums(slice [][]byte) [][]byte {
|
||||
@ -174,7 +173,7 @@ func (m *mockSnapshotter) SetSnapshotInterval(snapshotInterval uint64) {
|
||||
// setupBusyManager creates a manager with an empty store that is busy creating a snapshot at height 1.
|
||||
// The snapshot will complete when the returned closer is called.
|
||||
func setupBusyManager(t *testing.T) *snapshots.Manager {
|
||||
store, err := snapshots.NewStore(db.NewMemDB(), testutil.GetTempDir(t))
|
||||
store, err := snapshots.NewStore(db.NewMemDB(), t.TempDir())
|
||||
require.NoError(t, err)
|
||||
hung := newHungSnapshotter()
|
||||
hung.SetSnapshotInterval(opts.Interval)
|
||||
@ -258,7 +257,7 @@ func (s *extSnapshotter) SupportedFormats() []uint32 {
|
||||
|
||||
func (s *extSnapshotter) SnapshotExtension(height uint64, payloadWriter snapshottypes.ExtensionPayloadWriter) error {
|
||||
for _, i := range s.state {
|
||||
if err := payloadWriter(sdk.Uint64ToBigEndian(uint64(i))); err != nil {
|
||||
if err := payloadWriter(types.Uint64ToBigEndian(uint64(i))); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@ -273,7 +272,7 @@ func (s *extSnapshotter) RestoreExtension(height uint64, format uint32, payloadR
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
s.state = append(s.state, sdk.BigEndianToUint64(payload))
|
||||
s.state = append(s.state, types.BigEndianToUint64(payload))
|
||||
}
|
||||
// finalize restoration
|
||||
return nil
|
||||
|
||||
@ -4,7 +4,6 @@ import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"io"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@ -14,11 +13,10 @@ import (
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/store/snapshots"
|
||||
"github.com/cosmos/cosmos-sdk/store/snapshots/types"
|
||||
"github.com/cosmos/cosmos-sdk/testutil"
|
||||
)
|
||||
|
||||
func setupStore(t *testing.T) *snapshots.Store {
|
||||
store, err := snapshots.NewStore(db.NewMemDB(), testutil.GetTempDir(t))
|
||||
store, err := snapshots.NewStore(db.NewMemDB(), t.TempDir())
|
||||
require.NoError(t, err)
|
||||
|
||||
_, err = store.Save(1, 1, makeChunks([][]byte{
|
||||
@ -53,13 +51,6 @@ func TestNewStore_ErrNoDir(t *testing.T) {
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
||||
func TestNewStore_ErrDirFailure(t *testing.T) {
|
||||
notADir := filepath.Join(testutil.TempFile(t).Name(), "subdir")
|
||||
|
||||
_, err := snapshots.NewStore(db.NewMemDB(), notADir)
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
||||
func TestStore_Delete(t *testing.T) {
|
||||
store := setupStore(t)
|
||||
// Deleting a snapshot should remove it
|
||||
|
||||
@ -12,7 +12,6 @@ import (
|
||||
serverTypes "github.com/cosmos/cosmos-sdk/server/types"
|
||||
"github.com/cosmos/cosmos-sdk/store/streaming/file"
|
||||
"github.com/cosmos/cosmos-sdk/store/types"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/tendermint/tendermint/libs/log"
|
||||
|
||||
"github.com/spf13/cast"
|
||||
@ -139,7 +138,7 @@ func LoadStreamingServices(
|
||||
exposeKeyStrs := cast.ToStringSlice(appOpts.Get(fmt.Sprintf("streamers.%s.keys", streamerName)))
|
||||
|
||||
// if list contains '*', expose all store keys
|
||||
if sdk.SliceContains(exposeKeyStrs, "*") {
|
||||
if types.SliceContains(exposeKeyStrs, "*") {
|
||||
exposeStoreKeys = make([]types.StoreKey, 0, len(keys))
|
||||
for _, storeKey := range keys {
|
||||
exposeStoreKeys = append(exposeStoreKeys, storeKey)
|
||||
|
||||
@ -12,7 +12,6 @@ import (
|
||||
"github.com/cosmos/cosmos-sdk/store/streaming"
|
||||
"github.com/cosmos/cosmos-sdk/store/streaming/file"
|
||||
"github.com/cosmos/cosmos-sdk/store/types"
|
||||
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
|
||||
)
|
||||
|
||||
type fakeOptions struct{}
|
||||
@ -61,7 +60,7 @@ func TestLoadStreamingServices(t *testing.T) {
|
||||
activeStreamersLen int
|
||||
}{
|
||||
"empty app options": {
|
||||
appOpts: simtestutil.EmptyAppOptions{},
|
||||
appOpts: emptyAppOptions{},
|
||||
},
|
||||
"all StoreKeys exposed": {
|
||||
appOpts: streamingAppOptions{keys: []string{"*"}},
|
||||
@ -101,3 +100,9 @@ func (ao streamingAppOptions) Get(o string) interface{} {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
type emptyAppOptions struct{}
|
||||
|
||||
func (ao emptyAppOptions) Get(o string) interface{} {
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -16,7 +16,6 @@ import (
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/baseapp"
|
||||
"github.com/cosmos/cosmos-sdk/store/types"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
)
|
||||
|
||||
var _ baseapp.StreamingService = &StreamingService{}
|
||||
@ -225,8 +224,7 @@ func writeLengthPrefixedFile(path string, data []byte, fsync bool) (err error) {
|
||||
err = errors.Wrapf(err, "close file failed: %s", path)
|
||||
}
|
||||
}()
|
||||
|
||||
_, err = f.Write(sdk.Uint64ToBigEndian(uint64(len(data))))
|
||||
_, err = f.Write(types.Uint64ToBigEndian(uint64(len(data))))
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "write length prefix failed: %s", path)
|
||||
}
|
||||
|
||||
@ -16,7 +16,6 @@ import (
|
||||
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/store/types"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -328,8 +327,7 @@ func readInFile(name string) ([]byte, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
size := sdk.BigEndianToUint64(bz[:8])
|
||||
size := types.BigEndianToUint64(bz[:8])
|
||||
if len(bz) != int(size)+8 {
|
||||
return nil, errors.New("incomplete file ")
|
||||
}
|
||||
|
||||
89
store/types/codec.go
Normal file
89
store/types/codec.go
Normal file
@ -0,0 +1,89 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
fmt "fmt"
|
||||
|
||||
proto "github.com/cosmos/gogoproto/proto"
|
||||
)
|
||||
|
||||
// Codec defines a interface needed for the store package to marshal data
|
||||
type Codec interface {
|
||||
// Marshal returns binary encoding of v.
|
||||
Marshal(proto.Message) ([]byte, error)
|
||||
|
||||
// MarshalLengthPrefixed returns binary encoding of v with bytes length prefix.
|
||||
MarshalLengthPrefixed(proto.Message) ([]byte, error)
|
||||
|
||||
// Unmarshal parses the data encoded with Marshal method and stores the result
|
||||
// in the value pointed to by v.
|
||||
Unmarshal(bz []byte, ptr proto.Message) error
|
||||
|
||||
// Unmarshal parses the data encoded with UnmarshalLengthPrefixed method and stores
|
||||
// the result in the value pointed to by v.
|
||||
UnmarshalLengthPrefixed(bz []byte, ptr proto.Message) error
|
||||
}
|
||||
|
||||
// ============= TestCodec =============
|
||||
// TestCodec defines a codec that utilizes Protobuf for both binary and JSON
|
||||
// encoding.
|
||||
type TestCodec struct{}
|
||||
|
||||
var _ Codec = &TestCodec{}
|
||||
|
||||
func NewTestCodec() Codec {
|
||||
return &TestCodec{}
|
||||
}
|
||||
|
||||
// Marshal implements BinaryMarshaler.Marshal method.
|
||||
// NOTE: this function must be used with a concrete type which
|
||||
// implements proto.Message. For interface please use the codec.MarshalInterface
|
||||
func (pc *TestCodec) Marshal(o proto.Message) ([]byte, error) {
|
||||
// Size() check can catch the typed nil value.
|
||||
if o == nil || proto.Size(o) == 0 {
|
||||
// return empty bytes instead of nil, because nil has special meaning in places like store.Set
|
||||
return []byte{}, nil
|
||||
}
|
||||
return proto.Marshal(o)
|
||||
}
|
||||
|
||||
// MarshalLengthPrefixed implements BinaryMarshaler.MarshalLengthPrefixed method.
|
||||
func (pc *TestCodec) MarshalLengthPrefixed(o proto.Message) ([]byte, error) {
|
||||
bz, err := pc.Marshal(o)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var sizeBuf [binary.MaxVarintLen64]byte
|
||||
n := binary.PutUvarint(sizeBuf[:], uint64(len(bz)))
|
||||
return append(sizeBuf[:n], bz...), nil
|
||||
}
|
||||
|
||||
// Unmarshal implements BinaryMarshaler.Unmarshal method.
|
||||
// NOTE: this function must be used with a concrete type which
|
||||
// implements proto.Message. For interface please use the codec.UnmarshalInterface
|
||||
func (pc *TestCodec) Unmarshal(bz []byte, ptr proto.Message) error {
|
||||
err := proto.Unmarshal(bz, ptr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalLengthPrefixed implements BinaryMarshaler.UnmarshalLengthPrefixed method.
|
||||
func (pc *TestCodec) UnmarshalLengthPrefixed(bz []byte, ptr proto.Message) error {
|
||||
size, n := binary.Uvarint(bz)
|
||||
if n < 0 {
|
||||
return fmt.Errorf("invalid number of bytes read from length-prefixed encoding: %d", n)
|
||||
}
|
||||
|
||||
if size > uint64(len(bz)-n) {
|
||||
return fmt.Errorf("not enough bytes to read; want: %v, got: %v", size, len(bz)-n)
|
||||
} else if size < uint64(len(bz)-n) {
|
||||
return fmt.Errorf("too many bytes to read; want: %v, got: %v", size, len(bz)-n)
|
||||
}
|
||||
|
||||
bz = bz[n:]
|
||||
return proto.Unmarshal(bz, ptr)
|
||||
}
|
||||
@ -5,8 +5,6 @@ import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
proto "github.com/cosmos/gogoproto/proto"
|
||||
)
|
||||
|
||||
// KVStorePrefixIterator iterates over all the keys with a certain prefix in ascending order
|
||||
@ -66,83 +64,31 @@ func assertNoCommonPrefix(keys []string) {
|
||||
}
|
||||
}
|
||||
|
||||
// Codec defines a interface needed for the store package to marshal data
|
||||
type Codec interface {
|
||||
// Marshal returns binary encoding of v.
|
||||
Marshal(proto.Message) ([]byte, error)
|
||||
|
||||
// MarshalLengthPrefixed returns binary encoding of v with bytes length prefix.
|
||||
MarshalLengthPrefixed(proto.Message) ([]byte, error)
|
||||
|
||||
// Unmarshal parses the data encoded with Marshal method and stores the result
|
||||
// in the value pointed to by v.
|
||||
Unmarshal(bz []byte, ptr proto.Message) error
|
||||
|
||||
// Unmarshal parses the data encoded with UnmarshalLengthPrefixed method and stores
|
||||
// the result in the value pointed to by v.
|
||||
UnmarshalLengthPrefixed(bz []byte, ptr proto.Message) error
|
||||
// Uint64ToBigEndian - marshals uint64 to a bigendian byte slice so it can be sorted
|
||||
func Uint64ToBigEndian(i uint64) []byte {
|
||||
b := make([]byte, 8)
|
||||
binary.BigEndian.PutUint64(b, i)
|
||||
return b
|
||||
}
|
||||
|
||||
// ============= TestCodec =============
|
||||
// TestCodec defines a codec that utilizes Protobuf for both binary and JSON
|
||||
// encoding.
|
||||
type TestCodec struct{}
|
||||
|
||||
var _ Codec = &TestCodec{}
|
||||
|
||||
func NewTestCodec() Codec {
|
||||
return &TestCodec{}
|
||||
}
|
||||
|
||||
// Marshal implements BinaryMarshaler.Marshal method.
|
||||
// NOTE: this function must be used with a concrete type which
|
||||
// implements proto.Message. For interface please use the codec.MarshalInterface
|
||||
func (pc *TestCodec) Marshal(o proto.Message) ([]byte, error) {
|
||||
// Size() check can catch the typed nil value.
|
||||
if o == nil || proto.Size(o) == 0 {
|
||||
// return empty bytes instead of nil, because nil has special meaning in places like store.Set
|
||||
return []byte{}, nil
|
||||
}
|
||||
return proto.Marshal(o)
|
||||
}
|
||||
|
||||
// MarshalLengthPrefixed implements BinaryMarshaler.MarshalLengthPrefixed method.
|
||||
func (pc *TestCodec) MarshalLengthPrefixed(o proto.Message) ([]byte, error) {
|
||||
bz, err := pc.Marshal(o)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
// BigEndianToUint64 returns an uint64 from big endian encoded bytes. If encoding
|
||||
// is empty, zero is returned.
|
||||
func BigEndianToUint64(bz []byte) uint64 {
|
||||
if len(bz) == 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
var sizeBuf [binary.MaxVarintLen64]byte
|
||||
n := binary.PutUvarint(sizeBuf[:], uint64(len(bz)))
|
||||
return append(sizeBuf[:n], bz...), nil
|
||||
return binary.BigEndian.Uint64(bz)
|
||||
}
|
||||
|
||||
// Unmarshal implements BinaryMarshaler.Unmarshal method.
|
||||
// NOTE: this function must be used with a concrete type which
|
||||
// implements proto.Message. For interface please use the codec.UnmarshalInterface
|
||||
func (pc *TestCodec) Unmarshal(bz []byte, ptr proto.Message) error {
|
||||
err := proto.Unmarshal(bz, ptr)
|
||||
if err != nil {
|
||||
return err
|
||||
// SliceContains implements a generic function for checking if a slice contains
|
||||
// a certain value.
|
||||
func SliceContains[T comparable](elements []T, v T) bool {
|
||||
for _, s := range elements {
|
||||
if v == s {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalLengthPrefixed implements BinaryMarshaler.UnmarshalLengthPrefixed method.
|
||||
func (pc *TestCodec) UnmarshalLengthPrefixed(bz []byte, ptr proto.Message) error {
|
||||
size, n := binary.Uvarint(bz)
|
||||
if n < 0 {
|
||||
return fmt.Errorf("invalid number of bytes read from length-prefixed encoding: %d", n)
|
||||
}
|
||||
|
||||
if size > uint64(len(bz)-n) {
|
||||
return fmt.Errorf("not enough bytes to read; want: %v, got: %v", size, len(bz)-n)
|
||||
} else if size < uint64(len(bz)-n) {
|
||||
return fmt.Errorf("too many bytes to read; want: %v, got: %v", size, len(bz)-n)
|
||||
}
|
||||
|
||||
bz = bz[n:]
|
||||
return proto.Unmarshal(bz, ptr)
|
||||
return false
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user