Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Commit

Permalink
fix(epoch): fix function for calculating epochs
Browse files Browse the repository at this point in the history
  • Loading branch information
dtfiedler committed Jan 10, 2024
1 parent faa622e commit 9a35baf
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 11 deletions.
10 changes: 4 additions & 6 deletions src/actions/write/evolveState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {},
};
Expand Down
2 changes: 1 addition & 1 deletion src/actions/write/tick.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
6 changes: 3 additions & 3 deletions src/observers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion src/observers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() +
Expand Down

0 comments on commit 9a35baf

Please sign in to comment.