Skip to content

Commit

Permalink
adjust query to support ordering (#107)
Browse files Browse the repository at this point in the history
Fixes: #106 

This PR adds `sequenceNumber`, as well as `zkappAccountUpdateIds` to the
graphQL schema, which will expose enough information for clients to
verify the order in which actions were processed by consensus. It also
attempts to return actions in that order, and exposes the ordering spec
in a unit test.

We also overhaul the test contract and test suite to emit more random
and different values so that sequential events and actions can be
identified by their data.

I have tested this branch locally against the [sibling
branch](o1-labs/o1js#1917) in o1js.
  • Loading branch information
45930 authored Dec 17, 2024
1 parent 9dcee36 commit 44a437a
Show file tree
Hide file tree
Showing 16 changed files with 818 additions and 168 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
],
"rules": {
"@typescript-eslint/no-namespace": "off"
},
"overrides": [
{
"files": ["src/schema.ts"],
Expand Down
3 changes: 3 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ input ActionFilterOptionsInput {
}

type EventData {
accountUpdateId: String!
transactionInfo: TransactionInfo
data: [String]!
}
Expand Down Expand Up @@ -50,6 +51,8 @@ type TransactionInfo {
hash: String!
memo: String!
authorizationKind: String!
sequenceNumber: Int! # TODO: Is it ok to make this required?
zkappAccountUpdateIds: [Int]!
}

type ActionStates {
Expand Down
3 changes: 3 additions & 0 deletions src/blockchain/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export enum BlockStatusFilter {
}

export type Event = {
accountUpdateId: string;
transactionInfo: TransactionInfo;
data: string[];
};
Expand Down Expand Up @@ -41,6 +42,8 @@ export type TransactionInfo = {
hash: string;
memo: string;
authorizationKind: string;
sequenceNumber: number;
zkappAccountUpdateIds: number[];
};

export type Events = {
Expand Down
4 changes: 4 additions & 0 deletions src/blockchain/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,18 @@ export function createTransactionInfo(
hash: row.hash,
memo: row.memo,
authorizationKind: row.authorization_kind,
sequenceNumber: row.sequence_number,
zkappAccountUpdateIds: row.zkapp_account_updates_ids,
};
}

export function createEvent(
accountUpdateId: string,
data: string[],
transactionInfo: TransactionInfo
): Event {
return {
accountUpdateId,
data,
transactionInfo,
};
Expand Down
153 changes: 140 additions & 13 deletions src/db/sql/events-actions/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,23 @@ 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,
zkapp_account_updates_ids,
authorization_kind,
Expand All @@ -133,13 +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.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 @@ -150,13 +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.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 @@ -176,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 @@ -216,7 +313,37 @@ 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
`;
}
Expand Down
Loading

0 comments on commit 44a437a

Please sign in to comment.