Skip to content

Commit

Permalink
refactor: add constants + comments describing save boundaries PE-5141
Browse files Browse the repository at this point in the history
We avoid saving reports around the start and end of the epoch for a
combo of chain stability and height consistency (we want to make sure
all the heights are in the same epoch). This commit adds constants and
comments that describe some the reasoning behind that. Note that the
current save boundaries are conservative and may be adjusted in the
future.
  • Loading branch information
djwhitt committed Dec 1, 2023
1 parent b205c05 commit aa99bec
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,15 @@ export const prescribedObserversSource =
})
: undefined;

// Wait for chain stability before saving reports
const START_HEIGHT_START_OFFSET = MAX_FORK_DEPTH;

// Ensure there is enough time to save the report at the end of the epoch. We
// use 2 * MAX_FORK_DEPTH because it allows MAX_FORK_DEPTH blocks (somewhat
// arbitrary, but pleasingly symmetric) before we stop attempting to save
// altogether for consistency reasons at the end of the epoch.
const START_HEIGHT_END_OFFSET = 2 * MAX_FORK_DEPTH;

export async function updateAndSaveCurrentReport() {
try {
log.info('Generating report...');
Expand Down Expand Up @@ -286,8 +295,11 @@ export async function updateAndSaveCurrentReport() {
});
const saveAfterHeight =
report.epochStartHeight +
50 +
(entropy.readUInt32BE(0) % (EPOCH_BLOCK_LENGTH - 150));
START_HEIGHT_START_OFFSET +
(entropy.readUInt32BE(0) %
(EPOCH_BLOCK_LENGTH -
START_HEIGHT_START_OFFSET -
START_HEIGHT_END_OFFSET));

const currentHeight = await chainSource.getHeight();

Expand Down

0 comments on commit aa99bec

Please sign in to comment.