Skip to content

Commit

Permalink
fix: persist the savedChainSends for better recovery
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Aug 27, 2020
1 parent 50b1d7e commit d8f0eb6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 29 deletions.
39 changes: 20 additions & 19 deletions packages/cosmic-swingset/lib/block-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export default function makeBlockManager({
let currentActions = [];
let decohered;

async function blockManager(action) {
async function blockManager(action, savedChainSends) {
if (decohered) {
throw decohered;
}
Expand All @@ -84,6 +84,7 @@ export default function makeBlockManager({
`Committed height ${action.blockHeight} does not match computed height ${computedHeight}`,
);
}
flushChainSends(false);
break;
}

Expand Down Expand Up @@ -147,30 +148,30 @@ export default function makeBlockManager({
// eslint-disable-next-line no-await-in-loop
await kernelPerformAction(a);
}
}

// Always commit all the keeper state live.
const start = Date.now();
saveChainState();
const chainTime = Date.now() - start;
// We write out our on-chain state as a number of chainSends.
const start = Date.now();
saveChainState();
const chainTime = Date.now() - start;

// Advance our saved state variables.
savedActions = currentActions;
computedHeight = action.blockHeight;
// Advance our saved state variables.
savedActions = currentActions;
computedHeight = action.blockHeight;

// Save the kernel's computed state so that we can recover if we ever
// reset before Cosmos SDK commit.
const start2 = Date.now();
saveOutsideState(computedHeight, savedActions);
savedHeight = computedHeight;
// Save the kernel's computed state so that we can recover if we ever
// reset before Cosmos SDK commit.
const start2 = Date.now();
saveOutsideState(computedHeight, savedActions, savedChainSends);
savedHeight = computedHeight;

const saveTime = Date.now() - start2;
const saveTime = Date.now() - start2;

log.debug(
`wrote SwingSet checkpoint [run=${runTime}ms, chainSave=${chainTime}ms, outsideSave=${saveTime}ms]`,
);
currentActions = [];
log.debug(
`wrote SwingSet checkpoint [run=${runTime}ms, chainSave=${chainTime}ms, outsideSave=${saveTime}ms]`,
);
}

currentActions = [];
break;
}

Expand Down
11 changes: 7 additions & 4 deletions packages/cosmic-swingset/lib/chain-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ export default async function main(progname, args, { path, env, agcc }) {
stateDBDir,
mailboxStorage,
doOutboundBridge,
flushChainSends,
vatsdir,
argv,
);
Expand All @@ -230,15 +229,19 @@ export default async function main(progname, args, { path, env, agcc }) {
}

if (!blockManager) {
const fns = await launchAndInitializeSwingSet();
blockManager = makeBlockManager(fns);
const {
savedChainSends: scs,
...fns
} = await launchAndInitializeSwingSet();
savedChainSends = scs;
blockManager = makeBlockManager({ ...fns, flushChainSends });
}

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

return blockManager(action);
return blockManager(action, savedChainSends);
}
}
11 changes: 5 additions & 6 deletions packages/cosmic-swingset/lib/launch-chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ export async function launch(
kernelStateDBDir,
mailboxStorage,
doOutboundBridge,
flushChainSends,
vatsDir,
argv,
debugName = undefined,
Expand Down Expand Up @@ -83,10 +82,10 @@ export async function launch(
mailboxStorage.commit();
}

function saveOutsideState(savedHeight, savedActions) {
function saveOutsideState(savedHeight, savedActions, savedChainSends) {
storage.set(
SWING_STORE_META_KEY,
JSON.stringify([savedHeight, savedActions]),
JSON.stringify([savedHeight, savedActions, savedChainSends]),
);
commit();
}
Expand Down Expand Up @@ -122,18 +121,18 @@ export async function launch(
await controller.run();
}

const [savedHeight, savedActions] = JSON.parse(
storage.get(SWING_STORE_META_KEY) || '[0, []]',
const [savedHeight, savedActions, savedChainSends] = JSON.parse(
storage.get(SWING_STORE_META_KEY) || '[0, [], []]',
);
return {
deliverInbound,
doBridgeInbound,
// bridgeOutbound,
flushChainSends,
beginBlock,
saveChainState,
saveOutsideState,
savedHeight,
savedActions,
savedChainSends,
};
}

0 comments on commit d8f0eb6

Please sign in to comment.