From caf8f4403af507dc2694e6ef5e2aa2778564cf74 Mon Sep 17 00:00:00 2001 From: dongsam Date: Sun, 20 Mar 2022 23:02:03 +0900 Subject: [PATCH] fix: budgetsBySourceMap itrating deterministic --- app/sim_test.go | 4 ++-- x/budget/keeper/budget.go | 6 ++++-- x/budget/types/budget.go | 6 ++++-- x/budget/types/params.go | 12 ++++++------ 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/app/sim_test.go b/app/sim_test.go index 1ee8b1b..7f8bfdb 100644 --- a/app/sim_test.go +++ b/app/sim_test.go @@ -286,8 +286,8 @@ func TestAppStateDeterminism(t *testing.T) { config.AllInvariants = false config.ChainID = helpers.SimAppChainID - numSeeds := 1 - numTimesToRunPerSeed := 1 + numSeeds := 3 + numTimesToRunPerSeed := 5 appHashList := make([]json.RawMessage, numTimesToRunPerSeed) for i := 0; i < numSeeds; i++ { diff --git a/x/budget/keeper/budget.go b/x/budget/keeper/budget.go index c61c4eb..9f5702e 100644 --- a/x/budget/keeper/budget.go +++ b/x/budget/keeper/budget.go @@ -3,6 +3,7 @@ package keeper import ( sdk "github.com/cosmos/cosmos-sdk/types" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + "github.com/tendermint/budget/x/budget/types" ) @@ -20,8 +21,9 @@ func (k Keeper) CollectBudgets(ctx sdk.Context) error { // Get a map GetBudgetsBySourceMap that has a list of budgets and their total rate, which // contain the same SourceAddress - budgetsBySourceMap := types.GetBudgetsBySourceMap(budgets) - for source, budgetsBySource := range budgetsBySourceMap { + budgetsBySourceMap, budgetSources := types.GetBudgetsBySourceMap(budgets) + for _, source := range budgetSources { + budgetsBySource := budgetsBySourceMap[source] sourceAcc, err := sdk.AccAddressFromBech32(source) if err != nil { return err diff --git a/x/budget/types/budget.go b/x/budget/types/budget.go index c8097f3..1b84525 100644 --- a/x/budget/types/budget.go +++ b/x/budget/types/budget.go @@ -99,8 +99,9 @@ type BudgetsBySourceMap map[string]BudgetsBySource // GetBudgetsBySourceMap returns BudgetsBySourceMap that has a list of budgets and their total rate // which contain the same SourceAddress. It can be used to track of what budgets are available with SourceAddress // and validate their total rate. -func GetBudgetsBySourceMap(budgets []Budget) BudgetsBySourceMap { +func GetBudgetsBySourceMap(budgets []Budget) (BudgetsBySourceMap, []string) { budgetsMap := make(BudgetsBySourceMap) + budgetSources := []string{} for _, budget := range budgets { if budgetsBySource, ok := budgetsMap[budget.SourceAddress]; ok { budgetsBySource.TotalRate = budgetsBySource.TotalRate.Add(budget.Rate) @@ -111,7 +112,8 @@ func GetBudgetsBySourceMap(budgets []Budget) BudgetsBySourceMap { Budgets: []Budget{budget}, TotalRate: budget.Rate, } + budgetSources = append(budgetSources, budget.SourceAddress) } } - return budgetsMap + return budgetsMap, budgetSources } diff --git a/x/budget/types/params.go b/x/budget/types/params.go index 86b3a24..1819c52 100644 --- a/x/budget/types/params.go +++ b/x/budget/types/params.go @@ -86,14 +86,14 @@ func ValidateBudgets(input interface{}) error { } names[budget.Name] = true } - budgetsBySourceMap := GetBudgetsBySourceMap(budgets) - for addr, budgetsBySource := range budgetsBySourceMap { - if budgetsBySource.TotalRate.GT(sdk.OneDec()) { + budgetsBySourceMap, budgetSources := GetBudgetsBySourceMap(budgets) + for _, source := range budgetSources { + if budgetsBySourceMap[source].TotalRate.GT(sdk.OneDec()) { // If the TotalRate of Budgets with the same source address exceeds 1, // recalculate and verify the TotalRate of Budgets with overlapping time ranges. - for i, budget := range budgetsBySource.Budgets { + for i, budget := range budgetsBySourceMap[source].Budgets { totalRate := budget.Rate - for j, budgetToCheck := range budgetsBySource.Budgets { + for j, budgetToCheck := range budgetsBySourceMap[source].Budgets { if i != j && budgetToCheck.Collectible(budget.StartTime) { totalRate = totalRate.Add(budgetToCheck.Rate) } @@ -101,7 +101,7 @@ func ValidateBudgets(input interface{}) error { if totalRate.GT(sdk.OneDec()) { return sdkerrors.Wrapf( ErrInvalidTotalBudgetRate, - "total rate for source address %s must not exceed 1: %v", addr, totalRate) + "total rate for source address %s must not exceed 1: %v", source, totalRate) } }