Skip to content

Commit

Permalink
feat: CapTP to RChain (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
dckc committed Mar 12, 2022
1 parent b5dc213 commit 331e08d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 16 deletions.
35 changes: 19 additions & 16 deletions lib/capTPTool.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
// @ts-check
import '@endo/init';
import util from 'util';
import { E, makeCapTP } from '@endo/captp';

import { decodeToRhoTypes } from './marshal-rhoproto.js';
import { nodeFetch } from './vendor/rchain-api/curl.js';
import { RNode } from './vendor/rchain-api/rnode.js';
import { makePeer } from './messageQueue.js';
import { makeAccount } from './vendor/rchain-api/proxy.js';

async function main() {
const myconn = {
send: (msg) => {
console.log('send:', msg);
const rho = decodeToRhoTypes(msg);
console.log('rho:', JSON.stringify(rho, null, 2));
},
onReceive: (fn) => {
// ...
},
};
async function main({ https, setTimeout, clock, env }) {
const fetch = nodeFetch({ http: https });
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 account = makeAccount(pkHex, observer, { setTimeout, clock }, {});
const peer = makePeer(validator, observer);
const myconn = peer.makeConnection(account);

const myBootstrap = {};

Expand All @@ -26,15 +26,18 @@ async function main() {
myconn.send,
myBootstrap,
);
myconn.onReceive = (obj) => dispatch(obj);
myconn.onReceive((obj) => dispatch(obj));

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

// Tear down the CapTP connection if it fails (e.g. connection is closed).
abort(Error('Connection aborted by user.'));
// abort(Error('Connection aborted by user.'));
}

main().catch((err) => console.error(err));
(async () => {
const https = await import('https');
await main({ https, setTimeout, clock: () => Date.now(), env: process.env });
})().catch((err) => console.error(err));
35 changes: 35 additions & 0 deletions lib/messageQueue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// 2-way reliable messaging
// https://github.com/rchain-community/rchain-api/issues/94

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

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

export const makePeer = (validator, observer) => {
return harden({
makeConnection: (account) => {
const unAcked = [];
return harden({
/**
* @param { string } term
* @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 { expr } = await listenAtDeployId(observer, deploy);
return RhoExpr.parse(expr);
},
onReceive: (dispatch) => {
console.warn(
'TODO: start polling; call dispatch when a message is available',
);
},
});
},
});
};
harden(makePeer);

0 comments on commit 331e08d

Please sign in to comment.