Skip to content

Commit

Permalink
feat: send CapTP message in VatTP messaging layer
Browse files Browse the repository at this point in the history
  • Loading branch information
dckc committed May 14, 2022
1 parent 4762326 commit f6f90f7
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 27 deletions.
11 changes: 8 additions & 3 deletions lib/capTPTool.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ async function main({ https, setTimeout, clock, env }) {
const node = RNode(fetch);
const validator = node.validator('https://rnodeapi.rhobot.net');
const observer = node.observer('https://rnodeapi.rhobot.net');
const pkHex = env.BOOTSTRAP_ACCOUNT;
const { BOOTSTRAP_ACCOUNT: pkHex, VAT_ID: vatId } = env;

if (!pkHex) throw Error('$BOOTSTRAP_ACCOUNT');
const account = makeAccount(pkHex, observer, { setTimeout, clock }, {});
const peer = makePeer(validator, observer);
const myconn = peer.makeConnection(account);

if (!vatId) throw Error('$VAT_ID');
const myconn = peer.makeConnection(account, vatId);

const myBootstrap = {};

Expand All @@ -29,7 +33,8 @@ async function main({ https, setTimeout, clock, env }) {
myconn.onReceive((obj) => dispatch(obj));

// Get the remote's bootstrap object and call a remote method.
await E(getBootstrap())
const boot = await getBootstrap(); // TODO: validator.propose()
await E(boot)
.lookup('stuff')
.then((res) => console.log('got res', res));

Expand Down
13 changes: 13 additions & 0 deletions lib/makeVat.rho
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
new
reply,
lookup(`rho:registry:lookup`),
vatCh,
spawnCh in {
lookup!(`rho:id:66e7pqyjgxhng9o9wt4js7gtzhhmfcaxpkya1r53yizeunty7pjyje`, *spawnCh)
|
for (spawn <- spawnCh) {
spawn!(*vatCh) | for (@{"id": id, "entry": _} <- vatCh) {
reply!(id)
}
}
}
51 changes: 31 additions & 20 deletions lib/marshal-rhoproto.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

import { rhoBuilder } from './rhoBuilder.js';

/** @typedef {import('@endo/marshal').Encoding} Encoding */
/** @typedef {import('./rhoBuilder').Miranda} Miranda */

const { freeze: harden } = Object; // TODO: harden

const { isArray } = Array;
const { fromEntries } = Object;

const Nil = { ExprPar: { data: [] } };
// const { fromEntries } = Object;

/**
* @param {X[]} xs
Expand All @@ -21,14 +22,17 @@ const Nil = { ExprPar: { data: [] } };
const zip = (xs, ys) => xs.map((x, ix) => [x, ys[ix]]);

/**
* @param {Encoding} encoding
* @param {boolean=} shouldIndent
* @returns {RhoExpr}
* @param {Encoding} item
* @param {boolean=} _shouldIndent
* @returns {string}
*/
export const decodeToRhoTypes = (encoding) => {
export const decodeToRhoTerm = (item, _shouldIndent) => {
const builder = rhoBuilder();

/** @returns {Miranda} */
/**
* @param {Encoding} encoding
* @returns {Miranda}
*/
const recur = (encoding) => {
switch (typeof encoding) {
case 'boolean':
Expand All @@ -44,7 +48,7 @@ export const decodeToRhoTypes = (encoding) => {
case 'string':
return builder.primitive(encoding);
case 'object':
if (encoding === null) return builder.Nil;
if (encoding === null) return builder.Nil();
if ('@qclass' in encoding) {
switch (encoding['@qclass']) {
case 'undefined':
Expand All @@ -57,16 +61,20 @@ export const decodeToRhoTypes = (encoding) => {
switch (tag) {
case 'copySet':
throw Error('TODO');
return { ExprSet: { data: payload.map(recur) } };
// return { ExprSet: { data: payload.map(recur) } };
case 'copyMap': {
/** @type {{ keys: Encoding[], values: Encoding[] }} */
const { keys, values } = payload;
if (keys.filter((x) => typeof x !== 'string').length > 0) {
throw Error('TODO: non-string keys');

}
const entries = zip(keys, values.map(recur));
return builder.mapExpr(entries.map(([key, value]) => ({ key, value })));
const stringKeys = keys.map(x => {
if (x !== 'string') {
throw Error('TODO: non-string keys');
}
return x;
})
const entries = zip(stringKeys, values.map(recur));
return builder.mapExpr(
entries.map(([key, value]) => ({ key, value })),
);
}
default:
throw Error('TODO');
Expand All @@ -85,16 +93,19 @@ export const decodeToRhoTypes = (encoding) => {
throw Error('TODO: non-string keys');
const entries = zip(keys, values.map(recur));
// TODO: distinguish copyRecord from copyMap?
return builder.mapExpr(entries.map(([key, value]) => ({ key, value })));
return builder.mapExpr(
entries.map(([key, value]) => ({ key, value })),
);
}
default:
throw Error('TODO');
}
};
const m = recur(encoding);
const m = recur(item);
const parts = [];
const txt = m._printOn({ print: s => parts.push(s)});
// eslint-disable-next-line no-underscore-dangle
m._printOn({ begin: () => {}, print: (s) => parts.push(s), newline: () => [].push('\n'), end: () => {} });
return parts.join('');
};

harden(decodeToRhoTypes);
harden(decodeToRhoTerm);
27 changes: 23 additions & 4 deletions lib/messageQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,41 @@

import { startTerm, listenAtDeployId } from './vendor/rchain-api/proxy.js';
import { RhoExpr } from './vendor/rchain-api/rho-expr.js';
import { decodeToRhoTypes } from './marshal-rhoproto.js';
import { decodeToRhoTerm } from './marshal-rhoproto.js';

const { freeze: harden } = Object; // @@TODO: @endo/init harden

const lit = (s) => JSON.stringify(s);

const sendTerm = (vatId, msgTerm) => `
new ack(\`rho:rchain:deployId\`), deployerId(\`rho:rchain:deployerId\`) in {
match {[*deployerId, ${lit(vatId)}]} {
{*target} => target!(${msgTerm}, *ack)
}
}`;

export const makePeer = (validator, observer) => {
return harden({
makeConnection: (account) => {
/**
* @param {*} account
* @param {string} vatId
* @returns
*/
makeConnection: (account, vatId) => {
const unAcked = [];
return harden({
/**
* @param {*} msg
* @returns { Promise<void> } promise for message acknowledgement
*/
send: async (msg) => {
const term = decodeToRhoTypes(msg);
const deploy = await startTerm(term, validator, observer, account);
const msgTerm = decodeToRhoTerm(msg);
const deploy = await startTerm(
sendTerm(vatId, msgTerm),
validator,
observer,
account,
);
const { expr } = await listenAtDeployId(observer, deploy);
return RhoExpr.parse(expr);
},
Expand Down
42 changes: 42 additions & 0 deletions lib/vatBoot.rho
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
new
// log,
log(`rho:io:stdout`),
deployerId(`rho:rchain:deployerId`),
insertArb(`rho:registry:insertArbitrary`),
spawn in {
contract spawn(ret) = {
new vatName in {
match {*vatName}.toByteArray().bytesToHex() {
vatHex => {
match {[*deployerId, vatHex]} {
{*entry} => {
contract entry(@msg, ack) = {
log!(msg) |
ack!(msg.toByteArray().length()) // TODO: sequence number
| log!(msg.toByteArray().length())
}
|
ret!({"entry": bundle+{*entry}, "id": vatHex})
}
}
}
}
}
}

|
new testCh in {
spawn!(*testCh) | for (@{"entry": *testVatEntry, "id": hex} <- testCh) {
log!(*testVatEntry)|
log!({"hex id": hex}) |
testVatEntry!("Watson, come quickly!", *testVatEntry)
}
}
|
new uriCh in {
insertArb!(*spawn, *uriCh) |
for(@uri <- uriCh) {
log!({"spawn contract": uri})
}
}
}

0 comments on commit f6f90f7

Please sign in to comment.