Skip to content

Commit

Permalink
squashing
Browse files Browse the repository at this point in the history
  • Loading branch information
45930 committed Nov 20, 2024
1 parent 1bad4d3 commit 8822808
Show file tree
Hide file tree
Showing 16 changed files with 489 additions and 29 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"build": "npm run clean && npx tsc",
"start": "node build/src/index.js",
"dev": "cross-env NODE_NO_WARNINGS=1 npx nodemon src/index.ts",
"test": "npx tsc && for testfile in $(find build/tests -type f -name '*.test.js'); do node $testfile; done",
"test": "./run-tests.sh",
"clean": "rimraf ./build",
"gen-test-mocks": "cross-env NODE_NO_WARNINGS=1 node --loader ts-node/esm tests/mocked_sql/generate_mock_data.ts",
"benchmark": "cross-env NODE_NO_WARNINGS=1 node --loader ts-node/esm benchmark/setup.ts && npx artillery run benchmark/graphql.yaml --output benchmark/report.json",
Expand Down
11 changes: 11 additions & 0 deletions run-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash
set -e
shopt -s globstar # to expand '**' into nested directories./

npm run build

# find all unit tests in build and run them
for f in ./build/**/*test.js; do
echo "Running $f"
node --enable-source-maps --stack-trace-limit=1000 --test $f;
done
3 changes: 3 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ type TransactionInfo {
hash: String!
memo: String!
authorizationKind: String!
sequenceNumber: Int! # TODO: Is it ok to make this required?

Check notice on line 53 in schema.graphql

View workflow job for this annotation

GitHub Actions / GraphQL Inspector

Field 'sequenceNumber' was added to object type 'TransactionInfo'

Field 'sequenceNumber' was added to object type 'TransactionInfo'
zkappAccountUpdateIds: [Int]!

Check notice on line 54 in schema.graphql

View workflow job for this annotation

GitHub Actions / GraphQL Inspector

Field 'zkappAccountUpdateIds' was added to object type 'TransactionInfo'

Field 'zkappAccountUpdateIds' was added to object type 'TransactionInfo'
zkappEventElementIds: [Int]!

Check notice on line 55 in schema.graphql

View workflow job for this annotation

GitHub Actions / GraphQL Inspector

Field 'zkappEventElementIds' was added to object type 'TransactionInfo'

Field 'zkappEventElementIds' was added to object type 'TransactionInfo'
}

type ActionStates {
Expand Down
4 changes: 4 additions & 0 deletions src/blockchain/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export type Event = {

export type Action = {
accountUpdateId: string;
eventElementId: string;
transactionInfo: TransactionInfo;
data: string[];
};
Expand All @@ -41,6 +42,9 @@ export type TransactionInfo = {
hash: string;
memo: string;
authorizationKind: string;
sequenceNumber: number;
zkappAccountUpdateIds: number[];
zkappEventElementIds: number[];
};

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

Expand All @@ -39,11 +42,13 @@ export function createEvent(

export function createAction(
accountUpdateId: string,
eventElementId: string,
data: string[],
transactionInfo: TransactionInfo
): Action {
return {
accountUpdateId,
eventElementId,
data,
transactionInfo,
};
Expand Down
2 changes: 2 additions & 0 deletions src/db/sql/events-actions/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ function emittedZkAppCommandsCTE(db_client: postgres.Sql) {
SELECT
blocks_accessed.*,
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 Down Expand Up @@ -135,6 +136,7 @@ function emittedEventsCTE(db_client: postgres.Sql) {
SELECT
*,
zke.id AS zkapp_event_id,
zkf.id AS zkapp_event_element_id,
zke.element_ids AS zkapp_event_element_ids,
zkfa.id AS zkapp_event_array_id
FROM
Expand Down
6 changes: 6 additions & 0 deletions src/db/sql/events-actions/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ export type ArchiveNodeDatabaseRow = {
// Current status of the block within the chain.
chain_status: string;

// Sequence number of the transaction within a block
sequence_number: number;

// Hash representing the ledger state.
ledger_hash: string;

Expand All @@ -42,6 +45,9 @@ export type ArchiveNodeDatabaseRow = {
// Unique identifier for the zkapp account update.
zkapp_account_update_id: number;

// Unique identifier for the event element within an account update.
zkapp_event_id: number;

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

Expand Down
14 changes: 14 additions & 0 deletions src/resolvers-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,10 @@ export type TransactionInfo = {
authorizationKind: Scalars['String']['output'];
hash: Scalars['String']['output'];
memo: Scalars['String']['output'];
sequenceNumber: Scalars['Int']['output'];
status: Scalars['String']['output'];
zkappAccountUpdateIds: Array<Maybe<Scalars['Int']['output']>>;
zkappEventElementIds: Array<Maybe<Scalars['Int']['output']>>;
};

export type ResolverTypeWrapper<T> = Promise<T> | T;
Expand Down Expand Up @@ -448,7 +451,18 @@ export type TransactionInfoResolvers<
>;
hash?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
memo?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
sequenceNumber?: Resolver<ResolversTypes['Int'], ParentType, ContextType>;
status?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
zkappAccountUpdateIds?: Resolver<
Array<Maybe<ResolversTypes['Int']>>,
ParentType,
ContextType
>;
zkappEventElementIds?: Resolver<
Array<Maybe<ResolversTypes['Int']>>,
ParentType,
ContextType
>;
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
};

Expand Down
40 changes: 38 additions & 2 deletions src/services/actions-service/actions-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class ActionsService implements IActionsService {
);
for (let i = 0; i < blockTransactionEntries.length; i++) {
const transactions = blockTransactionEntries[i][1];
const transaction = transactions.values().next().value[0];
const transaction = transactions.values().next().value![0];
const blockInfo = createBlockInfo(transaction);
const {
action_state_value1,
Expand All @@ -143,7 +143,7 @@ class ActionsService implements IActionsService {
}
actions.push({
blockInfo,
actionData: actionsData.flat(),
actionData: this.sortActions(actionsData.flat()),
actionState: {
/* eslint-disable */
actionStateOne: action_state_value1!,
Expand All @@ -157,4 +157,40 @@ class ActionsService implements IActionsService {
}
return actions;
}

sortActions(actions: Action[]): Action[] {
return actions.sort((a, b) => {
// Sort by sequence number
if (
a.transactionInfo.sequenceNumber !== b.transactionInfo.sequenceNumber
) {
return (
a.transactionInfo.sequenceNumber - b.transactionInfo.sequenceNumber
);
}

// Sort by account update index within the transaction
const aAccountUpdateIndex =
a.transactionInfo.zkappAccountUpdateIds.indexOf(
Number(a.accountUpdateId)
);
const bAccountUpdateIndex =
b.transactionInfo.zkappAccountUpdateIds.indexOf(
Number(b.accountUpdateId)
);
if (aAccountUpdateIndex !== bAccountUpdateIndex) {
return aAccountUpdateIndex - bAccountUpdateIndex;
}

// Sort by element index within the account update
const aEventIndex = a.transactionInfo.zkappEventElementIds.indexOf(
Number(a.eventElementId)
);
const bEventIndex = b.transactionInfo.zkappEventElementIds.indexOf(
Number(b.eventElementId)
);

return aEventIndex - bEventIndex;
});
}
}
3 changes: 2 additions & 1 deletion src/services/data-adapters/database-row-adapters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,10 @@ function mapActionOrEvent(
const event = createEvent(elementIdToFieldValues, transactionInfo);
data.push(event);
} else {
const { zkapp_account_update_id } = rows[i];
const { zkapp_account_update_id, zkapp_event_id } = rows[i];
const action = createAction(
zkapp_account_update_id.toString(),
zkapp_event_id.toString(),
elementIdToFieldValues,
transactionInfo
);
Expand Down
66 changes: 66 additions & 0 deletions tests/makeActionsRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { createYoga, createSchema } from 'graphql-yoga';
import { loadSchemaSync } from '@graphql-tools/load';
import { GraphQLFileLoader } from '@graphql-tools/graphql-file-loader';
import { buildHTTPExecutor } from '@graphql-tools/executor-http';
import { parse } from 'graphql';
import { resolvers } from '../src/resolvers.js';
import { buildContext, GraphQLContext } from '../src/context.js';

const PG_CONN = 'postgresql://postgres:postgres@localhost:5432/archive ';
const zkappAccount = 'B62qmBrPiukbHj4VnXdgMzjj2zpoQZeSBxZA6JDYMeeShApRAKaorto';

const actionsQuery = `
query getActions($input: ActionFilterOptionsInput!) {
actions(input: $input) {
blockInfo {
stateHash
timestamp
height
parentHash
chainStatus
distanceFromMaxBlockHeight
globalSlotSinceGenesis
}
actionState {
actionStateOne
actionStateTwo
actionStateThree
actionStateFour
actionStateFive
}
actionData {
data
accountUpdateId
transactionInfo {
status
hash
memo
sequenceNumber
zkappAccountUpdateIds
zkappEventElementIds
}
}
}
}
`;

const schema = createSchema({
typeDefs: loadSchemaSync('./schema.graphql', {
loaders: [new GraphQLFileLoader()],
}),
resolvers,
});
const context = await buildContext(PG_CONN);
const yoga = createYoga<GraphQLContext>({ schema, context });
const executor = buildHTTPExecutor({
fetch: yoga.fetch,
});

const results = await executor({
variables: {
input: { address: zkappAccount },
},
document: parse(`${actionsQuery}`),
});

console.log(JSON.stringify(results));
Loading

0 comments on commit 8822808

Please sign in to comment.