Skip to content

Commit

Permalink
chore(types): adopt Passable
Browse files Browse the repository at this point in the history
  • Loading branch information
turadg committed May 6, 2024
1 parent e5df4b5 commit eef22a6
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 17 deletions.
8 changes: 6 additions & 2 deletions packages/cache/src/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ import { E, Far } from '@endo/far';

import { makeScalarStoreCoordinator } from './store.js';

/**
* @import {Passable, RemotableObject} from '@endo/pass-style';
*/

/**
* @typedef {{ [x: PropertyKey]: any } | string | symbol | bigint | null |
* undefined | number | ((oldValue: any) => ERef<unknown>)} Update a `newValue`
* undefined | number | ((oldValue: any) => ERef<Passable>)} Update a `newValue`
* to update to. If a function, then it should take an oldValue and return a
* `newValue` or promise for `newValue`
*/
Expand All @@ -19,7 +23,7 @@ export const makeCache = (coordinator = makeScalarStoreCoordinator()) => {
* The ground state for a cache key value is `undefined`. It is impossible to
* distinguish a set value of `undefined` from an unset key
*
* @param {unknown} key the cache key (any key type acceptable to the cache)
* @param {Passable} key the cache key (any key type acceptable to the cache)
* @param {[] | [Update] | [Update, Pattern]} optUpdateGuardPattern an optional
*/
const cache = (key, ...optUpdateGuardPattern) => {
Expand Down
11 changes: 6 additions & 5 deletions packages/cache/src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { withGroundState, makeState } from './state.js';
/** @import {Passable} from '@endo/pass-style' */

/**
* @param {(obj: unknown) => unknown} [sanitize]
* @param {(obj: Passable) => Passable} [sanitize]
* @returns {(key: Passable) => Promise<string>}
*/
const makeKeyToString = (sanitize = obj => obj) => {
Expand Down Expand Up @@ -36,16 +36,16 @@ const makeKeyToString = (sanitize = obj => obj) => {

/**
* @param {string} keyStr
* @param {(oldValue: unknown) => unknown} txn
* @param {(oldValue: Passable) => Passable} txn
* @param {Pattern} guardPattern
* @param {(obj: unknown) => unknown} sanitize Process keys and values with
* @param {(obj: Passable) => Passable} sanitize Process keys and values with
* this function before storing them
* @param {{
* get(key: string): import('./state.js').State;
* set(key: string, value: import('./state.js').State): void;
* init(key: string, value: import('./state.js').State): void;
* }} stateStore
* @returns {Promise<unknown>} the value of the updated state
* @returns {Promise<Passable>} the value of the updated state
*/
const applyCacheTransaction = async (
keyStr,
Expand Down Expand Up @@ -112,6 +112,7 @@ const applyCacheTransaction = async (
* @returns {Promise<string>}
*/
const stringifyStateStore = async (stateStore, marshaller) => {
/** @type {Passable} */
const obj = {};
for (const [key, value] of stateStore.entries()) {
obj[key] = E(marshaller).toCapData(value);
Expand All @@ -126,7 +127,7 @@ const stringifyStateStore = async (stateStore, marshaller) => {
* currently enforce any cache eviction, but that would be a useful feature.
*
* @param {MapStore<string, import('./state.js').State>} [stateStore]
* @param {(obj: unknown) => unknown} [sanitize] Process keys and values with
* @param {(obj: Passable) => Passable} [sanitize] Process keys and values with
* this function before storing them. Defaults to deeplyFulfilled.
*/
export const makeScalarStoreCoordinator = (
Expand Down
5 changes: 3 additions & 2 deletions packages/governance/src/contractGovernance/governApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from '../question.js';

/**
* @import {Passable, RemotableObject} from '@endo/pass-style';
* @import {Position, ApiGovernor, ApiInvocationIssue, PoserFacet, VoteOnApiInvocation} from '../types.js';
*/

Expand All @@ -21,7 +22,7 @@ const { Fail, quote: q } = assert;
* arguments that were provided.
*
* @param {string} apiMethodName
* @param {unknown[]} methodArgs
* @param {Passable[]} methodArgs
*/
const makeApiInvocationPositions = (apiMethodName, methodArgs) => {
const positive = harden({ apiMethodName, methodArgs });
Expand All @@ -32,7 +33,7 @@ const makeApiInvocationPositions = (apiMethodName, methodArgs) => {
/**
* manage contracts that allow governance to invoke functions.
*
* @param {ERef<{ [methodName: string]: (...args: any) => unknown }>} governedApis
* @param {ERef<{ [methodName: string]: (...args: any) => Passable }>} governedApis
* @param {Array<string | symbol>} governedNames names of the governed API methods
* @param {ERef<import('@agoric/time').TimerService>} timer
* @param {() => Promise<PoserFacet>} getUpdatedPoserFacet
Expand Down
1 change: 1 addition & 0 deletions packages/governance/src/contractGovernance/governParam.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ const setupParamGovernance = (
paramSpec,
) => {
const paramMgr = await E(paramManagerRetriever).get(paramSpec.paramPath);
/** @type {import('@endo/marshal').Passable} */
const changePs = {};
for (const name of Object.keys(paramSpec.changes)) {
const proposedValue = E(paramMgr).getVisibleValue(
Expand Down
3 changes: 2 additions & 1 deletion packages/governance/src/contractGovernorKit.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ export const prepareContractGovernorKit = (baggage, powers) => {
]);
trace('setupApiGovernance');
apiGovernance = governedNames.length
? setupApiGovernance(governedApis, governedNames, timer, () =>
? // @ts-expect-error FIXME
setupApiGovernance(governedApis, governedNames, timer, () =>
this.facets.helper.getUpdatedPoserFacet(),
)
: {
Expand Down
16 changes: 10 additions & 6 deletions packages/governance/src/types.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
export {};

/** @import {ContractStartFunction} from '@agoric/zoe/src/zoeService/utils.js' */
/**
* @import {Guarded} from '@endo/exo';
* @import {Passable, Container} from '@endo/pass-style';
* @import {ContractStartFunction} from '@agoric/zoe/src/zoeService/utils.js';
*/

/**
* @typedef { 'unranked' | 'order' | 'plurality' } ChoiceMethod
Expand Down Expand Up @@ -34,9 +38,9 @@ export {};
*/

/**
* @typedef { Amount | Brand | Installation | Instance | bigint |
* @typedef { Amount | Brand | Installation | Instance | number | bigint |
* Ratio | string | import('@agoric/time').TimestampRecord |
* import('@agoric/time').RelativeTimeRecord | unknown } ParamValue
* import('@agoric/time').RelativeTimeRecord | Container<any, any> } ParamValue
*/

// XXX better to use the manifest constant ParamTypes
Expand Down Expand Up @@ -484,7 +488,7 @@ export {};

/**
* @typedef {object} ChangeParamsPosition
* @property {Record<string,ParamValue>} changes one or more changes to parameters
* @property {Record<string, ParamValue>} changes one or more changes to parameters
*/

/**
Expand All @@ -500,7 +504,7 @@ export {};
/**
* @typedef {object} InvokeApiPosition
* @property {string} apiMethodName
* @property {unknown[]} methodArgs
* @property {Passable[]} methodArgs
*/

/**
Expand Down Expand Up @@ -625,7 +629,7 @@ export {};
/**
* @callback VoteOnApiInvocation
* @param {string} apiMethodName
* @param {unknown[]} methodArgs
* @param {Passable[]} methodArgs
* @param {Installation} voteCounterInstallation
* @param {import('@agoric/time').Timestamp} deadline
* @returns {Promise<ContractGovernanceVoteResult>}
Expand Down
2 changes: 2 additions & 0 deletions packages/governance/test/unitTests/test-typedParamManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ test('Invitation', async t => {

const { instance, zcf, zoe } = await setupZCFTest(issuerKeywordRecord, terms);

/** @type {Invitation} */
const invitation = await E(E(zoe).getPublicFacet(instance)).makeInvitation();

const invitationAmount = await E(E(zoe).getInvitationIssuer()).getAmountOf(
Expand All @@ -251,6 +252,7 @@ test('Invitation', async t => {
await eventLoopIteration();
const invitationActualAmount = paramManager.getInvite().value;
t.deepEqual(invitationActualAmount, invitationAmount.value);
// @ts-expect-error XXX
t.is(invitationActualAmount[0].description, 'simple');

t.is(await paramManager.getInternalParamValue('Invite'), invitation);
Expand Down
3 changes: 2 additions & 1 deletion packages/governance/tools/puppetContractGovernor.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { CONTRACT_ELECTORATE } from '../src/contractGovernance/governParam.js';
import { makeApiInvocationPositions } from '../src/contractGovernance/governApi.js';

/**
* @import {Passable, RemotableObject} from '@endo/pass-style';
* @import {GovernableStartFn, ParamChangesSpec} from '../src/types.js';
*/

Expand Down Expand Up @@ -72,7 +73,7 @@ export const start = async (zcf, privateArgs) => {

/**
* @param {string} apiMethodName
* @param {unknown[]} methodArgs
* @param {Passable[]} methodArgs
*/
const invokeAPI = async (apiMethodName, methodArgs) => {
const governedNames = await E(governedCF).getGovernedApiNames();
Expand Down

0 comments on commit eef22a6

Please sign in to comment.