From ce8c96bbf51a1ca2c6a7d1d4411eb7ba425230c3 Mon Sep 17 00:00:00 2001 From: Marko Date: Sat, 10 Dec 2022 22:56:36 +0000 Subject: [PATCH] chore: clean up sdk dependencies from store package (#14245) --- scripts/mockgen.sh | 2 +- store/dbadapter/store_test.go | 2 +- .../mock/tendermint_tm_db_DB.go | 0 store/snapshots/helpers_test.go | 9 +- store/snapshots/store_test.go | 11 +-- store/streaming/constructor.go | 3 +- store/streaming/constructor_test.go | 9 +- store/streaming/file/service.go | 4 +- store/streaming/file/service_test.go | 4 +- store/types/codec.go | 89 ++++++++++++++++++ store/types/utils.go | 92 ++++--------------- 11 files changed, 125 insertions(+), 100 deletions(-) rename {testutil => store}/mock/tendermint_tm_db_DB.go (100%) create mode 100644 store/types/codec.go diff --git a/scripts/mockgen.sh b/scripts/mockgen.sh index 6022c03db6..b4978b3585 100755 --- a/scripts/mockgen.sh +++ b/scripts/mockgen.sh @@ -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 diff --git a/store/dbadapter/store_test.go b/store/dbadapter/store_test.go index 3dca263051..dfc020c8ab 100644 --- a/store/dbadapter/store_test.go +++ b/store/dbadapter/store_test.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") diff --git a/testutil/mock/tendermint_tm_db_DB.go b/store/mock/tendermint_tm_db_DB.go similarity index 100% rename from testutil/mock/tendermint_tm_db_DB.go rename to store/mock/tendermint_tm_db_DB.go diff --git a/store/snapshots/helpers_test.go b/store/snapshots/helpers_test.go index ac99395594..d16797a4ae 100644 --- a/store/snapshots/helpers_test.go +++ b/store/snapshots/helpers_test.go @@ -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 diff --git a/store/snapshots/store_test.go b/store/snapshots/store_test.go index 0386daf107..f31bcde354 100644 --- a/store/snapshots/store_test.go +++ b/store/snapshots/store_test.go @@ -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 diff --git a/store/streaming/constructor.go b/store/streaming/constructor.go index c5c6b1bfb7..c756c61b0f 100644 --- a/store/streaming/constructor.go +++ b/store/streaming/constructor.go @@ -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) diff --git a/store/streaming/constructor_test.go b/store/streaming/constructor_test.go index d2ded40bbd..a70cbbabfb 100644 --- a/store/streaming/constructor_test.go +++ b/store/streaming/constructor_test.go @@ -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 +} diff --git a/store/streaming/file/service.go b/store/streaming/file/service.go index 5fc615bc72..a5be0538d7 100644 --- a/store/streaming/file/service.go +++ b/store/streaming/file/service.go @@ -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) } diff --git a/store/streaming/file/service_test.go b/store/streaming/file/service_test.go index 4f07872021..fab45260d3 100644 --- a/store/streaming/file/service_test.go +++ b/store/streaming/file/service_test.go @@ -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 ") } diff --git a/store/types/codec.go b/store/types/codec.go new file mode 100644 index 0000000000..4a5f424873 --- /dev/null +++ b/store/types/codec.go @@ -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) +} diff --git a/store/types/utils.go b/store/types/utils.go index a82a7058a2..a54d2746f7 100644 --- a/store/types/utils.go +++ b/store/types/utils.go @@ -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 }