Skip to content

Commit

Permalink
refactor: dont depend on patch for marshal
Browse files Browse the repository at this point in the history
  • Loading branch information
samsiegart committed Aug 15, 2023
1 parent 3a8b5cc commit 076a3e9
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 227 deletions.
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"type": "module",
"packageManager": "yarn@1.22.19",
"scripts": {
"postinstall": "patch-package",
"format": "yarn prettier --write packages",
"lint": "yarn workspaces run lint",
"test": "yarn workspaces run test"
Expand Down Expand Up @@ -38,8 +37,6 @@
"eslint-plugin-react-hooks": "^4.6.0",
"lerna": "^7.0.1",
"npm-run-all": "^4.1.5",
"patch-package": "^8.0.0",
"postinstall-postinstall": "^2.1.0",
"prettier": "^2.8.8",
"rollup": "^2.0.0"
},
Expand Down
6 changes: 4 additions & 2 deletions packages/rpc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
"vite-tsconfig-paths": "^4.2.0"
},
"devDependencies": {
"@agoric/smart-wallet": "0.5.3",
"ses":"0.18.7",
"@endo/eventual-send": "0.17.5",
"@endo/marshal": "0.8.8",
"@typescript-eslint/eslint-plugin": "^5.35.1",
"@typescript-eslint/parser": "^5.35.1",
"@vitest/coverage-c8": "^0.25.3",
Expand All @@ -30,6 +32,6 @@
"postinstall-postinstall": "^2.1.0",
"prettier": "^2.7.1",
"typescript": "^5.1.3",
"vitest": "^0.32.0"
"vitest": "0.34.1"
}
}
4 changes: 2 additions & 2 deletions packages/rpc/src/chainStorageWatcher.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-use-before-define */
/* eslint-disable import/extensions */
/* eslint-disable import/no-extraneous-dependencies */
import { makeImportContext } from '@agoric/smart-wallet/src/marshal-contexts';
import { makeMarshal } from 'marshal';
import { AgoricChainStoragePathKind } from './types';
import { batchVstorageQuery, keyToPath, pathToKey } from './batchQuery';
import type { UpdateHandler } from './types';
Expand Down Expand Up @@ -52,7 +52,7 @@ export const makeAgoricChainStorageWatcher = (
rpcAddr: string,
chainId: string,
onError?: (e: Error) => void,
marshal = makeImportContext().fromBoard,
marshal = makeMarshal(),
newPathQueryDelayMs = defaults.newPathQueryDelayMs,
refreshLowerBoundMs = defaults.refreshLowerBoundMs,
refreshUpperBoundMs = defaults.refreshUpperBoundMs,
Expand Down
40 changes: 40 additions & 0 deletions packages/rpc/src/marshal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* eslint-disable import/no-extraneous-dependencies */
import { Far, makeMarshal as endoMakeMarshal } from '@endo/marshal';

const makeTranslationTable = (
makeSlot: (val: unknown, size: number) => unknown,
makeVal: (slot: unknown, iface: string | undefined) => unknown,
) => {
const valToSlot = new Map();
const slotToVal = new Map();

const convertValToSlot = (val: unknown) => {
if (valToSlot.has(val)) return valToSlot.get(val);
const slot = makeSlot(val, valToSlot.size);
valToSlot.set(val, slot);
slotToVal.set(slot, val);
return slot;
};

const convertSlotToVal = (slot: unknown, iface: string | undefined) => {
if (slotToVal.has(slot)) return slotToVal.get(slot);
const val = makeVal(slot, iface);
valToSlot.set(val, slot);
slotToVal.set(slot, val);
return val;
};

return harden({ convertValToSlot, convertSlotToVal });
};

const synthesizeRemotable = (_slot: unknown, iface: string | undefined) =>
Far((iface ?? '').replace(/^Alleged: /, ''), {});

const { convertValToSlot, convertSlotToVal } = makeTranslationTable(slot => {
throw new Error(`unknown id: ${slot}`);
}, synthesizeRemotable);

export const makeMarshal = () =>
endoMakeMarshal(convertValToSlot, convertSlotToVal, {
serializeBodyFormat: 'smallcaps',
});
8 changes: 2 additions & 6 deletions packages/rpc/test/chainStorageWatcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ import { expect, it, describe, beforeEach, vi, afterEach } from 'vitest';
import { makeAgoricChainStorageWatcher } from '../src/chainStorageWatcher';
import { AgoricChainStoragePathKind } from '../src/types';

vi.mock('@agoric/smart-wallet/src/marshal-contexts', () => ({
makeImportContext: () => {},
}));

const fetch = vi.fn();
global.fetch = fetch;
global.harden = val => val;
Expand All @@ -19,6 +15,8 @@ const unmarshal = (val: unknown) => val;

let watcher: ReturnType<typeof makeAgoricChainStorageWatcher>;

vi.mock('../src/marshal', () => ({ makeMarshal: () => {} }));

describe('makeAgoricChainStorageWatcher', () => {
beforeEach(() => {
watcher = makeAgoricChainStorageWatcher(
Expand All @@ -27,10 +25,8 @@ describe('makeAgoricChainStorageWatcher', () => {
undefined,
{
fromCapData: unmarshal,
// @ts-expect-error mock
toCapData: marshal,
unserialize: unmarshal,
// @ts-expect-error mock
serialize: marshal,
},
);
Expand Down
13 changes: 0 additions & 13 deletions patches/@agoric+smart-wallet+0.5.3.patch

This file was deleted.

0 comments on commit 076a3e9

Please sign in to comment.