From 533cca91b45a399573d090f6b99b94bec17f6ab9 Mon Sep 17 00:00:00 2001 From: Shusetsu Toda Date: Tue, 21 Jan 2020 13:23:54 +0100 Subject: [PATCH 01/16] Revert "Fix processor block save ordering" This reverts commit df84226be257df4beb4715b9b1c767ed54d55e50. --- .../src/modules/chain/processor/processor.js | 10 +++---- .../specs/modules/chain/dpos/apply.spec.js | 1 - .../modules/chain/processor/processor.spec.js | 30 ++++++++----------- 3 files changed, 18 insertions(+), 23 deletions(-) diff --git a/framework/src/modules/chain/processor/processor.js b/framework/src/modules/chain/processor/processor.js index 6ba8e752133..77f2da932b4 100644 --- a/framework/src/modules/chain/processor/processor.js +++ b/framework/src/modules/chain/processor/processor.js @@ -304,6 +304,11 @@ class Processor { }); } + if (!skipSave) { + // TODO: After moving everything to state store, save should get the state store and finalize the state store + await this.blocksModule.save(blockJSON, tx); + } + // Apply should always be executed after save as it performs database calculations // i.e. Dpos.apply expects to have this processing block in the database await processor.apply.run({ @@ -314,11 +319,6 @@ class Processor { tx, }); - if (!skipSave) { - // TODO: After moving everything to state store, save should get the state store and finalize the state store - await this.blocksModule.save(blockJSON, tx); - } - if (removeFromTempTable) { await this.blocksModule.removeBlockFromTempTable(block.id, tx); this.logger.debug( diff --git a/framework/test/jest/unit/specs/modules/chain/dpos/apply.spec.js b/framework/test/jest/unit/specs/modules/chain/dpos/apply.spec.js index 0bbf1814ec7..bab29b47319 100644 --- a/framework/test/jest/unit/specs/modules/chain/dpos/apply.spec.js +++ b/framework/test/jest/unit/specs/modules/chain/dpos/apply.spec.js @@ -307,7 +307,6 @@ describe('dpos.apply()', () => { reward: rewardPerDelegate, height: 809 + i, })); - forgedBlocks.splice(forgedBlocks.length - 1); lastBlockOfTheRoundNine = { diff --git a/framework/test/jest/unit/specs/modules/chain/processor/processor.spec.js b/framework/test/jest/unit/specs/modules/chain/processor/processor.spec.js index dfe7eefb132..61c122d0e67 100644 --- a/framework/test/jest/unit/specs/modules/chain/processor/processor.spec.js +++ b/framework/test/jest/unit/specs/modules/chain/processor/processor.spec.js @@ -487,17 +487,15 @@ describe('processor', () => { }); it('should emit newBlock event for the block', async () => { - expect(channelStub.publish).toHaveBeenCalledWith( - 'chain:processor:newBlock', - { block: blockV0 }, - ); + expect( + channelStub.publish, + ).toHaveBeenCalledWith('chain:processor:newBlock', { block: blockV0 }); }); it('should emit broadcast event for the block', async () => { - expect(channelStub.publish).toHaveBeenCalledWith( - 'chain:processor:broadcast', - { block: blockV0 }, - ); + expect( + channelStub.publish, + ).toHaveBeenCalledWith('chain:processor:broadcast', { block: blockV0 }); }); }); @@ -710,17 +708,15 @@ describe('processor', () => { }); it('should broadcast with the block', async () => { - expect(channelStub.publish).toHaveBeenCalledWith( - 'chain:processor:broadcast', - { block: blockV0 }, - ); + expect( + channelStub.publish, + ).toHaveBeenCalledWith('chain:processor:broadcast', { block: blockV0 }); }); it('should emit newBlock event with the block', async () => { - expect(channelStub.publish).toHaveBeenCalledWith( - 'chain:processor:newBlock', - { block: blockV0 }, - ); + expect( + channelStub.publish, + ).toHaveBeenCalledWith('chain:processor:newBlock', { block: blockV0 }); }); }); }); @@ -990,7 +986,7 @@ describe('processor', () => { it('should apply the block', async () => { applySteps.forEach(step => { - expect(step).toHaveBeenCalled(); + expect(step).not.toHaveBeenCalled(); }); }); From b19953301d3dbd13864caa3aab33f11480bb91ae Mon Sep 17 00:00:00 2001 From: Shusetsu Toda Date: Tue, 21 Jan 2020 13:25:20 +0100 Subject: [PATCH 02/16] :bug: Move verify step in the correct pipeline --- framework/src/modules/chain/block_processor_v2.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/framework/src/modules/chain/block_processor_v2.js b/framework/src/modules/chain/block_processor_v2.js index a216c762ebd..f89d59464c0 100644 --- a/framework/src/modules/chain/block_processor_v2.js +++ b/framework/src/modules/chain/block_processor_v2.js @@ -223,11 +223,13 @@ class BlockProcessorV2 extends BaseBlockProcessor { ({ block, lastBlock }) => this.bftModule.forkChoice(block, lastBlock), // validate common block header ]); - this.verify.pipe([({ block }) => this.bftModule.verifyNewBlock(block)]); - - this.apply.pipe([ + this.verify.pipe([ + ({ block }) => this.bftModule.verifyNewBlock(block), ({ block, stateStore, skipExistingCheck }) => this.blocksModule.verify(block, stateStore, { skipExistingCheck }), + ]); + + this.apply.pipe([ ({ block, stateStore }) => this.blocksModule.apply(block, stateStore), ({ block, tx }) => this.dposModule.apply(block, { tx }), async ({ block, tx, stateStore }) => { From 94413dde309288edc49aa03249fea5eb7f2640ec Mon Sep 17 00:00:00 2001 From: Manu Nelamane Siddalingegowda Date: Wed, 22 Jan 2020 11:31:58 +0100 Subject: [PATCH 03/16] :recycle: Fix p2p exports --- elements/lisk-p2p/src/index.ts | 14 +++++---- framework/src/modules/network/network.js | 36 +++++++++++++----------- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/elements/lisk-p2p/src/index.ts b/elements/lisk-p2p/src/index.ts index 2175c7aeace..e535445c797 100644 --- a/elements/lisk-p2p/src/index.ts +++ b/elements/lisk-p2p/src/index.ts @@ -12,9 +12,11 @@ * Removal or modification of this copyright notice is prohibited. * */ -export * from './constants'; -export * from './errors'; -export * from './events'; -export * from './p2p_request'; -export * from './p2p_types'; -export * from './p2p'; +import * as constants from './constants'; +import * as errors from './errors'; +import * as events from './events'; +import { P2P } from './p2p'; +import * as p2p_request from './p2p_request'; +import * as p2p_types from './p2p_types'; + +export { constants, errors, events, p2p_request, p2p_types, P2P }; diff --git a/framework/src/modules/network/network.js b/framework/src/modules/network/network.js index aced001175b..166a5541eeb 100644 --- a/framework/src/modules/network/network.js +++ b/framework/src/modules/network/network.js @@ -16,23 +16,25 @@ const { getRandomBytes } = require('@liskhq/lisk-cryptography'); const { - P2P, - EVENT_NETWORK_READY, - EVENT_NEW_INBOUND_PEER, - EVENT_CLOSE_INBOUND, - EVENT_CLOSE_OUTBOUND, - EVENT_CONNECT_OUTBOUND, - EVENT_DISCOVERED_PEER, - EVENT_FAILED_TO_FETCH_PEER_INFO, - EVENT_FAILED_TO_PUSH_NODE_INFO, - EVENT_OUTBOUND_SOCKET_ERROR, - EVENT_INBOUND_SOCKET_ERROR, - EVENT_UPDATED_PEER_INFO, - EVENT_FAILED_PEER_INFO_UPDATE, - EVENT_REQUEST_RECEIVED, - EVENT_MESSAGE_RECEIVED, - EVENT_BAN_PEER, - EVENT_UNBAN_PEER, + p2p: { P2P }, + events: { + EVENT_NETWORK_READY, + EVENT_NEW_INBOUND_PEER, + EVENT_CLOSE_INBOUND, + EVENT_CLOSE_OUTBOUND, + EVENT_CONNECT_OUTBOUND, + EVENT_DISCOVERED_PEER, + EVENT_FAILED_TO_FETCH_PEER_INFO, + EVENT_FAILED_TO_PUSH_NODE_INFO, + EVENT_OUTBOUND_SOCKET_ERROR, + EVENT_INBOUND_SOCKET_ERROR, + EVENT_UPDATED_PEER_INFO, + EVENT_FAILED_PEER_INFO_UPDATE, + EVENT_REQUEST_RECEIVED, + EVENT_MESSAGE_RECEIVED, + EVENT_BAN_PEER, + EVENT_UNBAN_PEER, + }, } = require('@liskhq/lisk-p2p'); const { createLoggerComponent } = require('../../components/logger'); const { createStorageComponent } = require('../../components/storage'); From f1e30a7038003609d7a97172b6535c74be04015a Mon Sep 17 00:00:00 2001 From: Manu Nelamane Siddalingegowda Date: Wed, 22 Jan 2020 11:33:05 +0100 Subject: [PATCH 04/16] :package: Expose new elements packages --- elements/lisk-elements/package-lock.json | 6 ++++++ elements/lisk-elements/package.json | 5 +++++ elements/lisk-elements/src/index.ts | 9 ++++++--- templates/.prettierignore.tmpl | 3 +++ 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/elements/lisk-elements/package-lock.json b/elements/lisk-elements/package-lock.json index dd0fa2274ea..20892e70a52 100644 --- a/elements/lisk-elements/package-lock.json +++ b/elements/lisk-elements/package-lock.json @@ -281,6 +281,12 @@ "integrity": "sha512-wMHVg2EOHaMRxbzgFJ9gtjOOCrI80OHLG14rxi28XwOW8ux6IiEbRCGGGqCtdAIg4FQCbW20k9RsT4y3gJlFug==", "dev": true }, + "babelify": { + "version": "10.0.0", + "resolved": "https://registry.npmjs.org/babelify/-/babelify-10.0.0.tgz", + "integrity": "sha512-X40FaxyH7t3X+JFAKvb1H9wooWKLRCi8pg3m8poqtdZaIng+bjzp9RvKQCvRjF9isHiPkXspbbXT/zwXLtwgwg==", + "dev": true + }, "balanced-match": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", diff --git a/elements/lisk-elements/package.json b/elements/lisk-elements/package.json index a2bee385a56..13aae2db8bf 100644 --- a/elements/lisk-elements/package.json +++ b/elements/lisk-elements/package.json @@ -62,8 +62,11 @@ "@liskhq/lisk-api-client": "3.0.0", "@liskhq/lisk-constants": "1.3.0", "@liskhq/lisk-cryptography": "2.4.0", + "@liskhq/lisk-p2p": "0.4.0", "@liskhq/lisk-passphrase": "3.0.0", + "@liskhq/lisk-transaction-pool": "0.2.0", "@liskhq/lisk-transactions": "3.0.0", + "@liskhq/lisk-validator": "0.3.0", "@types/node": "12.12.11" }, "devDependencies": { @@ -71,6 +74,8 @@ "@types/expect": "1.20.3", "@types/jquery": "3.3.29", "@types/mocha": "5.2.5", + "@babel/preset-env": "7.8.3", + "babelify": "10.0.0", "browserify": "16.2.3", "chai": "4.2.0", "cypress": "3.4.1", diff --git a/elements/lisk-elements/src/index.ts b/elements/lisk-elements/src/index.ts index 7fe7cf832c7..9298c738e24 100644 --- a/elements/lisk-elements/src/index.ts +++ b/elements/lisk-elements/src/index.ts @@ -15,16 +15,19 @@ import { APIClient } from '@liskhq/lisk-api-client'; import * as constants from '@liskhq/lisk-constants'; import * as cryptography from '@liskhq/lisk-cryptography'; +import * as p2p from '@liskhq/lisk-p2p'; import * as passphrase from '@liskhq/lisk-passphrase'; +import * as transacationPool from '@liskhq/lisk-transaction-pool'; import * as transactions from '@liskhq/lisk-transactions'; +import * as validator from '@liskhq/lisk-validator'; export { APIClient, constants, cryptography, passphrase, + p2p, transactions, - // Also export as `transacation` for backward compatibility. - // See https://github.com/LiskHQ/lisk-sdk/issues/3925#issuecomment-508664703 - transactions as transaction, + transacationPool, + validator, }; diff --git a/templates/.prettierignore.tmpl b/templates/.prettierignore.tmpl index a781643f098..13dbec7fe39 100644 --- a/templates/.prettierignore.tmpl +++ b/templates/.prettierignore.tmpl @@ -12,6 +12,8 @@ mocha.opts *.png *.sql *.sh +*.html + ## jest snapshot *.snap @@ -21,3 +23,4 @@ coverage/ dist-node/ dist-browser/ browsertest.build/ +.coverage From 633c18d975fbb49cedabb50a5ad5a50b66202e17 Mon Sep 17 00:00:00 2001 From: Manu Nelamane Siddalingegowda Date: Wed, 22 Jan 2020 16:58:55 +0100 Subject: [PATCH 05/16] :recycle: Fix failing p2p tests --- .../functional/actions/apply_node_info.ts | 8 ++--- .../test/functional/actions/peer_banning.ts | 11 +++---- .../test/functional/actions/request.ts | 4 +-- .../lisk-p2p/test/functional/actions/send.ts | 4 +-- .../blacklist_whitelist_fixed_peers.ts | 14 ++++---- .../lisk-p2p/test/functional/broadcast.ts | 4 +-- .../test/functional/custom_peer_selection.ts | 5 +-- .../functional/events/connection_close.ts | 9 ++---- .../functional/events/connection_create.ts | 22 +++++++------ .../test/functional/events/startup_events.ts | 3 +- .../lisk-p2p/test/functional/message_rate.ts | 8 ++--- .../integration/backup_restore_peerinfo.ts | 11 ++++--- .../lisk-p2p/test/integration/discovery.ts | 32 +++++++++++-------- .../test/integration/outbound_shuffling.ts | 6 ++-- .../integration/peer_discovery_threshold.ts | 4 ++- .../integration/penalty_malformed_peerinfo.ts | 4 ++- .../penalty_malformed_peerinfo_list.ts | 4 ++- .../penalty_protocol_message_limit.ts | 10 ++---- .../test/integration/unseeded_network.ts | 4 +-- elements/lisk-p2p/test/unit/errors.ts | 4 +-- elements/lisk-p2p/test/unit/peer/base.ts | 8 ++--- elements/lisk-p2p/test/unit/peer/inbound.ts | 4 +-- .../lisk-p2p/test/unit/peer_book/base_list.ts | 6 ++-- elements/lisk-p2p/test/unit/peer_pool.ts | 4 ++- elements/lisk-p2p/test/unit/utils/validate.ts | 7 ++-- elements/lisk-p2p/test/utils/network_setup.ts | 8 +++-- 26 files changed, 111 insertions(+), 97 deletions(-) diff --git a/elements/lisk-p2p/test/functional/actions/apply_node_info.ts b/elements/lisk-p2p/test/functional/actions/apply_node_info.ts index 0d470dea302..c06ffc74e76 100644 --- a/elements/lisk-p2p/test/functional/actions/apply_node_info.ts +++ b/elements/lisk-p2p/test/functional/actions/apply_node_info.ts @@ -12,16 +12,14 @@ * Removal or modification of this copyright notice is prohibited. * */ -import { - P2P, - EVENT_MESSAGE_RECEIVED, - REMOTE_EVENT_POST_NODE_INFO, -} from '../../../src/index'; +import { P2P, events } from '../../../src/index'; import { InvalidNodeInfoError } from '../../../src/errors'; import { wait } from '../../utils/helpers'; import { platform } from 'os'; import { createNetwork, destroyNetwork } from '../../utils/network_setup'; +const { EVENT_MESSAGE_RECEIVED, REMOTE_EVENT_POST_NODE_INFO } = events; + describe('P2P.applyNodeInfo', () => { let p2pNodeList: P2P[] = []; let collectedMessages: Array = []; diff --git a/elements/lisk-p2p/test/functional/actions/peer_banning.ts b/elements/lisk-p2p/test/functional/actions/peer_banning.ts index b5dad423703..68829e8cea6 100644 --- a/elements/lisk-p2p/test/functional/actions/peer_banning.ts +++ b/elements/lisk-p2p/test/functional/actions/peer_banning.ts @@ -12,18 +12,15 @@ * Removal or modification of this copyright notice is prohibited. * */ -import { P2P, ProtocolPeerInfo } from '../../../src/index'; import { wait } from '../../utils/helpers'; import { createNetwork, destroyNetwork, SEED_PEER_IP, } from '../../utils/network_setup'; -import { - EVENT_BAN_PEER, - EVENT_UNBAN_PEER, - EVENT_CLOSE_INBOUND, -} from '../../../src/index'; +import { P2P, events, p2p_types } from '../../../src/index'; + +const { EVENT_BAN_PEER, EVENT_UNBAN_PEER, EVENT_CLOSE_INBOUND } = events; describe('Peer banning mechanism', () => { let p2pNodeList: ReadonlyArray = []; @@ -59,7 +56,7 @@ describe('Peer banning mechanism', () => { }); describe('when penalty is 100 or more', () => { - let badPeer: ProtocolPeerInfo; + let badPeer: p2p_types.ProtocolPeerInfo; beforeEach(async () => { const firstNode = p2pNodeList[0]; diff --git a/elements/lisk-p2p/test/functional/actions/request.ts b/elements/lisk-p2p/test/functional/actions/request.ts index f4bf1ccc4e5..b74319c1692 100644 --- a/elements/lisk-p2p/test/functional/actions/request.ts +++ b/elements/lisk-p2p/test/functional/actions/request.ts @@ -12,7 +12,7 @@ * Removal or modification of this copyright notice is prohibited. * */ -import { P2P, EVENT_REQUEST_RECEIVED } from '../../../src/index'; +import { P2P, events } from '../../../src/index'; import { createNetwork, destroyNetwork, @@ -27,7 +27,7 @@ describe('P2P.request', () => { for (let p2p of p2pNodeList) { // Collect port numbers to check which peer handled which request. - p2p.on(EVENT_REQUEST_RECEIVED, request => { + p2p.on(events.EVENT_REQUEST_RECEIVED, request => { if (!request.wasResponseSent) { request.end({ nodePort: p2p.nodeInfo.wsPort, diff --git a/elements/lisk-p2p/test/functional/actions/send.ts b/elements/lisk-p2p/test/functional/actions/send.ts index 6c82719a983..2022ff2206a 100644 --- a/elements/lisk-p2p/test/functional/actions/send.ts +++ b/elements/lisk-p2p/test/functional/actions/send.ts @@ -12,7 +12,7 @@ * Removal or modification of this copyright notice is prohibited. * */ -import { P2P, EVENT_MESSAGE_RECEIVED } from '../../../src/index'; +import { P2P, events } from '../../../src/index'; import { wait } from '../../utils/helpers'; import { createNetwork, @@ -32,7 +32,7 @@ describe('P2P.send', () => { randomP2PNode = p2pNodeList[randomPeerIndex]; for (let p2p of p2pNodeList) { - p2p.on(EVENT_MESSAGE_RECEIVED, message => { + p2p.on(events.EVENT_MESSAGE_RECEIVED, message => { collectedMessages.push({ nodePort: p2p.nodeInfo.wsPort, message, diff --git a/elements/lisk-p2p/test/functional/blacklist_whitelist_fixed_peers.ts b/elements/lisk-p2p/test/functional/blacklist_whitelist_fixed_peers.ts index 22b47e187a0..83f9340062f 100644 --- a/elements/lisk-p2p/test/functional/blacklist_whitelist_fixed_peers.ts +++ b/elements/lisk-p2p/test/functional/blacklist_whitelist_fixed_peers.ts @@ -12,18 +12,20 @@ * Removal or modification of this copyright notice is prohibited. * */ -import { - P2P, - EVENT_CLOSE_OUTBOUND, - INTENTIONAL_DISCONNECT_CODE, - SEED_PEER_DISCONNECTION_REASON, -} from '../../src/index'; +import { P2P, events, constants } from '../../src/index'; import { wait } from '../utils/helpers'; import cloneDeep = require('lodash.clonedeep'); import { SCServerSocket } from 'socketcluster-server'; import * as url from 'url'; import { createNetwork, destroyNetwork } from '../utils/network_setup'; +const { EVENT_CLOSE_OUTBOUND } = events; + +const { + INTENTIONAL_DISCONNECT_CODE, + SEED_PEER_DISCONNECTION_REASON, +} = constants; + describe('Blacklisted/fixed/whitelisted peers', () => { const FIVE_CONNECTIONS = 5; const NETWORK_START_PORT = 5000; diff --git a/elements/lisk-p2p/test/functional/broadcast.ts b/elements/lisk-p2p/test/functional/broadcast.ts index f3a99d461a7..599a9e64775 100644 --- a/elements/lisk-p2p/test/functional/broadcast.ts +++ b/elements/lisk-p2p/test/functional/broadcast.ts @@ -12,7 +12,7 @@ * Removal or modification of this copyright notice is prohibited. * */ -import { P2P, EVENT_MESSAGE_RECEIVED } from '../../src/index'; +import { P2P, events } from '../../src/index'; import { wait } from '../utils/helpers'; import { createNetwork, @@ -32,7 +32,7 @@ describe('P2P.broadcast', () => { collectedMessages = []; for (let p2p of p2pNodeList) { - p2p.on(EVENT_MESSAGE_RECEIVED, message => { + p2p.on(events.EVENT_MESSAGE_RECEIVED, message => { if (message.event === BROADCAST_EVENT) { collectedMessages.push({ nodePort: p2p.nodeInfo.wsPort, diff --git a/elements/lisk-p2p/test/functional/custom_peer_selection.ts b/elements/lisk-p2p/test/functional/custom_peer_selection.ts index c6154b37905..135a929ff68 100644 --- a/elements/lisk-p2p/test/functional/custom_peer_selection.ts +++ b/elements/lisk-p2p/test/functional/custom_peer_selection.ts @@ -12,7 +12,7 @@ * Removal or modification of this copyright notice is prohibited. * */ -import { P2P } from '../../src/index'; +import { P2P, constants } from '../../src/index'; import { wait } from '../utils/helpers'; import { P2PPeerSelectionForSendFunction, @@ -22,7 +22,8 @@ import { P2PPeerSelectionForRequestInput, P2PPeerSelectionForConnectionInput, } from '../../src/p2p_types'; -import { ConnectionKind } from '../../src/constants'; + +const { ConnectionKind } = constants; import { createNetwork, diff --git a/elements/lisk-p2p/test/functional/events/connection_close.ts b/elements/lisk-p2p/test/functional/events/connection_close.ts index d4805b830db..534250c1725 100644 --- a/elements/lisk-p2p/test/functional/events/connection_close.ts +++ b/elements/lisk-p2p/test/functional/events/connection_close.ts @@ -12,17 +12,14 @@ * Removal or modification of this copyright notice is prohibited. * */ -import { - P2P, - EVENT_CLOSE_INBOUND, - EVENT_CLOSE_OUTBOUND, - INTENTIONAL_DISCONNECT_CODE, -} from '../../../src/index'; +import { P2P, events, constants } from '../../../src/index'; import { wait } from '../../utils/helpers'; import { createNetwork, destroyNetwork } from '../../utils/network_setup'; const SOCKET_HUNG_UP_CODE = 1006; +const { EVENT_CLOSE_INBOUND, EVENT_CLOSE_OUTBOUND } = events; +const { INTENTIONAL_DISCONNECT_CODE } = constants; describe(`Events on Connection Close`, () => { let p2pNodeList: ReadonlyArray = []; diff --git a/elements/lisk-p2p/test/functional/events/connection_create.ts b/elements/lisk-p2p/test/functional/events/connection_create.ts index 870a79ad82c..0287673373b 100644 --- a/elements/lisk-p2p/test/functional/events/connection_create.ts +++ b/elements/lisk-p2p/test/functional/events/connection_create.ts @@ -12,24 +12,28 @@ * Removal or modification of this copyright notice is prohibited. * */ +import { P2P, events, constants } from '../../../src/index'; +import { wait } from '../../utils/helpers'; import { - P2P, + createNetwork, + destroyNetwork, + SEED_PEER_IP, + NETWORK_START_PORT, +} from '../../utils/network_setup'; + +const { EVENT_CONNECT_OUTBOUND, EVENT_NEW_INBOUND_PEER, EVENT_DISCOVERED_PEER, EVENT_UPDATED_PEER_INFO, EVENT_FAILED_TO_ADD_INBOUND_PEER, +} = events; + +const { INCOMPATIBLE_NETWORK_REASON, INCOMPATIBLE_PROTOCOL_VERSION_REASON, INVALID_CONNECTION_SELF_REASON, -} from '../../../src/index'; -import { wait } from '../../utils/helpers'; -import { - createNetwork, - destroyNetwork, - SEED_PEER_IP, - NETWORK_START_PORT, -} from '../../utils/network_setup'; +} = constants; describe(`Connection Create`, () => { describe(`Events`, () => { diff --git a/elements/lisk-p2p/test/functional/events/startup_events.ts b/elements/lisk-p2p/test/functional/events/startup_events.ts index f4b799f27a1..3001b2af403 100644 --- a/elements/lisk-p2p/test/functional/events/startup_events.ts +++ b/elements/lisk-p2p/test/functional/events/startup_events.ts @@ -12,10 +12,11 @@ * Removal or modification of this copyright notice is prohibited. * */ -import { P2P, EVENT_NETWORK_READY } from '../../../src/index'; +import { P2P, events } from '../../../src/index'; import { wait } from '../../utils/helpers'; import { createNetwork, destroyNetwork } from '../../utils/network_setup'; +const { EVENT_NETWORK_READY } = events; describe(`Event on ${EVENT_NETWORK_READY}`, () => { let p2pNodeList: ReadonlyArray = []; const collectedEvents = new Map(); diff --git a/elements/lisk-p2p/test/functional/message_rate.ts b/elements/lisk-p2p/test/functional/message_rate.ts index 7856dcae5b5..6eeb678baa2 100644 --- a/elements/lisk-p2p/test/functional/message_rate.ts +++ b/elements/lisk-p2p/test/functional/message_rate.ts @@ -12,11 +12,7 @@ * Removal or modification of this copyright notice is prohibited. * */ -import { - P2P, - EVENT_MESSAGE_RECEIVED, - EVENT_REMOVE_PEER, -} from '../../src/index'; +import { P2P, events } from '../../src/index'; import { wait } from '../utils/helpers'; import { createNetwork, @@ -24,6 +20,8 @@ import { NETWORK_START_PORT, } from '../utils/network_setup'; +const { EVENT_MESSAGE_RECEIVED, EVENT_REMOVE_PEER } = events; + describe('Message rate limit', () => { let p2pNodeList: ReadonlyArray = []; let collectedMessages: Array = []; diff --git a/elements/lisk-p2p/test/integration/backup_restore_peerinfo.ts b/elements/lisk-p2p/test/integration/backup_restore_peerinfo.ts index c1104d712ae..b96293798ff 100644 --- a/elements/lisk-p2p/test/integration/backup_restore_peerinfo.ts +++ b/elements/lisk-p2p/test/integration/backup_restore_peerinfo.ts @@ -12,15 +12,16 @@ * Removal or modification of this copyright notice is prohibited. * */ -import { - P2P, +import { P2P, events } from '../../src/index'; +import { wait } from '../utils/helpers'; +import { createNetwork, destroyNetwork } from '../utils/network_setup'; + +const { EVENT_MESSAGE_RECEIVED, EVENT_BAN_PEER, EVENT_REMOVE_PEER, EVENT_CLOSE_OUTBOUND, -} from '../../src/index'; -import { wait } from '../utils/helpers'; -import { createNetwork, destroyNetwork } from '../utils/network_setup'; +} = events; describe('Backup and Restore', () => { let p2pNodeList: ReadonlyArray = []; diff --git a/elements/lisk-p2p/test/integration/discovery.ts b/elements/lisk-p2p/test/integration/discovery.ts index 25ac566a319..f055dac59bf 100644 --- a/elements/lisk-p2p/test/integration/discovery.ts +++ b/elements/lisk-p2p/test/integration/discovery.ts @@ -12,8 +12,20 @@ * Removal or modification of this copyright notice is prohibited. * */ +import { P2P, events, constants } from '../../src/index'; +import { wait } from '../utils/helpers'; +import { platform } from 'os'; import { - P2P, + createNetwork, + destroyNetwork, + NETWORK_START_PORT, + NETWORK_PEER_COUNT, + SEED_PEER_IP, + NETWORK_CREATION_WAIT_TIME, +} from '../utils/network_setup'; +import { constructPeerId } from '../../src/utils'; + +const { EVENT_CONNECT_OUTBOUND, EVENT_DISCOVERED_PEER, EVENT_FAILED_TO_ADD_INBOUND_PEER, @@ -23,21 +35,13 @@ import { EVENT_UPDATED_PEER_INFO, EVENT_BAN_PEER, EVENT_CLOSE_OUTBOUND, + EVENT_REQUEST_RECEIVED, +} = events; + +const { INTENTIONAL_DISCONNECT_CODE, SEED_PEER_DISCONNECTION_REASON, - EVENT_REQUEST_RECEIVED, -} from '../../src/index'; -import { wait } from '../utils/helpers'; -import { platform } from 'os'; -import { - createNetwork, - destroyNetwork, - NETWORK_START_PORT, - NETWORK_PEER_COUNT, - SEED_PEER_IP, - NETWORK_CREATION_WAIT_TIME, -} from '../utils/network_setup'; -import { constructPeerId } from '../../src/utils'; +} = constants; describe('Network discovery', () => { const CUSTOM_FALLBACK_SEED_DISCOVERY_INTERVAL = 400; diff --git a/elements/lisk-p2p/test/integration/outbound_shuffling.ts b/elements/lisk-p2p/test/integration/outbound_shuffling.ts index bc170cb50f5..f7dbe23933a 100644 --- a/elements/lisk-p2p/test/integration/outbound_shuffling.ts +++ b/elements/lisk-p2p/test/integration/outbound_shuffling.ts @@ -12,10 +12,12 @@ * Removal or modification of this copyright notice is prohibited. * */ -import { P2P, EVENT_CLOSE_OUTBOUND, EVICTED_PEER_CODE } from '../../src/index'; +import { P2P, events, constants } from '../../src/index'; import { wait } from '../utils/helpers'; import { createNetwork, destroyNetwork } from '../utils/network_setup'; +const { EVENT_CLOSE_OUTBOUND } = events; + describe('Outbound peer shuffling', () => { let p2pNodeList: ReadonlyArray = []; const collectedEventsCount = new Map(); @@ -40,7 +42,7 @@ describe('Outbound peer shuffling', () => { p2pNodeList.forEach(p2p => { p2p.on(EVENT_CLOSE_OUTBOUND, msg => { - if (msg.code === EVICTED_PEER_CODE) { + if (msg.code === constants.EVICTED_PEER_CODE) { let evictedConnections = collectedEventsCount.get( p2p.nodeInfo.wsPort, ); diff --git a/elements/lisk-p2p/test/integration/peer_discovery_threshold.ts b/elements/lisk-p2p/test/integration/peer_discovery_threshold.ts index d1560ae9efe..ef2e4c32d2a 100644 --- a/elements/lisk-p2p/test/integration/peer_discovery_threshold.ts +++ b/elements/lisk-p2p/test/integration/peer_discovery_threshold.ts @@ -12,10 +12,12 @@ * Removal or modification of this copyright notice is prohibited. * */ -import { P2P, EVENT_DISCOVERED_PEER } from '../../src/index'; +import { P2P, events } from '../../src/index'; import { wait } from '../utils/helpers'; import { createNetwork, destroyNetwork } from '../utils/network_setup'; +const { EVENT_DISCOVERED_PEER } = events; + describe('Peer discovery threshold', () => { let p2pNodeList: ReadonlyArray = []; const MINIMUM_PEER_DISCOVERY_THRESHOLD = 10; diff --git a/elements/lisk-p2p/test/integration/penalty_malformed_peerinfo.ts b/elements/lisk-p2p/test/integration/penalty_malformed_peerinfo.ts index 65c73c39189..eac1cdedb16 100644 --- a/elements/lisk-p2p/test/integration/penalty_malformed_peerinfo.ts +++ b/elements/lisk-p2p/test/integration/penalty_malformed_peerinfo.ts @@ -12,11 +12,13 @@ * Removal or modification of this copyright notice is prohibited. * */ -import { P2P, EVENT_BAN_PEER } from '../../src/index'; +import { P2P, events } from '../../src/index'; import { wait } from '../utils/helpers'; import { platform } from 'os'; import { createNetwork, destroyNetwork } from '../utils/network_setup'; +const { EVENT_BAN_PEER } = events; + describe('penalty sending malformed peerInfo', () => { let p2pNodeList: P2P[] = []; const collectedEvents = new Map(); diff --git a/elements/lisk-p2p/test/integration/penalty_malformed_peerinfo_list.ts b/elements/lisk-p2p/test/integration/penalty_malformed_peerinfo_list.ts index d9745b0fb8f..e92a20c762b 100644 --- a/elements/lisk-p2p/test/integration/penalty_malformed_peerinfo_list.ts +++ b/elements/lisk-p2p/test/integration/penalty_malformed_peerinfo_list.ts @@ -12,7 +12,7 @@ * Removal or modification of this copyright notice is prohibited. * */ -import { P2P, EVENT_BAN_PEER } from '../../src/index'; +import { P2P, events } from '../../src/index'; import { createNetwork, destroyNetwork, @@ -21,6 +21,8 @@ import { import { wait } from '../utils/helpers'; import { constructPeerId } from '../../src/utils'; +const { EVENT_BAN_PEER } = events; + describe('penalty sending malformed Peer List', () => { describe('When Peer List is too long', () => { let p2pNodeList: ReadonlyArray = []; diff --git a/elements/lisk-p2p/test/integration/penalty_protocol_message_limit.ts b/elements/lisk-p2p/test/integration/penalty_protocol_message_limit.ts index c6db1c3205e..ebade3745e7 100644 --- a/elements/lisk-p2p/test/integration/penalty_protocol_message_limit.ts +++ b/elements/lisk-p2p/test/integration/penalty_protocol_message_limit.ts @@ -12,11 +12,7 @@ * Removal or modification of this copyright notice is prohibited. * */ -import { - P2P, - EVENT_BAN_PEER, - DEFAULT_WS_MAX_MESSAGE_RATE, -} from '../../src/index'; +import { P2P, events, constants } from '../../src/index'; import { createNetwork, destroyNetwork, @@ -35,7 +31,7 @@ describe('P2P protocol message limit', () => { maxOutboundConnections: index % 2 === 1 ? 3 : 20, fallbackSeedPeerDiscoveryInterval: index === 2 ? 100 : 10000, rateCalculationInterval: 1000, - wsMaxMessageRatePenalty: DEFAULT_WS_MAX_MESSAGE_RATE, + wsMaxMessageRatePenalty: constants.DEFAULT_WS_MAX_MESSAGE_RATE, populatorInterval: index === 2 ? 100 : 10000, }); @@ -46,7 +42,7 @@ describe('P2P protocol message limit', () => { }); for (let p2p of p2pNodeList) { - p2p.on(EVENT_BAN_PEER, peerId => { + p2p.on(events.EVENT_BAN_PEER, peerId => { bannedPeer = peerId; }); } diff --git a/elements/lisk-p2p/test/integration/unseeded_network.ts b/elements/lisk-p2p/test/integration/unseeded_network.ts index 96c967e3fa4..f9b17d3cc08 100644 --- a/elements/lisk-p2p/test/integration/unseeded_network.ts +++ b/elements/lisk-p2p/test/integration/unseeded_network.ts @@ -13,7 +13,7 @@ * */ -import { P2P, RequestFailError } from '../../src/index'; +import { P2P, errors } from '../../src/index'; import { createNetwork, destroyNetwork } from '../utils/network_setup'; describe('Unseeded network: Each node has an empty seedPeers list', () => { @@ -46,6 +46,6 @@ describe('Unseeded network: Each node has an empty seedPeers list', () => { data: 'bar', }); - return expect(response).rejects.toThrow(RequestFailError); + return expect(response).rejects.toThrow(errors.RequestFailError); }); }); diff --git a/elements/lisk-p2p/test/unit/errors.ts b/elements/lisk-p2p/test/unit/errors.ts index c161ff006de..3159ecf5809 100644 --- a/elements/lisk-p2p/test/unit/errors.ts +++ b/elements/lisk-p2p/test/unit/errors.ts @@ -24,7 +24,7 @@ import { ExistingPeerError, InvalidNodeInfoError, } from '../../src/errors'; -import { P2PPeerInfo } from '../../src'; +import { p2p_types } from '../../src'; import { constructPeerId } from '../../src/utils'; describe('errors', () => { @@ -103,7 +103,7 @@ describe('errors', () => { describe('#ExistingPeerError', () => { const existingPeerErrorMessagge = 'Peer already exists'; - const peerInfo: P2PPeerInfo = { + const peerInfo: p2p_types.P2PPeerInfo = { ipAddress: '0.0.0.0', wsPort: 5000, peerId: constructPeerId('0.0.0.0', 5000), diff --git a/elements/lisk-p2p/test/unit/peer/base.ts b/elements/lisk-p2p/test/unit/peer/base.ts index 5777522c406..094a3df4ba6 100644 --- a/elements/lisk-p2p/test/unit/peer/base.ts +++ b/elements/lisk-p2p/test/unit/peer/base.ts @@ -38,7 +38,7 @@ import { import { RPCResponseError } from '../../../src/errors'; import { SCServerSocket } from 'socketcluster-server'; import { getNetgroup, constructPeerId } from '../../../src/utils'; -import { P2PPeerInfo } from '../../../src'; +import { p2p_types } from '../../../src'; const createSocketStubInstance = () => ({ emit: jest.fn(), @@ -46,9 +46,9 @@ const createSocketStubInstance = () => ({ } as any); describe('peer/base', () => { - let defaultPeerInfo: P2PPeerInfo; + let defaultPeerInfo: p2p_types.P2PPeerInfo; let peerConfig: PeerConfig; - let p2pDiscoveredPeerInfo: P2PPeerInfo; + let p2pDiscoveredPeerInfo: p2p_types.P2PPeerInfo; let defaultPeer: Peer; beforeEach(() => { @@ -507,7 +507,7 @@ describe('peer/base', () => { }); describe('#discoverPeers', () => { - let discoveredPeers: ReadonlyArray; + let discoveredPeers: ReadonlyArray; beforeEach(() => { discoveredPeers = [ diff --git a/elements/lisk-p2p/test/unit/peer/inbound.ts b/elements/lisk-p2p/test/unit/peer/inbound.ts index b03ee0ac897..1b7e02ecbc7 100644 --- a/elements/lisk-p2p/test/unit/peer/inbound.ts +++ b/elements/lisk-p2p/test/unit/peer/inbound.ts @@ -25,10 +25,10 @@ import { REMOTE_SC_EVENT_RPC_REQUEST, REMOTE_EVENT_PING, } from '../../../src/events'; -import { P2PPeerInfo } from '../../../src'; +import { p2p_types } from '../../../src'; describe('peer/inbound', () => { - let defaultPeerInfo: P2PPeerInfo; + let defaultPeerInfo: p2p_types.P2PPeerInfo; let defaultPeerConfig: PeerConfig; let defaultInboundPeer: InboundPeer; let inboundSocket: SCServerSocket; diff --git a/elements/lisk-p2p/test/unit/peer_book/base_list.ts b/elements/lisk-p2p/test/unit/peer_book/base_list.ts index 298402b855b..8e82c7128dd 100644 --- a/elements/lisk-p2p/test/unit/peer_book/base_list.ts +++ b/elements/lisk-p2p/test/unit/peer_book/base_list.ts @@ -13,7 +13,7 @@ * */ import { BaseList } from '../../../src/peer_book/base_list'; -import { P2PEnhancedPeerInfo } from '../../../src/p2p_types'; +import { P2PEnhancedPeerInfo, P2PPeerInfo } from '../../../src/p2p_types'; import { initPeerInfoList } from '../../utils/peers'; import { PEER_TYPE } from '../../../src/utils'; import { @@ -22,7 +22,7 @@ import { DEFAULT_RANDOM_SECRET, } from '../../../src/constants'; import { getBucketId } from '../../../src/utils'; -import { ExistingPeerError, P2PPeerInfo } from '../../../src'; +import { errors } from '../../../src'; import 'jest-extended'; describe('Peers base list', () => { @@ -162,7 +162,7 @@ describe('Peers base list', () => { peerListObj.addPeer(samplePeers[0]); // 'Peer already exists' expect(() => peerListObj.addPeer(samplePeers[0])).toThrow( - ExistingPeerError, + errors.ExistingPeerError, ); }); diff --git a/elements/lisk-p2p/test/unit/peer_pool.ts b/elements/lisk-p2p/test/unit/peer_pool.ts index a788e46f480..bbd15a9d53d 100644 --- a/elements/lisk-p2p/test/unit/peer_pool.ts +++ b/elements/lisk-p2p/test/unit/peer_pool.ts @@ -51,7 +51,9 @@ import { DEFAULT_WS_MAX_MESSAGE_RATE, } from '../../src/constants'; import { constructPeerId } from '../../src/utils'; -import { RequestFailError, SendFailError } from '../../src'; +import { errors } from '../../src'; + +const { RequestFailError, SendFailError } = errors; describe('peerPool', () => { const peerPoolConfig = { diff --git a/elements/lisk-p2p/test/unit/utils/validate.ts b/elements/lisk-p2p/test/unit/utils/validate.ts index 019728d9133..77e1d1e9c64 100644 --- a/elements/lisk-p2p/test/unit/utils/validate.ts +++ b/elements/lisk-p2p/test/unit/utils/validate.ts @@ -27,12 +27,15 @@ import { P2PMessagePacket, P2PNodeInfo, } from '../../../src/p2p_types'; -import { +import { constants } from '../../../src'; + +const { DEFAULT_MAX_PEER_DISCOVERY_RESPONSE_LENGTH, DEFAULT_MAX_PEER_INFO_SIZE, PEER_INFO_LIST_TOO_LONG_REASON, INVALID_PEER_INFO_LIST_REASON, -} from '../../../src'; +} = constants; + describe('utils/validate', () => { describe('#validatePeerInfo', () => { describe('for valid peer response object', () => { diff --git a/elements/lisk-p2p/test/utils/network_setup.ts b/elements/lisk-p2p/test/utils/network_setup.ts index 160c3af8561..d4c254984ff 100644 --- a/elements/lisk-p2p/test/utils/network_setup.ts +++ b/elements/lisk-p2p/test/utils/network_setup.ts @@ -12,13 +12,15 @@ * Removal or modification of this copyright notice is prohibited. * */ -import { P2P } from '../../src/index'; +import { P2P, constants } from '../../src/index'; import { wait } from './helpers'; import { platform } from 'os'; -import { + +const { DEFAULT_MAX_OUTBOUND_CONNECTIONS, DEFAULT_MAX_INBOUND_CONNECTIONS, -} from '../../src'; +} = constants; + export const NETWORK_START_PORT = 5000; export const NETWORK_PEER_COUNT = 10; export const POPULATOR_INTERVAL = 50; From e951844bce6c66d3cdfbfdd19a7b83d249875f89 Mon Sep 17 00:00:00 2001 From: Manu Nelamane Siddalingegowda Date: Wed, 22 Jan 2020 17:08:11 +0100 Subject: [PATCH 06/16] :recycle: Fix network module require --- framework/src/modules/network/network.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/src/modules/network/network.js b/framework/src/modules/network/network.js index 166a5541eeb..e5faefd6b0e 100644 --- a/framework/src/modules/network/network.js +++ b/framework/src/modules/network/network.js @@ -16,7 +16,7 @@ const { getRandomBytes } = require('@liskhq/lisk-cryptography'); const { - p2p: { P2P }, + P2P, events: { EVENT_NETWORK_READY, EVENT_NEW_INBOUND_PEER, From 76fca24b914cf0ecb852b6c3379f129bf5039e20 Mon Sep 17 00:00:00 2001 From: Manu Nelamane Siddalingegowda Date: Wed, 22 Jan 2020 17:29:12 +0100 Subject: [PATCH 07/16] :recycle: Fix elements tests --- elements/lisk-elements/test/index.ts | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/elements/lisk-elements/test/index.ts b/elements/lisk-elements/test/index.ts index e8923a63f5f..efc9705f201 100644 --- a/elements/lisk-elements/test/index.ts +++ b/elements/lisk-elements/test/index.ts @@ -17,9 +17,11 @@ import { APIClient, constants, cryptography, + p2p, passphrase, + transacationPool, transactions, - transaction, + validator, } from '../src'; describe('lisk-elements', () => { @@ -35,15 +37,23 @@ describe('lisk-elements', () => { return expect(cryptography).to.be.an('object'); }); + it('p2p should be an object', () => { + return expect(p2p).to.be.an('object'); + }); + it('passphrase should be an object', () => { return expect(passphrase).to.be.an('object'); }); + it('transactionPool should be an object', () => { + return expect(transacationPool).to.be.an('object'); + }); + it('transactions should be an object', () => { return expect(transactions).to.be.an('object'); }); - it('transaction should be an object', () => { - return expect(transaction).to.be.an('object'); + it('validator should be an object', () => { + return expect(validator).to.be.an('object'); }); }); From b01585e8c4091b1c3bb93a26d324c1d7137c7c08 Mon Sep 17 00:00:00 2001 From: Pablo Vicente Date: Mon, 20 Jan 2020 17:21:31 +0100 Subject: [PATCH 08/16] Replace nethash term with networkId. Remove conversion code used for nethash/networkId. --- framework/src/controller/application.js | 11 +++--- framework/src/controller/application_state.js | 4 +-- framework/src/modules/chain/chain.js | 2 +- .../src/modules/http_api/controllers/node.js | 2 +- .../src/modules/http_api/helpers/utils.js | 12 +++++-- .../src/modules/http_api/schema/swagger.yml | 8 ++--- framework/src/modules/network/network.js | 35 ++++--------------- 7 files changed, 31 insertions(+), 43 deletions(-) diff --git a/framework/src/controller/application.js b/framework/src/controller/application.js index 4443f8482cd..9280b161dbb 100644 --- a/framework/src/controller/application.js +++ b/framework/src/controller/application.js @@ -23,6 +23,7 @@ const { MultisignatureTransaction, transactionInterface, } = require('@liskhq/lisk-transactions'); +const { getNetworkIdentifier } = require('@liskhq/lisk-cryptography'); const { validator: liskValidator } = require('@liskhq/lisk-validator'); const _ = require('lodash'); const Controller = require('./controller'); @@ -287,14 +288,16 @@ class Application { _compileAndValidateConfigurations() { const modules = this.getModules(); - - this.config.app.nethash = this.genesisBlock.payloadHash; + this.config.app.networkId = getNetworkIdentifier( + this.genesisBlock.payloadHash, + this.genesisBlock.communityIdentifier, + ); const appConfigToShareWithModules = { version: this.config.app.version, minVersion: this.config.app.minVersion, protocolVersion: this.config.app.protocolVersion, - nethash: this.config.app.nethash, + networkId: this.config.app.networkId, genesisBlock: this.genesisBlock, constants: this.constants, lastCommitId: this.config.app.lastCommitId, @@ -317,7 +320,7 @@ class Application { version: this.config.app.version, minVersion: this.config.app.minVersion, protocolVersion: this.config.app.protocolVersion, - nethash: this.config.app.nethash, + networkId: this.config.app.networkId, wsPort: this.config.modules.network.wsPort, httpPort: this.config.modules.http_api.httpPort, }; diff --git a/framework/src/controller/application_state.js b/framework/src/controller/application_state.js index c201d8d0b71..fd618d23c60 100644 --- a/framework/src/controller/application_state.js +++ b/framework/src/controller/application_state.js @@ -30,7 +30,7 @@ class ApplicationState { httpPort, minVersion, protocolVersion, - nethash, + networkId, }, logger, }) { @@ -45,7 +45,7 @@ class ApplicationState { height: 1, blockVersion: 0, maxHeightPrevoted: 0, - nethash, + networkId, }); } diff --git a/framework/src/modules/chain/chain.js b/framework/src/modules/chain/chain.js index 64ee17b1987..65b450c1fda 100644 --- a/framework/src/modules/chain/chain.js +++ b/framework/src/modules/chain/chain.js @@ -89,7 +89,7 @@ module.exports = class Chain { try { if (!this.options.genesisBlock) { - throw Error('Failed to assign nethash from genesis block'); + throw Error('Missing genesis block'); } if ( diff --git a/framework/src/modules/http_api/controllers/node.js b/framework/src/modules/http_api/controllers/node.js index 5431897f3a0..2252e1daeba 100644 --- a/framework/src/modules/http_api/controllers/node.js +++ b/framework/src/modules/http_api/controllers/node.js @@ -90,7 +90,7 @@ NodeController.getConstants = async (context, next) => { dappWithdrawal: FEES.DAPP_WITHDRAWAL.toString(), dappDeposit: FEES.DAPP_DEPOSIT.toString(), }, - nethash: library.config.nethash, + networkId: library.config.networkId, milestone: milestone.toString(), reward: reward.toString(), supply: supply.toString(), diff --git a/framework/src/modules/http_api/helpers/utils.js b/framework/src/modules/http_api/helpers/utils.js index 33123f68d7e..bc26a22736d 100644 --- a/framework/src/modules/http_api/helpers/utils.js +++ b/framework/src/modules/http_api/helpers/utils.js @@ -143,7 +143,7 @@ const consolidatePeers = (connectedPeers = [], disconnectedPeers = []) => { ipAddress, options, minVersion, - nethash, + networkId, nonce, blockVersion, maxHeightPrevoted, @@ -151,14 +151,19 @@ const consolidatePeers = (connectedPeers = [], disconnectedPeers = []) => { ...restOfPeerObject } = peer; - return { ip: ipAddress, ...restOfPeerObject, state: PEER_STATE_CONNECTED }; + return { + ip: ipAddress, + networkId, + ...restOfPeerObject, + state: PEER_STATE_CONNECTED, + }; }); const disconnectedList = [...disconnectedPeers].map(peer => { const { ipAddress, options, minVersion, - nethash, + networkId, nonce, blockVersion, maxHeightPrevoted, @@ -168,6 +173,7 @@ const consolidatePeers = (connectedPeers = [], disconnectedPeers = []) => { return { ip: ipAddress, + networkId, ...restOfPeerObject, state: PEER_STATE_DISCONNECTED, }; diff --git a/framework/src/modules/http_api/schema/swagger.yml b/framework/src/modules/http_api/schema/swagger.yml index 7f1a37dea33..ecbb6397b2c 100755 --- a/framework/src/modules/http_api/schema/swagger.yml +++ b/framework/src/modules/http_api/schema/swagger.yml @@ -2127,7 +2127,7 @@ definitions: required: - epoch - milestone - - nethash + - networkId - reward - supply - build @@ -2168,12 +2168,12 @@ definitions: format: protocolVersion example: 1.0 description: The Lisk Core protocol version, that the node is running on. - nethash: + networkId: type: string example: ed14889723f24ecc54871d058d98ce91ff2f973192075c0155ba2b7b70ad2511 description: | - Describes the network. - The nethash describes e.g. the Mainnet or the Testnet, that the node is connecting to. + Identifies the network. + The networkId that the node is connecting to, see LIP-0009 for more details. supply: type: string example: '10575384500000000' diff --git a/framework/src/modules/network/network.js b/framework/src/modules/network/network.js index e5faefd6b0e..97a0bf5d6f3 100644 --- a/framework/src/modules/network/network.js +++ b/framework/src/modules/network/network.js @@ -113,16 +113,11 @@ module.exports = class Network { this.secret = Number(secret); } - const sanitizeNodeInfo = nodeInfo => { - const { nethash, ...restOfNodeInfo } = nodeInfo; - - return { - ...restOfNodeInfo, - networkId: nethash, - wsPort: this.options.wsPort, - advertiseAddress: this.options.advertiseAddress, - }; - }; + const sanitizeNodeInfo = nodeInfo => ({ + ...nodeInfo, + wsPort: this.options.wsPort, + advertiseAddress: this.options.advertiseAddress, + }); const initialNodeInfo = sanitizeNodeInfo( await this.channel.invoke('app:getApplicationState'), @@ -385,24 +380,8 @@ module.exports = class Network { event: action.params.event, data: action.params.data, }), - getConnectedPeers: () => - this.p2p.getConnectedPeers().map(peerInfo => { - const { networkId, ...peerInfoNethash } = peerInfo; - - return { - ...peerInfoNethash, - nethash: networkId, - }; - }), - getDisconnectedPeers: () => - this.p2p.getDisconnectedPeers().map(peerInfo => { - const { networkId, ...peerInfoNethash } = peerInfo; - - return { - ...peerInfoNethash, - nethash: networkId, - }; - }), + getConnectedPeers: () => this.p2p.getConnectedPeers(), + getDisconnectedPeers: () => this.p2p.getDisconnectedPeers(), applyPenalty: action => this.p2p.applyPenalty({ peerId: action.params.peerId, From 71443cd0dea500390c8e46304bdfe8e0cb94e5c7 Mon Sep 17 00:00:00 2001 From: Pablo Vicente Date: Mon, 20 Jan 2020 17:59:51 +0100 Subject: [PATCH 09/16] Fix tests affected by replacing 'nethash' with 'networkId' --- .../jest/unit/specs/controller/application_state.spec.js | 4 ++-- .../test/jest/unit/specs/controller/controller.spec.js | 2 +- framework/test/mocha/functional/http/get/node/node.js | 8 ++++---- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/framework/test/jest/unit/specs/controller/application_state.spec.js b/framework/test/jest/unit/specs/controller/application_state.spec.js index 98740639ac6..a596dd57c30 100644 --- a/framework/test/jest/unit/specs/controller/application_state.spec.js +++ b/framework/test/jest/unit/specs/controller/application_state.spec.js @@ -29,7 +29,7 @@ describe('Application State', () => { httpPort: '3000', minVersion: '1.0.0-beta.0', protocolVersion: '1.0', - nethash: 'test nethash', + networkId: 'test networkId', maxHeightPrevoted: 0, height: 1, os: 'platformrelease', @@ -42,7 +42,7 @@ describe('Application State', () => { httpPort: '3000', minVersion: '1.0.0-beta.0', protocolVersion: '1.0', - nethash: 'test nethash', + networkId: 'test networkId', maxHeightPrevoted: 0, height: 1, }; diff --git a/framework/test/jest/unit/specs/controller/controller.spec.js b/framework/test/jest/unit/specs/controller/controller.spec.js index 0e970243e45..bfa00020cce 100644 --- a/framework/test/jest/unit/specs/controller/controller.spec.js +++ b/framework/test/jest/unit/specs/controller/controller.spec.js @@ -41,7 +41,7 @@ describe('Controller Class', () => { httpPort: '3000', minVersion: '1.0.0-beta.0', protocolVersion: '1.0', - nethash: 'test nethash', + networkId: 'test networkId', }; const systemDirs = { temp: `${config.tempPath}/${appLabel}/`, diff --git a/framework/test/mocha/functional/http/get/node/node.js b/framework/test/mocha/functional/http/get/node/node.js index e7961385746..7f8f55fb37a 100644 --- a/framework/test/mocha/functional/http/get/node/node.js +++ b/framework/test/mocha/functional/http/get/node/node.js @@ -23,6 +23,8 @@ const expectSwaggerParamError = apiHelpers.expectSwaggerParamError; describe('GET /node', () => { describe('/constants', () => { const endPoint = new SwaggerEndpoint('GET /node/constants 200'); + const devnetNetworkId = + '11a254dc30db5eb1ce4001acde35fd5a14d62584f886d30df161e4e883220eb7'; let constantsResponse; @@ -32,10 +34,8 @@ describe('GET /node', () => { }); }); - it('should return a result containing nethash = "23ce0366ef0a14a91e5fd4b1591fc880ffbef9d988ff8bebf8f3666b0c09597d"', async () => { - return expect(constantsResponse.nethash).to.be.equal( - '23ce0366ef0a14a91e5fd4b1591fc880ffbef9d988ff8bebf8f3666b0c09597d', - ); + it('should return a result containing networkId = "11a254dc30db5eb1ce4001acde35fd5a14d62584f886d30df161e4e883220eb7"', async () => { + return expect(constantsResponse.networkId).to.be.equal(devnetNetworkId); }); it('should return a result containing milestone that is a number <= 500000000', async () => { From 5b2e40efa3ab63f1f635d499b406f7b17361adb8 Mon Sep 17 00:00:00 2001 From: Pablo Vicente Date: Tue, 21 Jan 2020 10:03:32 +0100 Subject: [PATCH 10/16] Add networkId to swagger Peer definition --- framework/src/modules/http_api/schema/swagger.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/framework/src/modules/http_api/schema/swagger.yml b/framework/src/modules/http_api/schema/swagger.yml index ecbb6397b2c..7a7b250139a 100755 --- a/framework/src/modules/http_api/schema/swagger.yml +++ b/framework/src/modules/http_api/schema/swagger.yml @@ -2042,6 +2042,10 @@ definitions: example: 127.0.0.1 format: ip description: IPv4 address of the peer node. + networkId: + type: string + example: '11a254dc30db5eb1ce4001acde35fd5a14d62584f886d30df161e4e883220eb7' + description: The network identifier as per LIP-0009 httpPort: type: integer example: 8000 From f931a6fa7de6179e10ebf47db786317c2de0e875 Mon Sep 17 00:00:00 2001 From: Pablo Vicente Date: Tue, 21 Jan 2020 10:08:43 +0100 Subject: [PATCH 11/16] Fix tests utils after replacing nethash with networkId --- framework/test/utils/legacy/modules_loader.js | 2 +- framework/test/utils/p2p/generatePeerHeader.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/framework/test/utils/legacy/modules_loader.js b/framework/test/utils/legacy/modules_loader.js index 8616c4a72e4..f5402183df1 100644 --- a/framework/test/utils/legacy/modules_loader.js +++ b/framework/test/utils/legacy/modules_loader.js @@ -63,7 +63,7 @@ const modulesLoader = new (function() { suscribe: sinonSandbox.stub(), }, applicationState: { - nethash: __testContext.nethash, + networkId: __testContext.networkId, version: __testContext.version, wsPort: __testContext.wsPort, httpPort: __testContext.httpPort, diff --git a/framework/test/utils/p2p/generatePeerHeader.js b/framework/test/utils/p2p/generatePeerHeader.js index ca87572b261..d1c1118fcaa 100644 --- a/framework/test/utils/p2p/generatePeerHeader.js +++ b/framework/test/utils/p2p/generatePeerHeader.js @@ -25,7 +25,7 @@ const generatePeerHeader = function(headers = {}) { const httpPort = headers.httpPort || random.number(1025, 65536); const defaults = { - networkId: testConfig.app.nethash, + networkId: testConfig.app.networkId, height: 1, wsPort: headers.wsPort || httpPort - 1, httpPort, From d9946e9d2e2e642f37abc50622bfc4f380cb1aa8 Mon Sep 17 00:00:00 2001 From: Pablo Vicente Date: Tue, 21 Jan 2020 15:02:14 +0100 Subject: [PATCH 12/16] Replace nethash in commander and some elements with appropiate names based on each case --- commander/src/commands/network-identifier.ts | 11 ++++--- commander/src/utils/api.ts | 4 +-- commander/test/commands/config/set.test.ts | 29 ++++++++++--------- .../test/commands/network-identifier.test.ts | 6 ++-- elements/lisk-api-client/src/api_client.ts | 8 +++-- elements/lisk-api-client/src/api_types.ts | 2 +- elements/lisk-cryptography/src/hash.ts | 5 ++-- elements/lisk-cryptography/test/hash.ts | 6 ++-- 8 files changed, 38 insertions(+), 33 deletions(-) diff --git a/commander/src/commands/network-identifier.ts b/commander/src/commands/network-identifier.ts index 9e04e5d0bbf..d4eb43c9162 100644 --- a/commander/src/commands/network-identifier.ts +++ b/commander/src/commands/network-identifier.ts @@ -21,16 +21,16 @@ import { flags as commonFlags } from '../utils/flags'; export default class NetworkIdentifierCommand extends BaseCommand { static description = ` - Creates Network identifier for the given nethash and community identifier. + Creates Network identifier for the given genesis payload hash and community identifier. `; static examples = [ - 'network-identifier --nethash=da3ed6a45429278bac2666961289ca17ad86595d33b31037615d4b8e8f158bba', + 'network-identifier da3ed6a45429278bac2666961289ca17ad86595d33b31037615d4b8e8f158bba', ]; static args = [ { - name: 'nethash', + name: 'genesisPayloadHash', description: 'Payload hash of genesis block from the network.', required: true, }, @@ -48,11 +48,10 @@ export default class NetworkIdentifierCommand extends BaseCommand { async run(): Promise { const { flags: { 'community-identifier': communityIdentifier }, - args: { nethash }, + args: { genesisPayloadHash }, } = this.parse(NetworkIdentifierCommand); - const networkIdentifier = getNetworkIdentifier( - nethash as string, + genesisPayloadHash as string, communityIdentifier as string, ); this.print({ networkIdentifier }); diff --git a/commander/src/utils/api.ts b/commander/src/utils/api.ts index 9eb24a58699..616fc4ee1d5 100644 --- a/commander/src/utils/api.ts +++ b/commander/src/utils/api.ts @@ -31,8 +31,8 @@ export const getAPIClient = ({ nodes, network, }: APIClientOptions): APIClient => { - const nethash = NETHASHES[network] || network; + const genesisBlockPayloadHash = NETHASHES[network] || network; const clientNodes = nodes && nodes.length > 0 ? nodes : seedNodes[network]; - return new APIClient(clientNodes, { nethash }); + return new APIClient(clientNodes, { genesisBlockPayloadHash }); }; diff --git a/commander/test/commands/config/set.test.ts b/commander/test/commands/config/set.test.ts index 63d32ab017a..6071f77d105 100644 --- a/commander/test/commands/config/set.test.ts +++ b/commander/test/commands/config/set.test.ts @@ -195,19 +195,22 @@ describe('config:set', () => { setupTest() .stdout() .command(['config:set', 'api.network', validNethash]) - .it('should set api.network to the custom nethash', () => { - const newConfig = { - ...defaultConfig, - api: { - network: validNethash, - nodes: defaultConfig.api.nodes, - }, - }; - return expect(config.setConfig).to.be.calledWith( - defaultDir, - newConfig, - ); - }); + .it( + 'should set api.network to the custom genesisBlockPayloadHash', + () => { + const newConfig = { + ...defaultConfig, + api: { + network: validNethash, + nodes: defaultConfig.api.nodes, + }, + }; + return expect(config.setConfig).to.be.calledWith( + defaultDir, + newConfig, + ); + }, + ); setupTest() .stdout() diff --git a/commander/test/commands/network-identifier.test.ts b/commander/test/commands/network-identifier.test.ts index d626932b5cb..31a6cd30458 100644 --- a/commander/test/commands/network-identifier.test.ts +++ b/commander/test/commands/network-identifier.test.ts @@ -21,7 +21,7 @@ import NetworkIdentifierCommand from '../../src/commands/network-identifier'; describe('network-identifier command', () => { const networkIdentifier = { networkIdentifier: - '00adc544a063705a317d573ac2c2d88df717b9b7d472880ed4de7a830f611818', + '7dbdc2b4694bd5ab6663c4d078aa628ae032cb91ce0fe03a5077d7ef3ba2e8bc', }; const networkIdentifierStub = sandbox.stub(); @@ -55,9 +55,9 @@ describe('network-identifier command', () => { .it('should throw an error'); }); - describe('network-identifier --nethash=123', () => { + describe('network-identifier 123', () => { setupTest() - .command(['network-identifier', '--nethash=123']) + .command(['network-identifier', '123']) .it('should show networkIdentifier', () => { return expect(printMethodStub).to.be.calledWithExactly( networkIdentifier, diff --git a/elements/lisk-api-client/src/api_client.ts b/elements/lisk-api-client/src/api_client.ts index 2c1d85b774b..4d273be99c4 100644 --- a/elements/lisk-api-client/src/api_client.ts +++ b/elements/lisk-api-client/src/api_client.ts @@ -69,14 +69,14 @@ export class APIClient { public static createMainnetAPIClient(options?: InitOptions): APIClient { return new APIClient(constants.MAINNET_NODES, { - nethash: constants.MAINNET_NETHASH, + genesisBlockPayloadHash: constants.MAINNET_NETHASH, ...options, }); } public static createTestnetAPIClient(options?: InitOptions): APIClient { return new APIClient(constants.TESTNET_NODES, { - nethash: constants.TESTNET_NETHASH, + genesisBlockPayloadHash: constants.TESTNET_NETHASH, ...options, }); } @@ -173,7 +173,9 @@ export class APIClient { this.headers = { ...commonHeaders, - ...(options.nethash ? { nethash: options.nethash } : {}), + ...(options.genesisBlockPayloadHash + ? { nethash: options.genesisBlockPayloadHash } + : {}), ...(options.client ? getClientHeaders(options.client) : {}), }; diff --git a/elements/lisk-api-client/src/api_types.ts b/elements/lisk-api-client/src/api_types.ts index b008d957670..883267633c4 100644 --- a/elements/lisk-api-client/src/api_types.ts +++ b/elements/lisk-api-client/src/api_types.ts @@ -44,7 +44,7 @@ export interface HashMap { export interface InitOptions { readonly bannedNodes?: ReadonlyArray; readonly client?: object; - readonly nethash?: string; + readonly genesisBlockPayloadHash?: string; readonly node?: string; readonly randomizeNodes?: boolean; } diff --git a/elements/lisk-cryptography/src/hash.ts b/elements/lisk-cryptography/src/hash.ts index 9192259ba5e..29e43fc7a46 100644 --- a/elements/lisk-cryptography/src/hash.ts +++ b/elements/lisk-cryptography/src/hash.ts @@ -46,6 +46,7 @@ export const hash = (data: Buffer | string, format?: string): Buffer => { }; export const getNetworkIdentifier = ( - nethash: string, + genesisBlockPayloadHash: string, communityIdentifier: string, -) => hash(nethash + communityIdentifier, 'utf8').toString('hex'); +) => + hash(genesisBlockPayloadHash + communityIdentifier, 'utf8').toString('hex'); diff --git a/elements/lisk-cryptography/test/hash.ts b/elements/lisk-cryptography/test/hash.ts index ca43764eaf1..a106b2722fa 100644 --- a/elements/lisk-cryptography/test/hash.ts +++ b/elements/lisk-cryptography/test/hash.ts @@ -61,15 +61,15 @@ describe('hash', () => { }); describe('#getNetworkIdentifier', () => { - const nethash = + const genesisBlockPayloadHash = 'ed14889723f24ecc54871d058d98ce91ff2f973192075c0155ba2b7b70ad2511'; const communityIdentifier = 'LISK'; const expectedHash = '30d7505655f5a04d9238aa324b38ef729d1139791b67815c5e6306328b6a44a2'; - it('should generate a sha256 hash from nethash and community identifier', () => { + it('should generate a sha256 hash from Genesis Block Payload Hash and community identifier', () => { const networkIdentifier = getNetworkIdentifier( - nethash, + genesisBlockPayloadHash, communityIdentifier, ); From 610fb3c4388f950cb904e09ef5c242e4d8aa1e9f Mon Sep 17 00:00:00 2001 From: Pablo Vicente Date: Tue, 21 Jan 2020 15:11:25 +0100 Subject: [PATCH 13/16] Fix api_client test --- elements/lisk-api-client/test/api_client.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/elements/lisk-api-client/test/api_client.ts b/elements/lisk-api-client/test/api_client.ts index 87914193bdc..4e783d6f756 100644 --- a/elements/lisk-api-client/test/api_client.ts +++ b/elements/lisk-api-client/test/api_client.ts @@ -93,7 +93,7 @@ describe('APIClient module', () => { it('should call initialize with the nodes and provided options', () => { const providedOptions = { - nethash: + genesisBlockPayloadHash: '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef', }; apiClient = new APIClient(defaultNodes, providedOptions); @@ -202,7 +202,7 @@ describe('APIClient module', () => { it('should set custom headers with supplied options', () => { apiClient = new APIClient(defaultNodes, { - nethash: testnetHash, + genesisBlockPayloadHash: testnetHash, client: { name: 'LiskHub', version: '5.0', @@ -216,7 +216,7 @@ describe('APIClient module', () => { it('should not set User-Agent header when client options were not given', () => { apiClient = new APIClient(defaultNodes, { - nethash: testnetHash, + genesisBlockPayloadHash: testnetHash, }); return expect(apiClient.headers).to.not.have.property('User-Agent'); }); From f336d6e8f20ccb8657b5696ca3e0f55b39b283a5 Mon Sep 17 00:00:00 2001 From: Pablo Vicente Date: Tue, 21 Jan 2020 16:00:13 +0100 Subject: [PATCH 14/16] Update networkId description in swagger file --- framework/src/modules/http_api/schema/swagger.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/src/modules/http_api/schema/swagger.yml b/framework/src/modules/http_api/schema/swagger.yml index 7a7b250139a..5b767f90f9b 100755 --- a/framework/src/modules/http_api/schema/swagger.yml +++ b/framework/src/modules/http_api/schema/swagger.yml @@ -2176,7 +2176,7 @@ definitions: type: string example: ed14889723f24ecc54871d058d98ce91ff2f973192075c0155ba2b7b70ad2511 description: | - Identifies the network. + Unique identifier for the network. The networkId that the node is connecting to, see LIP-0009 for more details. supply: type: string From be3f62d78869029706d63fb11005d735d3a710c4 Mon Sep 17 00:00:00 2001 From: Shusetsu Toda Date: Thu, 23 Jan 2020 14:56:02 +0100 Subject: [PATCH 15/16] :recycle: Expose time function used in UI --- elements/lisk-transactions/src/index.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/elements/lisk-transactions/src/index.ts b/elements/lisk-transactions/src/index.ts index 2eab23f2790..399bc14c2e3 100644 --- a/elements/lisk-transactions/src/index.ts +++ b/elements/lisk-transactions/src/index.ts @@ -47,6 +47,7 @@ import { convertBeddowsToLSK, convertLSKToBeddows, getId, + getTimeFromBlockchainEpoch, prependMinusToPublicKeys, prependPlusToPublicKeys, validateMultisignatures, @@ -63,6 +64,7 @@ const exposedUtils = { BigNum, convertBeddowsToLSK, getId, + getTimeFromBlockchainEpoch, convertLSKToBeddows, prependMinusToPublicKeys, prependPlusToPublicKeys, From 3e20e2ce7a7a247ca8332e98863dc52eaf565b10 Mon Sep 17 00:00:00 2001 From: Manu Nelamane Siddalingegowda Date: Thu, 23 Jan 2020 15:21:40 +0100 Subject: [PATCH 16/16] :recycle: Fix commander install bug --- commander/src/commands/core/install.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/commander/src/commands/core/install.ts b/commander/src/commands/core/install.ts index f01b97c4236..15ae43f6062 100644 --- a/commander/src/commands/core/install.ts +++ b/commander/src/commands/core/install.ts @@ -42,7 +42,11 @@ import { startDatabase, stopDatabase, } from '../../utils/core/database'; -import { describeApplication, registerApplication } from '../../utils/core/pm2'; +import { + describeApplication, + registerApplication, + unRegisterApplication, +} from '../../utils/core/pm2'; import { getReleaseInfo } from '../../utils/core/release'; import { download, downloadAndValidate, extract } from '../../utils/download'; import { flags as commonFlags } from '../../utils/flags'; @@ -318,12 +322,13 @@ export default class InstallCommand extends BaseCommand { return; } } catch (error) { - this.error(JSON.stringify(error)); + await unRegisterApplication(name); const { installDir }: Options = error.context.options; const dirPath = installDir.substr(0, installDir.length - 1); fsExtra.emptyDirSync(installDir); fsExtra.rmdirSync(dirPath); + this.error(JSON.stringify(error)); } } }