Skip to content

Commit

Permalink
fix: make the fake-chain better
Browse files Browse the repository at this point in the history
* blocks are produced even when there is no input (in case a timer needs to fire)
* block height increments by 1 instead of 2
* blockTime is scaled to number of seconds
*
  • Loading branch information
michaelfig committed Mar 22, 2020
1 parent 8cf2482 commit b4e5b02
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/agoric-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"@agoric/make-promise": "^0.0.1",
"anylogger": "^0.21.0",
"chalk": "^2.4.2",
"esm": "^3.2.5",
"esm": "^3.2.25",
"minimist": "^1.2.0",
"ws": "^7.2.0",
"yarn": "^1.22.0"
Expand Down
20 changes: 11 additions & 9 deletions packages/cosmic-swingset/lib/ag-solo/fake-chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import makeBlockManager from '../block-manager';
const log = anylogger('fake-chain');

const PRETEND_BLOCK_DELAY = 5;
const scaleBlockTime = ms => Math.floor(ms / 1000);

async function readMap(file) {
let content;
Expand Down Expand Up @@ -49,11 +50,15 @@ export async function connectToFakeChain(basedir, GCI, role, delay, inbound) {
let blockTime =
savedActions.length > 0
? savedActions[0].blockTime
: Math.floor(Date.now() / 1000);
: scaleBlockTime(Date.now());
let intoChain = [];
let thisBlock = [];
let nextBlockTimeout = 0;

const maximumDelay = (delay || PRETEND_BLOCK_DELAY) * 1000;

async function simulateBlock() {
clearTimeout(nextBlockTimeout);
const actualStart = Date.now();
// Gather up the new messages into the latest block.
thisBlock.push(...intoChain);
Expand Down Expand Up @@ -81,15 +86,12 @@ export async function connectToFakeChain(basedir, GCI, role, delay, inbound) {
await blockManager({ type: 'COMMIT_BLOCK', blockHeight, blockTime });
await writeMap(mailboxFile, mailboxStorage);
thisBlock = [];
blockTime = blockTime + Date.now() - actualStart;
blockHeight += 1;
blockTime += scaleBlockTime(Date.now() - actualStart);
} catch (e) {
log.error(`error fake processing`, e);
}

if (delay) {
setTimeout(simulateBlock, delay * 1000);
}
nextBlockTimeout = setTimeout(simulateBlock, maximumDelay);

// TODO: maybe add latency to the inbound messages.
const mailboxJSON = mailboxStorage.get(`mailbox.${bootAddress}`);
Expand All @@ -107,8 +109,8 @@ export async function connectToFakeChain(basedir, GCI, role, delay, inbound) {
await simulateBlock();
}
}
if (delay) {
setTimeout(simulateBlock, delay * 1000);
}

// Start the first pretend block.
nextBlockTimeout = setTimeout(simulateBlock, maximumDelay);
return deliver;
}

0 comments on commit b4e5b02

Please sign in to comment.