Skip to content

Commit

Permalink
Merge pull request #3450 from Agoric/mfig-consensus-mode
Browse files Browse the repository at this point in the history
The beginnings of SwingSet consensus mode
  • Loading branch information
michaelfig authored Jul 14, 2021
2 parents e872c0c + dc0839b commit ae5330a
Show file tree
Hide file tree
Showing 17 changed files with 138 additions and 59 deletions.
8 changes: 4 additions & 4 deletions golang/cosmos/x/vbank/vbank.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func rewardRate(pool sdk.Coins, blocks int64) sdk.Coins {
}

func (ch portHandler) Receive(ctx *vm.ControllerContext, str string) (ret string, err error) {
fmt.Println("vbank.go downcall", str)
// fmt.Println("vbank.go downcall", str)
keeper := ch.keeper

var msg portMessage
Expand Down Expand Up @@ -223,13 +223,13 @@ func (ch portHandler) Receive(ctx *vm.ControllerContext, str string) (ret string
err = fmt.Errorf("unrecognized type %s", msg.Type)
}

fmt.Println("vbank.go downcall reply", ret, err)
// fmt.Println("vbank.go downcall reply", ret, err)
return
}

func (am AppModule) CallToController(ctx sdk.Context, send string) (string, error) {
// fmt.Println("ibc.go upcall", send)
// fmt.Println("vbank.go upcall", send)
reply, err := am.keeper.CallToController(ctx, send)
// fmt.Println("ibc.go upcall reply", reply, err)
// fmt.Println("vbank.go upcall reply", reply, err)
return reply, err
}
5 changes: 4 additions & 1 deletion packages/SwingSet/src/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ export function makeStartXSnap(bundles, { snapStore, env, spawn }) {
* slogFile?: string,
* testTrackDecref?: unknown,
* warehousePolicy?: { maxVatsOnline?: number },
* overrideVatManagerOptions?: { consensusMode?: boolean },
* spawn?: typeof import('child_process').spawn,
* env?: Record<string, string | undefined>
* }} runtimeOptions
Expand All @@ -147,6 +148,7 @@ export async function makeSwingsetController(
slogFile,
spawn = ambientSpawn,
warehousePolicy = {},
overrideVatManagerOptions = {},
} = runtimeOptions;
if (typeof Compartment === 'undefined') {
throw Error('SES must be installed before calling makeSwingsetController');
Expand Down Expand Up @@ -306,7 +308,7 @@ export async function makeSwingsetController(
gcAndFinalize: makeGcAndFinalize(engineGC),
};

const kernelOptions = { verbose, warehousePolicy };
const kernelOptions = { verbose, warehousePolicy, overrideVatManagerOptions };
/** @type { ReturnType<typeof import('./kernel').default> } */
const kernel = buildKernel(kernelEndowments, deviceEndowments, kernelOptions);

Expand Down Expand Up @@ -421,6 +423,7 @@ export async function makeSwingsetController(
* slogCallbacks?: unknown,
* testTrackDecref?: unknown,
* warehousePolicy?: { maxVatsOnline?: number },
* overrideVatManagerOptions?: { consensusMode?: boolean },
* slogFile?: string,
* }} runtimeOptions
* @typedef { import('@agoric/swing-store-simple').KVStore } KVStore
Expand Down
2 changes: 2 additions & 0 deletions packages/SwingSet/src/kernel/kernel.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export default function buildKernel(
verbose,
defaultManagerType = 'local',
warehousePolicy,
overrideVatManagerOptions = {},
} = kernelOptions;
const logStartup = verbose ? console.debug : () => 0;

Expand Down Expand Up @@ -800,6 +801,7 @@ export default function buildKernel(
panic,
buildVatSyscallHandler,
vatAdminRootKref,
overrideVatManagerOptions,
});

const vatWarehouse = makeVatWarehouse(
Expand Down
3 changes: 3 additions & 0 deletions packages/SwingSet/src/kernel/loadVat.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export function makeVatRootObjectSlot() {

export function makeVatLoader(stuff) {
const {
overrideVatManagerOptions = {},
vatManagerFactory,
kernelSlog,
makeVatConsole,
Expand Down Expand Up @@ -241,6 +242,7 @@ export function makeVatLoader(stuff) {
virtualObjectCacheSize,
useTranscript,
name,
...overrideVatManagerOptions,
};

const vatSyscallHandler = buildVatSyscallHandler(vatID, translators);
Expand All @@ -262,6 +264,7 @@ export function makeVatLoader(stuff) {
enableSetup: true,
managerType: 'local',
useTranscript: true,
...overrideVatManagerOptions,
};
const translators = makeVatTranslators(vatID, kernelKeeper);
const vatSyscallHandler = buildVatSyscallHandler(vatID, translators);
Expand Down
1 change: 1 addition & 0 deletions packages/SwingSet/src/kernel/vatManager/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export function makeVatManagerFactory({

function validateManagerOptions(managerOptions) {
assertKnownOptions(managerOptions, [
'consensusMode',
'enablePipelining',
'managerType',
'gcEveryCrank',
Expand Down
6 changes: 5 additions & 1 deletion packages/SwingSet/src/kernel/vatManager/manager-local.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
makeSupervisorDispatch,
makeMeteredDispatch,
makeSupervisorSyscall,
makeVatConsole,
} from './supervisor-helper.js';

export function makeLocalVatManagerFactory(tools) {
Expand Down Expand Up @@ -91,6 +92,7 @@ export function makeLocalVatManagerFactory(tools) {
vatSyscallHandler,
) {
const {
consensusMode,
metered = false,
enableDisavow = false,
enableSetup = false,
Expand Down Expand Up @@ -148,7 +150,9 @@ export function makeLocalVatManagerFactory(tools) {
const endowments = harden({
...vatEndowments,
...ls.vatGlobals,
console: vatConsole,
console: makeVatConsole(vatConsole, (logger, args) => {
consensusMode || logger(...args);
}),
assert,
});
const inescapableTransforms = [];
Expand Down
2 changes: 2 additions & 0 deletions packages/SwingSet/src/kernel/vatManager/manager-nodeworker.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export function makeNodeWorkerVatManagerFactory(tools) {

function createFromBundle(vatID, bundle, managerOptions, vatSyscallHandler) {
const {
consensusMode,
vatParameters,
virtualObjectCacheSize,
enableDisavow,
Expand Down Expand Up @@ -116,6 +117,7 @@ export function makeNodeWorkerVatManagerFactory(tools) {
virtualObjectCacheSize,
enableDisavow,
enableVatstore,
consensusMode,
]);

function deliverToWorker(delivery) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export function makeNodeSubprocessFactory(tools) {

function createFromBundle(vatID, bundle, managerOptions, vatSyscallHandler) {
const {
consensusMode,
vatParameters,
virtualObjectCacheSize,
enableDisavow,
Expand Down Expand Up @@ -111,6 +112,7 @@ export function makeNodeSubprocessFactory(tools) {
virtualObjectCacheSize,
enableDisavow,
enableVatstore,
consensusMode,
]);

function shutdown() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export function makeXsSubprocessFactory({
) {
parentLog(vatID, 'createFromBundle', { vatID });
const {
consensusMode,
vatParameters,
virtualObjectCacheSize,
enableDisavow,
Expand All @@ -60,6 +61,7 @@ export function makeXsSubprocessFactory({
metered,
compareSyscalls,
useTranscript,
vatConsole,
} = managerOptions;
assert(
!managerOptions.enableSetup,
Expand All @@ -86,9 +88,9 @@ export function makeXsSubprocessFactory({
return mk.syscallFromWorker(vso);
}
case 'console': {
const [level, tag, ...rest] = args;
if (typeof level === 'string' && level in console) {
console[level](tag, ...rest);
const [level, ...rest] = args;
if (typeof level === 'string' && level in vatConsole) {
vatConsole[level](...rest);
} else {
console.error('bad console level', level);
}
Expand Down Expand Up @@ -142,6 +144,7 @@ export function makeXsSubprocessFactory({
virtualObjectCacheSize,
enableDisavow,
enableVatstore,
consensusMode,
gcEveryCrank,
]);
if (bundleReply[0] === 'dispatchReady') {
Expand All @@ -160,7 +163,7 @@ export function makeXsSubprocessFactory({
parentLog(vatID, `sending delivery`, delivery);
let result;
try {
result = await issueTagged(['deliver', delivery]);
result = await issueTagged(['deliver', delivery, consensusMode]);
} catch (err) {
parentLog('issueTagged error:', err.code, err.message);
let message;
Expand Down
38 changes: 37 additions & 1 deletion packages/SwingSet/src/kernel/vatManager/supervisor-helper.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @ts-check
import { assert } from '@agoric/assert';
import { assert, details as X } from '@agoric/assert';
import {
insistVatSyscallObject,
insistVatSyscallResult,
Expand Down Expand Up @@ -191,3 +191,39 @@ function makeSupervisorSyscall(syscallToManager, workerCanBlock) {

harden(makeSupervisorSyscall);
export { makeSupervisorSyscall };

/**
* Create a vat console, or a no-op if consensusMode is true.
*
* TODO: consider other methods per SES VirtualConsole.
* See https://github.com/Agoric/agoric-sdk/issues/2146
*
* @param {Record<'debug' | 'log' | 'info' | 'warn' | 'error', (...args: any[]) => void>} logger
* the backing log method
* @param {(logger: any, args: any[]) => void} [wrapper]
*/
function makeVatConsole(logger, wrapper) {
assert.typeof(
wrapper,
'function',
X`Invalid VatConsole wrapper value ${wrapper}`,
);
const cons = Object.fromEntries(
['debug', 'log', 'info', 'warn', 'error'].map(level => {
const backingLog = logger[level];

return [
level,
(...args) => {
// Wrap the actual backing log message, in case there is logic to impose.
wrapper(backingLog, args);
},
];
}),
);

return harden(cons);
}

harden(makeVatConsole);
export { makeVatConsole };
18 changes: 8 additions & 10 deletions packages/SwingSet/src/kernel/vatManager/supervisor-nodeworker.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { makeLiveSlots } from '../liveSlots.js';
import {
makeSupervisorDispatch,
makeSupervisorSyscall,
makeVatConsole,
} from './supervisor-helper.js';

assert(parentPort, 'parentPort somehow missing, am I not a Worker?');
Expand All @@ -28,15 +29,6 @@ function workerLog(first, ...args) {

workerLog(`supervisor started`);

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);
}

function sendUplink(msg) {
assert(msg instanceof Array, X`msg must be an Array`);
assert(parentPort, 'parentPort somehow missing, am I not a Worker?');
Expand All @@ -58,6 +50,7 @@ parentPort.on('message', ([type, ...margs]) => {
virtualObjectCacheSize,
enableDisavow,
enableVatstore,
consensusMode,
] = margs;

function testLog(...args) {
Expand Down Expand Up @@ -96,7 +89,12 @@ parentPort.on('message', ([type, ...margs]) => {

const endowments = {
...ls.vatGlobals,
console: makeConsole(`SwingSet:vatWorker`),
console: makeVatConsole(
anylogger(`SwingSet:vat:${vatID}`),
(logger, args) => {
consensusMode || logger(...args);
},
),
assert,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { makeLiveSlots } from '../liveSlots.js';
import {
makeSupervisorDispatch,
makeSupervisorSyscall,
makeVatConsole,
} from './supervisor-helper.js';

// eslint-disable-next-line no-unused-vars
Expand All @@ -32,15 +33,6 @@ function workerLog(first, ...args) {

workerLog(`supervisor started`);

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);
}

let dispatch;

const toParent = arrayEncoderStream();
Expand Down Expand Up @@ -76,6 +68,7 @@ fromParent.on('data', ([type, ...margs]) => {
virtualObjectCacheSize,
enableDisavow,
enableVatstore,
consensusMode,
] = margs;

function testLog(...args) {
Expand Down Expand Up @@ -110,9 +103,15 @@ fromParent.on('data', ([type, ...margs]) => {
gcTools,
);

// Enable or disable the console accordingly.
const endowments = {
...ls.vatGlobals,
console: makeConsole(`SwingSet:vatWorker`),
console: makeVatConsole(
anylogger(`SwingSet:vat:${vatID}`),
(logger, args) => {
consensusMode || logger(...args);
},
),
assert,
};

Expand Down
Loading

0 comments on commit ae5330a

Please sign in to comment.