From 9e3493ad4d8c0a6a9230ad6a4c22a3254a867115 Mon Sep 17 00:00:00 2001 From: Michael FIG Date: Fri, 9 Oct 2020 15:42:12 -0600 Subject: [PATCH] fix: update @agoric/store types and imports --- .../src/vats/network/internal-types.js | 5 - packages/SwingSet/src/vats/network/network.js | 1 + packages/SwingSet/src/vats/network/router.js | 1 + packages/SwingSet/src/vats/plugin-manager.js | 4 +- packages/SwingSet/test/test-external-store.js | 203 ------------------ .../lib/ag-solo/vats/bridge.js | 3 +- .../cosmic-swingset/lib/ag-solo/vats/ibc.js | 6 +- packages/store/exported.js | 1 + packages/store/src/external/hydrate.js | 2 +- packages/store/src/external/memory.js | 2 +- packages/store/src/types.js | 12 +- packages/zoe/exported.js | 1 + packages/zoe/src/internal-types.js | 10 - packages/zoe/src/zoeService/zoe.js | 1 + 14 files changed, 19 insertions(+), 233 deletions(-) delete mode 100644 packages/SwingSet/test/test-external-store.js create mode 100644 packages/store/exported.js diff --git a/packages/SwingSet/src/vats/network/internal-types.js b/packages/SwingSet/src/vats/network/internal-types.js index a3093a409a1..d232bfa4532 100644 --- a/packages/SwingSet/src/vats/network/internal-types.js +++ b/packages/SwingSet/src/vats/network/internal-types.js @@ -1,8 +1,3 @@ -/** - * @template K,V - * @typedef {import('@agoric/store').Store} Store - */ - /** * @template T * @typedef {import('@agoric/promise-kit').PromiseRecord} PromiseRecord diff --git a/packages/SwingSet/src/vats/network/network.js b/packages/SwingSet/src/vats/network/network.js index 48a7da30c2a..c8bfc4f7071 100644 --- a/packages/SwingSet/src/vats/network/network.js +++ b/packages/SwingSet/src/vats/network/network.js @@ -5,6 +5,7 @@ import { E } from '@agoric/eventual-send'; import { makePromiseKit } from '@agoric/promise-kit'; import { toBytes } from './bytes'; +import '@agoric/store/exported'; import './types'; import './internal-types'; diff --git a/packages/SwingSet/src/vats/network/router.js b/packages/SwingSet/src/vats/network/router.js index 45e7684d1de..2f428329742 100644 --- a/packages/SwingSet/src/vats/network/router.js +++ b/packages/SwingSet/src/vats/network/router.js @@ -4,6 +4,7 @@ import { E as defaultE } from '@agoric/eventual-send'; import makeStore from '@agoric/store'; import { makeNetworkProtocol, ENDPOINT_SEPARATOR } from './network'; +import '@agoric/store/exported'; import './types'; import './internal-types'; diff --git a/packages/SwingSet/src/vats/plugin-manager.js b/packages/SwingSet/src/vats/plugin-manager.js index 0bf432ebb09..262baddf49c 100644 --- a/packages/SwingSet/src/vats/plugin-manager.js +++ b/packages/SwingSet/src/vats/plugin-manager.js @@ -5,6 +5,8 @@ import { makeCapTP } from '@agoric/captp'; import { makePromiseKit } from '@agoric/promise-kit'; import { E, HandledPromise } from '@agoric/eventual-send'; +import '@agoric/store/exported'; + /** * @template T * @typedef {T | PromiseLike} ERef @@ -70,7 +72,7 @@ export function makePluginManager(pluginDevice, { D, ...vatPowers }) { */ /** - * @type {import('@agoric/store').Store} + * @type {Store} */ const modConnection = makeStore('moduleIndex'); diff --git a/packages/SwingSet/test/test-external-store.js b/packages/SwingSet/test/test-external-store.js deleted file mode 100644 index fb7e0babfa0..00000000000 --- a/packages/SwingSet/test/test-external-store.js +++ /dev/null @@ -1,203 +0,0 @@ -// @ts-check -/* eslint-disable no-use-before-define */ -import '@agoric/install-ses'; -import test from 'ava'; -import makeStore from '@agoric/store'; - -const moduleLevel = 'module-level'; - -const runTests = (t, mf) => { - const h = mf('Hello'); - t.deepEqual(h.getCount(), { invocationCount: 26, moduleLevel }); - t.is(h.hello('World'), `Hello, World!`); - t.deepEqual(h.getCount(), { invocationCount: 27, moduleLevel }); - t.is(h.hello('second'), `Hello, second!`); - t.deepEqual(h.getCount(), { invocationCount: 28, moduleLevel }); - return h; -}; - -test('original sources', t => { - // This is the original source code. - const { make: makeFoo } = makeCollection((msg = 'Hello') => { - let startCount = 24; - startCount += 1; - let invocationCount = startCount; - const obj = { - hello(nick) { - invocationCount += 1; - return `${msg}, ${nick}!`; - }, - getCount() { - return { moduleLevel, invocationCount }; - }, - }; - obj.hello('init'); - return obj; - }); - - runTests(t, makeFoo); -}); - -test('rewritten code', t => { - /** - * This is the rewritten source code, line numbers can be preserved. - * - * The return value drives the analysis of which expressions need to be - * evaluated to resurrect the object. Those expressions determine which - * variables need to be captured by $hdata to write to and read from the store. - * - * Side-effecting expressions are rewritten to be conditional on whether the - * initialisation step is taking place. This is done on a per-expression - * basis, since it is known any variables that are changed by the side-effect - * are either already captured by $hdata, or aren't needed by the return - * value. - * - * Declarations are not considered side-effects. - */ - const store = makeSwingSetCollection( - (msg = 'Hello') => ({ msg }), - $hinit => $hdata => { - let startCount = $hinit && 24; - $hinit && (startCount += 1); - $hinit && ($hdata.invocationCount = startCount); - const obj = { - hello(nick) { - $hdata.invocationCount += 1; - return `${$hdata.msg}, ${nick}!`; - }, - getCount() { - return { moduleLevel, invocationCount: $hdata.invocationCount }; - }, - }; - $hinit && obj.hello('init'); - return obj; - }, - ); - - const h = runTests(t, store.make); - const key = store.getKey(h); - t.is(key, '1'); - const h2 = store.load(key); - - // We get a different representative, which shares the key. - t.not(h2, h); - t.is(store.getKey(h2), `1`); - - // The methods are there now, too. - const last = h.getCount(); - t.deepEqual(h2.getCount(), last); - h2.hello('restored'); - - // Note that the explicitly-loaded object state evolves independently. - const next = h2.getCount(); - t.deepEqual(next, { - ...last, - invocationCount: last.invocationCount + 1, - }); - t.deepEqual(h.getCount(), last); -}); - -/** The rest of this file is some implementation. */ - -/** - * @typedef {(...args: any[]) => Record} AdaptArguments - * @typedef {(data: Record) => any} Hydrate - * @typedef {(init: boolean) => Hydrate} MakeHydrate when init is falsy, prevent - */ - -/** - * @typedef {Object} StringStore - * @property {(key: string, value: string) => void} set - * @property {(key: string, value: string) => void} init - * @property {(key: string) => string} get - * - * @typedef {Object} Collection - * @property {(...args: Array) => any} make - * @property {(value: any) => string} getKey - * @property {(key: string) => any} load - */ - -/** - * @param {(...args: Array) => any} maker - * @returns {Collection} - */ -const makeCollection = maker => { - // The default store has no query ability. - return harden({ - make(...args) { - return maker(...args); - }, - load(_key) { - throw Error('unimplemented'); - }, - getKey(_obj) { - throw Error('unimplemented'); - }, - }); -}; - -/** - * @param {AdaptArguments} adaptArguments - * @param {MakeHydrate} makeHydrate - * @returns {Collection} - */ -function makeSwingSetCollection(adaptArguments, makeHydrate) { - const serialize = JSON.stringify; - const unserialize = JSON.parse; - - /** @type {WeakMap} */ - const valueToKey = new WeakMap(); - let lastEntryKey = 0; - /** @type {import('@agoric/store').Store} */ - const store = makeStore('entryKey'); - - /** - * Create a data object that queues writes to the store. - * - * @param {string} key - * @param {Record} data - */ - const makeActiveData = (key, data) => { - const write = () => store.set(key, serialize(data)); - const activeData = {}; - for (const prop of Object.getOwnPropertyNames(data)) { - Object.defineProperty(activeData, prop, { - get: () => data[prop], - set: value => { - data[prop] = value; - write(); - }, - }); - } - return harden(activeData); - }; - - const initHydrate = makeHydrate(true); - const hydrate = makeHydrate(false); - const estore = { - make(...args) { - const data = adaptArguments(...args); - // Create a new object with the above guts. - lastEntryKey += 1; - const key = `${lastEntryKey}`; - initHydrate(data); - - // We store and reload it to sanity-check the initial state and also to - // ensure that the new object has active data. - store.init(key, serialize(data)); - return estore.load(key); - }, - getKey(value) { - return valueToKey.get(value); - }, - load(key) { - // FIXME: Maybe try to handle returning a single identity. - const data = unserialize(store.get(key)); - const activeData = makeActiveData(key, data); - const obj = hydrate(activeData); - valueToKey.set(obj, key); - return obj; - }, - }; - return estore; -} diff --git a/packages/cosmic-swingset/lib/ag-solo/vats/bridge.js b/packages/cosmic-swingset/lib/ag-solo/vats/bridge.js index f8598862fa1..97c06675a56 100644 --- a/packages/cosmic-swingset/lib/ag-solo/vats/bridge.js +++ b/packages/cosmic-swingset/lib/ag-solo/vats/bridge.js @@ -1,6 +1,7 @@ // @ts-check import makeStore from '@agoric/store'; +import '@agoric/store/exported'; /** * @template T @@ -36,7 +37,7 @@ import makeStore from '@agoric/store'; /* eslint-enable jsdoc/valid-types */ export function makeBridgeManager(E, D, bridgeDevice) { /** - * @type {import('@agoric/store').Store} + * @type {Store} */ const srcHandlers = makeStore('srcID'); diff --git a/packages/cosmic-swingset/lib/ag-solo/vats/ibc.js b/packages/cosmic-swingset/lib/ag-solo/vats/ibc.js index ff81f140dd1..aea8f996a2c 100644 --- a/packages/cosmic-swingset/lib/ag-solo/vats/ibc.js +++ b/packages/cosmic-swingset/lib/ag-solo/vats/ibc.js @@ -9,6 +9,7 @@ import makeStore from '@agoric/store'; import { makePromiseKit } from '@agoric/promise-kit'; import { generateSparseInts } from '@agoric/sparse-ints'; +import '@agoric/store/exported'; import '@agoric/swingset-vat/src/vats/network/types'; import { makeWithQueue } from './queue'; @@ -31,11 +32,6 @@ const FIXME_ALLOW_NAIVE_RELAYS = true; * @typedef {import('@agoric/promise-kit').PromiseRecord} PromiseRecord */ -/** - * @template K,V - * @typedef {import('@agoric/store').Store} Store - */ - /** * @typedef {string} IBCPortID * @typedef {string} IBCChannelID diff --git a/packages/store/exported.js b/packages/store/exported.js new file mode 100644 index 00000000000..bc6da56dd1e --- /dev/null +++ b/packages/store/exported.js @@ -0,0 +1 @@ +import './src/types'; diff --git a/packages/store/src/external/hydrate.js b/packages/store/src/external/hydrate.js index 96b25af3982..9db9c9e1718 100644 --- a/packages/store/src/external/hydrate.js +++ b/packages/store/src/external/hydrate.js @@ -16,7 +16,7 @@ import { makeStore } from '../store'; * the Closure interface that the rewriter targets. * * @template {Array} A - * @template {Instance} T + * @template {ExternalInstance} T * @param {MakeBackingStore} makeBackingStore * @returns {MakeHydrateExternalStore} */ diff --git a/packages/store/src/external/memory.js b/packages/store/src/external/memory.js index 722b876cb73..977c8b07ef6 100644 --- a/packages/store/src/external/memory.js +++ b/packages/store/src/external/memory.js @@ -9,7 +9,7 @@ import '../types'; * garbage-collected in the usual way, but it will not page out any objects to * secondary storage. * - * @template {(...args: any[]) => Instance} M + * @template {(...args: any[]) => ExternalInstance} M * @param {string} instanceName * @param {M} maker * @returns {ExternalStore} diff --git a/packages/store/src/types.js b/packages/store/src/types.js index 9070b7d7230..1bf8c12dadc 100644 --- a/packages/store/src/types.js +++ b/packages/store/src/types.js @@ -1,5 +1,5 @@ /** - * @typedef {Record} Instance + * @typedef {Record} ExternalInstance */ /** @@ -42,7 +42,7 @@ /** * An external store for a given constructor. * - * @template {(...args: Array) => Instance} C + * @template {(...args: Array) => ExternalInstance} C * @typedef {Object} ExternalStore * @property {C} makeInstance * @property {MakeWeakStore, any>} makeWeakStore @@ -64,7 +64,7 @@ * "representative" instance. * * @template {Array} A - * @template {Instance} T + * @template {ExternalInstance} T * @callback MakeHydrateExternalStore * @param {string} instanceKind * @param {(...args: A) => HydrateData} adaptArguments @@ -73,11 +73,11 @@ */ /** - * @typedef {Store & { makeWeakStore: () => WeakStore }}} InstanceStore + * @typedef {Store & { makeWeakStore: () => WeakStore }}} ExternalInstanceStore */ /** * @typedef {Object} BackingStore - * @property {(storeId: string, instanceKind: string) => InstanceStore} makeStore - * @property {(storeId: string) => InstanceStore} findStore + * @property {(storeId: string, instanceKind: string) => ExternalInstanceStore} makeStore + * @property {(storeId: string) => ExternalInstanceStore} findStore */ diff --git a/packages/zoe/exported.js b/packages/zoe/exported.js index eba7acedb68..a07c1a3bda2 100644 --- a/packages/zoe/exported.js +++ b/packages/zoe/exported.js @@ -3,3 +3,4 @@ import './src/zoeService/types'; import './src/contractSupport/types'; import './src/types'; import '@agoric/ertp/exported'; +import '@agoric/store/exported'; diff --git a/packages/zoe/src/internal-types.js b/packages/zoe/src/internal-types.js index b8dc5102683..dee89ac5887 100644 --- a/packages/zoe/src/internal-types.js +++ b/packages/zoe/src/internal-types.js @@ -3,16 +3,6 @@ * @typedef {import('@agoric/promise-kit').ERef} ERef */ -/** - * @template K,V - * @typedef {import('@agoric/store').Store} Store - */ - -/** - * @template K,V - * @typedef {import('@agoric/weak-store').WeakStore} WeakStore - */ - /** * @template T * @typedef {import('@agoric/promise-kit').PromiseRecord} PromiseRecord diff --git a/packages/zoe/src/zoeService/zoe.js b/packages/zoe/src/zoeService/zoe.js index 4e989f320ae..b2561e0b8b8 100644 --- a/packages/zoe/src/zoeService/zoe.js +++ b/packages/zoe/src/zoeService/zoe.js @@ -8,6 +8,7 @@ import { makePromiseKit } from '@agoric/promise-kit'; * Zoe uses ERTP, the Electronic Rights Transfer Protocol */ import '@agoric/ertp/exported'; +import '@agoric/store/exported'; import { makeIssuerKit, MathKind } from '@agoric/ertp'; import '../../exported';