Skip to content

Commit

Permalink
feat(x/swingset): Require a non-empty vat cleanup budget to include `…
Browse files Browse the repository at this point in the history
…default`
  • Loading branch information
gibson042 committed Oct 22, 2024
1 parent 0976f30 commit bb28c19
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
12 changes: 11 additions & 1 deletion golang/cosmos/x/swingset/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,20 @@ func validateQueueMax(i interface{}) error {
}

func validateVatCleanupBudget(i interface{}) error {
_, ok := i.([]UintMapEntry)
entries, ok := i.([]UintMapEntry)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
hasDefault := false
for _, entry := range entries {
if entry.Key == VatCleanupDefault {
hasDefault = true
break
}
}
if len(entries) > 0 && !hasDefault {
return fmt.Errorf("`default` must be present in a non-empty vat cleanup budget")
}
return nil
}

Expand Down
34 changes: 34 additions & 0 deletions golang/cosmos/x/swingset/types/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,37 @@ func TestUpdateParamsFromExisting(t *testing.T) {
t.Errorf("GOT\n%v\nWANTED\n%v", got, want)
}
}

func TestValidateParams(t *testing.T) {
params := Params{
BeansPerUnit: DefaultBeansPerUnit(),
BootstrapVatConfig: "foo",
FeeUnitPrice: sdk.NewCoins(sdk.NewInt64Coin("denom", 789)),
PowerFlagFees: DefaultPowerFlagFees,
QueueMax: DefaultQueueMax,
VatCleanupBudget: DefaultVatCleanupBudget,
}
err := params.ValidateBasic()
if err != nil {
t.Errorf("unexpected ValidateBasic() error with default params: %v", err)
}

customVatCleanup := UintMapEntry{"corge", sdk.NewUint(4)}
params.VatCleanupBudget = append(params.VatCleanupBudget, customVatCleanup)
err = params.ValidateBasic()
if err != nil {
t.Errorf("unexpected ValidateBasic() error with extended params: %v", err)
}

params.VatCleanupBudget = params.VatCleanupBudget[1:]
err = params.ValidateBasic()
if err == nil {
t.Errorf("ValidateBasic() failed to reject VatCleanupBudget with missing `default` %v", params.VatCleanupBudget)
}

params.VatCleanupBudget = []UintMapEntry{}
err = params.ValidateBasic()
if err != nil {
t.Errorf("unexpected ValidateBasic() error with empty VatCleanupBudget: %v", params.VatCleanupBudget)
}
}

0 comments on commit bb28c19

Please sign in to comment.