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: manage keepers in app.go in a map for better organization #2013

Merged
merged 3 commits into from
Nov 6, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
introduce typed keeper cache
cgorenflo committed Nov 4, 2023
commit e6c2fcfde09a83a5331b1f1f13326765736d8a30
24 changes: 17 additions & 7 deletions app/app.go
Original file line number Diff line number Diff line change
@@ -227,7 +227,7 @@ func NewAxelarApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest
tkeys := sdk.NewTransientStoreKeys(paramstypes.TStoreKey)
memKeys := sdk.NewMemoryStoreKeys(capabilitytypes.MemStoreKey)

var keepers = make(map[string]any)
keepers := newKeeperCache()
setKeeper(keepers, initParamsKeeper(appCodec, encodingConfig.Amino, keys[paramstypes.StoreKey], tkeys[paramstypes.TStoreKey]))

// set the BaseApp's parameter store
@@ -1060,26 +1060,36 @@ func GetModuleBasics() module.BasicManager {
return ModuleBasics
}

func getSubspace(keepers map[string]any, moduleName string) paramstypes.Subspace {
paramsK := getKeeper[paramskeeper.Keeper](keepers)
type keeperCache struct {
repository map[string]any
}

func newKeeperCache() *keeperCache {
return &keeperCache{
repository: make(map[string]any),
}
}

func getSubspace(k *keeperCache, moduleName string) paramstypes.Subspace {
paramsK := getKeeper[paramskeeper.Keeper](k)
subspace, ok := paramsK.GetSubspace(moduleName)
if !ok {
panic(fmt.Sprintf("subspace %s not found", moduleName))
}
return subspace
}

func getKeeper[T any](keepers map[string]any) T {
func getKeeper[T any](k *keeperCache) T {
key := fullTypeName[T]()
keeper, ok := keepers[key].(T)
keeper, ok := k.repository[key].(T)
if !ok {
panic(fmt.Sprintf("keeper %s not found", key))
}
return keeper
}

func setKeeper[T any](keepers map[string]any, keeper T) {
keepers[fullTypeName[T]()] = keeper
func setKeeper[T any](k *keeperCache, keeper T) {
k.repository[fullTypeName[T]()] = keeper
}

func fullTypeName[T any]() string {