From d4c76af61ed6afa6d690490a7644d5778a463b59 Mon Sep 17 00:00:00 2001 From: Manu Nelamane Siddalingegowda Date: Tue, 16 Mar 2021 16:12:51 +0100 Subject: [PATCH] :recycle: Update channel communication for block and transaction publish transaction announcement and block info after decoding in transport layer --- framework/src/constants.ts | 4 ++++ framework/src/node/network/network.ts | 17 ++++++++++++----- framework/src/node/transport/transport.ts | 17 ++++++++++++++++- 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/framework/src/constants.ts b/framework/src/constants.ts index 0aa02ef37fc..10315e65be2 100644 --- a/framework/src/constants.ts +++ b/framework/src/constants.ts @@ -35,3 +35,7 @@ export const APP_EVENT_CHAIN_FORK = 'app:chain:fork'; export const APP_EVENT_CHAIN_VALIDATORS_CHANGE = 'app:chain:validators:change'; export const APP_EVENT_BLOCK_NEW = 'app:block:new'; export const APP_EVENT_BLOCK_DELETE = 'app:block:delete'; + +export const EVENT_POST_BLOCK = 'postBlock'; +export const EVENT_POST_TRANSACTION_ANNOUNCEMENT = 'postTransactionsAnnouncement'; +export const EVENT_POST_NODE_INFO = 'postNodeInfo'; diff --git a/framework/src/node/network/network.ts b/framework/src/node/network/network.ts index d3eb72694a2..0d46cf92b6a 100644 --- a/framework/src/node/network/network.ts +++ b/framework/src/node/network/network.ts @@ -18,7 +18,13 @@ import { KVStore, NotFoundError } from '@liskhq/lisk-db'; import { EventEmitter } from 'events'; import * as liskP2P from '@liskhq/lisk-p2p'; -import { APP_EVENT_NETWORK_READY, APP_EVENT_NETWORK_EVENT } from '../../constants'; +import { + APP_EVENT_NETWORK_READY, + APP_EVENT_NETWORK_EVENT, + EVENT_POST_BLOCK, + EVENT_POST_NODE_INFO, + EVENT_POST_TRANSACTION_ANNOUNCEMENT, +} from '../../constants'; import { InMemoryChannel } from '../../controller/channels'; import { lookupPeersIPs } from './utils'; import { Logger } from '../../logger'; @@ -50,7 +56,11 @@ const DB_KEY_NETWORK_NODE_SECRET = 'network:nodeSecret'; const DB_KEY_NETWORK_TRIED_PEERS_LIST = 'network:triedPeersList'; const DEFAULT_PEER_SAVE_INTERVAL = 10 * 60 * 1000; // 10min in ms -const REMOTE_EVENTS_WHITE_LIST = ['postTransactionsAnnouncement', 'postBlock', 'postNodeInfo']; +const REMOTE_EVENTS_WHITE_LIST = [ + EVENT_POST_BLOCK, + EVENT_POST_NODE_INFO, + EVENT_POST_TRANSACTION_ANNOUNCEMENT, +]; interface NodeInfoOptions { [key: string]: unknown; @@ -373,9 +383,6 @@ export class Network { 'EVENT_MESSAGE_RECEIVED: Received inbound message', ); this.events.emit(APP_EVENT_NETWORK_EVENT, packet); - const data = - packet.data && Buffer.isBuffer(packet.data) ? packet.data.toString('hex') : packet.data; - this._channel.publish(APP_EVENT_NETWORK_EVENT, { ...packet, data }); }, ); diff --git a/framework/src/node/transport/transport.ts b/framework/src/node/transport/transport.ts index ba87d539985..4ebffdae8a3 100644 --- a/framework/src/node/transport/transport.ts +++ b/framework/src/node/transport/transport.ts @@ -43,7 +43,12 @@ import { Broadcaster } from './broadcaster'; import { InMemoryChannel } from '../../controller/channels'; import { Network } from '../network'; import { ApplyPenaltyError } from '../../errors'; -import { APP_EVENT_TRANSACTION_NEW } from '../../constants'; +import { + APP_EVENT_TRANSACTION_NEW, + APP_EVENT_NETWORK_EVENT, + EVENT_POST_BLOCK, + EVENT_POST_TRANSACTION_ANNOUNCEMENT, +} from '../../constants'; const DEFAULT_RATE_RESET_TIME = 10000; const DEFAULT_RATE_LIMIT_FREQUENCY = 3; @@ -274,6 +279,10 @@ export class Transport { } const { block: blockBytes } = decodedData; + this._channel.publish(APP_EVENT_NETWORK_EVENT, { + event: EVENT_POST_BLOCK, + data: { block: blockBytes.toString('hex') }, + }); let block: Block; try { @@ -440,6 +449,12 @@ export class Transport { const { transactionIds } = decodedData as EventPostTransactionsAnnouncementData; + const encodedIds = transactionIds.map(id => id.toString('hex')); + this._channel.publish(APP_EVENT_NETWORK_EVENT, { + event: EVENT_POST_TRANSACTION_ANNOUNCEMENT, + data: { transactionIds: encodedIds }, + }); + const unknownTransactionIDs = await this._obtainUnknownTransactionIDs(transactionIds); if (unknownTransactionIDs.length > 0) { const transactionIdsBuffer = codec.encode(transactionIdsSchema, {