Skip to content

Commit

Permalink
refactor(mina-consensus.ts): remove unused function findAllIndexes to…
Browse files Browse the repository at this point in the history
… improve code cleanliness

refactor(actions-service.ts): rename deriveActionsFromBlocks to blocksToActions for better clarity
refactor(events-service.ts): rename deriveEventsFromBlocks to blocksToEvents for better clarity
refactor(utils.ts): move findAllIndexes function from mina-consensus.ts to utils.ts as it's being used there
refactor(types.ts): rename FieldElementIdWithValueMap to FieldIdValuePairs for better clarity
refactor(types.ts): add detailed comments to ArchiveNodeDatabaseRow type for better understanding
refactor(tsconfig.json): include scripts/run-compare.ts in tsconfig for TypeScript compilation
refactor(queries.ts): remove unused fields from SQL queries to improve performance
refactor(utils.ts): refactor partitionBlocks and removeRedundantEmittedFields functions for better readability and performance
refactor(actions-service.ts): refactor getActionData function for better readability and performance
refactor(events-service.ts): refactor getEventData function for better readability and performance
  • Loading branch information
MartinMinkov committed Aug 14, 2023
1 parent 3ce60ac commit 368d685
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 125 deletions.
12 changes: 1 addition & 11 deletions src/consensus/mina-consensus.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
import { blake2bHex } from 'blakejs';
import { type BlockInfo } from 'src/models/types';
export { select, findAllIndexes, getAllPredicate, filterBestTip };

function findAllIndexes<T>(arr: T[], target: T): number[] {
const indexes: number[] = [];
arr.forEach((element, index) => {
if (element === target) {
indexes.push(index);
}
});
return indexes;
}
export { select, getAllPredicate, filterBestTip };

function getAllPredicate<T>(array: T[], predicate: (arg: T) => boolean) {
const data: T[] = [];
Expand Down
73 changes: 50 additions & 23 deletions src/db/archive-node-adapter/actions-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import {
BlockStatusFilter,
BlocksWithTransactionsMap,
DEFAULT_TOKEN_ID,
FieldElementIdWithValueMap,
FieldIdValuePairs,
Action,
Actions,
Transaction,
} from '../../models/types';
import { ActionFilterOptionsInput } from 'src/resolvers-types';
import { TracingService } from 'src/tracing/tracing';
Expand All @@ -31,25 +32,28 @@ class ActionsService {
}

async getActions(input: ActionFilterOptionsInput): Promise<Actions> {
const actionsData = await this.getActionData(input);
return actionsData ?? [];
return (await this.getActionData(input)) ?? [];
}

async getActionData(input: ActionFilterOptionsInput): Promise<Actions> {
// Request action zkApp info from the Archive Node Database
this.tracingService.startSpan('Actions SQL');
const rows = await this.executeActionsQuery(input);
this.tracingService.endSpan();

this.tracingService.startSpan('Actions Processing');
// Partition the rows into a map where the keys are element ids and the values are field values.
const elementIdFieldValues = getElementIdFieldValues(rows);
// Partition the rows into a map where the keys are block hashes and the values are maps of transaction hashes to array of rows.
const blocksWithTransactions = partitionBlocks(rows);
const actionsData = this.deriveActionsFromBlocks(
// Map the rows into Action instances.
const actionsData = this.blocksToActions(
blocksWithTransactions,
elementIdFieldValues
);
this.tracingService.endSpan();
const sortedActionsData = sortAndFilterBlocks(actionsData);
return sortedActionsData;
// Sort and filter the actions.
return sortAndFilterBlocks(actionsData);
}

async executeActionsQuery(input: ActionFilterOptionsInput) {
Expand All @@ -72,15 +76,26 @@ class ActionsService {
);
}

deriveActionsFromBlocks(
/**
* This function is used to map the rows into Action instances.
* The rows are partitioned into a map where the keys are block hashes and the values are maps of transaction hashes to array of rows.
* We
*
* @param blocksWithTransactions
* @param elementIdFieldValues
* @returns
*/
blocksToActions(
blocksWithTransactions: BlocksWithTransactionsMap,
elementIdFieldValues: FieldElementIdWithValueMap
elementIdFieldValues: FieldIdValuePairs
) {
const actions: Actions = [];
const blockMapEntries = Array.from(blocksWithTransactions.entries());
for (let i = 0; i < blockMapEntries.length; i++) {
const transactions = blockMapEntries[i][1];
const transaction = transactions.values().next().value[0];
const blockTransactionEntries = Array.from(
blocksWithTransactions.entries()
);
for (let i = 0; i < blockTransactionEntries.length; i++) {
const transactions = blockTransactionEntries[i][1];
const transaction = transactions.values().next().value[0]; // Get the first transaction row in the block.
const blockInfo = createBlockInfo(transaction);
const {
action_state_value1,
Expand All @@ -90,19 +105,13 @@ class ActionsService {
action_state_value5,
} = transaction;

const actionsData: Action[][] = [];
for (const [, transaction] of transactions) {
const filteredBlocks = removeRedundantEmittedFields(transaction);
const actionData = mapActionOrEvent(
'action',
filteredBlocks,
elementIdFieldValues
) as Action[];
actionsData.push(actionData);
}
const actionsData = this.transactionToActions(
elementIdFieldValues,
transactions
);
actions.push({
blockInfo,
actionData: actionsData.flat(),
actionData: actionsData,
actionState: {
/* eslint-disable */
actionStateOne: action_state_value1!,
Expand All @@ -116,4 +125,22 @@ class ActionsService {
}
return actions;
}

transactionToActions(
elementIdFieldValues: FieldIdValuePairs,
transactions: Transaction
) {
const actions: Action[][] = [];

for (const [, transactionRow] of transactions) {
const filteredTransactions = removeRedundantEmittedFields(transactionRow);
const actionData = mapActionOrEvent(
'action',
filteredTransactions,
elementIdFieldValues
) as Action[];
actions.push(actionData);
}
return actions.flat();
}
}
11 changes: 5 additions & 6 deletions src/db/archive-node-adapter/events-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
BlockStatusFilter,
BlocksWithTransactionsMap,
DEFAULT_TOKEN_ID,
FieldElementIdWithValueMap,
FieldIdValuePairs,
Events,
Event,
} from '../../models/types';
Expand Down Expand Up @@ -34,8 +34,7 @@ class EventsService {
}

async getEvents(input: EventFilterOptionsInput): Promise<Events> {
const eventsData = await this.getEventData(input);
return eventsData ?? [];
return (await this.getEventData(input)) ?? [];
}

async getEventData(input: EventFilterOptionsInput): Promise<Events> {
Expand All @@ -51,7 +50,7 @@ class EventsService {
// print out map
// console.log(util.inspect(blocksWithTransactions, false, null, true));

const eventsData = this.deriveEventsFromBlocks(
const eventsData = this.blocksToEvents(
blocksWithTransactions,
elementIdFieldValues
);
Expand Down Expand Up @@ -80,9 +79,9 @@ class EventsService {
);
}

deriveEventsFromBlocks(
blocksToEvents(
blocksWithTransactions: BlocksWithTransactionsMap,
elementIdFieldValues: FieldElementIdWithValueMap
elementIdFieldValues: FieldIdValuePairs
) {
const events: Events = [];
const blockMapEntries = Array.from(blocksWithTransactions.entries());
Expand Down
5 changes: 0 additions & 5 deletions src/db/archive-node-adapter/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ function blocksAccessedCTE(
SELECT
requesting_zkapp_account_identifier_id,
block_id,
account_identifier_id,
zkapp_id,
id AS account_access_id,
state_hash,
parent_hash,
height,
Expand Down Expand Up @@ -110,14 +108,11 @@ function emittedZkAppCommandsCTE(db_client: postgres.Sql) {
SELECT
blocks_accessed.*,
zkcu.id AS zkapp_account_update_id,
zkapp_fee_payer_body_id,
zkapp_account_updates_ids,
authorization_kind,
status,
memo,
hash,
body_id,
events_id,
actions_id
FROM
blocks_accessed
Expand Down
Loading

0 comments on commit 368d685

Please sign in to comment.