Skip to content

Commit

Permalink
refactor: make resolution descriptions uniform everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
FUDCo committed Feb 17, 2021
1 parent 3da2426 commit 4fa4daf
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 95 deletions.
4 changes: 2 additions & 2 deletions packages/SwingSet/src/kernel/liveSlots.js
Original file line number Diff line number Diff line change
Expand Up @@ -561,8 +561,8 @@ function build(syscall, forVatID, cacheSize, vatPowers, vatParameters) {
assert(didRoot);
beginCollectingPromiseImports();
for (const resolution of resolutions) {
const [vpid, vp] = resolution;
notifyOnePromise(vpid, vp.rejected, vp.data);
const [vpid, rejected, data] = resolution;
notifyOnePromise(vpid, rejected, data);
}
for (const resolution of resolutions) {
const [vpid] = resolution;
Expand Down
14 changes: 7 additions & 7 deletions packages/SwingSet/src/kernel/vatTranslator.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ function makeTranslateKernelDeliveryToVatDelivery(vatID, kernelKeeper) {

function translatePromiseDescriptor(kp) {
if (kp.state === 'fulfilled' || kp.state === 'rejected') {
return {
rejected: kp.state === 'rejected',
data: {
return [
kp.state === 'rejected',
{
...kp.data,
slots: kp.data.slots.map(slot => mapKernelSlotToVatSlot(slot)),
},
};
];
} else if (kp.state === 'redirected') {
// TODO unimplemented
throw new Error('not implemented yet');
Expand All @@ -75,9 +75,9 @@ function makeTranslateKernelDeliveryToVatDelivery(vatID, kernelKeeper) {
const [kpid, p] = resolution;
assert(p.state !== 'unresolved', X`spurious notification ${kpid}`);
const vpid = mapKernelSlotToVatSlot(kpid);
const vres = translatePromiseDescriptor(p);
vResolutions.push([vpid, vres]);
kdebug(`notify ${idx} ${kpid}/${vpid} ${JSON.stringify(vres)}`);
const vres = [vpid, ...translatePromiseDescriptor(p)];
vResolutions.push(vres);
kdebug(`notify ${idx} ${kpid} ${JSON.stringify(vres)}`);
idx += 1;
}
const vatDelivery = harden(['notify', vResolutions]);
Expand Down
6 changes: 4 additions & 2 deletions packages/SwingSet/src/vats/comms/clist-kernel.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,12 @@ export function makeKernel(state, syscall, stateKit) {
// the resolution and then immediately retire the vpid again.

const fresh = allocateResolvedPromiseID();
// console.log(`fresh: ${fresh} for ${vpid}`, p.resolution);
// console.log(`fresh: ${fresh} for ${vpid}`, p.rejected, p.data);
// we must tell the kernel about the resolution *after* the message
// which introduces it
Promise.resolve().then(() => resolveToKernel([[fresh, p.resolution]]));
Promise.resolve().then(() =>
resolveToKernel([[fresh, p.rejected, p.data]]),
);
return fresh;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/SwingSet/src/vats/comms/clist-outbound.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export function makeOutbound(state, stateKit) {
// we must send the resolution *after* the message which introduces it
const { remoteID } = remote;
Promise.resolve().then(() =>
resolveToRemote(remoteID, [[vpid, p.resolution]]),
resolveToRemote(remoteID, [[vpid, p.rejected, p.data]]),
);
} else {
// or arrange to send it later, once it resolves
Expand Down
44 changes: 18 additions & 26 deletions packages/SwingSet/src/vats/comms/delivery.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,13 @@ export function makeDeliveryKit(state, syscall, transmit, clistKit, stateKit) {
handleSend(localDelivery);
}

function mapResolutionFromKernel(resolution, doNotSubscribeSet) {
return harden({
...resolution,
data: mapDataFromKernel(resolution.data, doNotSubscribeSet),
});
}

// dispatch.notify from kernel lands here (local vat resolving some
// Promise, we need to notify remote machines): translate to local, join
// with handleResolutions
function resolveFromKernel(resolutions, doNotSubscribeSet) {
const localResolutions = [];
for (const resolution of resolutions) {
const [vpid, value] = resolution;
const [vpid, rejected, data] = resolution;

// I *think* we should never get here for local promises, since the
// controller only does sendOnly. But if we change that, we need to catch
Expand All @@ -95,7 +88,8 @@ export function makeDeliveryKit(state, syscall, transmit, clistKit, stateKit) {
changeDeciderFromKernelToComms(vpid);
localResolutions.push([
vpid,
mapResolutionFromKernel(value, doNotSubscribeSet),
rejected,
mapDataFromKernel(data, doNotSubscribeSet),
]);
}
handleResolutions(localResolutions);
Expand Down Expand Up @@ -170,7 +164,7 @@ export function makeDeliveryKit(state, syscall, transmit, clistKit, stateKit) {
const body = submsg.slice(sci + 1);
const data = harden({ body, slots });
changeDeciderFromRemoteToComms(vpid, remoteID);
resolutions.push([vpid, { rejected, data }]);
resolutions.push([vpid, rejected, data]);
}
handleResolutions(harden(resolutions));
}
Expand Down Expand Up @@ -221,10 +215,10 @@ export function makeDeliveryKit(state, syscall, transmit, clistKit, stateKit) {
assert(p);

if (p.resolved) {
if (p.resolution.rejected) {
return { reject: p.resolution.data };
if (p.rejected) {
return { reject: p.data };
}
const targetPresence = extractPresenceIfPresent(p.resolution.data);
const targetPresence = extractPresenceIfPresent(p.data);
if (targetPresence) {
return resolveTarget(targetPresence, method);
} else {
Expand Down Expand Up @@ -260,9 +254,7 @@ export function makeDeliveryKit(state, syscall, transmit, clistKit, stateKit) {
if (!localDelivery.result) {
return; // sendOnly, nowhere to send the rejection
}
const resolutions = harden([
[localDelivery.result, { rejected: true, data: where.reject }],
]);
const resolutions = harden([[localDelivery.result, true, where.reject]]);
handleResolutions(resolutions);
return;
}
Expand Down Expand Up @@ -314,16 +306,16 @@ export function makeDeliveryKit(state, syscall, transmit, clistKit, stateKit) {
primaryVpid,
);
for (const resolution of resolutions) {
const [vpid, value] = resolution;
// value: { rejected: boolean, data: capdata }
insistCapData(value.data);
const [vpid, rejected, data] = resolution;
// rejected: boolean, data: capdata
insistCapData(data);
insistVatType('promise', vpid);
insistPromiseIsUnresolved(vpid);
insistDeciderIsComms(vpid);

// mark it as resolved in the promise table, so later messages to it will
// be handled properly
markPromiseAsResolved(vpid, value);
markPromiseAsResolved(vpid, rejected, data);
}

// what remotes need to know?
Expand All @@ -350,33 +342,33 @@ export function makeDeliveryKit(state, syscall, transmit, clistKit, stateKit) {
function resolveToRemote(remoteID, resolutions) {
const msgs = [];
for (const resolution of resolutions) {
const [vpid, value] = resolution;
const [vpid, rejected, data] = resolution;

const rpid = getRemoteForLocal(remoteID, vpid);
// rpid should be rp+NN
insistRemoteType('promise', rpid);
// assert(parseRemoteSlot(rpid).allocatedByRecipient, rpid); // rp+NN for them
function mapSlots() {
const { slots } = value.data;
const { slots } = data;
let ss = slots.map(s => provideRemoteForLocal(remoteID, s)).join(':');
if (ss) {
ss = `:${ss}`;
}
return ss;
}

const rejected = value.rejected ? 'reject' : 'fulfill';
const rejectedTag = rejected ? 'reject' : 'fulfill';
// prettier-ignore
msgs.push(`resolve:${rejected}:${rpid}${mapSlots()};${value.data.body}`);
msgs.push(`resolve:${rejectedTag}:${rpid}${mapSlots()};${data.body}`);
}
transmit(remoteID, msgs.join('\n'));
}

function resolveToKernel(localResolutions) {
const resolutions = [];
for (const localResolution of localResolutions) {
const [vpid, value] = localResolution;
resolutions.push([vpid, value.rejected, mapDataToKernel(value.data)]);
const [vpid, rejected, data] = localResolution;
resolutions.push([vpid, rejected, mapDataToKernel(data)]);
}
syscall.resolve(resolutions);
}
Expand Down
7 changes: 3 additions & 4 deletions packages/SwingSet/src/vats/comms/dispatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,10 @@ export function buildCommsDispatch(syscall) {
function notify(resolutions) {
const willBeResolved = new Set();
for (const resolution of resolutions) {
const [vpid, value] = resolution;
const [vpid, _rejected, data] = resolution;
willBeResolved.add(vpid);
assert(typeof value === 'object');
insistCapData(value.data);
// console.debug(`comms.notify(${vpid}, ${value})`);
insistCapData(data);
// console.debug(`comms.notify(${vpid}, ${rejected}, ${data})`);
// dumpState(state);
}
resolveFromKernel(resolutions, willBeResolved);
Expand Down
12 changes: 9 additions & 3 deletions packages/SwingSet/src/vats/comms/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,14 +238,20 @@ export function makeStateKit(state) {
assert(!p.resolved, X`${vpid} was already resolved`);
}

function markPromiseAsResolved(vpid, resolution) {
function markPromiseAsResolved(vpid, rejected, data) {
const p = state.promiseTable.get(vpid);
assert(p, X`unknown ${vpid}`);
assert(!p.resolved);
insistCapData(resolution.data);
assert.typeof(
rejected,
'boolean',
X`non-boolean "rejected" flag: ${rejected}`,
);
insistCapData(data);
p.resolved = true;
p.kernelAwaitingResolve = true;
p.resolution = resolution;
p.rejected = rejected;
p.data = data;
p.decider = undefined;
p.subscribers = undefined;
p.kernelIsSubscribed = undefined;
Expand Down
2 changes: 1 addition & 1 deletion packages/SwingSet/test/test-kernel.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function capargs(args, slots = []) {
const slot0arg = { '@qclass': 'slot', index: 0 };

function oneResolution(promiseID, rejected, data) {
return [[promiseID, { rejected, data }]];
return [[promiseID, rejected, data]];
}

function checkPromises(t, kernel, expected) {
Expand Down
2 changes: 1 addition & 1 deletion packages/SwingSet/test/test-liveslots.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function capargs(args, slots = []) {
}

function oneResolution(promiseID, rejected, data) {
return [[promiseID, { rejected, data }]];
return [[promiseID, rejected, data]];
}

function buildSyscall() {
Expand Down
64 changes: 17 additions & 47 deletions packages/SwingSet/test/test-vpid-kernel.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function capargs(args, slots = []) {
}

function oneResolution(promiseID, rejected, data) {
return [[promiseID, { rejected, data }]];
return [[promiseID, rejected, data]];
}

function makeConsole(tag) {
Expand Down Expand Up @@ -743,25 +743,15 @@ test(`kernel vpid handling crossing resolutions`, async t => {
await kernel.run();
t.deepEqual(logX.shift(), {
type: 'notify',
resolutions: [
[
exportedUseResultAvatX,
{
rejected: false,
data: capargs(undefinedArg, []),
},
],
],
resolutions: [[exportedUseResultAvatX, false, capargs(undefinedArg, [])]],
});
t.deepEqual(logX.shift(), {
type: 'notify',
resolutions: [
[
exportedGenResultAvatX,
{
rejected: false,
data: capargs([slot0arg], [exportedGenResultBvatX]),
},
false,
capargs([slot0arg], [exportedGenResultBvatX]),
],
],
});
Expand Down Expand Up @@ -795,32 +785,20 @@ test(`kernel vpid handling crossing resolutions`, async t => {
await kernel.run();
t.deepEqual(logX.shift(), {
type: 'notify',
resolutions: [
[
exportedUseResultBvatX,
{
rejected: false,
data: capargs(undefinedArg, []),
},
],
],
resolutions: [[exportedUseResultBvatX, false, capargs(undefinedArg, [])]],
});
t.deepEqual(logX.shift(), {
type: 'notify',
resolutions: [
[
exportedGenResultBvatX,
{
rejected: false,
data: capargs([slot0arg], [importedGenResultAvatX]),
},
false,
capargs([slot0arg], [importedGenResultAvatX]),
],
[
importedGenResultAvatX,
{
rejected: false,
data: capargs([slot0arg], [exportedGenResultBvatX]),
},
false,
capargs([slot0arg], [exportedGenResultBvatX]),
],
],
});
Expand All @@ -830,17 +808,13 @@ test(`kernel vpid handling crossing resolutions`, async t => {
resolutions: [
[
importedGenResultAvatB,
{
rejected: false,
data: capargs([slot0arg], [importedGenResultB2vatB]),
},
false,
capargs([slot0arg], [importedGenResultB2vatB]),
],
[
importedGenResultB2vatB,
{
rejected: false,
data: capargs([slot0arg], [importedGenResultAvatB]),
},
false,
capargs([slot0arg], [importedGenResultAvatB]),
],
],
});
Expand All @@ -850,17 +824,13 @@ test(`kernel vpid handling crossing resolutions`, async t => {
resolutions: [
[
importedGenResultBvatA,
{
rejected: false,
data: capargs([slot0arg], [importedGenResultA2vatA]),
},
false,
capargs([slot0arg], [importedGenResultA2vatA]),
],
[
importedGenResultA2vatA,
{
rejected: false,
data: capargs([slot0arg], [importedGenResultBvatA]),
},
false,
capargs([slot0arg], [importedGenResultBvatA]),
],
],
});
Expand Down
2 changes: 1 addition & 1 deletion packages/SwingSet/test/test-vpid-liveslots.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function capargs(args, slots = []) {
}

function oneResolution(promiseID, rejected, data) {
return [[promiseID, { rejected, data }]];
return [[promiseID, rejected, data]];
}

function buildSyscall() {
Expand Down

0 comments on commit 4fa4daf

Please sign in to comment.