Skip to content

Commit

Permalink
fix(slot): prevent crosstalk by tracking inbound vs. outbound slots
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Oct 31, 2019
1 parent 98ea387 commit 19906c4
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/captp.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function makeCapTP(ourId, send, bootstrapObj = undefined) {
if (Promise.resolve(val) === val) {
lastPromiseID += 1;
const promiseID = lastPromiseID;
slot = `p${promiseID}`;
slot = `p+${promiseID}`;
val.then(
res =>
send({
Expand All @@ -52,7 +52,7 @@ export function makeCapTP(ourId, send, bootstrapObj = undefined) {
} else {
lastExportID += 1;
const exportID = lastExportID;
slot = `o${exportID}`;
slot = `o+${exportID}`;
}
valToSlot.set(val, slot);
slotToVal.set(slot, val);
Expand Down Expand Up @@ -114,8 +114,10 @@ export function makeCapTP(ourId, send, bootstrapObj = undefined) {
}

function unserializeSlot(data, slots) {
const slot = slots[Nat(data.index)];
const theirSlot = slots[Nat(data.index)];
let val;
const otherDir = theirSlot[1] === '+' ? '-' : '+';
const slot = `${theirSlot[0]}${otherDir}${theirSlot.slice(2)}`;
if (!slotToVal.has(slot)) {
// Make a new handled promise for the slot.
const pr = makeRemote(slot);
Expand All @@ -127,7 +129,7 @@ export function makeCapTP(ourId, send, bootstrapObj = undefined) {
val = presence;
} else {
// A new promise
imports.set(Number(slot.slice(1)), pr);
imports.set(Number(slot.slice(2)), pr);
val = pr.p;
}
slotToVal.set(slot, val);
Expand Down
48 changes: 48 additions & 0 deletions test/crosstalk.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { test } from 'tape-promise/tape';
import { harden, makeCapTP, E } from '../lib/captp';

test('prevent crosstalk', async t => {
try {
const debug = false;
let rightDispatch;
const { dispatch: leftDispatch, getBootstrap: leftBootstrap } = makeCapTP(
'left',
obj => {
if (debug) {
console.log('toRight', obj);
}
rightDispatch(obj);
},
);
({ dispatch: rightDispatch } = makeCapTP(
'right',
obj => {
if (debug) {
console.log('toLeft', obj);
}
leftDispatch(obj);
},
harden({
isSide(objP, side) {
return E(objP).side().then(s => t.equal(s, side, `obj.side() is ${side}`));
},
side() {
return 'right';
},
})
));
const rightRef = leftBootstrap();

await E(rightRef).isSide(rightRef, 'right');
const leftRef = harden({
side() {
return 'left';
},
});
await E(rightRef).isSide(leftRef, 'left');
} catch (e) {
t.isNot(e, e, 'unexpected exception');
} finally {
t.end();
}
});

0 comments on commit 19906c4

Please sign in to comment.