From fd3ebbd091aef77839a2fd59fb6b95ace88e9ef1 Mon Sep 17 00:00:00 2001 From: Peter Bukva Date: Fri, 1 Sep 2023 13:19:06 +0100 Subject: [PATCH 1/3] Municipal Inlf. Cache: null ptr in GetInflation(...) --- Makefile | 2 +- x/mint/cache/municipal_inflation_cahe.go | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 26a572d54d..3c9ba0cd01 100644 --- a/Makefile +++ b/Makefile @@ -332,7 +332,7 @@ golangci_lint_cmd=$(DOCKER) run --rm -v $(CURDIR):/work -w /work $(containerGola lint: lint-go-diff @#if docker ps -a --format '{{.Names}}' | grep -Eq "^${containerMarkdownLint}$$"; then docker start -a $(containerMarkdownLint); else docker run --name $(containerMarkdownLint) -i -v "$(CURDIR):/work" $(markdownLintImage); fi - docker run -i --rm -v "$(CURDIR):/work" $(containerMarkdownLintImage) . + docker run -i --rm -v "$(CURDIR):/work" $(containerMarkdownLintImage) -- . lint-fix: @#if docker ps -a --format '{{.Names}}' | grep -Eq "^${containerMarkdownLintFix}$$"; then docker start -a $(containerMarkdownLintFix); else docker run --name $(containerMarkdownLintFix) -i -v "$(CURDIR):/work" $(markdownLintImage) . --fix; fi diff --git a/x/mint/cache/municipal_inflation_cahe.go b/x/mint/cache/municipal_inflation_cahe.go index 5b2695b7bc..7708a638b8 100644 --- a/x/mint/cache/municipal_inflation_cahe.go +++ b/x/mint/cache/municipal_inflation_cahe.go @@ -57,14 +57,14 @@ func (cache *MunicipalInflationCache) RefreshIfNecessary(inflations *[]*types.Mu } } -func (cache *MunicipalInflationCache) GetInflation(denom string) (MunicipalInflationCacheItem, bool) { +func (cache *MunicipalInflationCache) GetInflation(denom string) (*MunicipalInflationCacheItem, bool) { val := cache.internal.Load() if val == nil { - return MunicipalInflationCacheItem{}, false + return nil, false } infl, exists := val.(*MunicipalInflationCacheInternal).inflations[denom] - return *infl, exists + return infl, exists } func (cache *MunicipalInflationCache) GetOriginal() *[]*types.MunicipalInflationPair { From 64f2fd0d8c84155028c6fbdbae803a1ec59c4f9c Mon Sep 17 00:00:00 2001 From: Peter Bukva Date: Tue, 5 Sep 2023 13:13:07 +0100 Subject: [PATCH 2/3] Simplifying usage of `GetInflation(...)` (return value) --- x/mint/abci.go | 4 ++-- x/mint/cache/municipal_inflation_cahe.go | 11 ++++++++--- x/mint/keeper/grpc_query.go | 4 ++-- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/x/mint/abci.go b/x/mint/abci.go index 283cb57bdc..46064ba68a 100644 --- a/x/mint/abci.go +++ b/x/mint/abci.go @@ -23,9 +23,9 @@ func HandleMunicipalInflation(minter *types.Minter, params *types.Params, ctx *s // gather supply value & calculate number of new tokens created from relevant inflation totalDenomSupply := k.BankKeeper.GetSupply(*ctx, pair.Denom) - cacheItem, exists := cache.GMunicipalInflationCache.GetInflation(pair.Denom) + cacheItem := cache.GMunicipalInflationCache.GetInflation(pair.Denom) - if !exists { + if cacheItem == nil { panic(fmt.Errorf("numicipal inflation: missing cache item for the \"%s\" denomination", pair.Denom)) } diff --git a/x/mint/cache/municipal_inflation_cahe.go b/x/mint/cache/municipal_inflation_cahe.go index 7708a638b8..7b61dc6c38 100644 --- a/x/mint/cache/municipal_inflation_cahe.go +++ b/x/mint/cache/municipal_inflation_cahe.go @@ -57,14 +57,19 @@ func (cache *MunicipalInflationCache) RefreshIfNecessary(inflations *[]*types.Mu } } -func (cache *MunicipalInflationCache) GetInflation(denom string) (*MunicipalInflationCacheItem, bool) { +func (cache *MunicipalInflationCache) GetInflation(denom string) *MunicipalInflationCacheItem { val := cache.internal.Load() if val == nil { - return nil, false + return nil } infl, exists := val.(*MunicipalInflationCacheInternal).inflations[denom] - return infl, exists + + if exists && infl != nil { + return infl + } + + return nil } func (cache *MunicipalInflationCache) GetOriginal() *[]*types.MunicipalInflationPair { diff --git a/x/mint/keeper/grpc_query.go b/x/mint/keeper/grpc_query.go index 8763214746..f9facec323 100644 --- a/x/mint/keeper/grpc_query.go +++ b/x/mint/keeper/grpc_query.go @@ -35,8 +35,8 @@ func (k Keeper) MunicipalInflation(c context.Context, req *types.QueryMunicipalI return &types.QueryMunicipalInflationResponse{Inflations: *cache.GMunicipalInflationCache.GetOriginal()}, nil } - infl, exists := cache.GMunicipalInflationCache.GetInflation(denom) - if !exists { + infl := cache.GMunicipalInflationCache.GetInflation(denom) + if infl == nil { return nil, fmt.Errorf("there is no municipal inflation defined for requested \"%s\" denomination", denom) } From c3f2457ae78130213814f7ffeaa6b65af63b76d5 Mon Sep 17 00:00:00 2001 From: Peter Bukva Date: Tue, 5 Sep 2023 13:49:21 +0100 Subject: [PATCH 3/3] Dropping unnecessary check --- x/mint/cache/municipal_inflation_cahe.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/mint/cache/municipal_inflation_cahe.go b/x/mint/cache/municipal_inflation_cahe.go index 7b61dc6c38..38d8a12849 100644 --- a/x/mint/cache/municipal_inflation_cahe.go +++ b/x/mint/cache/municipal_inflation_cahe.go @@ -65,7 +65,7 @@ func (cache *MunicipalInflationCache) GetInflation(denom string) *MunicipalInfla infl, exists := val.(*MunicipalInflationCacheInternal).inflations[denom] - if exists && infl != nil { + if exists { return infl }