Skip to content

Commit

Permalink
feat(ertp,kernel): use pattern-based compression
Browse files Browse the repository at this point in the history
  • Loading branch information
erights committed Sep 7, 2024
1 parent 67275c2 commit dac921d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 18 deletions.
1 change: 1 addition & 0 deletions packages/ERTP/src/paymentLedger.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const amountShapeFromElementShape = (brand, assetKind, elementShape) => {
if (elementShape === undefined) {
valueShape = M.arrayOf(M.key());
} else {
// M.and compresses only according to its last conjunct
valueShape = M.arrayOf(M.and(M.key(), elementShape));
}
break;
Expand Down
21 changes: 14 additions & 7 deletions packages/swingset-liveslots/src/collectionManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import {
getRankCover,
getCopyMapEntries,
getCopySetKeys,
mustCompress,
mustDecompress,
} from '@endo/patterns';
import { isCopyMap, isCopySet } from '@agoric/store';
import { makeBaseRef, parseVatSlot } from './parseVatSlots.js';
Expand Down Expand Up @@ -279,19 +281,24 @@ export function makeCollectionManager(

const serializeValue = value => {
const { valueShape, label } = getSchema();
if (valueShape !== undefined) {
mustMatch(value, valueShape, makeInvalidValueTypeMsg(label));
if (valueShape === undefined) {
return serialize(value);
}
return serialize(value);
return serialize(
mustCompress(value, valueShape, makeInvalidValueTypeMsg(label)),
);
};

const unserializeValue = data => {
const { valueShape, label } = getSchema();
const value = unserialize(data);
if (valueShape !== undefined) {
mustMatch(value, valueShape, makeInvalidValueTypeMsg(label));
if (valueShape === undefined) {
return unserialize(data);
}
return value;
return mustDecompress(
unserialize(data),
valueShape,
makeInvalidValueTypeMsg(label),
);
};

function prefix(dbEntryKey) {
Expand Down
35 changes: 24 additions & 11 deletions packages/swingset-liveslots/src/virtualObjectManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import { environmentOptionsListHas } from '@endo/env-options';
import { assert, Fail, q, b } from '@endo/errors';
import { assertPattern, mustMatch } from '@agoric/store';
import { assertPattern, mustCompress, mustDecompress } from '@endo/patterns';
import { defendPrototype, defendPrototypeKit } from '@endo/exo/tools.js';
import { Far, passStyleOf } from '@endo/marshal';
import { Nat } from '@endo/nat';
Expand Down Expand Up @@ -816,18 +816,30 @@ export const makeVirtualObjectManager = (

/** @type {(prop: string) => void} */
let checkStateProperty = _prop => {};
/** @type {(value: any, prop: string) => void} */
let checkStatePropertyValue = (_value, _prop) => {};
let serializeStatePropertyValue = (value, _prop, _label) => {
return serialize(value);
};
let unserializeStatePropertyValue = (data, _prop, _label) => {
return harden(unserialize(data));
};
if (stateShape) {
checkStateProperty = prop => {
hasOwn(stateShape, prop) ||
Fail`State must only have fields described by stateShape: ${q(
ownKeys(stateShape),
)}`;
};
checkStatePropertyValue = (value, prop) => {
serializeStatePropertyValue = (value, prop, label) => {
checkStateProperty(prop);
mustMatch(value, stateShape[prop]);
return serialize(mustCompress(value, stateShape[prop], label));
};
unserializeStatePropertyValue = (data, prop, label) => {
checkStateProperty(prop);
return mustDecompress(
harden(unserialize(data)),
stateShape[prop],
label,
);
};
}

Expand Down Expand Up @@ -861,16 +873,18 @@ export const makeVirtualObjectManager = (
assert(record !== undefined);
const { valueMap, capdatas } = record;
if (!valueMap.has(prop)) {
const value = harden(unserialize(capdatas[prop]));
checkStatePropertyValue(value, prop);
const value = unserializeStatePropertyValue(
capdatas[prop],
prop,
prop,
);
valueMap.set(prop, value);
}
return valueMap.get(prop);
},
set(value) {
const baseRef = getBaseRef(this);
checkStatePropertyValue(value, prop);
const capdata = serialize(value);
const capdata = serializeStatePropertyValue(value, prop, prop);
assertAcceptableSyscallCapdataSize([capdata]);
if (isDurable) {
insistDurableCapdata(vrm, prop, capdata, true);
Expand Down Expand Up @@ -1061,8 +1075,7 @@ export const makeVirtualObjectManager = (
const valueMap = new Map();
for (const prop of getOwnPropertyNames(initialData)) {
const value = initialData[prop];
checkStatePropertyValue(value, prop);
const valueCD = serialize(value);
const valueCD = serializeStatePropertyValue(value, prop, prop);
// TODO: we're only checking the size of one property at a
// time, but the real constraint is the vatstoreSet of the
// aggregate record. We should apply this check to the full
Expand Down

0 comments on commit dac921d

Please sign in to comment.