Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup JS code, added Docker configuration, added read example #9

Merged
merged 17 commits into from
Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 0 additions & 21 deletions rchat/package.json

This file was deleted.

45 changes: 0 additions & 45 deletions rchat/rchainDeploy.js

This file was deleted.

2 changes: 2 additions & 0 deletions rchat/README.md → src/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
## !! THIS DOCUMENTATION IS NOT UP TO DATE WITH THE CODE, SEE ROOT README FILE !!

# rchat: RChain replica of Zulip governance discussion

Using a largely unmodified zulip installation, we listen to
Expand Down
90 changes: 26 additions & 64 deletions rchat/chain_replica.js → src/chain_replica.mjs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
/* global Buffer */

import fs from 'fs'; // ISSUE: AMBIENT
// Reference to TypeScript definitions for IntelliSense in VSCode
/// <reference path="../rnode-grpc-gen/js/rnode-grpc-js.d.ts" />

import postgres from 'postgres'; // ISSUE: AMBIENT
import grpcLib from '@grpc/grpc-js'; //@@ AMBIENT

import rnode_grpc_js from '@tgrospic/rnode-grpc-js';
// requires --experimental-json-modules
import protoSchema from '../rchain-proto/rnode-grpc-gen/js/pbjs_generated.json';
import '../rchain-proto/rnode-grpc-gen/js/DeployServiceV1_pb.js'; // proto global

const { signDeploy, rnodeDeploy, getAddrFromPrivateKey } = rnode_grpc_js;
// RNode with environment parameters
import { rnodeService } from './rnode-env.mjs';

// Load .env file
import { config } from 'dotenv';
config();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ambient authority grumble...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have any nice solution for this. dotenv uses fs and path directly and I didn't find some more SES compatible variant.

Do you have any suggestions? Is it possible to use SES shim to load it inside separate Compartment with provided fs and path as only global variables?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... dotenv uses fs and path directly ...

yeah; that's a bug. reimplementing dotenv with ocap discipline is probably about 20 lines of code. But it can wait for a later PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to use SES shim to load it inside separate Compartment with provided fs and path as only global variables?

Yes, that can work too, if you really have to use 3rd party code and you can't change it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah; that's a bug. reimplementing dotenv with ocap discipline is probably about 20 lines of code. But it can wait for a later PR.

👍 I wonder should it be update of existing lib or a new package?


I tried to import dotenv in isolated Compartment.

import 'ses';
lockdown();

const c = new Compartment({fs, path});

const r = c.evaluate(`
  import { config } from 'dotenv';
  config();
`);

But I see this error:

(SyntaxError#1)
SyntaxError#1: Cannot use import statement outside a module

  at Object.eval (eval at makeEvaluateFactory (file:///home/tomi/projects/rchain/rchat/node_modules/ses/dist/ses.umd.js:1400:10), <anonymous>:8:30)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should it be update of existing lib or a new package?

I would probably just add a .js file in this project.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cannot use import statement outside a module

right... your browser will tell you the same thing if you try to eval an import declaration.

To load modules into a Compartment, you have to use @endo/compartment-mapper. Either importLocation or writeArchive followed by importArchive, I think. I asked for clarification in endojs/endo#963

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue to track updates #12.


const harden = x => Object.freeze(x); // ISSUE: @agoric/harden for deep-freeze?

Expand All @@ -20,7 +18,7 @@ const zulip_db_config = {
port: 5432,
database: 'zulip',
username: 'zulip',
password: 'REPLACE_WITH_SECURE_POSTGRES_PASSWORD',
password: process.env.POSTGRES_PASSWORD,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ambient authority grumble...

};

const zulip_ephemera = [
Expand All @@ -29,67 +27,35 @@ const zulip_ephemera = [
'zerver_useractivity', 'zerver_useractivityinterval', 'zerver_userpresence',
];

async function main(argv, env, { timer, postgres, grpcLib }) {
const [_node, _script] = argv;

async function main(argv, env, { timer, fsp, postgres, grpcLib }) {
const [_node, _script, filename] = argv;

// Postgres connection
const sql = postgres(zulip_db_config);

// RNode connection
const { sendDeploy, proposeBlock } = rnodeService(env, grpcLib);

const channel = 'mirror';
await prepare_to_listen(sql, channel);

let dest;
if (env.RNODE && env.SECRET_KEY) {
const deployService = rnodeDeploy({ grpcLib, host: env.RNODE || '127.0.0.1:40401', protoSchema });
const secretKey = Buffer.from(env.SECRET_KEY, 'hex');
const validafterblocknumber = parseInt(env.BLOCKNUM) || -1; // TODO: warn if missing?
const phlolimit = 10e7;
console.log({ validafterblocknumber, phlolimit });
dest = chain_dest(secretKey, deployService, { validafterblocknumber, phlolimit });
} else {
if (!filename) {
throw new Error('need file arg or RNODE and SECRET_KEY env variables');
}
const out = await fsp.open(filename, 'w');
dest = file_spool(out);
}
const dest = chain_dest({sendDeploy, proposeBlock});
const queue = batchingQueue({ max_qty: 64, quiesce_time: 4 * 1000 }, timer, dest);

mirror_events(sql, channel, queue);
}

function chain_dest(secretKey, deployService, { validafterblocknumber, phlolimit }) {
const keyInfo = getAddrFromPrivateKey(secretKey.toString('hex'));
console.log({deployKey: keyInfo.pubKey, eth: keyInfo.ethAddr });

return async (terms) => {
function chain_dest({sendDeploy, proposeBlock}) {
return async terms => {
const term = terms.join('\n|\n');
const deployData = {
term,
phloprice: 1, // TODO: when rchain economics evolve
phlolimit,
validafterblocknumber, // ISSUE: should get updated over time
};
const signed = signDeploy(secretKey, deployData);
console.log({ timestamp: signed.timestamp, terms: terms.length });
const result = await deployService.doDeploy(signed);
console.log({ deployResponse: result });
};
}

function file_spool(out) {
let first = true;
return (terms) => {
console.log('spooling', terms.length);
for (const rho of terms) {
if (first) {
first = false;
} else {
out.write('|\n');
}
out.write(rho);
out.write('\n');
}
// Send deploy
const {response: deployResponse, sig} = await sendDeploy({term});
console.log({ deployResponse });

// Propose block
const proposeResponse = await proposeBlock();
console.log({ proposeResponse });
};
}

Expand Down Expand Up @@ -207,7 +173,6 @@ function batchingQueue(

return harden({
push: (item) => {
let due = false;
const t = current_timestamp();
buf.push(item);
if (buf.length >= max_qty) {
Expand Down Expand Up @@ -238,15 +203,12 @@ function batchingQueue(


/* global process, setTimeout, clearTimeout */
main(process.argv, process.env, {
setTimeout,
fsp: fs.promises,
await main(process.argv, process.env, {
timer: {
current_timestamp: () => Date.now(),
setTimeout,
clearTimeout,
},
postgres,
grpcLib,
})
.catch(err => console.error(err));
});
46 changes: 46 additions & 0 deletions src/iddb-deploy.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Reference to TypeScript definitions for IntelliSense in VSCode
/// <reference path="../rnode-grpc-gen/js/rnode-grpc-js.d.ts" />

import grpcLib from '@grpc/grpc-js';
import { promises as fs } from 'fs'
import util from 'util';

// RNode with environment parameters
import { rnodeService } from './rnode-env.mjs';

// Load .env file
import { config } from 'dotenv';
config();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ambient authority grumble...


/**
* @param {object} arg
* @param {typeof file} arg.fs
* @param {typeof path} arg.path
*/
async function main(env, {fs, grpcLib}) {
// Get content of 'iddb.rho' file
const rhoFile = new URL('iddb.rho', import.meta.url);
const rhoCode = await fs.readFile(rhoFile, 'utf8');

// RNode connection
const { sendDeploy, getDeployResult, proposeBlock } = rnodeService(env, grpcLib);

// Send `iddb.rho` deploy
const {response: deployResponse, sig} = await sendDeploy({term: rhoCode});
console.log({ deployResponse });

// Propose block
const proposeResponse = await proposeBlock();
console.log({ proposeResponse });

// Get registered URI (sent on `rho:rchain:deployId`)
const deployResult = await getDeployResult({sig});
// console.log({deployResult: util.inspect(deployResult, {depth: 10, colors: true})});

// Extract registry URI
const uri = deployResult.payload?.blockinfoList[0]?.postblockdataList[0]?.exprsList[0]?.gUri;
console.log({uri});
console.log(`Copy URI to .env file IDDB_CONTRACT_URI variable.`);
};

await main(process.env, {fs, grpcLib});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yay for passing process.env explicitly.

6 changes: 3 additions & 3 deletions rchat/iddb.rho → src/iddb.rho
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
* IdDB - DB with id primary key column in all tables
*/
new
deployId, //(`rho:rchain:deployId`),
deployerId, //(`rho:rchain:deployerId`),
deployId(`rho:rchain:deployId`),
deployerId(`rho:rchain:deployerId`),
insertArbitrary(`rho:registry:insertArbitrary`),
IdDB,
log(`rho:io:stderr`)
Expand Down Expand Up @@ -201,4 +201,4 @@ in {
}
}
}
}
}
49 changes: 49 additions & 0 deletions src/myzulipdb-deploy.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Reference to TypeScript definitions for IntelliSense in VSCode
/// <reference path="../rnode-grpc-gen/js/rnode-grpc-js.d.ts" />

import grpcLib from '@grpc/grpc-js';
import { promises as fs } from 'fs'
import util from 'util';

// RNode with environment parameters
import { rnodeService } from './rnode-env.mjs';

// Load .env file
import { config } from 'dotenv';
config();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ambient authority grumble...


/**
* @param {object} arg
* @param {typeof file} arg.fs
* @param {typeof path} arg.path
*/
async function main(env, {fs, grpcLib}) {
const { IDDB_CONTRACT_URI } = env
if (!IDDB_CONTRACT_URI) throw Error(`Environment parameter not set IDDB_CONTRACT_URI, check .env file.`);

// Get content of 'iddb.rho' file
const rhoFile = new URL('myzulipdb.rho', import.meta.url);
const rhoCodeTemplate = await fs.readFile(rhoFile, 'utf8');
const rhoCode = rhoCodeTemplate.replace(/__ID_DB_URI__/, IDDB_CONTRACT_URI);

// RNode connection
const { sendDeploy, getDeployResult, proposeBlock } = rnodeService(env, grpcLib);

// Send `iddb.rho` deploy
const {response: deployResponse, sig} = await sendDeploy({term: rhoCode});
console.log({ deployResponse });

// Propose block
const proposeResponse = await proposeBlock();
console.log({ proposeResponse });

// Get registered URI (sent on `rho:rchain:deployId`)
const deployResult = await getDeployResult({sig});
// console.log({deployResult: util.inspect(deployResult, {depth: 10, colors: true})});

// Extract registry URI
const uri = deployResult.payload?.blockinfoList[0]?.postblockdataList[0]?.exprsList[0]?.gUri;
console.log({uri});
};

await main(process.env, {fs, grpcLib});
6 changes: 3 additions & 3 deletions rchat/myzulipdb.rho → src/myzulipdb.rho
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
* myzulipdb -- IdDB for deployer with zulip tables
*/
new
deployId, //(`rho:rchain:deployId`),
deployerId, //(`rho:rchain:deployerId`),
deployId(`rho:rchain:deployId`),
deployerId(`rho:rchain:deployerId`),
lookup(`rho:registry:lookup`),
insertArbitrary(`rho:registry:insertArbitrary`),
IdDBCh,
log(`rho:io:stderr`)
in {
lookup!(`rho:id:FIXME` /* ISSUE: how to sync IdDB registry URI? */, *IdDBCh) |
lookup!(`__ID_DB_URI__`, *IdDBCh) |
for (IdDB <- IdDBCh) {
log!({"IdDB": *IdDB}) |
new dbCh in {
Expand Down
Loading