diff --git a/services/blockchain-connector/events/controller/blockchain.js b/services/blockchain-connector/events/controller/blockchain.js index 713c78c055..00b4282bb3 100644 --- a/services/blockchain-connector/events/controller/blockchain.js +++ b/services/blockchain-connector/events/controller/blockchain.js @@ -14,8 +14,11 @@ * */ const { Signals } = require('lisk-service-framework'); +const { getBlockByID } = require('../../shared/sdk/endpoints'); const { formatBlock: formatBlockFromFormatter } = require('../../shared/sdk/formatter'); +const EMPTY_TREE_ROOT_HASH = 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'; + const appReadyController = async cb => { const appReadyListener = async payload => cb(payload); Signals.get('appReady').add(appReadyListener); @@ -54,12 +57,33 @@ const chainValidatorsChangeController = async cb => { const formatBlock = payload => formatBlockFromFormatter({ header: payload.blockHeader, - assets: [], - transactions: [], + assets: payload.assets || [], + transactions: payload.transactions || [], }); const chainNewBlockController = async cb => { - const chainNewBlockListener = async payload => cb(formatBlock(payload)); + const chainNewBlockListener = async payload => { + const { blockHeader } = payload; + let transactions = []; + let assets = []; + + if ( + blockHeader.transactionRoot !== EMPTY_TREE_ROOT_HASH || + blockHeader.assetRoot !== EMPTY_TREE_ROOT_HASH + ) { + const block = await getBlockByID(blockHeader.id); + transactions = block.transactions; + assets = block.assets; + } + + cb( + formatBlock({ + blockHeader, + assets, + transactions, + }), + ); + }; Signals.get('chainNewBlock').add(chainNewBlockListener); }; diff --git a/services/blockchain-coordinator/shared/eventsScheduler.js b/services/blockchain-coordinator/shared/eventsScheduler.js index ef13255071..bdc11149a0 100644 --- a/services/blockchain-coordinator/shared/eventsScheduler.js +++ b/services/blockchain-coordinator/shared/eventsScheduler.js @@ -24,11 +24,10 @@ const eventMessageQueue = new MessageQueue(config.queue.event.name, config.endpo defaultJobOptions: config.queue.defaultJobOptions, }); -const scheduleUpdatesOnNewBlock = async payload => { - const { header } = payload; - logger.debug(`Scheduling indexing new block at height: ${header.height}.`); - await eventMessageQueue.add({ header, isNewBlock: true }); - logger.info(`Finished scheduling indexing new block at height: ${header.height}.`); +const scheduleUpdatesOnNewBlock = async block => { + logger.debug(`Scheduling indexing new block at height: ${block.height}.`); + await eventMessageQueue.add({ block, isNewBlock: true }); + logger.info(`Finished scheduling indexing new block at height: ${block.height}.`); }; const scheduleDeleteBlock = async payload => { diff --git a/services/blockchain-indexer/events/blockchain.js b/services/blockchain-indexer/events/blockchain.js index e01a13c40d..89f23003ca 100644 --- a/services/blockchain-indexer/events/blockchain.js +++ b/services/blockchain-indexer/events/blockchain.js @@ -17,7 +17,6 @@ const { Logger, Signals } = require('lisk-service-framework'); const { reloadAllPendingTransactions, - getTransactionsByBlockID, reloadGeneratorsCache, getGenerators, } = require('../shared/dataService'); @@ -64,12 +63,21 @@ module.exports = [ try { if (payload && Array.isArray(payload.data)) { const [block] = payload.data; - if (block.numberOfTransactions > 0) { + const { numberOfTransactions } = block; + + if (numberOfTransactions > 0) { logger.debug( `Block (${block.id}) arrived containing ${block.numberOfTransactions} new transactions`, ); - const transactionData = await getTransactionsByBlockID(block.id); - callback(transactionData); + const transactionsPayload = { + data: block.transactions, + meta: { + count: numberOfTransactions, + offset: 0, + total: numberOfTransactions, + }, + }; + callback(transactionsPayload); } } else { const payloadStr = JSON.stringify(payload); diff --git a/services/blockchain-indexer/shared/dataService/blocks.js b/services/blockchain-indexer/shared/dataService/blocks.js index 8d1d38e2a8..2f309f2fca 100644 --- a/services/blockchain-indexer/shared/dataService/blocks.js +++ b/services/blockchain-indexer/shared/dataService/blocks.js @@ -73,20 +73,24 @@ const getBlocksTotal = async (params, blocksResponse) => { ) { total = blocksResponse.meta.total; } else { - total = await getTotalNumberOfBlocks(); + total = (await getTotalNumberOfBlocks()) || blocksResponse.data.length; } return total; }; -const formatBlock = async (header, isDeletedBlock = false) => { +const formatBlock = async (blockInfo, isDeletedBlock = false) => { const blocksResponse = { data: [], meta: {}, }; const response = await business.formatBlock( - { header, assets: [], transactions: [] }, + { + header: {}, + assets: blockInfo.assets || [], + transactions: blockInfo.transactions || [], + }, isDeletedBlock, ); blocksResponse.data.push(response); diff --git a/services/blockchain-indexer/shared/dataService/business/blocks.js b/services/blockchain-indexer/shared/dataService/business/blocks.js index b73a4caa40..19ab81f0cb 100644 --- a/services/blockchain-indexer/shared/dataService/business/blocks.js +++ b/services/blockchain-indexer/shared/dataService/business/blocks.js @@ -27,7 +27,7 @@ const { const logger = Logger(); const { getEventsByHeight, getEventsByBlockID } = require('./events'); -const { getFinalizedHeight, MODULE, EVENT } = require('../../constants'); +const { getFinalizedHeight, MODULE, EVENT, getGenesisHeight } = require('../../constants'); const blocksTableSchema = require('../../database/schema/blocks'); const { getIndexedAccountInfo } = require('../utils/account'); @@ -75,7 +75,13 @@ const normalizeBlock = async (originalBlock, isDeletedBlock = false) => { block.isFinal = block.height <= (await getFinalizedHeight()); block.numberOfTransactions = block.transactions.length; - block.numberOfAssets = block.assets.length; + block.numberOfAssets = + block.height !== (await getGenesisHeight()) + ? block.assets.length + : await (async () => { + const response = await requestConnector('getGenesisAssetsLength'); + return Object.entries(response).length; + })(); const { numberOfEvents, reward } = await (async () => { const [dbResponse] = await blocksTable.find({ height: block.height, limit: 1 }, [ diff --git a/services/blockchain-indexer/shared/messageProcessor.js b/services/blockchain-indexer/shared/messageProcessor.js index 208e660217..08cb0b7289 100644 --- a/services/blockchain-indexer/shared/messageProcessor.js +++ b/services/blockchain-indexer/shared/messageProcessor.js @@ -75,15 +75,16 @@ const initQueueStatus = async () => { await queueStatus(eventMessageQueue); }; -const newBlockProcessor = async header => { - logger.debug(`New block (${header.id}) received at height ${header.height}.`); - const response = await formatBlock(header); +const newBlockProcessor = async block => { + logger.debug(`New block (${block.id}) received at height ${block.height}.`); + const response = await formatBlock(block); const [newBlock] = response.data; + await indexNewBlock(newBlock); await performLastBlockUpdate(newBlock); Signals.get('newBlock').dispatch(response); logger.info( - `Finished scheduling new block (${header.id}) event for the block at height ${header.height}.`, + `Finished scheduling new block (${block.id}) event for the block at height ${block.height}.`, ); }; @@ -92,7 +93,7 @@ const deleteBlockProcessor = async header => { logger.debug( `Scheduling the delete block (${header.id}) event for the block at height ${header.height}.`, ); - const response = await formatBlock(header, true); + const response = await formatBlock({ header }, true); await scheduleBlockDeletion(header); Signals.get('deleteBlock').dispatch(response); logger.info( @@ -141,8 +142,8 @@ const initMessageProcessors = async () => { const { isNewBlock, isDeleteBlock, isNewRound } = job.data; if (isNewBlock) { - const { header } = job.data; - await newBlockProcessor(header); + const { block } = job.data; + await newBlockProcessor(block); } else if (isDeleteBlock) { try { const { header } = job.data;