From 9a35baf55277dd1c149c0f5e4f76a1a51588b767 Mon Sep 17 00:00:00 2001 From: dtfiedler Date: Tue, 9 Jan 2024 18:51:23 -0700 Subject: [PATCH] fix(epoch): fix function for calculating epochs --- src/actions/write/evolveState.ts | 10 ++++------ src/actions/write/tick.test.ts | 2 +- src/observers.test.ts | 6 +++--- src/observers.ts | 3 ++- 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/actions/write/evolveState.ts b/src/actions/write/evolveState.ts index b50a65ba..9e401680 100644 --- a/src/actions/write/evolveState.ts +++ b/src/actions/write/evolveState.ts @@ -15,13 +15,11 @@ export const evolveState = async ( throw new ContractError(NON_CONTRACT_OWNER_MESSAGE); } - const epochEndHeight = - +SmartWeave.block.height + DEFAULT_EPOCH_BLOCK_LENGTH - 1; - + const epochStartHeight = +SmartWeave.block.height; state.distributions = { - epochZeroStartHeight: +SmartWeave.block.height, - epochStartHeight: +SmartWeave.block.height, - epochEndHeight, + epochZeroStartHeight: epochStartHeight, + epochStartHeight: epochStartHeight, + epochEndHeight: epochStartHeight + DEFAULT_EPOCH_BLOCK_LENGTH - 1, gateways: {}, observers: {}, }; diff --git a/src/actions/write/tick.test.ts b/src/actions/write/tick.test.ts index a9967cb0..7eeb5b59 100644 --- a/src/actions/write/tick.test.ts +++ b/src/actions/write/tick.test.ts @@ -861,7 +861,7 @@ describe('tickRewardDistribution', () => { TALLY_PERIOD_BLOCKS + 1, Number.MAX_SAFE_INTEGER, ])( - 'should not distribute rewards if the current block height is equal to the last epoch end height + the required tallying period', + 'should not distribute rewards if the current block height is equal to the last epoch end height + %s blocks', async (blockHeight) => { const initialState: IOState = { ...getBaselineState(), diff --git a/src/observers.test.ts b/src/observers.test.ts index 27294f51..93df298b 100644 --- a/src/observers.test.ts +++ b/src/observers.test.ts @@ -281,12 +281,12 @@ describe('isGatewayEligibleForDistribution', () => { describe('getEpochBoundariesForHeight', () => { it.each([ - [1, 1, 1, 2, 2], // --> this is a weird case + [1, 1, 1, 1, 1], [19, 2, 100, 2, 101], [34, 0, Number.MAX_SAFE_INTEGER, 0, Number.MAX_SAFE_INTEGER - 1], - // [5, 0, undefined, 0, DEFAULT_EPOCH_BLOCK_LENGTH], + [1340134, 1339961, 50, 1340111, 1340160], ])( - 'should, given current height of %d, zero block height of %d and epoch length of %d return the epoch start of %d and epoch end %d for block height %d', + 'should for current height of %d, zero block height of %d and epoch length of %d return the epoch start of %d and epoch end %d', ( currentHeight, zeroHeight, diff --git a/src/observers.ts b/src/observers.ts index 5d8fc34f..c21bfae4 100644 --- a/src/observers.ts +++ b/src/observers.ts @@ -28,7 +28,8 @@ export function getEpochBoundariesForHeight({ epochEndHeight: BlockHeight; } { const epochIndexForCurrentBlockHeight = Math.floor( - currentBlockHeight.valueOf() / epochBlockLength.valueOf(), + (currentBlockHeight.valueOf() - epochZeroStartHeight.valueOf()) / + epochBlockLength.valueOf(), ); const epochStartHeight = epochZeroStartHeight.valueOf() +