Skip to content

Commit

Permalink
Merge pull request #115 from tendermint/114-deterministic-map-iterating
Browse files Browse the repository at this point in the history
fix: iterating budgetsBySourceMap deterministic
  • Loading branch information
dongsam authored Mar 21, 2022
2 parents 460d16b + caf8f44 commit 6041fc0
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
4 changes: 2 additions & 2 deletions app/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++ {
Expand Down
6 changes: 4 additions & 2 deletions x/budget/keeper/budget.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions x/budget/types/budget.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
12 changes: 6 additions & 6 deletions x/budget/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,22 +86,22 @@ 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)
}
}
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)
}
}

Expand Down

0 comments on commit 6041fc0

Please sign in to comment.