Skip to content

Commit

Permalink
adding docs, tests, cleaning up a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
45930 committed Jan 21, 2025
1 parent 052b5b9 commit 16bc050
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 40 deletions.
50 changes: 49 additions & 1 deletion schema.graphql
Original file line number Diff line number Diff line change
@@ -1,24 +1,66 @@
"""
Filter for the consensus status of the block
"""
enum BlockStatusFilter {

Check notice on line 4 in schema.graphql

View workflow job for this annotation

GitHub Actions / GraphQL Inspector

Object type 'BlockStatusFilter' has description 'Filter for the consensus status of the block'

Object type 'BlockStatusFilter' has description 'Filter for the consensus status of the block'
"""
All blocks
"""
ALL

Check notice on line 8 in schema.graphql

View workflow job for this annotation

GitHub Actions / GraphQL Inspector

Description 'All blocks' was added to enum value 'BlockStatusFilter.ALL'

Description 'All blocks' was added to enum value 'BlockStatusFilter.ALL'
"""
Only pending blocks
"""
PENDING

Check notice on line 12 in schema.graphql

View workflow job for this annotation

GitHub Actions / GraphQL Inspector

Description 'Only pending blocks' was added to enum value 'BlockStatusFilter.PENDING'

Description 'Only pending blocks' was added to enum value 'BlockStatusFilter.PENDING'
"""
Only canonical (finalized) blocks
"""
CANONICAL

Check notice on line 16 in schema.graphql

View workflow job for this annotation

GitHub Actions / GraphQL Inspector

Description 'Only canonical (finalized) blocks' was added to enum value 'BlockStatusFilter.CANONICAL'

Description 'Only canonical (finalized) blocks' was added to enum value 'BlockStatusFilter.CANONICAL'
}

"""
Filter events from a specific account
**WARNING**: The graphQL schema server will limit the block scan range to a fixed number of blocks. The default is 10,000 blocks, but can be changed by the host.
It is the responsibility of the client to use a block range that is within the limit, which will guarantee that all events are eventually returned. It is possible to get a partial result if you do not specify both a `from` and a `to` parameter.
"""
input EventFilterOptionsInput {

Check notice on line 25 in schema.graphql

View workflow job for this annotation

GitHub Actions / GraphQL Inspector

Object type 'EventFilterOptionsInput' has description 'Filter events from a specific account **WARNING**: The graphQL schema server will limit the block scan range to a fixed number of blocks. The default is 10,000 blocks, but can be changed by the host. It is the responsibility of the client to use a block range that is within the limit, which will guarantee that all events are eventually returned. It is possible to get a partial result if you do not specify both a `from` and a `to` parameter.'

Object type 'EventFilterOptionsInput' has description 'Filter events from a specific account **WARNING**: The graphQL schema server will limit the block scan range to a fixed number of blocks. The default is 10,000 blocks, but can be changed by the host. It is the responsibility of the client to use a block range that is within the limit, which will guarantee that all events are eventually returned. It is possible to get a partial result if you do not specify both a `from` and a `to` parameter.'
address: String!
tokenId: String
status: BlockStatusFilter
"""
Mina block height to filter events to, exclusive
"""
to: Int

Check notice on line 32 in schema.graphql

View workflow job for this annotation

GitHub Actions / GraphQL Inspector

Input field 'EventFilterOptionsInput.to' has description 'Mina block height to filter events to, exclusive'

Input field 'EventFilterOptionsInput.to' has description 'Mina block height to filter events to, exclusive'
"""
Mina block height to filter events from, inclusive
"""
from: Int

Check notice on line 36 in schema.graphql

View workflow job for this annotation

GitHub Actions / GraphQL Inspector

Input field 'EventFilterOptionsInput.from' has description 'Mina block height to filter events from, inclusive'

Input field 'EventFilterOptionsInput.from' has description 'Mina block height to filter events from, inclusive'
}

"""
Filter actions from a specific account
**WARNING**: The graphQL schema server will limit the block scan range to a fixed number of blocks. The default is 10,000 blocks, but can be changed by the host.
It is the responsibility of the client to use a block range that is within the limit, which will guarantee that all actions are eventually returned. It is possible to get a partial result if you do not specify both a `from` and a `to` parameter.
"""
input ActionFilterOptionsInput {

Check notice on line 45 in schema.graphql

View workflow job for this annotation

GitHub Actions / GraphQL Inspector

Object type 'ActionFilterOptionsInput' has description 'Filter actions from a specific account **WARNING**: The graphQL schema server will limit the block scan range to a fixed number of blocks. The default is 10,000 blocks, but can be changed by the host. It is the responsibility of the client to use a block range that is within the limit, which will guarantee that all actions are eventually returned. It is possible to get a partial result if you do not specify both a `from` and a `to` parameter.'

Object type 'ActionFilterOptionsInput' has description 'Filter actions from a specific account **WARNING**: The graphQL schema server will limit the block scan range to a fixed number of blocks. The default is 10,000 blocks, but can be changed by the host. It is the responsibility of the client to use a block range that is within the limit, which will guarantee that all actions are eventually returned. It is possible to get a partial result if you do not specify both a `from` and a `to` parameter.'
address: String!
tokenId: String
status: BlockStatusFilter
"""
Mina block height to filter actions to, exclusive
"""
to: Int

Check notice on line 52 in schema.graphql

View workflow job for this annotation

GitHub Actions / GraphQL Inspector

Input field 'ActionFilterOptionsInput.to' has description 'Mina block height to filter actions to, exclusive'

Input field 'ActionFilterOptionsInput.to' has description 'Mina block height to filter actions to, exclusive'
"""
Mina block height to filter actions from, inclusive
"""
from: Int

Check notice on line 56 in schema.graphql

View workflow job for this annotation

GitHub Actions / GraphQL Inspector

Input field 'ActionFilterOptionsInput.from' has description 'Mina block height to filter actions from, inclusive'

Input field 'ActionFilterOptionsInput.from' has description 'Mina block height to filter actions from, inclusive'
"""
Filter for actions that happened after this action state, inclusive
"""
fromActionState: String

Check notice on line 60 in schema.graphql

View workflow job for this annotation

GitHub Actions / GraphQL Inspector

Input field 'ActionFilterOptionsInput.fromActionState' has description 'Filter for actions that happened after this action state, inclusive'

Input field 'ActionFilterOptionsInput.fromActionState' has description 'Filter for actions that happened after this action state, inclusive'
"""
Filter for actions that happened before this action state, inclusive
"""
endActionState: String

Check notice on line 64 in schema.graphql

View workflow job for this annotation

GitHub Actions / GraphQL Inspector

Input field 'ActionFilterOptionsInput.endActionState' has description 'Filter for actions that happened before this action state, inclusive'

Input field 'ActionFilterOptionsInput.endActionState' has description 'Filter for actions that happened before this action state, inclusive'
}

Expand Down Expand Up @@ -56,7 +98,7 @@ type TransactionInfo {
hash: String!
memo: String!
authorizationKind: String!
sequenceNumber: Int! # TODO: Is it ok to make this required?
sequenceNumber: Int!
zkappAccountUpdateIds: [Int]!
}

Expand All @@ -80,7 +122,13 @@ type ActionOutput {
actionState: ActionStates!
}

"""
Metadata about the network
"""
type NetworkStateOutput {

Check notice on line 128 in schema.graphql

View workflow job for this annotation

GitHub Actions / GraphQL Inspector

Object type 'NetworkStateOutput' has description 'Metadata about the network'

Object type 'NetworkStateOutput' has description 'Metadata about the network'
"""
Returns the latest pending and canonical block heights that are synced by the archive node. If the archive node is not fully synced, the pending block height will be lower than the actual network state. Wait some time for the archive node to get back in sync.
"""
maxBlockHeight: MaxBlockHeightInfo

Check notice on line 132 in schema.graphql

View workflow job for this annotation

GitHub Actions / GraphQL Inspector

Field 'NetworkStateOutput.maxBlockHeight' has description 'Returns the latest pending and canonical block heights that are synced by the archive node. If the archive node is not fully synced, the pending block height will be lower than the actual network state. Wait some time for the archive node to get back in sync.'

Field 'NetworkStateOutput.maxBlockHeight' has description 'Returns the latest pending and canonical block heights that are synced by the archive node. If the archive node is not fully synced, the pending block height will be lower than the actual network state. Wait some time for the archive node to get back in sync.'
}

Expand Down
5 changes: 2 additions & 3 deletions src/db/sql/events-actions/queries.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type postgres from 'postgres';
import { ArchiveNodeDatabaseRow } from './types.js';
import { BlockStatusFilter } from '../../../blockchain/types.js';

const BLOCK_RANGE_SIZE = Number(process.env.BLOCK_RANGE_SIZE) || 10000;
import { BLOCK_RANGE_SIZE } from '../../../server/server.js';

function fullChainCTE(db_client: postgres.Sql, from?: string, to?: string) {
let toAsNum = to ? Number(to) : undefined;
Expand Down Expand Up @@ -53,7 +52,7 @@ function fullChainCTE(db_client: postgres.Sql, from?: string, to?: string) {
// If fromAsNum is not undefined, then we have also set toAsNum and can safely query the range
// If no params ar provided, then we query the last BLOCK_RANGE_SIZE blocks
fromAsNum
? db_client`AND b.height >= ${fromAsNum} AND b.height >= ${toAsNum!}`
? db_client`AND b.height >= ${fromAsNum} AND b.height < ${toAsNum!}`
: db_client`AND b.height >= (
SELECT MAX(b2.height)
FROM blocks b2
Expand Down
6 changes: 5 additions & 1 deletion src/errors/error.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GraphQLError } from 'graphql';

export { throwGraphQLError, throwActionStateError };
export { throwGraphQLError, throwActionStateError, throwBlockRangeError };

function throwGraphQLError(message: string, code?: string, status?: number) {
throw new GraphQLError(message, {
Expand All @@ -14,3 +14,7 @@ function throwGraphQLError(message: string, code?: string, status?: number) {
function throwActionStateError(message: string) {
throwGraphQLError(message, 'ACTION_STATE_NOT_FOUND', 400);
}

function throwBlockRangeError(message: string) {
throwGraphQLError(message, 'BLOCK_RANGE_ERROR', 400);
}
1 change: 1 addition & 0 deletions src/resolvers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { makeExecutableSchema } from '@graphql-tools/schema';
import { loadSchemaSync } from '@graphql-tools/load';
import { GraphQLFileLoader } from '@graphql-tools/graphql-file-loader';
import { GraphQLError } from 'graphql';

Check failure on line 4 in src/resolvers.ts

View workflow job for this annotation

GitHub Actions / Linting

'GraphQLError' is defined but never used

import { Resolvers } from './resolvers-types.js';
import {
Expand Down
3 changes: 2 additions & 1 deletion src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { Plugin } from '@envelop/core';
import { schema } from '../resolvers.js';
import type { GraphQLContext } from '../context.js';

export { buildServer };
export { BLOCK_RANGE_SIZE, buildServer };

const LOG_LEVEL = (process.env.LOG_LEVEL as LogLevel) || 'info';
const BLOCK_RANGE_SIZE = Number(process.env.BLOCK_RANGE_SIZE) || 10000;

function buildServer(context: GraphQLContext, plugins: Plugin[]) {
const yoga = createYoga<GraphQLContext>({
Expand Down
16 changes: 14 additions & 2 deletions src/services/actions-service/actions-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@ import {
TracingState,
extractTraceStateFromOptions,
} from '../../tracing/tracer.js';
import { throwActionStateError } from '../../errors/error.js';
import {
throwActionStateError,
throwBlockRangeError,
} from '../../errors/error.js';
import dotenv from 'dotenv';
import { BLOCK_RANGE_SIZE } from '../../server/server.js';

dotenv.config();

export { ActionsService };

Expand Down Expand Up @@ -97,7 +104,12 @@ class ActionsService implements IActionsService {
tokenId ||= DEFAULT_TOKEN_ID;
status ||= BlockStatusFilter.all;
if (to && from && to < from) {
throw new Error('to must be greater than from');
throwBlockRangeError('to must be greater than from');
}
if (to && from && to - from > BLOCK_RANGE_SIZE) {
throwBlockRangeError(
`The block range is too large. The maximum range is ${BLOCK_RANGE_SIZE}`
);
}

return getActionsQuery(
Expand Down
9 changes: 8 additions & 1 deletion src/services/events-service/events-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import {
TracingState,
extractTraceStateFromOptions,
} from '../../tracing/tracer.js';
import { BLOCK_RANGE_SIZE } from '../../server/server.js';
import { throwBlockRangeError } from '../../errors/error.js';

export { EventsService };

Expand Down Expand Up @@ -67,7 +69,12 @@ class EventsService implements IEventsService {
tokenId ||= DEFAULT_TOKEN_ID;
status ||= BlockStatusFilter.all;
if (to && from && to < from) {
throw new Error('to must be greater than from');
throwBlockRangeError('to must be greater than from');
}
if (to && from && to - from > BLOCK_RANGE_SIZE) {
throwBlockRangeError(
`The block range is too large. The maximum range is ${BLOCK_RANGE_SIZE}`
);
}

return getEventsQuery(
Expand Down
Loading

0 comments on commit 16bc050

Please sign in to comment.