Skip to content

Commit

Permalink
feat(beans): use sdk.Uint whole beans to prevent negatives
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Nov 3, 2021
1 parent f7e0710 commit 46f7fdc
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 53 deletions.
4 changes: 2 additions & 2 deletions golang/cosmos/proto/agoric/swingset/swingset.proto
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ message Params {
message Beans {
option (gogoproto.equal) = true;

string int = 1 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
string whole = 1 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Uint",
(gogoproto.nullable) = false
];
}
10 changes: 3 additions & 7 deletions golang/cosmos/x/swingset/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ var (
ParamStoreKeyFeeUnitPrice = []byte("fee_unit_price")
)

func NewBeans(i sdk.Int) Beans {
func NewBeans(u sdk.Uint) Beans {
return Beans{
Int: i,
Whole: u,
}
}

Expand Down Expand Up @@ -65,14 +65,10 @@ func validateBeansPerUnit(i interface{}) error {
return fmt.Errorf("invalid parameter type: %T", i)
}

for unit, beans := range v {
for unit := range v {
if unit == "" {
return fmt.Errorf("unit must not be empty")
}

if beans.Int.IsNegative() {
return fmt.Errorf("beans per unit %s must not be negative: %s", unit, beans.Int)
}
}

return nil
Expand Down
16 changes: 8 additions & 8 deletions golang/cosmos/x/swingset/types/sim-params.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,24 @@ const (
)

var (
DefaultBeansPerXsnapComputron = sdk.NewInt(100)
DefaultBeansPerXsnapComputron = sdk.NewUint(100)

// DefaultBeansPerBlockComputeLimit is how many computron beans we allow
// before starting a new block. Some analysis (#3459) suggests this leads to
// about 2/3rds utilization, based on 5 sec voting time and up to 10 sec of
// computation.
DefaultBeansPerBlockComputeLimit = sdk.NewInt(8000000).Mul(DefaultBeansPerXsnapComputron)
DefaultBeansPerBlockComputeLimit = sdk.NewUint(8000000).Mul(DefaultBeansPerXsnapComputron)
// observed: 0.385 sec
DefaultBeansPerVatCreation = sdk.NewInt(300000).Mul(DefaultBeansPerXsnapComputron)
DefaultBeansPerVatCreation = sdk.NewUint(300000).Mul(DefaultBeansPerXsnapComputron)

// Fees are denominated in units of $1 RUN.
DefaultFeeUnitPrice = sdk.NewCoins(sdk.NewInt64Coin("urun", 1000000))

DefaultBeansPerFeeUnit = sdk.NewInt(1000000000000) // $1
DefaultBeansPerInboundTx = DefaultBeansPerFeeUnit.Quo(sdk.NewInt(100000)) // $0.00001
DefaultBeansPerMessage = DefaultBeansPerFeeUnit.Quo(sdk.NewInt(1000000)) // $0.000001
DefaultBeansPerMessageByte = DefaultBeansPerFeeUnit.Quo(sdk.NewInt(50000000)) // $0.0000002
DefaultBeansPerMinFeeDebit = DefaultBeansPerFeeUnit.Quo(sdk.NewInt(4)) // $0.25
DefaultBeansPerFeeUnit = sdk.NewUint(1000000000000) // $1
DefaultBeansPerInboundTx = DefaultBeansPerFeeUnit.Quo(sdk.NewUint(100000)) // $0.00001
DefaultBeansPerMessage = DefaultBeansPerFeeUnit.Quo(sdk.NewUint(1000000)) // $0.000001
DefaultBeansPerMessageByte = DefaultBeansPerFeeUnit.Quo(sdk.NewUint(50000000)) // $0.0000002
DefaultBeansPerMinFeeDebit = DefaultBeansPerFeeUnit.Quo(sdk.NewUint(4)) // $0.25

DefaultBeansPerUnit = map[string]Beans{
BeansPerBlockComputeLimit: NewBeans(DefaultBeansPerBlockComputeLimit),
Expand Down
66 changes: 33 additions & 33 deletions golang/cosmos/x/swingset/types/swingset.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/cosmic-swingset/src/block-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ const parseParams = params => {
beans,
X`${key} beans must be an object, not ${beans}`,
);
const { int, ...rest } = beans;
const { whole, ...rest } = beans;
assert.equal(
Object.keys(rest).length,
0,
X`${key} beans must have no unexpected properties; had ${rest}`,
);
return [key, stringToNat(int)];
return [key, stringToNat(whole)];
}),
);

Expand Down
2 changes: 1 addition & 1 deletion packages/cosmic-swingset/src/sim-params.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Nat } from '@agoric/nat';

const makeBeans = int => ({ int: `${Nat(int)}` });
const makeBeans = whole => ({ whole: `${Nat(whole)}` });

// This should roughly match the values in
// `agoric-sdk/golang/cosmos/x/swingset/types/sim-params.go`.
Expand Down

0 comments on commit 46f7fdc

Please sign in to comment.