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

[Feature]: Add support for test sdk.Context with n store keys, plus mem store keys #16998

Closed
damiannolan opened this issue Jul 14, 2023 · 2 comments · Fixed by #17216
Closed
Assignees
Labels
T: Dev UX UX for SDK developers (i.e. how to call our code) T:feature-request T: Tests

Comments

@damiannolan
Copy link
Member

damiannolan commented Jul 14, 2023

Summary

Add support for test sdk.Context to be able to mount any number (and supported type - key, transientKey, memKey) of keys on the cms before creating and returning the test context.

Problem Definition

This is a super nice test helper for creating a sdk.Context with a custom store key - https://github.com/cosmos/cosmos-sdk/blob/main/testutil/context.go#L20.

However, it doesn't support mounting multiple store keys at once and the ability to mount MemoryStoreKeys on the cms.

It's likely no one had faced an issue with this as module's tend to have a single store key. However I ran into an issue when refactoring tests for the sdk v0.50 upgrade, as x/capability deals with mem store keys. See cosmos/ibc-go#4068 (comment)

Proposal

Add something like this as to not break the current usage in existing tests. Or rip the bandaid off and change the DefaultContext API.

func DefaultContextWithKeys(
	keys map[string]*storetypes.KVStoreKey,
	transKeys map[string]*storetypes.TransientStoreKey,
	memKeys map[string]*storetypes.MemoryStoreKey,
) sdk.Context {
	db := dbm.NewMemDB()
	cms := store.NewCommitMultiStore(db, log.NewNopLogger(), metrics.NewNoOpMetrics())

	for _, key := range keys {
		cms.MountStoreWithDB(key, storetypes.StoreTypeIAVL, db)
	}

	for _, tKey := range transKeys {
		cms.MountStoreWithDB(tKey, storetypes.StoreTypeTransient, db)
	}

	for _, memkey := range memKeys {
		cms.MountStoreWithDB(memkey, storetypes.StoreTypeMemory, db)
	}

	err := cms.LoadLatestVersion()
	if err != nil {
		panic(err)
	}

	return sdk.NewContext(cms, cmtproto.Header{}, false, log.NewNopLogger())
}

Usage looks something like:

func TestFoo() {
	keys := storetypes.NewKVStoreKeys("mock")
	tKeys := storetypes.NewTransientStoreKeys("t-mock")
	memKeys := storetypes.NewMemoryStoreKeys("mem-mock", "mem-mock-2")

	ctx := DefaultContextWithKeys(keys, tKeys, memKeys)
}
@julienrbrt
Copy link
Member

Seems useful to me, do you want a submit a PR for that?

@damiannolan
Copy link
Member Author

Yeah definitely, I'll try get to it next week when I'm back around

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T: Dev UX UX for SDK developers (i.e. how to call our code) T:feature-request T: Tests
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants