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(cosmic-swingset): execute during voting time #6741

Draft
wants to merge 15 commits into
base: master
Choose a base branch
from
Draft
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
23 changes: 13 additions & 10 deletions golang/cosmos/x/swingset/types/default-params.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ import (
// experience if they don't.

const (
BeansPerFeeUnit = "feeUnit"
BeansPerInboundTx = "inboundTx"
BeansPerBlockComputeLimit = "blockComputeLimit"
BeansPerMessage = "message"
BeansPerMessageByte = "messageByte"
BeansPerMinFeeDebit = "minFeeDebit"
BeansPerVatCreation = "vatCreation"
BeansPerXsnapComputron = "xsnapComputron"
BeansPerFeeUnit = "feeUnit"
BeansPerInboundTx = "inboundTx"
BeansPerBlockComputeLimit = "blockComputeLimit"
BeansPerInterBlockComputeLimit = "interBlockComputeLimit"
BeansPerMessage = "message"
BeansPerMessageByte = "messageByte"
BeansPerMinFeeDebit = "minFeeDebit"
BeansPerVatCreation = "vatCreation"
BeansPerXsnapComputron = "xsnapComputron"

// QueueSize keys.
// Keep up-to-date with updateQueueAllowed() in packanges/cosmic-swingset/src/launch-chain.js
Expand All @@ -33,7 +34,8 @@ var (
// before starting a new block. Some analysis (#3459) suggests this leads to
// about 2/3rds utilization, based on 5 sec voting time and up to 10 sec of
// computation.
DefaultBeansPerBlockComputeLimit = sdk.NewUint(8000000).Mul(DefaultBeansPerXsnapComputron)
DefaultBeansPerBlockComputeLimit = sdk.NewUint(8000000).Mul(DefaultBeansPerXsnapComputron)
DefaultBeansPerInterBlockComputeLimit = sdk.NewUint(24000000).Mul(DefaultBeansPerXsnapComputron)
// observed: 0.385 sec
DefaultBeansPerVatCreation = sdk.NewUint(300000).Mul(DefaultBeansPerXsnapComputron)

Expand Down Expand Up @@ -65,6 +67,7 @@ var (
func DefaultBeansPerUnit() []StringBeans {
return []StringBeans{
NewStringBeans(BeansPerBlockComputeLimit, DefaultBeansPerBlockComputeLimit),
NewStringBeans(BeansPerInterBlockComputeLimit, DefaultBeansPerInterBlockComputeLimit),
NewStringBeans(BeansPerFeeUnit, DefaultBeansPerFeeUnit),
NewStringBeans(BeansPerInboundTx, DefaultBeansPerInboundTx),
NewStringBeans(BeansPerMessage, DefaultBeansPerMessage),
Expand All @@ -73,4 +76,4 @@ func DefaultBeansPerUnit() []StringBeans {
NewStringBeans(BeansPerVatCreation, DefaultBeansPerVatCreation),
NewStringBeans(BeansPerXsnapComputron, DefaultBeansPerXsnapComputron),
}
}
}
4 changes: 2 additions & 2 deletions packages/SwingSet/src/devices/bridge/device-bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ function sanitize(data) {

/**
* @typedef {object} BridgeDevice
* @property {(dstID: string, obj: any) => any} callOutbound
* @property {(handler: { inbound: (srcID: string, obj: any) => void}) => void} registerInboundHandler
* @property {(...args: any[]) => any} callOutbound
* @property {(handler: { inbound: (...args: any[]) => void}) => void} registerInboundHandler
*/

/**
Expand Down
6 changes: 6 additions & 0 deletions packages/SwingSet/src/kernel/kernel.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ function abbreviateReplacer(_, arg) {
// truncate long strings
return `${arg.slice(0, 15)}...${arg.slice(arg.length - 15)}`;
}
if (arg === undefined) {
return 'undefined';
}
if (typeof arg === 'symbol') {
return String(arg);
}
return arg;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ function abbreviateReplacer(_, arg) {
// truncate long strings
return `${arg.slice(0, 15)}...${arg.slice(arg.length - 15)}`;
}
if (arg === undefined) {
return 'undefined';
}
if (typeof arg === 'symbol') {
return String(arg);
}
return arg;
}

Expand Down
4 changes: 3 additions & 1 deletion packages/SwingSet/test/test-device-bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from '../src/index.js';

test('bridge device', async t => {
t.plan(6);
const outboundLog = [];
function outboundCallback(argv0, argv1) {
outboundLog.push(argv0);
Expand Down Expand Up @@ -116,6 +117,7 @@ test('bridge device', async t => {
});

test('bridge device can return undefined', async t => {
t.plan(2);
const outboundLog = [];
function outboundCallback(argv0, argv1) {
outboundLog.push(argv0);
Expand Down Expand Up @@ -153,5 +155,5 @@ test('bridge device can return undefined', async t => {
await c.run();

t.deepEqual(outboundLog, argv);
t.deepEqual(c.dump().log, ['outbound retval', '', 'true']);
t.deepEqual(c.dump().log, ['outbound retval', '"undefined"', 'true']);
});
1 change: 1 addition & 0 deletions packages/cosmic-swingset/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@endo/import-bundle": "^0.3.0",
"@endo/init": "^0.5.52",
"@endo/marshal": "^0.8.1",
"@endo/promise-kit": "^0.2.52",
"@iarna/toml": "^2.2.3",
"@opentelemetry/sdk-metrics": "^0.32.0",
"anylogger": "^0.21.0",
Expand Down
66 changes: 66 additions & 0 deletions packages/cosmic-swingset/src/active-guard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { makePromiseKit } from '@endo/promise-kit';

export const makeActiveGuard = () => {
/** @type {Promise<void> | undefined} */
let jobsCompleted;
let active = false;
/** @type {Array<{job: () => any, resolve: (result: any) => void}>} */
const pendingJobs = [];

const maybeProcessJobs = () => {
if (!active) {
return;
}

const jobs = pendingJobs.splice(0);
jobsCompleted = Promise.all([
jobsCompleted,
...jobs.map(({ job, resolve }) => {
const result = Promise.resolve().then(job);
resolve(result);
return result.then(
() => {},
() => {},
);
}),
]).then(() => {});
};

/** @param {() => any} job */
const whenActive = async job => {
const { resolve, promise } = makePromiseKit();
pendingJobs.push({ job, resolve });
maybeProcessJobs();
return promise;
};

/**
* @template {(...args: any[]) => any} T
* @template {boolean} [D=true]
* @param {T} fn
* @param {D} [dropResult=true]
* @returns {(...args: Parameters<T>) => (false extends D ? Promise<Awaited<ReturnType<T>>> : never) | (true extends D ? undefined : never)}
*/
const whenActiveWrap =
(fn, dropResult = true) =>
(...args) => {
const result = whenActive(() => fn(...args));
if (dropResult) {
result.catch(err => {
console.warn('Unhandled rejection for active wrapped task', err);
});
return undefined;
} else {
return result;
}
};

/** @param {boolean} newValue */
const updateActive = async newValue => {
active = newValue;
maybeProcessJobs();
return jobsCompleted;
};

return { whenActive, whenActiveWrap, updateActive };
};
42 changes: 32 additions & 10 deletions packages/cosmic-swingset/src/chain-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,24 @@ export default async function main(progname, args, { env, homedir, agcc }) {

let savedChainSends = [];

let swingsetActive = false;

const checkSwingsetActive = () => {
if (!swingsetActive) {
throw new Error(`Cannot perform send while swingset inactive`);
}
};

// Send a chain downcall, recording what we sent and received.
function chainSend(...sendArgs) {
checkSwingsetActive();
const ret = agcc.send(...sendArgs);
savedChainSends.push([sendArgs, ret]);
return ret;
}

const clearChainSends = () => {
checkSwingsetActive();
const chainSends = savedChainSends;
savedChainSends = [];
return chainSends;
Expand All @@ -208,6 +218,7 @@ export default async function main(progname, args, { env, homedir, agcc }) {
// Replay and clear the chain send queue.
// While replaying each send, insist it has the same return result.
function replayChainSends() {
checkSwingsetActive();
// Remove our queue.
const chainSends = [...savedChainSends];

Expand All @@ -227,6 +238,9 @@ export default async function main(progname, args, { env, homedir, agcc }) {
}
}

/** @type {Awaited<ReturnType<typeof launch>>['blockingSend']} */
let blockingSend;

// this storagePort changes for every single message. We define it out here
// so the 'externalStorage' object can close over the single mutable
// instance, and we update the 'portNums.storage' value each time toSwingSet is called
Expand Down Expand Up @@ -417,10 +431,10 @@ export default async function main(progname, args, { env, homedir, agcc }) {

savedChainSends = s.savedChainSends;

return s.blockingSend;
blockingSend = s.blockingSend;
return blockingSend;
}

let blockingSend;
async function toSwingSet(action, _replier) {
// console.log(`toSwingSet`, action);
if (action.vibcPort) {
Expand All @@ -441,14 +455,22 @@ export default async function main(progname, args, { env, homedir, agcc }) {
portNums.lien = action.lienPort;
}

// Ensure that initialization has completed.
blockingSend = await (blockingSend || launchAndInitializeSwingSet(action));

if (action.type === AG_COSMOS_INIT) {
// console.error('got AG_COSMOS_INIT', action);
return true;
}
swingsetActive = true;

return blockingSend(action);
return Promise.resolve(
// Ensure that initialization has completed.
blockingSend || launchAndInitializeSwingSet(action),
)
.then(doBlockingSend => {
if (action.type === AG_COSMOS_INIT) {
// console.error('got AG_COSMOS_INIT', action);
return true;
} else {
return doBlockingSend(action);
}
michaelfig marked this conversation as resolved.
Show resolved Hide resolved
})
.finally(() => {
swingsetActive = false;
});
}
}
Loading