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

Commit

Permalink
Merge pull request #6748 from LiskHQ/6669-init-validators-module
Browse files Browse the repository at this point in the history
 Create module class, constants, schemas for validators module - Closes #6669
  • Loading branch information
shuse2 authored Sep 9, 2021
2 parents 3468e44 + 1030109 commit 6100421
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import { BaseAPI } from '../base_api';
import { ImmutableAPIContext } from '../../node/state_machine';

export class ValidatorAPI extends BaseAPI {
export class ValidatorsAPI extends BaseAPI {
// eslint-disable-next-line @typescript-eslint/require-await
public async getGenerator(_apiContext: ImmutableAPIContext, _timestamp: number): Promise<Buffer> {
return Buffer.alloc(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,9 @@
* Removal or modification of this copyright notice is prohibited.
*/

import { BaseModule } from '..';
import { ValidatorAPI } from './api';
import { ValidatorEndpoint } from './endpoint';

export class ValidatorModule extends BaseModule {
public id = 10;
public name = 'validator';
public api = new ValidatorAPI(this.id);
public endpoint = new ValidatorEndpoint(this.id);
}
export const MODULE_ID_VALIDATORS = 11; // TBD
export const STORE_PREFIX_VALIDATORS_DATA = 0x0000;
export const STORE_PREFIX_GENERATOR_LIST = 0x4000;
export const STORE_PREFIX_BLS_KEYS = 0x8000;
export const STORE_PREFIX_GENESIS_DATA = 0xc000;
export const INVALID_BLS_KEY = Buffer.alloc(48);
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@

import { BaseEndpoint } from '../base_endpoint';

export class ValidatorEndpoint extends BaseEndpoint {}
export class ValidatorsEndpoint extends BaseEndpoint {}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
* Removal or modification of this copyright notice is prohibited.
*/

export { ValidatorModule } from './module';
export type { ValidatorAPI } from './api';
export { ValidatorsModule } from './module';
export type { ValidatorsAPI } from './api';
47 changes: 47 additions & 0 deletions framework/src/modules/validators/module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright © 2021 Lisk Foundation
*
* See the LICENSE file at the top-level directory of this distribution
* for licensing information.
*
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation,
* no part of this software, including this file, may be copied, modified,
* propagated, or distributed except according to the terms contained in the
* LICENSE file.
*
* Removal or modification of this copyright notice is prohibited.
*/

import { BaseModule, ModuleInitArgs } from '../base_module';
import { MODULE_ID_VALIDATORS } from './constants';
import { GenesisBlockExecuteContext } from '../../node/state_machine';
import { ValidatorsAPI } from './api';
import { ValidatorsEndpoint } from './endpoint';

export class ValidatorsModule extends BaseModule {
public id = MODULE_ID_VALIDATORS;
public name = 'validators';
public api = new ValidatorsAPI(this.id);
public endpoint = new ValidatorsEndpoint(this.id);
private _blockTime!: number;

// eslint-disable-next-line @typescript-eslint/require-await
public async init(args: ModuleInitArgs): Promise<void> {
const { moduleConfig } = args;
const { blockTime } = moduleConfig;

if (!blockTime || typeof blockTime !== 'number') {
throw new Error('BlockTime must be a number.');
}
if (blockTime < 1) {
throw new Error('Block time cannot be less than 1.');
}
this._blockTime = blockTime;
}

// eslint-disable-next-line @typescript-eslint/require-await
public async afterGenesisBlockExecute(_context: GenesisBlockExecuteContext): Promise<void> {
// eslint-disable-next-line no-console
console.log(this._blockTime);
}
}
64 changes: 64 additions & 0 deletions framework/src/modules/validators/schemas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright © 2021 Lisk Foundation
*
* See the LICENSE file at the top-level directory of this distribution
* for licensing information.
*
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation,
* no part of this software, including this file, may be copied, modified,
* propagated, or distributed except according to the terms contained in the
* LICENSE file.
*
* Removal or modification of this copyright notice is prohibited.
*/

export const validatorAccountSchema = {
type: 'object',
properties: {
generatorKey: {
dataType: 'bytes',
fieldNumber: 1,
},
blsKey: {
dataType: 'bytes',
fieldNumber: 2,
},
},
required: ['generatorKey', 'blsKey'],
};

export const generatorListSchema = {
type: 'object',
properties: {
addresses: {
type: 'array',
fieldNumber: 1,
items: {
dataType: 'bytes',
},
},
},
required: ['addresses'],
};

export const validatorAddressSchema = {
type: 'object',
properties: {
address: {
dataType: 'bytes',
fieldNumber: 1,
},
},
required: ['address'],
};

export const genesisDataSchema = {
type: 'object',
properties: {
timestamp: {
dataType: 'uint64',
fieldNumber: 1,
},
},
required: ['timestamp'],
};
26 changes: 26 additions & 0 deletions framework/src/modules/validators/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright © 2021 Lisk Foundation
*
* See the LICENSE file at the top-level directory of this distribution
* for licensing information.
*
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation,
* no part of this software, including this file, may be copied, modified,
* propagated, or distributed except according to the terms contained in the
* LICENSE file.
*
* Removal or modification of this copyright notice is prohibited.
*/

import { APIContext } from '../../node/state_machine/types';

export interface ValidatorKeys {
generatorKey: Buffer;
blsKey: Buffer;
}

export interface ValidatorsAPI {
getGenerator: (apiContext: APIContext, timestamp: number) => Promise<Buffer>;
getSlotNumber: (apiContext: APIContext, timestamp: number) => number;
getSlotTime: (apiContext: APIContext, slot: number) => number;
}
20 changes: 10 additions & 10 deletions framework/src/node/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { Consensus, CONSENSUS_EVENT_BLOCK_DELETE, CONSENSUS_EVENT_BLOCK_NEW } fr
import { Generator } from './generator';
import { Endpoint } from './endpoint';
import { getRegisteredModules, getSchema, isReservedEndpointFunction } from './utils/modules';
import { ValidatorAPI, ValidatorModule } from '../modules/validator';
import { ValidatorsAPI, ValidatorsModule } from '../modules/validators';
import { LiskBFTAPI, LiskBFTModule } from '../modules/liskbft';

const MINIMUM_MODULE_ID = 2;
Expand All @@ -54,7 +54,7 @@ export class Node {
private readonly _network: Network;
private readonly _chain: Chain;
private readonly _endpoint: Endpoint;
private readonly _validatorModule: ValidatorModule;
private readonly _validatorsModule: ValidatorsModule;
private readonly _liskBFTModule: LiskBFTModule;
private _channel!: InMemoryChannel;
private _logger!: Logger;
Expand All @@ -76,22 +76,22 @@ export class Node {
});

this._stateMachine = new StateMachine();
this._validatorModule = new ValidatorModule();
this._validatorsModule = new ValidatorsModule();
this._liskBFTModule = new LiskBFTModule();
this._registeredModules.push(this._validatorModule, this._liskBFTModule);
this._registeredModules.push(this._validatorsModule, this._liskBFTModule);
this._consensus = new Consensus({
stateMachine: this._stateMachine,
network: this._network,
chain: this._chain,
genesisConfig: this._options.genesisConfig,
liskBFTAPI: this._liskBFTModule.api,
validatorAPI: this._validatorModule.api,
validatorAPI: this._validatorsModule.api,
});
this._generator = new Generator({
chain: this._chain,
consensus: this._consensus,
liskBFTAPI: this._liskBFTModule.api,
validatorAPI: this._validatorModule.api,
validatorAPI: this._validatorsModule.api,
generationConfig: this._options.generation,
network: this._network,
stateMachine: this._stateMachine,
Expand All @@ -103,9 +103,9 @@ export class Node {
consensus: this._consensus,
generator: this._generator,
});
this._stateMachine.registerSystemModule(this._validatorModule);
this._stateMachine.registerSystemModule(this._validatorsModule);
this._stateMachine.registerSystemModule(this._liskBFTModule);
this._generator.registerModule(this._validatorModule);
this._generator.registerModule(this._validatorsModule);
this._generator.registerModule(this._liskBFTModule);
}

Expand Down Expand Up @@ -258,8 +258,8 @@ export class Node {
return this._liskBFTModule.api;
}

public get validatorAPI(): ValidatorAPI {
return this._validatorModule.api;
public get validatorAPI(): ValidatorsAPI {
return this._validatorsModule.api;
}

public get networkIdentifier(): Buffer {
Expand Down
6 changes: 5 additions & 1 deletion framework/test/fixtures/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ export const nodeOptions = ({
activeDelegates: 101,
standbyDelegates: 2,
delegateListRoundOffset: 2,
modules: {},
modules: {
validators: {
blockTime: 10,
},
},
},
genesisConfig: {
blockTime: 10, // 10 seconds
Expand Down
2 changes: 1 addition & 1 deletion framework/test/unit/node/node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ describe('Node', () => {
expect(node['_network']).toBeDefined();
expect(node['_chain']).toBeDefined();
expect(node['_stateMachine']).toBeDefined();
expect(node['_validatorModule']).toBeDefined();
expect(node['_validatorsModule']).toBeDefined();
expect(node['_liskBFTModule']).toBeDefined();
expect(node['_consensus']).toBeDefined();
expect(node['_generator']).toBeDefined();
Expand Down

0 comments on commit 6100421

Please sign in to comment.