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: remove sdk dep from store #14603

Merged
merged 15 commits into from
Jan 13, 2023
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ extension interfaces. `module.Manager.Modules` is now of type `map[string]interf
* (x/auth)[#13780](https://github.com/cosmos/cosmos-sdk/pull/13780) Querying with `id` (type of int64) in `AccountAddressByID` grpc query now throws error, use account-id(type of uint64) instead.
* (snapshots) [14048](https://github.com/cosmos/cosmos-sdk/pull/14048) Move the Snapshot package to the store package. This is done in an effort group all storage related logic under one package.
* (baseapp) [#14050](https://github.com/cosmos/cosmos-sdk/pull/14050) refactor `ABCIListener` interface to accept go contexts
* (store/streaming)[#14603](https://github.com/cosmos/cosmos-sdk/pull/14603) `StoreDecoderRegistry` moved from store to `types/simulations` this breaks the `AppModuleSimulation` interface.

### CLI Breaking Changes

Expand Down
5 changes: 4 additions & 1 deletion baseapp/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"

dbm "github.com/cosmos/cosmos-db"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec/types"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
"github.com/cosmos/cosmos-sdk/store/metrics"
Expand All @@ -15,6 +16,7 @@ import (
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/mempool"
"github.com/spf13/cast"
)

// File for storing in-package BaseApp optional functions,
Expand Down Expand Up @@ -239,7 +241,8 @@ func (app *BaseApp) SetStreamingService(
appCodec storetypes.Codec,
keys map[string]*storetypes.KVStoreKey,
) error {
streamers, _, err := streaming.LoadStreamingServices(appOpts, appCodec, app.logger, keys)
homePath := cast.ToString(appOpts.Get(flags.FlagHome))
streamers, _, err := streaming.LoadStreamingServices(appOpts, appCodec, app.logger, keys, homePath)
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions simapp/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ replace (
github.com/99designs/keyring => github.com/cosmos/keyring v1.2.0
// Simapp always use the latest version of the cosmos-sdk
github.com/cosmos/cosmos-sdk => ../.
github.com/cosmos/cosmos-sdk/x/nft => ../x/nft
// Fix upstream GHSA-h395-qcrw-5vmq vulnerability.
// TODO Remove it: https://github.com/cosmos/cosmos-sdk/issues/10409
github.com/gin-gonic/gin => github.com/gin-gonic/gin v1.8.1
Expand Down
3 changes: 2 additions & 1 deletion store/listenkv/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"

"github.com/cosmos/cosmos-sdk/store/dbadapter"
"github.com/cosmos/cosmos-sdk/store/internal/kv"
"github.com/cosmos/cosmos-sdk/store/listenkv"
"github.com/cosmos/cosmos-sdk/store/prefix"
"github.com/cosmos/cosmos-sdk/store/types"
Expand All @@ -21,7 +22,7 @@ func bz(s string) []byte { return []byte(s) }
func keyFmt(i int) []byte { return bz(fmt.Sprintf("key%0.8d", i)) }
func valFmt(i int) []byte { return bz(fmt.Sprintf("value%0.8d", i)) }

var kvPairs = []types.KVPair{
var kvPairs = []kv.Pair{
{Key: keyFmt(1), Value: valFmt(1)},
{Key: keyFmt(2), Value: valFmt(2)},
{Key: keyFmt(3), Value: valFmt(3)},
Expand Down
1 change: 0 additions & 1 deletion store/reexport.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ type (
CacheMultiStore = types.CacheMultiStore
CommitMultiStore = types.CommitMultiStore
KVStore = types.KVStore
KVPair = types.KVPair
Iterator = types.Iterator
CacheKVStore = types.CacheKVStore
CommitKVStore = types.CommitKVStore
Expand Down
21 changes: 13 additions & 8 deletions store/streaming/constructor.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,21 @@ import (
"strings"
"sync"

"github.com/cosmos/cosmos-sdk/client/flags"
serverTypes "github.com/cosmos/cosmos-sdk/server/types"
"github.com/cosmos/cosmos-sdk/store/streaming/file"
"github.com/cosmos/cosmos-sdk/store/types"
"github.com/tendermint/tendermint/libs/log"

"github.com/spf13/cast"
)

// ServiceConstructor is used to construct a streaming service
type ServiceConstructor func(serverTypes.AppOptions, []types.StoreKey, types.Codec, log.Logger) (types.StreamingService, error)
type (
// AppOptions is an interface for accessing application options
AppOptions interface {
Get(string) interface{}
}
// ServiceConstructor is used to construct a streaming service
ServiceConstructor func(AppOptions, []types.StoreKey, types.Codec, log.Logger, string) (types.StreamingService, error)
)

// ServiceType enum for specifying the type of StreamingService
type ServiceType int
Expand Down Expand Up @@ -85,12 +89,12 @@ func NewServiceConstructor(name string) (ServiceConstructor, error) {
// NewFileStreamingService is the streaming.ServiceConstructor function for
// creating a FileStreamingService.
func NewFileStreamingService(
opts serverTypes.AppOptions,
opts AppOptions,
keys []types.StoreKey,
marshaller types.Codec,
logger log.Logger,
homePath string,
) (types.StreamingService, error) {
homePath := cast.ToString(opts.Get(flags.FlagHome))
filePrefix := cast.ToString(opts.Get(OptStreamersFilePrefix))
fileDir := cast.ToString(opts.Get(OptStreamersFileWriteDir))
outputMetadata := cast.ToBool(opts.Get(OptStreamersFileOutputMetadata))
Expand All @@ -117,10 +121,11 @@ func NewFileStreamingService(
// WaitGroup and quit channel used to synchronize with the streaming services
// and any error that occurs during the setup.
func LoadStreamingServices(
appOpts serverTypes.AppOptions,
appOpts AppOptions,
appCodec types.Codec,
logger log.Logger,
keys map[string]*types.KVStoreKey,
homePath string,
) ([]types.StreamingService, *sync.WaitGroup, error) {
// waitgroup and quit channel for optional shutdown coordination of the streaming service(s)
wg := new(sync.WaitGroup)
Expand Down Expand Up @@ -167,7 +172,7 @@ func LoadStreamingServices(

// Generate the streaming service using the constructor, appOptions, and the
// StoreKeys we want to expose.
streamingService, err := constructor(appOpts, exposeStoreKeys, appCodec, logger)
streamingService, err := constructor(appOpts, exposeStoreKeys, appCodec, logger, homePath)
if err != nil {
// Close any services we may have already spun up before hitting the error
// on this one.
Expand Down
7 changes: 3 additions & 4 deletions store/streaming/constructor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/libs/log"

serverTypes "github.com/cosmos/cosmos-sdk/server/types"
"github.com/cosmos/cosmos-sdk/store/streaming"
"github.com/cosmos/cosmos-sdk/store/streaming/file"
"github.com/cosmos/cosmos-sdk/store/types"
Expand Down Expand Up @@ -36,7 +35,7 @@ func TestStreamingServiceConstructor(t *testing.T) {
var expectedType streaming.ServiceConstructor
require.IsType(t, expectedType, constructor)

serv, err := constructor(mockOptions, mockKeys, testMarshaller, log.NewNopLogger())
serv, err := constructor(mockOptions, mockKeys, testMarshaller, log.NewNopLogger(), "path/to/data")
require.Nil(t, err)
require.IsType(t, &file.StreamingService{}, serv)
listeners := serv.Listeners()
Expand All @@ -51,7 +50,7 @@ func TestLoadStreamingServices(t *testing.T) {
keys := types.NewKVStoreKeys("mockKey1", "mockKey2")

testCases := map[string]struct {
appOpts serverTypes.AppOptions
appOpts streaming.AppOptions
activeStreamersLen int
}{
"empty app options": {
Expand All @@ -72,7 +71,7 @@ func TestLoadStreamingServices(t *testing.T) {

for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
activeStreamers, _, err := streaming.LoadStreamingServices(tc.appOpts, encCdc, log.NewNopLogger(), keys)
activeStreamers, _, err := streaming.LoadStreamingServices(tc.appOpts, encCdc, log.NewNopLogger(), keys, "path/to/data")
require.NoError(t, err)
require.Equal(t, tc.activeStreamersLen, len(activeStreamers))
})
Expand Down
3 changes: 2 additions & 1 deletion store/tracekv/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
dbm "github.com/cosmos/cosmos-db"

"github.com/cosmos/cosmos-sdk/store/dbadapter"
"github.com/cosmos/cosmos-sdk/store/internal/kv"
"github.com/cosmos/cosmos-sdk/store/prefix"
"github.com/cosmos/cosmos-sdk/store/tracekv"
"github.com/cosmos/cosmos-sdk/store/types"
Expand All @@ -21,7 +22,7 @@ func bz(s string) []byte { return []byte(s) }
func keyFmt(i int) []byte { return bz(fmt.Sprintf("key%0.8d", i)) }
func valFmt(i int) []byte { return bz(fmt.Sprintf("value%0.8d", i)) }

var kvPairs = []types.KVPair{
var kvPairs = []kv.Pair{
{Key: keyFmt(1), Value: valFmt(1)},
{Key: keyFmt(2), Value: valFmt(2)},
{Key: keyFmt(3), Value: valFmt(3)},
Expand Down
10 changes: 0 additions & 10 deletions store/types/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/cosmos/cosmos-sdk/store/metrics"
pruningtypes "github.com/cosmos/cosmos-sdk/store/pruning/types"
snapshottypes "github.com/cosmos/cosmos-sdk/store/snapshots/types"
"github.com/cosmos/cosmos-sdk/types/kv"
)

type Store interface {
Expand Down Expand Up @@ -433,11 +432,6 @@ func (key *MemoryStoreKey) String() string {

//----------------------------------------

// key-value result for iterator queries
type KVPair kv.Pair

//----------------------------------------

// TraceContext contains TraceKVStore context data. It will be written with
// every trace operation.
type TraceContext map[string]interface{}
Expand Down Expand Up @@ -514,7 +508,3 @@ func NewMemoryStoreKeys(names ...string) map[string]*MemoryStoreKey {

return keys
}

// StoreDecoderRegistry defines each of the modules store decoders. Used for ImportExport
// simulation.
type StoreDecoderRegistry map[string]func(kvA, kvB kv.Pair) string
1 change: 1 addition & 0 deletions tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ replace (
github.com/99designs/keyring => github.com/cosmos/keyring v1.2.0
// We always want to test against the latest version of the SDK.
github.com/cosmos/cosmos-sdk => ../.
github.com/cosmos/cosmos-sdk/x/nft => ../x/nft
// Fix upstream GHSA-h395-qcrw-5vmq vulnerability.
// TODO Remove it: https://github.com/cosmos/cosmos-sdk/issues/10409
github.com/gin-gonic/gin => github.com/gin-gonic/gin v1.8.1
Expand Down
2 changes: 1 addition & 1 deletion testutil/sims/simulation_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func PrintStats(db dbm.DB) {

// GetSimulationLog unmarshals the KVPair's Value to the corresponding type based on the
// each's module store key and the prefix bytes of the KVPair's key.
func GetSimulationLog(storeName string, sdr storetypes.StoreDecoderRegistry, kvAs, kvBs []kv.Pair) (log string) {
func GetSimulationLog(storeName string, sdr simtypes.StoreDecoderRegistry, kvAs, kvBs []kv.Pair) (log string) {
for i := 0; i < len(kvAs); i++ {
if len(kvAs[i].Value) == 0 && len(kvBs[i].Value) == 0 {
// skip if the value doesn't have any bytes
Expand Down
3 changes: 2 additions & 1 deletion testutil/sims/simulation_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ import (

storetypes "github.com/cosmos/cosmos-sdk/store/types"
"github.com/cosmos/cosmos-sdk/types/kv"
"github.com/cosmos/cosmos-sdk/types/simulation"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
)

func TestGetSimulationLog(t *testing.T) {
legacyAmino := codec.NewLegacyAmino()
decoders := make(storetypes.StoreDecoderRegistry)
decoders := make(simulation.StoreDecoderRegistry)
decoders[authtypes.StoreKey] = func(kvAs, kvBs kv.Pair) string { return "10" }

tests := []struct {
Expand Down
7 changes: 3 additions & 4 deletions types/module/simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

sdkmath "cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
"github.com/cosmos/cosmos-sdk/types/simulation"
)

Expand All @@ -22,7 +21,7 @@ type AppModuleSimulation interface {
ProposalContents(simState SimulationState) []simulation.WeightedProposalContent

// register a func to decode the each module's defined types from their corresponding store key
RegisterStoreDecoder(storetypes.StoreDecoderRegistry)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is interface breaking but I think its acceptable since other wise store has a simulation type for no reason

RegisterStoreDecoder(simulation.StoreDecoderRegistry)

// simulation operations (i.e msgs) with their respective weight
WeightedOperations(simState SimulationState) []simulation.WeightedOperation
Expand All @@ -32,7 +31,7 @@ type AppModuleSimulation interface {
// for managing and executing simulation functionalities for a group of modules
type SimulationManager struct {
Modules []AppModuleSimulation // array of app modules; we use an array for deterministic simulation tests
StoreDecoders storetypes.StoreDecoderRegistry // functions to decode the key-value pairs from each module's store
StoreDecoders simulation.StoreDecoderRegistry // functions to decode the key-value pairs from each module's store
}

// NewSimulationManager creates a new SimulationManager object
Expand All @@ -41,7 +40,7 @@ type SimulationManager struct {
func NewSimulationManager(modules ...AppModuleSimulation) *SimulationManager {
return &SimulationManager{
Modules: modules,
StoreDecoders: make(storetypes.StoreDecoderRegistry),
StoreDecoders: make(simulation.StoreDecoderRegistry),
}
}

Expand Down
5 changes: 5 additions & 0 deletions types/simulation/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/kv"
"github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx"
)

Expand Down Expand Up @@ -172,3 +173,7 @@ type Params interface {
LivenessTransitionMatrix() TransitionMatrix
BlockSizeTransitionMatrix() TransitionMatrix
}

// StoreDecoderRegistry defines each of the modules store decoders. Used for ImportExport
// simulation.
type StoreDecoderRegistry map[string]func(kvA, kvB kv.Pair) string
2 changes: 1 addition & 1 deletion x/auth/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func (AppModule) ProposalContents(simState module.SimulationState) []simtypes.We
}

// RegisterStoreDecoder registers a decoder for auth module's types
func (am AppModule) RegisterStoreDecoder(sdr store.StoreDecoderRegistry) {
func (am AppModule) RegisterStoreDecoder(sdr simtypes.StoreDecoderRegistry) {
sdr[types.StoreKey] = simulation.NewDecodeStore(am.accountKeeper)
}

Expand Down
2 changes: 1 addition & 1 deletion x/authz/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func (am AppModule) ProposalContents(simState module.SimulationState) []simtypes
}

// RegisterStoreDecoder registers a decoder for authz module's types
func (am AppModule) RegisterStoreDecoder(sdr store.StoreDecoderRegistry) {
func (am AppModule) RegisterStoreDecoder(sdr simtypes.StoreDecoderRegistry) {
sdr[keeper.StoreKey] = simulation.NewDecodeStore(am.cdc)
}

Expand Down
2 changes: 1 addition & 1 deletion x/bank/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func (AppModule) ProposalContents(_ module.SimulationState) []simtypes.WeightedP
}

// RegisterStoreDecoder registers a decoder for supply module's types
func (am AppModule) RegisterStoreDecoder(_ store.StoreDecoderRegistry) {}
func (am AppModule) RegisterStoreDecoder(_ simtypes.StoreDecoderRegistry) {}

// WeightedOperations returns the all the gov module operations with their respective weights.
func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation {
Expand Down
2 changes: 1 addition & 1 deletion x/capability/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (am AppModule) ProposalContents(simState module.SimulationState) []simtypes
}

// RegisterStoreDecoder registers a decoder for capability module's types
func (am AppModule) RegisterStoreDecoder(sdr store.StoreDecoderRegistry) {
func (am AppModule) RegisterStoreDecoder(sdr simtypes.StoreDecoderRegistry) {
sdr[types.StoreKey] = simulation.NewDecodeStore(am.cdc)
}

Expand Down
2 changes: 1 addition & 1 deletion x/distribution/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func (am AppModule) ProposalContents(simState module.SimulationState) []simtypes
}

// RegisterStoreDecoder registers a decoder for distribution module's types
func (am AppModule) RegisterStoreDecoder(sdr store.StoreDecoderRegistry) {
func (am AppModule) RegisterStoreDecoder(sdr simtypes.StoreDecoderRegistry) {
sdr[types.StoreKey] = simulation.NewDecodeStore(am.cdc)
}

Expand Down
2 changes: 1 addition & 1 deletion x/evidence/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func (am AppModule) ProposalContents(simState module.SimulationState) []simtypes
}

// RegisterStoreDecoder registers a decoder for evidence module's types
func (am AppModule) RegisterStoreDecoder(sdr store.StoreDecoderRegistry) {
func (am AppModule) RegisterStoreDecoder(sdr simtypes.StoreDecoderRegistry) {
sdr[types.StoreKey] = simulation.NewDecodeStore(am.keeper)
}

Expand Down
2 changes: 1 addition & 1 deletion x/feegrant/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func (AppModule) ProposalContents(simState module.SimulationState) []simtypes.We
}

// RegisterStoreDecoder registers a decoder for feegrant module's types
func (am AppModule) RegisterStoreDecoder(sdr store.StoreDecoderRegistry) {
func (am AppModule) RegisterStoreDecoder(sdr simtypes.StoreDecoderRegistry) {
sdr[feegrant.StoreKey] = simulation.NewDecodeStore(am.cdc)
}

Expand Down
2 changes: 1 addition & 1 deletion x/gov/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ func (AppModule) ProposalContents(simState module.SimulationState) []simtypes.We
}

// RegisterStoreDecoder registers a decoder for gov module's types
func (am AppModule) RegisterStoreDecoder(sdr store.StoreDecoderRegistry) {
func (am AppModule) RegisterStoreDecoder(sdr simtypes.StoreDecoderRegistry) {
sdr[govtypes.StoreKey] = simulation.NewDecodeStore(am.cdc)
}

Expand Down
2 changes: 1 addition & 1 deletion x/group/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func (am AppModule) ProposalContents(simState module.SimulationState) []simtypes
}

// RegisterStoreDecoder registers a decoder for group module's types
func (am AppModule) RegisterStoreDecoder(sdr store.StoreDecoderRegistry) {
func (am AppModule) RegisterStoreDecoder(sdr simtypes.StoreDecoderRegistry) {
sdr[group.StoreKey] = simulation.NewDecodeStore(am.cdc)
}

Expand Down
2 changes: 1 addition & 1 deletion x/mint/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func (AppModule) ProposalContents(simState module.SimulationState) []simtypes.We
}

// RegisterStoreDecoder registers a decoder for mint module's types.
func (am AppModule) RegisterStoreDecoder(sdr store.StoreDecoderRegistry) {
func (am AppModule) RegisterStoreDecoder(sdr simtypes.StoreDecoderRegistry) {
sdr[types.StoreKey] = simulation.NewDecodeStore(am.cdc)
}

Expand Down
2 changes: 1 addition & 1 deletion x/nft/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (am AppModule) ProposalContents(simState module.SimulationState) []simtypes
}

// RegisterStoreDecoder registers a decoder for nft module's types
func (am AppModule) RegisterStoreDecoder(sdr store.StoreDecoderRegistry) {
func (am AppModule) RegisterStoreDecoder(sdr simtypes.StoreDecoderRegistry) {
sdr[keeper.StoreKey] = simulation.NewDecodeStore(am.cdc)
}

Expand Down
Loading