Skip to content

Commit

Permalink
feat(cosmos): integrate new proto definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Nov 2, 2021
1 parent 2d8dffc commit a3e9689
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 77 deletions.
100 changes: 35 additions & 65 deletions golang/cosmos/x/swingset/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ import (

// Parameter keys
var (
ParamStoreKeyMaxComputronsPerBlock = []byte("max_computrons_per_block")
ParamStoreKeyEstimatedComputronsPerVatCreation = []byte("estimated_computrons_per_vat_creation")
ParamStoreKeyFeeDenom = []byte("fee_denom")
ParamStoreKeyFeePerInboundTx = []byte("fee_per_inbound_tx")
ParamStoreKeyFeePerMessage = []byte("fee_per_message")
ParamStoreKeyFeePerMessageByte = []byte("fee_per_message_byte")
ParamStoreKeyFeePerMessageSlot = []byte("fee_per_message_slot")
ParamStoreKeyBeansPerUnit = []byte("beans_per_unit")
ParamStoreKeyFeeUnitPrice = []byte("fee_unit_price")
)

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

// ParamKeyTable returns the parameter key table.
func ParamKeyTable() paramtypes.KeyTable {
return paramtypes.NewKeyTable().RegisterParamSet(&Params{})
Expand All @@ -28,13 +29,8 @@ func ParamKeyTable() paramtypes.KeyTable {
// DefaultParams returns default swingset parameters
func DefaultParams() Params {
return Params{
MaxComputronsPerBlock: DefaultMaxComputronsPerBlock,
EstimatedComputronsPerVatCreation: DefaultEstimatedComputronsPerVatCreation,
FeeDenom: DefaultFeeDenom,
FeePerInboundTx: DefaultFeePerInboundTx,
FeePerMessage: DefaultFeePerMessage,
FeePerMessageByte: DefaultFeePerMessageByte,
FeePerMessageSlot: DefaultFeePerMessageSlot,
BeansPerUnit: DefaultBeansPerUnit,
FeeUnitPrice: DefaultFeeUnitPrice,
}
}

Expand All @@ -46,82 +42,56 @@ func (p Params) String() string {
// ParamSetPairs returns the parameter set pairs.
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{
paramtypes.NewParamSetPair(ParamStoreKeyMaxComputronsPerBlock, &p.MaxComputronsPerBlock, validateMaxComputronsPerBlock),
paramtypes.NewParamSetPair(ParamStoreKeyEstimatedComputronsPerVatCreation, &p.EstimatedComputronsPerVatCreation, validateEstimatedComputronsPerVatCreation),
paramtypes.NewParamSetPair(ParamStoreKeyFeeDenom, &p.FeeDenom, validateFeeDenom),
paramtypes.NewParamSetPair(ParamStoreKeyFeePerInboundTx, &p.FeePerInboundTx, validateFeePerInboundTx),
paramtypes.NewParamSetPair(ParamStoreKeyFeePerMessage, &p.FeePerMessage, validateFeePerMessage),
paramtypes.NewParamSetPair(ParamStoreKeyFeePerMessageByte, &p.FeePerMessageByte, validateFeePerMessageByte),
paramtypes.NewParamSetPair(ParamStoreKeyFeePerMessageSlot, &p.FeePerMessageSlot, validateFeePerMessageSlot),
paramtypes.NewParamSetPair(ParamStoreKeyBeansPerUnit, &p.BeansPerUnit, validateBeansPerUnit),
paramtypes.NewParamSetPair(ParamStoreKeyFeeUnitPrice, &p.FeeUnitPrice, validateFeeUnitPrice),
}
}

// ValidateBasic performs basic validation on swingset parameters.
func (p Params) ValidateBasic() error {
if err := validateMaxComputronsPerBlock(p.MaxComputronsPerBlock); err != nil {
if err := validateBeansPerUnit(p.BeansPerUnit); err != nil {
return err
}

return nil
}

func validateMaxComputronsPerBlock(i interface{}) error {
v, ok := i.(sdk.Int)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}

if !v.IsPositive() {
return fmt.Errorf("max computrons per block must be positive: %s", v)
if err := validateFeeUnitPrice(p.FeeUnitPrice); err != nil {
return err
}

return nil
}

func validateEstimatedComputronsPerVatCreation(i interface{}) error {
v, ok := i.(sdk.Int)
func validateBeansPerUnit(i interface{}) error {
v, ok := i.(map[string]Beans)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}

if v.IsNegative() {
return fmt.Errorf("estimated computrons per vat creation must not be negative: %s", v)
for unit, beans := 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
}

func validateFeeDenom(i interface{}) error {
v, ok := i.(string)
func validateFeeUnitPrice(i interface{}) error {
v, ok := i.(sdk.Coins)
if !ok {
return fmt.Errorf("invalid fee denom parameter type: %T", i)
}

if err := sdk.ValidateDenom(v); err != nil {
return fmt.Errorf("invalid fee denom %s: %w", v, err)
return fmt.Errorf("invalid parameter type: %T", i)
}

return nil
}

func makeFeeValidator(description string) func(interface{}) error {
return func(i interface{}) error {
v, ok := i.(sdk.Dec)
if !ok {
return fmt.Errorf("invalid %s parameter type: %T", description, i)
for _, coin := range v {
if err := sdk.ValidateDenom(coin.Denom); err != nil {
return fmt.Errorf("fee unit price denom %s must be valid: %e", coin.Denom, err)
}

if v.IsNegative() {
return fmt.Errorf("%s amount must not be negative: %s", description, v)
if coin.Amount.IsNegative() {
return fmt.Errorf("fee unit price %s must not be negative: %s", coin.Denom, coin.Amount)
}

return nil
}
}

var (
validateFeePerInboundTx = makeFeeValidator("fee per inbound tx")
validateFeePerMessage = makeFeeValidator("fee per message")
validateFeePerMessageByte = makeFeeValidator("fee per message byte")
validateFeePerMessageSlot = makeFeeValidator("fee per message slot")
)
return nil
}
51 changes: 39 additions & 12 deletions golang/cosmos/x/swingset/types/sim-params.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,48 @@ import (
// This should roughly match the values in
// `agoric-sdk/packages/cosmic-swingset/src/sim-params.js`.
//
// Nothing bad happens if they diverge, but it makes for a better simulation
// Nothing bad happens if they diverge, but it makes for a truer simulation
// experience if they don't.

const (
BeansPerFeeUnit = "feeUnit"
BeansPerInboundTx = "inboundTx"
BeansPerBlockComputeLimit = "blockComputeLimit"
BeansPerMessage = "message"
BeansPerMessageByte = "messageByte"
BeansPerMinFeeDebit = "minFeeDebit"
BeansPerVatCreation = "vatCreation"
BeansPerXsnapComputron = "xsnapComputron"
)

var (
// This is how many computrons 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.
DefaultMaxComputronsPerBlock = sdk.NewInt(8000000)
DefaultBeansPerXsnapComputron = sdk.NewInt(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)
// observed: 0.385 sec
DefaultEstimatedComputronsPerVatCreation = sdk.NewInt(300000)
DefaultBeansPerVatCreation = sdk.NewInt(300000).Mul(DefaultBeansPerXsnapComputron)

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

// These fee defaults are denominated in urun.
DefaultFeeDenom = "urun"
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

DefaultFeePerInboundTx = sdk.MustNewDecFromStr("100")
DefaultFeePerMessage = sdk.MustNewDecFromStr("10")
DefaultFeePerMessageSlot = sdk.MustNewDecFromStr("1")
DefaultFeePerMessageByte = sdk.MustNewDecFromStr("0.02")
DefaultBeansPerUnit = map[string]Beans{
BeansPerBlockComputeLimit: NewBeans(DefaultBeansPerBlockComputeLimit),
BeansPerFeeUnit: NewBeans(DefaultBeansPerFeeUnit),
BeansPerInboundTx: NewBeans(DefaultBeansPerInboundTx),
BeansPerMessage: NewBeans(DefaultBeansPerMessage),
BeansPerMessageByte: NewBeans(DefaultBeansPerMessageByte),
BeansPerMinFeeDebit: NewBeans(DefaultBeansPerMinFeeDebit),
BeansPerVatCreation: NewBeans(DefaultBeansPerVatCreation),
BeansPerXsnapComputron: NewBeans(DefaultBeansPerXsnapComputron),
}
)

0 comments on commit a3e9689

Please sign in to comment.