Skip to content

Commit

Permalink
refactor(swingset-vat): move VatData to new vat-data package (#4603)
Browse files Browse the repository at this point in the history
* refactor(swingset-vat): provide an export for VatData

* create vat-data package

* bend less to missing VatData global

* working types

* stop exporting VatData from swingset-vat package so there's one package to get it
  • Loading branch information
turadg authored Mar 23, 2022
1 parent 23bf3c8 commit d5f9cad
Show file tree
Hide file tree
Showing 41 changed files with 224 additions and 137 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test-all-packages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ jobs:
run: cd packages/wallet && yarn test
- name: yarn test (eslint-config)
run: cd packages/eslint-config && yarn test
- name: yarn test (vat-data)
run: cd packages/vat-data && yarn test

# The meta-test!
- name: Check for untested packages
Expand Down
11 changes: 0 additions & 11 deletions packages/ERTP/globals.d.ts

This file was deleted.

1 change: 1 addition & 0 deletions packages/ERTP/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"@agoric/notifier": "^0.3.35",
"@agoric/store": "^0.6.10",
"@agoric/swingset-vat": "^0.25.1",
"@agoric/vat-data": "^0.1.0",
"@endo/marshal": "^0.6.3",
"@endo/promise-kit": "^0.2.37"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/ERTP/src/payment.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @ts-check

import { defineKind } from '@agoric/swingset-vat/src/storeModule.js';
import { defineKind } from '@agoric/vat-data';

/**
* @template {AssetKind} K
Expand Down
2 changes: 1 addition & 1 deletion packages/ERTP/src/paymentLedger.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { E } from '@endo/eventual-send';
import { isPromise } from '@endo/promise-kit';
import { Far, assertCopyArray } from '@endo/marshal';
import { fit } from '@agoric/store';
import { makeScalarBigWeakMapStore } from '@agoric/swingset-vat/src/storeModule.js';
import { makeScalarBigWeakMapStore } from '@agoric/vat-data';
import { AmountMath } from './amountMath.js';
import { makePaymentMaker } from './payment.js';
import { makePurseMaker } from './purse.js';
Expand Down
2 changes: 1 addition & 1 deletion packages/ERTP/src/purse.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { makeNotifierKit } from '@agoric/notifier';
import { defineKind } from '@agoric/swingset-vat/src/storeModule.js';
import { defineKind } from '@agoric/vat-data';
import { AmountMath } from './amountMath.js';

export const makePurseMaker = (allegedName, assetKind, brand, purseMethods) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/SwingSet/docs/virtual-objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ The kernel currently provides two secondary storage mechanisms for the use of (a
- Virtual and durable objects
- Persistent stores

These are accessed via properties of the `VatData` global made available to vat code, or more stylishly by importing from `@agoric/swingset-vat/src/storeModule.js` (that's a working placeholder module which we will be replacing it with a more ergonomic package name once we figure out what that should be).
These are accessed via properties of the `VatData` global made available to vat code, or more stylishly by importing from `@agoric/vat-data` (that's a working placeholder module which we will be replacing it with a more ergonomic package name once we figure out what that should be).

The APIs described here all have to do with storing data on disk. However, you should understand an important distinction made in these APIs between the labels "virtual" and "durable". In our usage here, things that are "virtual" will automatically swap their state to disk storage and thus don't eat up RAM space in the running vat process even if they grow large in number. In contrast, things that are "durable" are not only stored on disk but survive the lifetime of the vat process holding them and may be retrieved later in a future version of the vat.

Expand Down
1 change: 1 addition & 0 deletions packages/SwingSet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"@agoric/store": "^0.6.10",
"@agoric/swing-store": "^0.6.5",
"@agoric/xsnap": "^0.11.2",
"@agoric/vat-data": "^0.1.0",
"@endo/base64": "^0.2.21",
"@endo/bundle-source": "^2.1.1",
"@endo/captp": "^2.0.3",
Expand Down
46 changes: 0 additions & 46 deletions packages/SwingSet/src/storeModule.js

This file was deleted.

3 changes: 1 addition & 2 deletions packages/SwingSet/test/test-vat-env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* global VatData */
import { test } from '../tools/prepare-test-env-ava.js';
import { test, VatData } from '../tools/prepare-test-env-ava.js';

import { provideHostStorage } from '../src/controller/hostStorage.js';
import { buildVatController } from '../src/index.js';
Expand Down
2 changes: 1 addition & 1 deletion packages/SwingSet/test/virtualObjects/vat-orphan-bob.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Far } from '@endo/marshal';
import { defineKind } from '../../src/storeModule.js';
import { defineKind } from '@agoric/vat-data';

export function buildRootObject(vatPowers) {
const { testLog } = vatPowers;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Far } from '@endo/marshal';
import { defineKind } from '../../src/storeModule.js';
import { defineKind } from '@agoric/vat-data';

const makeThing = defineKind(
'thing',
Expand Down
2 changes: 1 addition & 1 deletion packages/SwingSet/test/virtualObjects/vat-vom-gc-bob.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { E } from '@endo/eventual-send';
import { Far } from '@endo/marshal';
import { defineKind } from '../../src/storeModule.js';
import { defineKind } from '@agoric/vat-data';

const things = [];

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Far } from '@endo/marshal';
import { defineKind } from '../../src/storeModule.js';
import { defineKind } from '@agoric/vat-data';

const makeHolder = defineKind(
'holder-vo',
Expand Down
5 changes: 5 additions & 0 deletions packages/SwingSet/tools/prepare-test-env-ava.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@ import rawTest from 'ava';

/** @type {typeof rawTest} */
export const test = wrapTest(rawTest);

// Does not import from a module because we're testing the global env
/* global globalThis */
export const VatData = globalThis.VatData;
assert(VatData);
1 change: 1 addition & 0 deletions packages/agoric-cli/src/sdk-package-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default [
"@agoric/swingset-vat",
"@agoric/telemetry",
"@agoric/ui-components",
"@agoric/vat-data",
"@agoric/vats",
"@agoric/wallet",
"@agoric/wallet-backend",
Expand Down
11 changes: 0 additions & 11 deletions packages/deploy-script-support/globals.d.ts

This file was deleted.

11 changes: 0 additions & 11 deletions packages/pegasus/globals.d.ts

This file was deleted.

11 changes: 0 additions & 11 deletions packages/run-protocol/globals.d.ts

This file was deleted.

1 change: 1 addition & 0 deletions packages/run-protocol/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@agoric/notifier": "^0.3.35",
"@agoric/store": "^0.6.10",
"@agoric/swingset-vat": "^0.25.1",
"@agoric/vat-data": "^0.1.0",
"@agoric/vats": "^0.7.0",
"@agoric/zoe": "^0.21.3",
"@endo/bundle-source": "^2.1.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @ts-check
// XXX avoid deep imports https://github.com/Agoric/agoric-sdk/issues/4255#issuecomment-1032117527
import { makeScalarBigMapStore } from '@agoric/swingset-vat/src/storeModule.js';
import { makeScalarBigMapStore } from '@agoric/vat-data';
import { fromVaultKey, toVaultKey } from './storeUtils.js';

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/run-protocol/src/vaultFactory/vaultManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { makeNotifierKit, observeNotifier } from '@agoric/notifier';
import { AmountMath } from '@agoric/ertp';
import { Far } from '@endo/marshal';

import { makeScalarBigMapStore } from '@agoric/swingset-vat/src/storeModule';
import { makeScalarBigMapStore } from '@agoric/vat-data';
import { makeInnerVault } from './vault.js';
import { makePrioritizedVaults } from './prioritizedVaults.js';
import { liquidate } from './liquidation.js';
Expand Down
2 changes: 1 addition & 1 deletion packages/swingset-runner/demo/vatStore1/vat-bob.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Far } from '@endo/marshal';
import { defineKind } from '@agoric/swingset-vat/src/storeModule.js';
import { defineKind } from '@agoric/vat-data';

const p = console.log;

Expand Down
5 changes: 1 addition & 4 deletions packages/swingset-runner/demo/vatStore2/thingHolder.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { E } from '@endo/eventual-send';
import { Far } from '@endo/marshal';
import {
defineKind,
makeScalarBigWeakMapStore,
} from '@agoric/swingset-vat/src/storeModule.js';
import { defineKind, makeScalarBigWeakMapStore } from '@agoric/vat-data';

const p = console.log;

Expand Down
2 changes: 1 addition & 1 deletion packages/swingset-runner/demo/vatStore3/vat-bob.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { E } from '@endo/eventual-send';
import { Far } from '@endo/marshal';
import { defineKind } from '@agoric/swingset-vat/src/storeModule.js';
import { defineKind } from '@agoric/vat-data';

const things = [];

Expand Down
2 changes: 1 addition & 1 deletion packages/swingset-runner/demo/virtualObjectGC/vat-bob.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { E } from '@endo/eventual-send';
import { Far } from '@endo/marshal';
import { defineKind } from '@agoric/swingset-vat/src/storeModule.js';
import { defineKind } from '@agoric/vat-data';

export function buildRootObject(_vatPowers) {
const makeThing = defineKind(
Expand Down
1 change: 1 addition & 0 deletions packages/swingset-runner/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"@agoric/store": "^0.6.10",
"@agoric/swing-store": "^0.6.5",
"@agoric/swingset-vat": "^0.25.1",
"@agoric/vat-data": "^0.1.0",
"@agoric/zoe": "^0.21.3",
"@endo/bundle-source": "^2.1.1",
"@endo/init": "^0.5.37",
Expand Down
40 changes: 40 additions & 0 deletions packages/vat-data/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "@agoric/vat-data",
"version": "0.1.0",
"description": "Safe access to VatData global",
"type": "module",
"repository": "https://github.com/Agoric/agoric-sdk",
"main": "./src/index.js",
"scripts": {
"build": "exit 0",
"test": "ava",
"test:c8": "exit 0",
"test:xs": "exit 0",
"lint-fix": "yarn lint:eslint --fix",
"lint": "run-s --continue-on-error lint:*",
"lint:eslint": "eslint '**/*.js'",
"lint:types": "tsc"
},
"keywords": [],
"author": "Agoric",
"license": "Apache-2.0",
"dependencies": {
"@agoric/assert": "*",
"@agoric/store": "^0.6.10"
},
"devDependencies": {
"ava": "^3.12.1",
"@agoric/swingset-vat": "*"
},
"eslintConfig": {
"extends": [
"@agoric"
]
},
"publishConfig": {
"access": "public"
},
"engines": {
"node": ">=14.15.0"
}
}
48 changes: 48 additions & 0 deletions packages/vat-data/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* global globalThis */
import { assert } from '@agoric/assert';
import {
M,
makeScalarMapStore,
makeScalarWeakMapStore,
makeScalarSetStore,
makeScalarWeakSetStore,
} from '@agoric/store';

export {
M,
makeScalarMapStore,
makeScalarWeakMapStore,
makeScalarSetStore,
makeScalarWeakSetStore,
};

/** @type {import('./types').VatData} */
let VatDataGlobal;
if ('VatData' in globalThis) {
assert(globalThis.VatData, 'VatData defined in global as null or undefined');
VatDataGlobal = globalThis.VatData;
} else {
// XXX this module has been known to get imported (transitively) in cases that
// never use it so we make a version that will satisfy module resolution but
// fail at runtime.
const unvailable = () => assert.fail('VatData unavailable');
VatDataGlobal = {
defineKind: unvailable,
defineDurableKind: unvailable,
makeKindHandle: unvailable,
makeScalarBigMapStore: unvailable,
makeScalarBigWeakMapStore: unvailable,
makeScalarBigSetStore: unvailable,
makeScalarBigWeakSetStore: unvailable,
};
}

export const {
defineKind,
defineDurableKind,
makeKindHandle,
makeScalarBigMapStore,
makeScalarBigWeakMapStore,
makeScalarBigSetStore,
makeScalarBigWeakSetStore,
} = VatDataGlobal;
24 changes: 24 additions & 0 deletions packages/vat-data/src/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { defineKind } from '.';

export const makePaymentMaker = (allegedName: string, brand: unknown) => {
const makePayment = defineKind(
`${allegedName} payment`,
() => ({}),
// @ts-expect-error "state" type implied by init() doesn't provide "a"
({ a: something }) => ({
getAllegedBrand: () => brand,
}),
);
return makePayment;
};

type FlorgState = { str: string };
const makeFlorg = defineKind(
'florg',
(num: number) => ({ str: String(num), extra: 'extra' }),
({ str }: FlorgState) => str,
);
const f = makeFlorg(42);
f.concat; // string
// @ts-expect-error
makeFlorg('notnumber');
Loading

0 comments on commit d5f9cad

Please sign in to comment.