Skip to content

Commit

Permalink
feat(cosmic-swingset): Update parseParams to read and validate vat cl…
Browse files Browse the repository at this point in the history
…eanup budget data

Ref #8928
  • Loading branch information
gibson042 committed Nov 8, 2024
1 parent 5c74e47 commit 50103ea
Showing 1 changed file with 41 additions and 2 deletions.
43 changes: 41 additions & 2 deletions packages/cosmic-swingset/src/params.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
// @ts-check
// @jessie-check

import { Fail } from '@endo/errors';
import { X, Fail, makeError } from '@endo/errors';
import { Nat, isNat } from '@endo/nat';

/**
* @param {string} s
* @returns {bigint}
*/
export const stringToNat = s => {
typeof s === 'string' || Fail`${s} must be a string`;
const bint = BigInt(s);
Expand All @@ -12,6 +16,27 @@ export const stringToNat = s => {
return nat;
};

/**
* @template T
* @template U
* @param {Array<[key: string, value: T]>} entries
* @param {(value: T) => U} [mapper]
*/
const recordFromEntries = (
entries,
mapper = x => /** @type {U} */ (/** @type {unknown} */ (x)),
) =>
Object.fromEntries(
entries.map(([key, value]) => {
typeof key === 'string' || Fail`Key ${key} must be a string`;
try {
return [key, mapper(value)];
} catch (err) {
throw makeError(X`${key} value was invalid`, undefined, { cause: err });
}
}),
);

/** @param {{key: string, size: number}[]} queueSizeEntries */
export const parseQueueSizes = queueSizeEntries =>
Object.fromEntries(
Expand All @@ -38,7 +63,9 @@ export const parseParams = params => {
beans_per_unit: rawBeansPerUnit,
fee_unit_price: rawFeeUnitPrice,
queue_max: rawQueueMax,
vat_cleanup_budget: rawVatCleanupBudget,
} = params;

Array.isArray(rawBeansPerUnit) ||
Fail`beansPerUnit must be an array, not ${rawBeansPerUnit}`;
const beansPerUnit = Object.fromEntries(
Expand All @@ -47,16 +74,28 @@ export const parseParams = params => {
return [key, stringToNat(beans)];
}),
);

Array.isArray(rawFeeUnitPrice) ||
Fail`feeUnitPrice ${rawFeeUnitPrice} must be an array`;
const feeUnitPrice = rawFeeUnitPrice.map(({ denom, amount }) => {
typeof denom === 'string' || Fail`denom ${denom} must be a string`;
denom || Fail`denom ${denom} must be non-empty`;
return { denom, amount: stringToNat(amount) };
});

Array.isArray(rawQueueMax) ||
Fail`queueMax must be an array, not ${rawQueueMax}`;
const queueMax = parseQueueSizes(rawQueueMax);

return { beansPerUnit, feeUnitPrice, queueMax };
Array.isArray(rawVatCleanupBudget) ||
Fail`vatCleanupBudget must be an array, not ${rawVatCleanupBudget}`;
const vatCleanupBudget = recordFromEntries(
rawVatCleanupBudget.map(({ key, value }) => [key, value]),
s => Number(stringToNat(s)),
);
rawVatCleanupBudget.length === 0 ||
vatCleanupBudget.default !== undefined ||
Fail`vatCleanupBudget.default must be provided when vatCleanupBudget is not empty`;

return { beansPerUnit, feeUnitPrice, queueMax, vatCleanupBudget };
};

0 comments on commit 50103ea

Please sign in to comment.