-
Notifications
You must be signed in to change notification settings - Fork 8
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
Changes from 1 commit
1f9298f
8f0fa77
d6e2784
90d4d70
c4f6aed
718f46f
3d3350e
34b0d77
6c4e3e0
ed420b8
d27caa1
63804fc
82d84b3
11e403d
7c64884
32cfd17
304acbf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
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(); | ||
|
||
const harden = x => Object.freeze(x); // ISSUE: @agoric/harden for deep-freeze? | ||
|
||
|
@@ -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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ambient authority grumble... |
||
}; | ||
|
||
const zulip_ephemera = [ | ||
|
@@ -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 }); | ||
}; | ||
} | ||
|
||
|
@@ -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) { | ||
|
@@ -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)); | ||
}); |
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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yay for passing |
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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ambient authority grumble...
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that can work too, if you really have to use 3rd party code and you can't change it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 I wonder should it be update of existing lib or a new package?
I tried to import dotenv in isolated Compartment.
But I see this error:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would probably just add a .js file in this project.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
orwriteArchive
followed byimportArchive
, I think. I asked for clarification in endojs/endo#963There was a problem hiding this comment.
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.