Skip to content

Commit 44a437a

Browse files
authored
adjust query to support ordering (#107)
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.
1 parent 9dcee36 commit 44a437a

File tree

16 files changed

+818
-168
lines changed

16 files changed

+818
-168
lines changed

Diff for: .eslintrc.json

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
"plugin:@typescript-eslint/eslint-recommended",
1414
"plugin:@typescript-eslint/recommended"
1515
],
16+
"rules": {
17+
"@typescript-eslint/no-namespace": "off"
18+
},
1619
"overrides": [
1720
{
1821
"files": ["src/schema.ts"],

Diff for: schema.graphql

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ input ActionFilterOptionsInput {
2323
}
2424

2525
type EventData {
26+
accountUpdateId: String!
2627
transactionInfo: TransactionInfo
2728
data: [String]!
2829
}
@@ -50,6 +51,8 @@ type TransactionInfo {
5051
hash: String!
5152
memo: String!
5253
authorizationKind: String!
54+
sequenceNumber: Int! # TODO: Is it ok to make this required?
55+
zkappAccountUpdateIds: [Int]!
5356
}
5457

5558
type ActionStates {

Diff for: src/blockchain/types.ts

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export enum BlockStatusFilter {
1313
}
1414

1515
export type Event = {
16+
accountUpdateId: string;
1617
transactionInfo: TransactionInfo;
1718
data: string[];
1819
};
@@ -41,6 +42,8 @@ export type TransactionInfo = {
4142
hash: string;
4243
memo: string;
4344
authorizationKind: string;
45+
sequenceNumber: number;
46+
zkappAccountUpdateIds: number[];
4447
};
4548

4649
export type Events = {

Diff for: src/blockchain/utils.ts

+4
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,18 @@ export function createTransactionInfo(
2424
hash: row.hash,
2525
memo: row.memo,
2626
authorizationKind: row.authorization_kind,
27+
sequenceNumber: row.sequence_number,
28+
zkappAccountUpdateIds: row.zkapp_account_updates_ids,
2729
};
2830
}
2931

3032
export function createEvent(
33+
accountUpdateId: string,
3134
data: string[],
3235
transactionInfo: TransactionInfo
3336
): Event {
3437
return {
38+
accountUpdateId,
3539
data,
3640
transactionInfo,
3741
};

Diff for: src/db/sql/events-actions/queries.ts

+140-13
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,23 @@ function emittedZkAppCommandsCTE(db_client: postgres.Sql) {
106106
return db_client`
107107
emitted_zkapp_commands AS (
108108
SELECT
109-
blocks_accessed.*,
109+
blocks_accessed.requesting_zkapp_account_identifier_id,
110+
blocks_accessed.block_id,
111+
blocks_accessed.account_identifier_id,
112+
blocks_accessed.zkapp_id,
113+
blocks_accessed.account_access_id,
114+
blocks_accessed.state_hash,
115+
blocks_accessed.parent_hash,
116+
blocks_accessed.height,
117+
blocks_accessed.global_slot_since_genesis,
118+
blocks_accessed.global_slot_since_hard_fork,
119+
blocks_accessed.timestamp,
120+
blocks_accessed.chain_status,
121+
blocks_accessed.ledger_hash,
122+
blocks_accessed.distance_from_max_block_height,
123+
blocks_accessed.last_vrf_output,
110124
zkcu.id AS zkapp_account_update_id,
125+
bzkc.sequence_no AS sequence_number,
111126
zkapp_fee_payer_body_id,
112127
zkapp_account_updates_ids,
113128
authorization_kind,
@@ -133,13 +148,41 @@ function emittedEventsCTE(db_client: postgres.Sql) {
133148
return db_client`
134149
emitted_events AS (
135150
SELECT
136-
*,
137-
zke.id AS zkapp_event_id,
138-
zke.element_ids AS zkapp_event_element_ids,
139-
zkfa.id AS zkapp_event_array_id
151+
emitted_zkapp_commands.requesting_zkapp_account_identifier_id,
152+
emitted_zkapp_commands.block_id,
153+
emitted_zkapp_commands.account_identifier_id,
154+
emitted_zkapp_commands.zkapp_id,
155+
emitted_zkapp_commands.account_access_id,
156+
emitted_zkapp_commands.state_hash,
157+
emitted_zkapp_commands.parent_hash,
158+
emitted_zkapp_commands.height,
159+
emitted_zkapp_commands.global_slot_since_genesis,
160+
emitted_zkapp_commands.global_slot_since_hard_fork,
161+
emitted_zkapp_commands.timestamp,
162+
emitted_zkapp_commands.chain_status,
163+
emitted_zkapp_commands.ledger_hash,
164+
emitted_zkapp_commands.distance_from_max_block_height,
165+
emitted_zkapp_commands.last_vrf_output,
166+
emitted_zkapp_commands.zkapp_account_update_id,
167+
emitted_zkapp_commands.sequence_number,
168+
emitted_zkapp_commands.zkapp_fee_payer_body_id,
169+
emitted_zkapp_commands.zkapp_account_updates_ids,
170+
emitted_zkapp_commands.authorization_kind,
171+
emitted_zkapp_commands.status,
172+
emitted_zkapp_commands.memo,
173+
emitted_zkapp_commands.hash,
174+
emitted_zkapp_commands.body_id,
175+
emitted_zkapp_commands.events_id,
176+
emitted_zkapp_commands.actions_id,
177+
zke.id AS account_update_event_id,
178+
zke.element_ids AS event_element_ids,
179+
zkfa.element_ids AS event_field_element_ids,
180+
zkfa.id AS event_field_elements_id,
181+
zkf.id AS field_id,
182+
zkf.field AS field_value
140183
FROM
141184
emitted_zkapp_commands
142-
INNER JOIN zkapp_events zke ON zke.id = events_id
185+
INNER JOIN zkapp_events zke ON zke.id = emitted_zkapp_commands.events_id
143186
INNER JOIN zkapp_field_array zkfa ON zkfa.id = ANY(zke.element_ids)
144187
INNER JOIN zkapp_field zkf ON zkf.id = ANY(zkfa.element_ids)
145188
)
@@ -150,13 +193,38 @@ function emittedActionsCTE(db_client: postgres.Sql) {
150193
return db_client`
151194
emitted_actions AS (
152195
SELECT
153-
*,
154-
zke.id AS zkapp_event_id,
155-
zke.element_ids AS zkapp_event_element_ids,
156-
zkfa.id AS zkapp_event_array_id
196+
emitted_zkapp_commands.block_id,
197+
emitted_zkapp_commands.zkapp_id,
198+
emitted_zkapp_commands.state_hash,
199+
emitted_zkapp_commands.parent_hash,
200+
emitted_zkapp_commands.height,
201+
emitted_zkapp_commands.global_slot_since_genesis,
202+
emitted_zkapp_commands.global_slot_since_hard_fork,
203+
emitted_zkapp_commands.timestamp,
204+
emitted_zkapp_commands.chain_status,
205+
emitted_zkapp_commands.ledger_hash,
206+
emitted_zkapp_commands.distance_from_max_block_height,
207+
emitted_zkapp_commands.last_vrf_output,
208+
emitted_zkapp_commands.zkapp_account_update_id,
209+
emitted_zkapp_commands.sequence_number,
210+
emitted_zkapp_commands.zkapp_fee_payer_body_id,
211+
emitted_zkapp_commands.zkapp_account_updates_ids,
212+
emitted_zkapp_commands.authorization_kind,
213+
emitted_zkapp_commands.status,
214+
emitted_zkapp_commands.memo,
215+
emitted_zkapp_commands.hash,
216+
emitted_zkapp_commands.body_id,
217+
emitted_zkapp_commands.events_id,
218+
emitted_zkapp_commands.actions_id,
219+
zke.id AS account_update_event_id,
220+
zke.element_ids AS event_element_ids,
221+
zkfa.element_ids AS event_field_element_ids,
222+
zkfa.id AS event_field_elements_id,
223+
zkf.id AS field_id,
224+
zkf.field AS field_value
157225
FROM
158226
emitted_zkapp_commands
159-
INNER JOIN zkapp_events zke ON zke.id = actions_id
227+
INNER JOIN zkapp_events zke ON zke.id = emitted_zkapp_commands.actions_id
160228
INNER JOIN zkapp_field_array zkfa ON zkfa.id = ANY(zke.element_ids)
161229
INNER JOIN zkapp_field zkf ON zkf.id = ANY(zkfa.element_ids)
162230
)
@@ -176,7 +244,36 @@ function emittedActionStateCTE(
176244
zkf2.field AS action_state_value3,
177245
zkf3.field AS action_state_value4,
178246
zkf4.field AS action_state_value5,
179-
emitted_actions.*
247+
emitted_actions.last_vrf_output,
248+
emitted_actions.block_id,
249+
emitted_actions.zkapp_id,
250+
emitted_actions.state_hash,
251+
emitted_actions.parent_hash,
252+
emitted_actions.height,
253+
emitted_actions.global_slot_since_genesis,
254+
emitted_actions.global_slot_since_hard_fork,
255+
emitted_actions.timestamp,
256+
emitted_actions.chain_status,
257+
emitted_actions.ledger_hash,
258+
emitted_actions.distance_from_max_block_height,
259+
emitted_actions.last_vrf_output,
260+
emitted_actions.zkapp_account_update_id,
261+
emitted_actions.sequence_number,
262+
emitted_actions.zkapp_fee_payer_body_id,
263+
emitted_actions.zkapp_account_updates_ids,
264+
emitted_actions.authorization_kind,
265+
emitted_actions.status,
266+
emitted_actions.memo,
267+
emitted_actions.hash,
268+
emitted_actions.body_id,
269+
emitted_actions.events_id,
270+
emitted_actions.actions_id,
271+
emitted_actions.account_update_event_id,
272+
emitted_actions.event_element_ids,
273+
emitted_actions.event_field_element_ids,
274+
emitted_actions.event_field_elements_id,
275+
emitted_actions.field_id,
276+
emitted_actions.field_value
180277
FROM
181278
emitted_actions
182279
INNER JOIN zkapp_accounts zkacc ON zkacc.id = emitted_actions.zkapp_id
@@ -216,7 +313,37 @@ export function getEventsQuery(
216313
${blocksAccessedCTE(db_client, status, to, from)},
217314
${emittedZkAppCommandsCTE(db_client)},
218315
${emittedEventsCTE(db_client)}
219-
SELECT *
316+
SELECT
317+
last_vrf_output,
318+
block_id,
319+
zkapp_id,
320+
state_hash,
321+
parent_hash,
322+
height,
323+
global_slot_since_genesis,
324+
global_slot_since_hard_fork,
325+
timestamp,
326+
chain_status,
327+
ledger_hash,
328+
distance_from_max_block_height,
329+
last_vrf_output,
330+
zkapp_account_update_id,
331+
sequence_number,
332+
zkapp_fee_payer_body_id,
333+
zkapp_account_updates_ids,
334+
authorization_kind,
335+
status,
336+
memo,
337+
hash,
338+
body_id,
339+
events_id,
340+
actions_id,
341+
account_update_event_id,
342+
event_element_ids,
343+
event_field_element_ids,
344+
event_field_elements_id,
345+
field_id,
346+
field_value
220347
FROM emitted_events
221348
`;
222349
}

0 commit comments

Comments
 (0)