-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(bundle): backend implementation for upload-contract, register-http
- Loading branch information
1 parent
e8d2cde
commit b10e244
Showing
8 changed files
with
230 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
/* eslint-disable no-await-in-loop */ | ||
import parseArgs from 'minimist'; | ||
import WebSocket from 'ws'; | ||
import { E } from '@agoric/eventual-send'; | ||
import { evaluateProgram } from '@agoric/evaluate'; | ||
import { makeCapTP } from '@agoric/captp'; | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
|
||
import buildSourceBundle from '@agoric/bundle-source'; | ||
|
||
const makePromise = () => { | ||
const pr = {}; | ||
pr.p = new Promise((resolve, reject) => { | ||
pr.res = resolve; | ||
pr.rej = reject; | ||
}); | ||
return pr; | ||
}; | ||
|
||
const sendJSON = (ws, obj) => { | ||
if (ws.readyState !== ws.OPEN) { | ||
return; | ||
} | ||
// console.log('sending', obj); | ||
ws.send(JSON.stringify(obj)); | ||
}; | ||
|
||
export default async function bundle(insistIsBasedir, args) { | ||
const { _: a, evaluate, once, output, 'ag-solo': agSolo } = parseArgs(args, { | ||
boolean: ['once', 'evaluate'], | ||
alias: {o: 'output', e: 'evaluate'}, | ||
stopEarly: true, | ||
}); | ||
|
||
const [mainModule, ...namePaths] = a; | ||
if (!mainModule) { | ||
console.error('You must specify a main module to bundle'); | ||
return 1; | ||
} | ||
|
||
if (!output && !evaluate) { | ||
console.error(`You must specify at least one of '--output' or '--evaluate'`); | ||
return 1; | ||
} | ||
|
||
const bundled = {}; | ||
|
||
const moduleFile = `${__dirname}/${mainModule}.js`; | ||
await Promise.all([`main=${moduleFile}`, ...namePaths].map(async namePath => { | ||
const match = namePath.match(/^([^=]+)=(.+)$/); | ||
if (!match) { | ||
throw Error(`${namePath} isn't NAME=PATH`); | ||
} | ||
const name = match[1]; | ||
const filepath = match[2]; | ||
bundled[name] = await buildSourceBundle(filepath); | ||
})); | ||
|
||
if (output) { | ||
await fs.promises.writeFile(output, JSON.stringify(bundled)); | ||
} | ||
|
||
if (!evaluate) { | ||
return 0; | ||
} | ||
const actualSources = `(${bundled.main.source}\n)\n${bundled.main.sourceMap}`; | ||
// console.log(actualSources); | ||
const mainNS = evaluateProgram(actualSources, { require })(); | ||
const main = mainNS.default; | ||
if (typeof main !== 'function') { | ||
console.error(`Bundle main does not have an export default function`); | ||
return 1; | ||
} | ||
|
||
let wsurl = agSolo; | ||
if (!agSolo) { | ||
const basedir = insistIsBasedir(); | ||
const cjson = await fs.promises.readFile( | ||
path.join(basedir, 'connections.json'), | ||
); | ||
for (const conn of JSON.parse(cjson)) { | ||
if (conn.type === 'http') { | ||
wsurl = `ws://${conn.host}:${conn.port}/captp`; | ||
} | ||
} | ||
} | ||
|
||
const ws = new WebSocket(wsurl, { origin: 'http://127.0.0.1' }); | ||
const exit = makePromise(); | ||
ws.on('open', async () => { | ||
try { | ||
const { dispatch, getBootstrap } = makeCapTP('bundle', obj => | ||
sendJSON(ws, obj), | ||
); | ||
ws.on('message', data => { | ||
// console.log(data); | ||
try { | ||
const obj = JSON.parse(data); | ||
if (obj.type === 'CTP_ERROR') { | ||
throw obj.error; | ||
} | ||
dispatch(obj); | ||
} catch (e) { | ||
console.error('server error processing message', data, e); | ||
exit.rej(e); | ||
} | ||
}); | ||
|
||
// Wait for the chain to become ready. | ||
let bootC = E.C(getBootstrap()); | ||
console.error('Chain loaded:', await bootC.G.LOADING.P); | ||
// Take a new copy, since the chain objects have been added to bootstrap. | ||
bootC = E.C(getBootstrap()); | ||
if (once) { | ||
if (await bootC.G.READY.M.isReady().P) { | ||
console.error('Singleton bundle already installed'); | ||
ws.close(); | ||
exit.res(0); | ||
return; | ||
} | ||
} | ||
|
||
console.error(`Running bundle main entry point...`); | ||
await main({ bundle: bundled, home: bootC.P }); | ||
console.error('Success!'); | ||
if (once) { | ||
await bootC.G.READY.M.resolve('initialized').P; | ||
} | ||
ws.close(); | ||
exit.res(0); | ||
} catch (e) { | ||
exit.rej(e); | ||
} | ||
}); | ||
ws.on('close', (_code, _reason) => { | ||
// console.log('connection closed'); | ||
exit.res(1); | ||
}); | ||
return exit.p; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
export default async function registerHttp({ home, bundle }) { | ||
console.error(`Upgrading Dapp handlers...`); | ||
await register(home, bundle, Object.keys(bundle).filter(k => k !== 'main').sort()); | ||
} | ||
|
||
export async function register(homeP, bundle, keys) { | ||
const targetObj = await homeP~.http; | ||
if (!targetObj) { | ||
throw Error(`HTTP registration object not available`); | ||
} | ||
await Promise.all(keys.map(key => { | ||
const { source, moduleFormat } = bundle[key]; | ||
// console.error(`Uploading ${source}`); | ||
|
||
// Register the HTTP handler. | ||
contractsAP.push(targetObj~.register(key, source, moduleFormat)); | ||
})); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,135 +1,39 @@ | ||
/* eslint-disable no-await-in-loop */ | ||
import parseArgs from 'minimist'; | ||
import WebSocket from 'ws'; | ||
import { E } from '@agoric/eventual-send'; | ||
import { makeCapTP } from '@agoric/captp'; | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
|
||
import buildSourceBundle from '@agoric/bundle-source'; | ||
|
||
const makePromise = () => { | ||
const pr = {}; | ||
pr.p = new Promise((resolve, reject) => { | ||
pr.res = resolve; | ||
pr.rej = reject; | ||
}); | ||
return pr; | ||
}; | ||
|
||
const sendJSON = (ws, obj) => { | ||
if (ws.readyState !== ws.OPEN) { | ||
return; | ||
} | ||
// console.log('sending', obj); | ||
ws.send(JSON.stringify(obj)); | ||
}; | ||
|
||
export default async function upload(insistIsBasedir, args) { | ||
const { _: namePaths, once, 'ag-solo': agSolo } = parseArgs(args, { | ||
boolean: ['once'], | ||
stopEarly: true, | ||
}); | ||
if (namePaths.length === 0) { | ||
console.error('You must specify TARGET-NAME=PATH arguments to upload'); | ||
return 1; | ||
} | ||
export default async function installContracts({ home, bundle }) { | ||
console.error(`Installing targeted contracts...`); | ||
await install(home, bundle, Object.keys(bundle).filter(k => k !== 'main').sort()); | ||
} | ||
|
||
let wsurl = agSolo; | ||
if (!agSolo) { | ||
const basedir = insistIsBasedir(); | ||
const cjson = await fs.promises.readFile( | ||
path.join(basedir, 'connections.json'), | ||
); | ||
for (const conn of JSON.parse(cjson)) { | ||
if (conn.type === 'http') { | ||
wsurl = `ws://${conn.host}:${conn.port}/captp`; | ||
} | ||
export async function install(homeP, bundle, keys) { | ||
const names = []; | ||
const contractsAP = []; | ||
for (const key of keys) { | ||
const match = key.match(/^(([^:]+):[^=]+)$/); | ||
if (!match) { | ||
throw Error(`${key} isn't TARGET:NAME`); | ||
} | ||
} | ||
|
||
const ws = new WebSocket(wsurl, { origin: 'http://127.0.0.1' }); | ||
const exit = makePromise(); | ||
ws.on('open', async () => { | ||
try { | ||
const { dispatch, getBootstrap } = makeCapTP('upload', obj => | ||
sendJSON(ws, obj), | ||
const name = match[1]; | ||
const target = match[2]; | ||
const { source, moduleFormat } = bundle[key]; | ||
// console.error(`Uploading ${source}`); | ||
|
||
const targetObj = await homeP~.[target]; | ||
if (!targetObj) { | ||
console.error( | ||
`Contract installation target object ${target} is not available for ${name}; skipping...`, | ||
); | ||
ws.on('message', data => { | ||
// console.log(data); | ||
try { | ||
const obj = JSON.parse(data); | ||
if (obj.type === 'CTP_ERROR') { | ||
throw obj.error; | ||
} | ||
dispatch(obj); | ||
} catch (e) { | ||
console.error('server error processing message', data, e); | ||
exit.rej(e); | ||
} | ||
}); | ||
|
||
// Wait for the chain to become ready. | ||
let bootC = E.C(getBootstrap()); | ||
console.error('Chain loaded:', await bootC.G.LOADING.P); | ||
// Take a new copy, since the contract targets should exist. | ||
bootC = E.C(getBootstrap()); | ||
if (once) { | ||
if (await bootC.G.READY.M.isReady().P) { | ||
console.error('Contracts already uploaded'); | ||
ws.close(); | ||
exit.res(0); | ||
return; | ||
} | ||
} | ||
const uploadsC = bootC.G.uploads; | ||
|
||
console.error(`Uploading contracts...`); | ||
|
||
const names = []; | ||
const contractsAP = []; | ||
for (const namePath of namePaths) { | ||
const match = namePath.match(/^(([^\W-]+)-[^=]+)=(.+)$/); | ||
if (!match) { | ||
throw Error(`${namePath} isn't TARGET-NAME=PATH`); | ||
} | ||
const name = match[1]; | ||
const target = match[2]; | ||
const filepath = match[3]; | ||
const { source, moduleFormat } = await buildSourceBundle(filepath); | ||
// console.error(`Uploading ${source}`); | ||
|
||
const targetObj = await bootC.G[target].P; | ||
if (!targetObj) { | ||
console.error( | ||
`Contract installation target object ${target} is not available for ${name}; skipping...`, | ||
); | ||
} else { | ||
// Install the contract, then save it in home.uploads. | ||
console.log(name) | ||
contractsAP.push(E(targetObj).install(source, moduleFormat)); | ||
names.push(name); | ||
} | ||
} | ||
} else { | ||
// Install the contract, then save it in home.uploads. | ||
console.log(name) | ||
contractsAP.push(targetObj~.install(source, moduleFormat)); | ||
names.push(name); | ||
} | ||
} | ||
|
||
const contracts = await Promise.all(contractsAP); | ||
for (let i = 0; i < contracts.length; i ++) { | ||
await uploadsC.M.set(names[i], contracts[i]).P; | ||
} | ||
const uploadsP = homeP~.uploads; | ||
const contracts = await Promise.all(contractsAP); | ||
for (let i = 0; i < contracts.length; i ++) { | ||
await uploadsP~.set(names[i], contracts[i]); | ||
} | ||
|
||
console.error('Success! See home.uploads~.list()'); | ||
if (once) { | ||
await bootC.G.READY.M.resolve('contracts uploaded').P; | ||
} | ||
ws.close(); | ||
exit.res(0); | ||
} catch (e) { | ||
exit.rej(e); | ||
} | ||
}); | ||
ws.on('close', (_code, _reason) => { | ||
// console.log('connection closed'); | ||
exit.res(1); | ||
}); | ||
return exit.p; | ||
console.error('See home.uploads~.list()'); | ||
} |
Oops, something went wrong.