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 #1932 from LiskHQ/1930-incorrect-information-in-ne…
Browse files Browse the repository at this point in the history
…w-block-event-payload

Incorrect information in new.block event payload
  • Loading branch information
vardan10 authored Nov 22, 2023
2 parents e179feb + 6b8d2c3 commit 4735a6d
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 24 deletions.
30 changes: 27 additions & 3 deletions services/blockchain-connector/events/controller/blockchain.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
};

Expand Down
9 changes: 4 additions & 5 deletions services/blockchain-coordinator/shared/eventsScheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
16 changes: 12 additions & 4 deletions services/blockchain-indexer/events/blockchain.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const { Logger, Signals } = require('lisk-service-framework');

const {
reloadAllPendingTransactions,
getTransactionsByBlockID,
reloadGeneratorsCache,
getGenerators,
} = require('../shared/dataService');
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 7 additions & 3 deletions services/blockchain-indexer/shared/dataService/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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 }, [
Expand Down
15 changes: 8 additions & 7 deletions services/blockchain-indexer/shared/messageProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}.`,
);
};

Expand All @@ -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(
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 4735a6d

Please sign in to comment.