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

Replace epochTime with genesis block timestamp - Closes #5280 #5430

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
1ebd6a6
Change epoch to be genesis block time
pablitovicente Jun 11, 2020
3ba2ce0
Refactor some methods in Slots and adjust rest of libraries to them
pablitovicente Jun 11, 2020
1543e5d
Remove references to epoch and update libraries and framework accordi…
pablitovicente Jun 12, 2020
da7379e
Fix tests for lisk constants
pablitovicente Jun 12, 2020
2755438
Rename variables based on PR discussion
pablitovicente Jun 12, 2020
306d894
Make getSlotTime() to return actual time instead of only offset
pablitovicente Jun 12, 2020
4125b2a
Make getSlotNumber check for missing timestamp more strict
pablitovicente Jun 15, 2020
e8ec305
Fix slots test in lisk-chain
pablitovicente Jun 15, 2020
bf01950
Merge branch 'development' into 5280-replace_epochtime_with_genesis_b…
pablitovicente Jun 15, 2020
7531cbe
Fix all elements tests after replacing epoch with genesis block time …
pablitovicente Jun 15, 2020
7263a02
Fix framework integration tests after replacing epoch with genesis bl…
pablitovicente Jun 15, 2020
8a2f346
Remove reference to epoch from test utils
pablitovicente Jun 15, 2020
f3c9919
Fix unit tests in frameworks and fix bugs in forger.ts
pablitovicente Jun 15, 2020
55cb682
Remove remaining epochTime references from unit tests
pablitovicente Jun 15, 2020
05e695f
Undo jest config snapshot changes
pablitovicente Jun 15, 2020
db07ae4
Change getSlotTime to use seconds instead of milliseconds
pablitovicente Jun 16, 2020
a574f59
Change genesis time to be unix timestamp instead of date in Slots
pablitovicente Jun 16, 2020
afb3c81
Merge branch 'development' into 5280-replace_epochtime_with_genesis_b…
pablitovicente Jun 16, 2020
44f5651
Fix unit tests for forger after changing Slots to use only seconds as…
pablitovicente Jun 16, 2020
46c6625
Fix integration test helpers and fix tests after making Slots work wi…
pablitovicente Jun 16, 2020
1205f27
Merge branch 'development' into 5280-replace_epochtime_with_genesis_b…
pablitovicente Jun 16, 2020
63f7fb5
Fix functional tests setup
pablitovicente Jun 16, 2020
e99a5e8
:recycle: Add database clean before running applicatoin for functiona…
shuse2 Jun 16, 2020
4bdf077
Merge branch 'development' into 5280-replace_epochtime_with_genesis_b…
shuse2 Jun 16, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion elements/lisk-bft/src/bft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export class BFT extends EventEmitter {
// Current time since Lisk Epoch
const receivedBlock = {
...blockHeader,
receivedAt: this._chain.slots.getEpochTime(),
receivedAt: this._chain.slots.timeSinceGenesis(),
};

/* Cases are numbered following LIP-0014 Fork choice rule.
Expand Down
2 changes: 1 addition & 1 deletion elements/lisk-bft/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export interface Chain {
slotNumber: number,
receivedAt: number | undefined,
) => boolean;
readonly getEpochTime: (time?: number) => number;
readonly timeSinceGenesis: (time?: number) => number;
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('FinalityManager', () => {
slots: {
getSlotNumber: jest.Mock;
isWithinTimeslot: jest.Mock;
getEpochTime: jest.Mock;
timeSinceGenesis: jest.Mock;
};
};
let dposStub: {
Expand All @@ -74,7 +74,7 @@ describe('FinalityManager', () => {
slots: {
getSlotNumber: jest.fn(),
isWithinTimeslot: jest.fn(),
getEpochTime: jest.fn(),
timeSinceGenesis: jest.fn(),
},
};
dposStub = {
Expand Down Expand Up @@ -158,7 +158,7 @@ describe('FinalityManager', () => {
slots: {
getSlotNumber: jest.fn(),
isWithinTimeslot: jest.fn(),
getEpochTime: jest.fn(),
timeSinceGenesis: jest.fn(),
},
};
dposStub = {
Expand Down Expand Up @@ -278,7 +278,7 @@ describe('FinalityManager', () => {
slots: {
getSlotNumber: jest.fn(),
isWithinTimeslot: jest.fn(),
getEpochTime: jest.fn(),
timeSinceGenesis: jest.fn(),
},
};
dposStub = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import forkChoiceSpecs = require('../bft_specs/bft_fork_choice_rules.json');

const constants = {
ACTIVE_DELEGATES: 101,
EPOCH_TIME: '2016-05-24T17:00:00.000Z',
BLOCK_TIME: 10,
};

Expand All @@ -45,7 +44,7 @@ describe('bft', () => {

beforeEach(() => {
const slots = new Slots({
epochTime: constants.EPOCH_TIME,
genesisBlockTimestamp: 0,
interval: constants.BLOCK_TIME,
});
chainStub = {
Expand Down Expand Up @@ -77,22 +76,24 @@ describe('bft', () => {
forkChoiceSpecs.testCases.forEach((testCase: any) => {
describe(testCase.description, () => {
it('should have accurate fork status', () => {
const epochTime = testCase.config
? testCase.config.epochTime
: forkChoiceSpecs.config.epochTime;
const genesisBlockTimestamp = 0;
const interval = testCase.config
? testCase.config.blockInterval
: forkChoiceSpecs.config.blockInterval;
const lastBlock = testCase.config
? testCase.config.lastBlock
: forkChoiceSpecs.config.lastBlock;

(chainStub.slots as any)._epochTime = new Date(epochTime);
(chainStub.slots as any)._genesisTime = new Date(
genesisBlockTimestamp,
);
(chainStub.slots as any)._interval = interval;

Date.now = jest.fn(
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
() => epochTime + testCase.input.receivedBlock.receivedAt * 1000,
() =>
genesisBlockTimestamp +
testCase.input.receivedBlock.receivedAt * 1000,
);

const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('FinalityManager', () => {
slots: {
getSlotNumber: jest.Mock;
isWithinTimeslot: jest.Mock;
getEpochTime: jest.Mock;
timeSinceGenesis: jest.Mock;
};
};
let dposStub: {
Expand All @@ -46,7 +46,7 @@ describe('FinalityManager', () => {
slots: {
getSlotNumber: jest.fn(),
isWithinTimeslot: jest.fn(),
getEpochTime: jest.fn(),
timeSinceGenesis: jest.fn(),
},
};
dposStub = {
Expand Down
4 changes: 2 additions & 2 deletions elements/lisk-bft/test/unit/bft.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe('bft', () => {
slots: {
getSlotNumber: jest.Mock;
isWithinTimeslot: jest.Mock;
getEpochTime: jest.Mock;
timeSinceGenesis: jest.Mock;
};
};
let dposStub: {
Expand All @@ -89,7 +89,7 @@ describe('bft', () => {
slots: {
getSlotNumber: jest.fn(),
isWithinTimeslot: jest.fn(),
getEpochTime: jest.fn(),
timeSinceGenesis: jest.fn(),
},
};

Expand Down
4 changes: 2 additions & 2 deletions elements/lisk-bft/test/unit/finality_manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('finality_manager', () => {
slots: {
getSlotNumber: jest.Mock;
isWithinTimeslot: jest.Mock;
getEpochTime: jest.Mock;
timeSinceGenesis: jest.Mock;
};
};
let dposStub: {
Expand All @@ -69,7 +69,7 @@ describe('finality_manager', () => {
slots: {
getSlotNumber: jest.fn(),
isWithinTimeslot: jest.fn(),
getEpochTime: jest.fn(),
timeSinceGenesis: jest.fn(),
},
};

Expand Down
32 changes: 16 additions & 16 deletions elements/lisk-bft/test/unit/fork_choice_rule.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,30 @@ import {
} from '../../src/fork_choice_rule';
import { BlockHeader } from '../../src/types';

const EPOCH_TIME = new Date(Date.UTC(2016, 4, 24, 17, 0, 0, 0)).toISOString();
const GENESIS_BLOCK_TIME_STAMP =
new Date(Date.UTC(2016, 4, 24, 17, 0, 0, 0)).getTime() / 1000;
const BLOCK_TIME = 10;

const createBlock = (data?: Partial<BlockHeader>): BlockHeader =>
({
height: data?.height ?? 0,
timestamp: data?.timestamp ?? 0,
version: 2,
id: data?.id ?? Buffer.from('id'),
generatorPublicKey: Buffer.from('generator'),
previousBlockID: data?.previousBlockID ?? Buffer.from('previoud block'),
receivedAt: data?.receivedAt ?? 0,
asset: {
maxHeightPrevoted: data?.asset?.maxHeightPrevoted ?? 0,
maxHeightPreviouslyForged: data?.asset?.maxHeightPreviouslyForged ?? 0,
},
});
const createBlock = (data?: Partial<BlockHeader>): BlockHeader => ({
height: data?.height ?? 0,
timestamp: data?.timestamp ?? 0,
version: 2,
id: data?.id ?? Buffer.from('id'),
generatorPublicKey: Buffer.from('generator'),
previousBlockID: data?.previousBlockID ?? Buffer.from('previoud block'),
receivedAt: data?.receivedAt ?? 0,
asset: {
maxHeightPrevoted: data?.asset?.maxHeightPrevoted ?? 0,
maxHeightPreviouslyForged: data?.asset?.maxHeightPreviouslyForged ?? 0,
},
});

describe('Fork Choice Rule', () => {
let slots: Slots;

beforeEach(() => {
slots = new Slots({
epochTime: EPOCH_TIME,
genesisBlockTimestamp: GENESIS_BLOCK_TIME_STAMP,
interval: BLOCK_TIME,
});
});
Expand Down
9 changes: 4 additions & 5 deletions elements/lisk-chain/src/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ interface ChainConstructor {
default: object;
};
// Constants
readonly epochTime: string;
readonly blockTime: number;
readonly networkIdentifier: Buffer;
readonly maxPayloadLength: number;
Expand Down Expand Up @@ -119,7 +118,6 @@ export class Chain {
private readonly _genesisBlock: Block;
private readonly constants: {
readonly stateBlockSize: number;
readonly epochTime: string;
readonly blockTime: number;
readonly maxPayloadLength: number;
};
Expand All @@ -134,7 +132,6 @@ export class Chain {
accountAsset,
registeredTransactions,
// Constants
epochTime,
blockTime,
networkIdentifier,
maxPayloadLength,
Expand Down Expand Up @@ -183,7 +180,10 @@ export class Chain {
this._lastBlock = genesisBlock;
this._networkIdentifier = networkIdentifier;
this._genesisBlock = genesisBlock;
this.slots = new Slots({ epochTime, interval: blockTime });
this.slots = new Slots({
genesisBlockTimestamp: genesisBlock.header.timestamp,
interval: blockTime,
});
this.blockRewardArgs = {
distance: rewardDistance,
rewardOffset,
Expand All @@ -200,7 +200,6 @@ export class Chain {
};
this.constants = {
stateBlockSize,
epochTime,
blockTime,
maxPayloadLength,
};
Expand Down
32 changes: 15 additions & 17 deletions elements/lisk-chain/src/slots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,45 +13,43 @@
*/

interface SlotsInput {
readonly epochTime: string;
readonly genesisBlockTimestamp: number;
readonly interval: number;
}

const SEC_IN_MS = 1000;

export class Slots {
private readonly _epochTime: Date;
private readonly _genesisTime: number;
private readonly _interval: number;

public constructor({ epochTime, interval }: SlotsInput) {
this._epochTime = new Date(epochTime);
public constructor({ genesisBlockTimestamp, interval }: SlotsInput) {
this._genesisTime = genesisBlockTimestamp;
this._interval = interval;
}

public getEpochTime(): number {
return Math.floor((Date.now() - this._epochTime.getTime()) / SEC_IN_MS);
public timeSinceGenesis(): number {
return Math.floor((Date.now() - this._genesisTime * SEC_IN_MS) / SEC_IN_MS);
}

public blockTime(): number {
return this._interval;
}

public getRealTime(time: number): number {
return (
Math.floor(this._epochTime.getTime() / SEC_IN_MS) * SEC_IN_MS +
time * SEC_IN_MS
public getSlotNumber(timeStamp?: number): number {
const elapsedTime = Math.floor(
((timeStamp !== undefined ? timeStamp * SEC_IN_MS : Date.now()) -
this._genesisTime * SEC_IN_MS) /
SEC_IN_MS,
);
}

public getSlotNumber(epochTime?: number): number {
const parsedEpochTime =
epochTime === undefined ? this.getEpochTime() : epochTime;

return Math.floor(parsedEpochTime / this._interval);
return Math.floor(elapsedTime / this._interval);
}

public getSlotTime(slot: number): number {
return slot * this._interval;
const slotGensisTimeOffset = slot * this._interval;

return this._genesisTime + slotGensisTimeOffset;
}

public getNextSlot(): number {
Expand Down
Loading