Skip to content

Commit

Permalink
messing with query
Browse files Browse the repository at this point in the history
  • Loading branch information
45930 committed Dec 11, 2024
1 parent 29c936b commit 49cb8b0
Show file tree
Hide file tree
Showing 7 changed files with 256 additions and 49 deletions.
14 changes: 8 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/blockchain/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ export function createTransactionInfo(
authorizationKind: row.authorization_kind,
sequenceNumber: row.sequence_number,
zkappAccountUpdateIds: row.zkapp_account_updates_ids,
zkappEventElementIds: row.zkapp_event_element_ids,
zkappFieldArrayElementIds: row.zkapp_field_array_element_ids,
zkappEventElementIds: row.event_element_ids,
zkappFieldArrayElementIds: row.event_field_element_ids,
};
}

Expand Down
165 changes: 150 additions & 15 deletions src/db/sql/events-actions/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,21 @@ function emittedZkAppCommandsCTE(db_client: postgres.Sql) {
return db_client`
emitted_zkapp_commands AS (
SELECT
blocks_accessed.*,
blocks_accessed.requesting_zkapp_account_identifier_id,
blocks_accessed.block_id,
blocks_accessed.account_identifier_id,
blocks_accessed.zkapp_id,
blocks_accessed.account_access_id,
blocks_accessed.state_hash,
blocks_accessed.parent_hash,
blocks_accessed.height,
blocks_accessed.global_slot_since_genesis,
blocks_accessed.global_slot_since_hard_fork,
blocks_accessed.timestamp,
blocks_accessed.chain_status,
blocks_accessed.ledger_hash,
blocks_accessed.distance_from_max_block_height,
blocks_accessed.last_vrf_output,
zkcu.id AS zkapp_account_update_id,
bzkc.sequence_no AS sequence_number,
zkapp_fee_payer_body_id,
Expand Down Expand Up @@ -134,14 +148,41 @@ function emittedEventsCTE(db_client: postgres.Sql) {
return db_client`
emitted_events AS (
SELECT
*,
zke.id AS zkapp_event_id,
zke.element_ids AS zkapp_event_element_ids,
zkfa.element_ids AS zkapp_field_array_element_ids,
zkfa.id AS zkapp_event_array_id
emitted_zkapp_commands.requesting_zkapp_account_identifier_id,
emitted_zkapp_commands.block_id,
emitted_zkapp_commands.account_identifier_id,
emitted_zkapp_commands.zkapp_id,
emitted_zkapp_commands.account_access_id,
emitted_zkapp_commands.state_hash,
emitted_zkapp_commands.parent_hash,
emitted_zkapp_commands.height,
emitted_zkapp_commands.global_slot_since_genesis,
emitted_zkapp_commands.global_slot_since_hard_fork,
emitted_zkapp_commands.timestamp,
emitted_zkapp_commands.chain_status,
emitted_zkapp_commands.ledger_hash,
emitted_zkapp_commands.distance_from_max_block_height,
emitted_zkapp_commands.last_vrf_output,
emitted_zkapp_commands.zkapp_account_update_id,
emitted_zkapp_commands.sequence_number,
emitted_zkapp_commands.zkapp_fee_payer_body_id,
emitted_zkapp_commands.zkapp_account_updates_ids,
emitted_zkapp_commands.authorization_kind,
emitted_zkapp_commands.status,
emitted_zkapp_commands.memo,
emitted_zkapp_commands.hash,
emitted_zkapp_commands.body_id,
emitted_zkapp_commands.events_id,
emitted_zkapp_commands.actions_id,
zke.id AS account_update_event_id,
zke.element_ids AS event_element_ids,
zkfa.element_ids AS event_field_element_ids,
zkfa.id AS event_field_elements_id,
zkf.id AS field_id,
zkf.field AS field_value
FROM
emitted_zkapp_commands
INNER JOIN zkapp_events zke ON zke.id = events_id
INNER JOIN zkapp_events zke ON zke.id = emitted_zkapp_commands.events_id
INNER JOIN zkapp_field_array zkfa ON zkfa.id = ANY(zke.element_ids)
INNER JOIN zkapp_field zkf ON zkf.id = ANY(zkfa.element_ids)
)
Expand All @@ -152,14 +193,38 @@ function emittedActionsCTE(db_client: postgres.Sql) {
return db_client`
emitted_actions AS (
SELECT
*,
zke.id AS zkapp_event_id,
zke.element_ids AS zkapp_event_element_ids,
zkfa.element_ids AS zkapp_field_array_element_ids,
zkfa.id AS zkapp_event_array_id
emitted_zkapp_commands.block_id,
emitted_zkapp_commands.zkapp_id,
emitted_zkapp_commands.state_hash,
emitted_zkapp_commands.parent_hash,
emitted_zkapp_commands.height,
emitted_zkapp_commands.global_slot_since_genesis,
emitted_zkapp_commands.global_slot_since_hard_fork,
emitted_zkapp_commands.timestamp,
emitted_zkapp_commands.chain_status,
emitted_zkapp_commands.ledger_hash,
emitted_zkapp_commands.distance_from_max_block_height,
emitted_zkapp_commands.last_vrf_output,
emitted_zkapp_commands.zkapp_account_update_id,
emitted_zkapp_commands.sequence_number,
emitted_zkapp_commands.zkapp_fee_payer_body_id,
emitted_zkapp_commands.zkapp_account_updates_ids,
emitted_zkapp_commands.authorization_kind,
emitted_zkapp_commands.status,
emitted_zkapp_commands.memo,
emitted_zkapp_commands.hash,
emitted_zkapp_commands.body_id,
emitted_zkapp_commands.events_id,
emitted_zkapp_commands.actions_id,
zke.id AS account_update_event_id,
zke.element_ids AS event_element_ids,
zkfa.element_ids AS event_field_element_ids,
zkfa.id AS event_field_elements_id,
zkf.id AS field_id,
zkf.field AS field_value
FROM
emitted_zkapp_commands
INNER JOIN zkapp_events zke ON zke.id = actions_id
INNER JOIN zkapp_events zke ON zke.id = emitted_zkapp_commands.actions_id
INNER JOIN zkapp_field_array zkfa ON zkfa.id = ANY(zke.element_ids)
INNER JOIN zkapp_field zkf ON zkf.id = ANY(zkfa.element_ids)
)
Expand All @@ -179,7 +244,36 @@ function emittedActionStateCTE(
zkf2.field AS action_state_value3,
zkf3.field AS action_state_value4,
zkf4.field AS action_state_value5,
emitted_actions.*
emitted_actions.last_vrf_output,
emitted_actions.block_id,
emitted_actions.zkapp_id,
emitted_actions.state_hash,
emitted_actions.parent_hash,
emitted_actions.height,
emitted_actions.global_slot_since_genesis,
emitted_actions.global_slot_since_hard_fork,
emitted_actions.timestamp,
emitted_actions.chain_status,
emitted_actions.ledger_hash,
emitted_actions.distance_from_max_block_height,
emitted_actions.last_vrf_output,
emitted_actions.zkapp_account_update_id,
emitted_actions.sequence_number,
emitted_actions.zkapp_fee_payer_body_id,
emitted_actions.zkapp_account_updates_ids,
emitted_actions.authorization_kind,
emitted_actions.status,
emitted_actions.memo,
emitted_actions.hash,
emitted_actions.body_id,
emitted_actions.events_id,
emitted_actions.actions_id,
emitted_actions.account_update_event_id,
emitted_actions.event_element_ids,
emitted_actions.event_field_element_ids,
emitted_actions.event_field_elements_id,
emitted_actions.field_id,
emitted_actions.field_value
FROM
emitted_actions
INNER JOIN zkapp_accounts zkacc ON zkacc.id = emitted_actions.zkapp_id
Expand Down Expand Up @@ -219,11 +313,52 @@ export function getEventsQuery(
${blocksAccessedCTE(db_client, status, to, from)},
${emittedZkAppCommandsCTE(db_client)},
${emittedEventsCTE(db_client)}
SELECT *
SELECT
last_vrf_output,
block_id,
zkapp_id,
state_hash,
parent_hash,
height,
global_slot_since_genesis,
global_slot_since_hard_fork,
timestamp,
chain_status,
ledger_hash,
distance_from_max_block_height,
last_vrf_output,
zkapp_account_update_id,
sequence_number,
zkapp_fee_payer_body_id,
zkapp_account_updates_ids,
authorization_kind,
status,
memo,
hash,
body_id,
events_id,
actions_id,
account_update_event_id,
event_element_ids,
event_field_element_ids,
event_field_elements_id,
field_id,
field_value
FROM emitted_events
`;
}

async function logQuery(strings: TemplateStringsArray, ...values: any[]) {

Check failure on line 351 in src/db/sql/events-actions/queries.ts

View workflow job for this annotation

GitHub Actions / Linting

'logQuery' is defined but never used

Check failure on line 351 in src/db/sql/events-actions/queries.ts

View workflow job for this annotation

GitHub Actions / Linting

Unexpected any. Specify a different type
const resolvedValues = await Promise.all(values);

Check failure on line 352 in src/db/sql/events-actions/queries.ts

View workflow job for this annotation

GitHub Actions / Linting

'resolvedValues' is assigned a value but never used

let query = '';
strings.forEach((string, i) => {
query += string + (values[i] !== undefined ? values[i] : '');
});
console.log('Interpolated Query:', query);
return query;
}

export function getActionsQuery(
db_client: postgres.Sql,
address: string,
Expand Down
35 changes: 19 additions & 16 deletions src/db/sql/events-actions/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ export type ArchiveNodeDatabaseRow = {
// Unique identifier for the zkapp account update.
zkapp_account_update_id: number;

// TODO: Would this be more accurately named `zkapp_event_field_array_id`?
// Unique identifier for the event within an account update.
zkapp_event_id: number;

// List of identifiers inside a zkapp account update.
zkapp_account_updates_ids: number[];

Expand All @@ -61,25 +57,32 @@ export type ArchiveNodeDatabaseRow = {
// Unique hash identifier.
hash: string;

// The unique identifier that maps events/actions to a specific zkApp.
zkapp_event_array_id: number;
// id of a single event in an account update
account_update_event_id: number;

// TODO: Would this be more accuratley named `zkapp_event_field_array_ids`?
// List of `element_ids` that are used to construct the zkApp event.
zkapp_event_element_ids: number[];
// id of an array of events per account update
// account_update_events_id points to an array of account_update_event_id
account_update_events_id: number;

// List of `element_ids` that are used to construct the field array.
zkapp_field_array_element_ids: number[];
// List of ids of the field arrays used to construct the event array
// each account_update_event may have many event elements
event_element_ids: number[];

// `element_ids` represent a list of identifiers that map to specific field values.
// These are used to identify which field values are used in a zkApp transaction and construct the data returned to the user.
element_ids: number[];
// id of an array of field elements
// each event_element_id in event_element_ids is an event_field_elements_id
event_field_elements_id: number;

// List of `element_ids` that are used to construct the field array.
// Each event_field_elements_id points to an array of event_field_element_ids
event_field_element_ids: number[];

// Unique id for a `field` value. Each field value in the Archive Node has it's own unique id.
id: number;
// Each element_id in event_field_element_ids is a field_id
field_id: number;

// Field value information.
field: string;
// Each field_id points to a single field_value
field_value: string;

// Output of the last VRF (Verifiable Random Function).
last_vrf_output: string;
Expand Down
1 change: 1 addition & 0 deletions src/services/actions-service/actions-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class ActionsService implements IActionsService {
): Promise<Actions> {
const sqlSpan = tracingState.startSpan('actions.SQL');
const rows = await this.executeActionsQuery(input);

sqlSpan.end();

const processingSpan = tracingState.startSpan('actions.processing');
Expand Down
20 changes: 10 additions & 10 deletions src/services/data-adapters/database-row-adapters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ function partitionBlocks(rows: ArchiveNodeDatabaseRow[]) {
function getElementIdFieldValues(rows: ArchiveNodeDatabaseRow[]) {
const elementIdValues: FieldElementIdWithValueMap = new Map();
for (let i = 0; i < rows.length; i++) {
const { id, field } = rows[i];
elementIdValues.set(id.toString(), field);
const { field_id, field_value } = rows[i];
elementIdValues.set(field_id.toString(), field_value);
}
return elementIdValues;
}
Expand Down Expand Up @@ -123,8 +123,8 @@ function removeRedundantEmittedFields(
for (let i = 0; i < archiveNodeRow.length; i++) {
const currentRow = archiveNodeRow[i];
const {
zkapp_event_array_id, // The unique id for the event/action emitted
zkapp_event_element_ids, // The list of field ids that make up the event/action
account_update_event_id, // The unique id for the event/action emitted
event_field_element_ids, // The list of field ids that make up the event/action
zkapp_account_update_id, // The unique id for the account update that emitted the event/action
zkapp_account_updates_ids, // List of all account update ids inside the transaction
} = currentRow;
Expand All @@ -133,15 +133,15 @@ function removeRedundantEmittedFields(
// This is used to check if we have already seen this event/action before.
const uniqueEventId = createUniqueEventId(
zkapp_account_update_id,
zkapp_event_array_id
account_update_event_id
);

if (!seenEventOrActionIds.has(uniqueEventId)) {
// Since multiple events/actions can be emitted in a single account update, we want to put back the event/action
// in the correct place. To do this, we need to know the index of the event array id in the list of event array ids (these stored in order by the Archive Node)
const emittedEventOrActionIndexes = findAllIndexes(
zkapp_event_element_ids,
zkapp_event_array_id
event_field_element_ids,
account_update_event_id
);

// Since multiple account updates can be emitted in a single transaction, we need to know the index of the account update id in the list of account update ids
Expand All @@ -152,7 +152,7 @@ function removeRedundantEmittedFields(

if (accountUpdateIndexes.length === 0) {
throw new Error(
`No matching account update found for the given account update ID (${zkapp_account_update_id}) and event array ID (${zkapp_event_array_id}).`
`No matching account update found for the given account update ID (${zkapp_account_update_id}) and event array ID (${account_update_event_id}).`
);
}

Expand Down Expand Up @@ -199,10 +199,10 @@ function mapActionOrEvent(
) {
const data: (Event | Action)[] = [];
for (let i = 0; i < rows.length; i++) {
const { zkapp_account_update_id, element_ids } = rows[i];
const { zkapp_account_update_id, event_field_element_ids } = rows[i];
const transactionInfo = createTransactionInfo(rows[i]);
const elementIdToFieldValues = getFieldValuesFromElementIds(
element_ids,
event_field_element_ids,
elementIdFieldValues
);

Expand Down
Loading

0 comments on commit 49cb8b0

Please sign in to comment.