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 Jun 13, 2024
1 parent 3e597a8 commit 926e938
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 @@ -37,6 +37,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 @@ -17,6 +17,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 @@ -274,19 +276,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 @@ -8,7 +8,7 @@ import {
quote as q,
bare as 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 @@ -821,18 +821,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 @@ -866,16 +878,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 @@ -1066,8 +1080,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 926e938

Please sign in to comment.