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

feat: reintroduce anylogger as the console endowment #1285

Merged
merged 2 commits into from
Jul 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
42 changes: 26 additions & 16 deletions packages/SwingSet/src/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,19 @@ import { insistStorageAPI } from './storageAPI';
import { insistCapData } from './capdata';
import { parseVatSlot } from './parseVatSlots';

const log = anylogger('SwingSet:controller');
function makeConsole(tag) {
const log = anylogger(tag);
const cons = {};
for (const level of ['debug', 'log', 'info', 'warn', 'error']) {
cons[level] = log[level];
}
return harden(cons);
}
const console = makeConsole('SwingSet:controller');

// FIXME: Put this somewhere better.
process.on('unhandledRejection', e =>
log.error('UnhandledPromiseRejectionWarning:', e),
console.error('UnhandledPromiseRejectionWarning:', e),
);

const ADMIN_DEVICE_PATH = require.resolve('./kernel/vatAdmin/vatAdmin-src');
Expand Down Expand Up @@ -62,7 +70,7 @@ function byName(a, b) {
* lots of places).
*/
export function loadBasedir(basedir) {
log.debug(`= loading config from basedir ${basedir}`);
console.debug(`= loading config from basedir ${basedir}`);
const vats = new Map(); // name -> { sourcepath, options }
const subs = fs.readdirSync(basedir, { withFileTypes: true });
subs.sort(byName);
Expand All @@ -82,7 +90,7 @@ export function loadBasedir(basedir) {
const vatSourcePath = path.resolve(basedir, dirent.name);
vats.set(name, { sourcepath: vatSourcePath, options: {} });
} else {
log.debug('ignoring ', dirent.name);
console.debug('ignoring ', dirent.name);
}
});
let bootstrapIndexJS = path.resolve(basedir, 'bootstrap.js');
Expand Down Expand Up @@ -128,7 +136,7 @@ export async function buildVatController(config, argv = []) {
const kernelNS = await importBundle(kernelSource, {
filePrefix: 'kernel',
endowments: {
console,
console: makeConsole('SwingSet:kernel'),
require: kernelRequire,
HandledPromise,
},
Expand Down Expand Up @@ -171,14 +179,16 @@ export async function buildVatController(config, argv = []) {
throw Error(`vatRequire unprepared to satisfy require(${what})`);
}

const vatEndowments = harden({
console,
require: vatRequire,
HandledPromise,
// re2 is a RegExp work-a-like that disables backtracking expressions for
// safer memory consumption
RegExp: re2,
});
function makeVatEndowments(consoleTag) {
return harden({
require: vatRequire,
console: makeConsole(`SwingSet:${consoleTag}`),
HandledPromise,
// re2 is a RegExp work-a-like that disables backtracking expressions for
// safer memory consumption
RegExp: re2,
});
}

async function loadStaticVat(sourceIndex, name) {
if (!(sourceIndex[0] === '.' || path.isAbsolute(sourceIndex))) {
Expand All @@ -189,7 +199,7 @@ export async function buildVatController(config, argv = []) {
const bundle = await bundleSource(sourceIndex);
const vatNS = await importBundle(bundle, {
filePrefix: name,
endowments: vatEndowments,
endowments: makeVatEndowments(name),
});
let setup;
if ('buildRootObject' in vatNS) {
Expand Down Expand Up @@ -230,7 +240,7 @@ export async function buildVatController(config, argv = []) {
const kernelEndowments = {
waitUntilQuiescent,
hostStorage,
vatEndowments,
makeVatEndowments,
vatAdminDevSetup: await loadStaticVat(ADMIN_DEVICE_PATH, 'dev-vatAdmin'),
vatAdminVatSetup: await loadStaticVat(ADMIN_VAT_PATH, 'vat-vatAdmin'),
replaceGlobalMeter,
Expand All @@ -245,7 +255,7 @@ export async function buildVatController(config, argv = []) {
}

async function addGenesisVat(name, sourceIndex, options = {}) {
log.debug(`= adding vat '${name}' from ${sourceIndex}`);
console.debug(`= adding vat '${name}' from ${sourceIndex}`);
const setup = await loadStaticVat(sourceIndex, `vat-${name}`);
kernel.addGenesisVat(name, setup, options);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/SwingSet/src/kernel/dynamicVat.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function makeDynamicVatCreator(stuff) {
const {
allocateUnusedVatID,
vatNameToID,
vatEndowments,
makeVatEndowments,
dynamicVatPowers,
transformMetering,
makeGetMeter,
Expand Down Expand Up @@ -86,7 +86,7 @@ export function makeDynamicVatCreator(stuff) {
const vatNS = await importBundle(vatSourceBundle, {
filePrefix: vatID,
transforms,
endowments: { ...vatEndowments, getMeter },
endowments: { ...makeVatEndowments(vatID), getMeter },
});
if (typeof vatNS.buildRootObject !== 'function') {
throw Error(
Expand Down
4 changes: 2 additions & 2 deletions packages/SwingSet/src/kernel/kernel.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default function buildKernel(kernelEndowments) {
const {
waitUntilQuiescent,
hostStorage,
vatEndowments,
makeVatEndowments,
vatAdminVatSetup,
vatAdminDevSetup,
replaceGlobalMeter,
Expand Down Expand Up @@ -769,7 +769,7 @@ export default function buildKernel(kernelEndowments) {
const createVatDynamically = makeDynamicVatCreator({
allocateUnusedVatID: kernelKeeper.allocateUnusedVatID,
vatNameToID,
vatEndowments,
makeVatEndowments,
dynamicVatPowers,
transformMetering,
makeGetMeter,
Expand Down
24 changes: 12 additions & 12 deletions packages/agoric-cli/lib/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ const RETRY_DELAY_MS = 1000;

export default async function deployMain(progname, rawArgs, powers, opts) {
const { anylogger, makeWebSocket } = powers;
const log = anylogger('agoric:deploy');
const console = anylogger('agoric:deploy');

const args = rawArgs.slice(1);

if (args.length === 0) {
log.error('you must specify at least one deploy.js to run');
console.error('you must specify at least one deploy.js to run');
return 1;
}

Expand All @@ -34,7 +34,7 @@ export default async function deployMain(progname, rawArgs, powers, opts) {
return;
}
const body = JSON.stringify(obj);
log.debug('sendJSON', body.slice(0, 200));
console.debug('sendJSON', body.slice(0, 200));
ws.send(body);
};

Expand All @@ -44,48 +44,48 @@ export default async function deployMain(progname, rawArgs, powers, opts) {
let retries = 0;
const retryWebsocket = () => {
retries += 1;
log.info(`Open CapTP connection to ${wsurl} (try=${retries})...`);
console.info(`Open CapTP connection to ${wsurl} (try=${retries})...`);
const ws = makeWebSocket(wsurl, { origin: 'http://127.0.0.1' });
ws.on('open', async () => {
connected = true;
try {
log.info('Connected to CapTP!');
console.debug('Connected to CapTP!');
const { dispatch, getBootstrap } = makeCapTP('bundle', obj =>
sendJSON(ws, obj),
);
ws.on('message', data => {
try {
const obj = JSON.parse(data);
log.debug('receiving', data.slice(0, 200));
console.debug('receiving', data.slice(0, 200));
if (obj.type === 'CTP_ERROR') {
throw obj.error;
}
dispatch(obj);
} catch (e) {
log.error('server error processing message', data, e);
console.error('server error processing message', data, e);
exit.reject(e);
}
});

// Wait for the chain to become ready.
let bootP = getBootstrap();
const loaded = await E.G(bootP).LOADING;
log.info('Chain loaded:', loaded);
console.debug('Chain loaded:', loaded);
// Take a new copy, since the chain objects have been added to bootstrap.
bootP = getBootstrap();

for (const arg of args) {
const moduleFile = path.resolve(process.cwd(), arg);
const pathResolve = (...resArgs) =>
path.resolve(path.dirname(moduleFile), ...resArgs);
log('running', moduleFile);
console.log('running', moduleFile);

// use a dynamic import to load the deploy script, it is unconfined
// eslint-disable-next-line import/no-dynamic-require,global-require
const mainNS = require(pathResolve(moduleFile));
const main = mainNS.default;
if (typeof main !== 'function') {
log.error(
console.error(
`${moduleFile} does not have an export default function main`,
);
} else {
Expand All @@ -96,15 +96,15 @@ export default async function deployMain(progname, rawArgs, powers, opts) {
}
}

log.info('Done!');
console.debug('Done!');
ws.close();
exit.resolve(0);
} catch (e) {
exit.reject(e);
}
});
ws.on('close', (_code, _reason) => {
log.debug('connection closed');
console.debug('connection closed');
if (connected) {
exit.resolve(1);
}
Expand Down
18 changes: 9 additions & 9 deletions packages/cosmic-swingset/lib/ag-solo/vats/ibc.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ export function makeIBCProtocolHandler(E, callIBCDevice) {

return harden({
async onOpen(conn, localAddr, remoteAddr, _handler) {
console.info(
console.debug(
'onOpen Remote IBC Connection',
channelID,
portID,
Expand Down Expand Up @@ -304,7 +304,7 @@ export function makeIBCProtocolHandler(E, callIBCDevice) {
*/
const protocol = harden({
async onCreate(impl, _protocolHandler) {
console.info('IBC onCreate');
console.debug('IBC onCreate');
protocolImpl = impl;
},
async generatePortID(_localAddr, _protocolHandler) {
Expand All @@ -324,7 +324,7 @@ export function makeIBCProtocolHandler(E, callIBCDevice) {
return callIBCDevice('bindPort', { packet });
},
async onConnect(port, localAddr, remoteAddr, chandler, _protocolHandler) {
console.warn('IBC onConnect', localAddr, remoteAddr);
console.debug('IBC onConnect', localAddr, remoteAddr);
const portID = localAddrToPortID(localAddr);
const pendingConns = portToPendingConns.get(port);

Expand Down Expand Up @@ -423,14 +423,14 @@ EOF
.catch(rethrowUnlessMissing);
return onConnectP.promise;
},
async onListen(_port, _localAddr, _listenHandler) {
// console.warn('IBC onListen', localAddr);
async onListen(_port, localAddr, _listenHandler) {
console.debug('IBC onListen', localAddr);
},
async onListenRemove(_port, localAddr, _listenHandler) {
console.warn('IBC onListenRemove', localAddr);
console.debug('IBC onListenRemove', localAddr);
},
async onRevoke(port, localAddr, _protocolHandler) {
console.warn('IBC onRevoke', localAddr);
console.debug('IBC onRevoke', localAddr);
const pendingConns = portToPendingConns.get(port);
portToPendingConns.delete(port);
portToCircuits.delete(port);
Expand All @@ -444,7 +444,7 @@ EOF
return harden({
...protocol,
async fromBridge(srcID, obj) {
console.warn('IBC fromBridge', srcID, obj);
console.debug('IBC fromBridge', srcID, obj);
switch (obj.event) {
case 'channelOpenInit': {
// This event is sent by a naive relayer that wants to initiate
Expand Down Expand Up @@ -512,7 +512,7 @@ EOF
const versionSuffix = version ? `/${version}` : '';
const localAddr = `/ibc-port/${portID}/${order.toLowerCase()}${versionSuffix}`;
const ibcHops = hops.map(hop => `/ibc-hop/${hop}`).join('/');
const remoteAddr = `${ibcHops}/ibc-port/${rPortID}/${order.toLowerCase()}/${rVersion}`;
const remoteAddr = `${ibcHops}/ibc-port/${rPortID}/${order.toLowerCase()}/${rVersion}/ibc-channel/${rChannelID}`;

// See if we allow an inbound attempt for this address pair (without rejecting).
const attemptP = E(protocolImpl).inbound(localAddr, remoteAddr);
Expand Down
4 changes: 2 additions & 2 deletions packages/cosmic-swingset/lib/ag-solo/vats/vat-wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,11 @@ function build(E, _D, _log) {
function getCommandHandler() {
return harden({
onOpen(_obj, meta) {
console.error('Adding adminHandle', meta);
console.debug('Adding adminHandle', meta);
adminHandles.add(meta.channelHandle);
},
onClose(_obj, meta) {
console.error('Removing adminHandle', meta);
console.debug('Removing adminHandle', meta);
adminHandles.delete(meta.channelHandle);
},
onMessage: adminOnMessage,
Expand Down