Skip to content

Commit

Permalink
updating the schema, adding multi-emit test
Browse files Browse the repository at this point in the history
  • Loading branch information
45930 committed Oct 23, 2024
1 parent ddd8b27 commit d8f0b60
Show file tree
Hide file tree
Showing 9 changed files with 230 additions and 62 deletions.
6 changes: 3 additions & 3 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ type EventData {
type ActionData {
accountUpdateId: String!
transactionInfo: TransactionInfo
sequenceNumber: Int!
zkappAccountUpdateIds: [Int]!
zkappEventElementIds: [Int]!
data: [String]!
}

Expand All @@ -53,6 +50,9 @@ type TransactionInfo {
hash: String!
memo: String!
authorizationKind: String!
sequenceNumber: Int! # TODO: Is it ok to make this required?
zkappAccountUpdateIds: [Int]!
zkappEventElementIds: [Int]!
}

type ActionStates {
Expand Down
6 changes: 3 additions & 3 deletions src/blockchain/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ export type Action = {
accountUpdateId: string;
transactionInfo: TransactionInfo;
data: string[];
sequenceNumber: number;
zkappAccountUpdateIds: number[];
zkappEventElementIds: number[];
};

export type BlockInfo = {
Expand All @@ -44,6 +41,9 @@ export type TransactionInfo = {
hash: string;
memo: string;
authorizationKind: string;
sequenceNumber: number;
zkappAccountUpdateIds: number[];
zkappEventElementIds: number[];
};

export type Events = {
Expand Down
11 changes: 4 additions & 7 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 @@ -40,17 +43,11 @@ export function createEvent(
export function createAction(
accountUpdateId: string,
data: string[],
transactionInfo: TransactionInfo,
sequenceNumber: number,
zkappAccountUpdateIds: number[],
zkappEventElementIds: number[]
transactionInfo: TransactionInfo
): Action {
return {
accountUpdateId,
data,
transactionInfo,
sequenceNumber,
zkappAccountUpdateIds,
zkappEventElementIds,
};
}
28 changes: 14 additions & 14 deletions src/resolvers-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ export type ActionData = {
__typename?: 'ActionData';
accountUpdateId: Scalars['String']['output'];
data: Array<Maybe<Scalars['String']['output']>>;
sequenceNumber: Scalars['Int']['output'];
transactionInfo?: Maybe<TransactionInfo>;
zkappAccountUpdateIds: Array<Maybe<Scalars['Int']['output']>>;
zkappEventElementIds: Array<Maybe<Scalars['Int']['output']>>;
};

export type ActionFilterOptionsInput = {
Expand Down Expand Up @@ -127,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 @@ -281,22 +281,11 @@ export type ActionDataResolvers<
ParentType,
ContextType
>;
sequenceNumber?: Resolver<ResolversTypes['Int'], ParentType, ContextType>;
transactionInfo?: Resolver<
Maybe<ResolversTypes['TransactionInfo']>,
ParentType,
ContextType
>;
zkappAccountUpdateIds?: Resolver<
Array<Maybe<ResolversTypes['Int']>>,
ParentType,
ContextType
>;
zkappEventElementIds?: Resolver<
Array<Maybe<ResolversTypes['Int']>>,
ParentType,
ContextType
>;
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
};

Expand Down Expand Up @@ -462,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
12 changes: 2 additions & 10 deletions src/services/data-adapters/database-row-adapters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,19 +210,11 @@ function mapActionOrEvent(
const event = createEvent(elementIdToFieldValues, transactionInfo);
data.push(event);
} else {
const {
zkapp_account_update_id,
zkapp_account_updates_ids,
sequence_number,
zkapp_event_element_ids,
} = rows[i];
const { zkapp_account_update_id } = rows[i];
const action = createAction(
zkapp_account_update_id.toString(),
elementIdToFieldValues,
transactionInfo,
sequence_number,
zkapp_account_updates_ids,
zkapp_event_element_ids
transactionInfo
);
data.push(action);
}
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));
95 changes: 74 additions & 21 deletions tests/resolvers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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 { PrivateKey, Lightnet } from 'o1js';
import { PrivateKey, Lightnet, Mina } from 'o1js';
import { resolvers } from '../src/resolvers.js';
import { buildContext, GraphQLContext } from '../src/context.js';
import {
Expand All @@ -15,8 +15,10 @@ import {
emitSingleEvent,
setNetworkConfig,
Keypair,
emitActionsFromMultipleSenders,
} from '../zkapp/utils.js';
import { HelloWorld } from '../zkapp/contract.js';
import { Actions } from 'src/blockchain/types.js';

const eventsQuery = `
query getEvents($input: EventFilterOptionsInput!) {
Expand Down Expand Up @@ -69,12 +71,16 @@ query getActions($input: ActionFilterOptionsInput!) {
status
hash
memo
sequenceNumber
zkappAccountUpdateIds
}
}
}
}
`;

console.log('Running tests');

// This is the default connection string provided by the lightnet postgres container
const PG_CONN = 'postgresql://postgres:postgres@localhost:5432/archive ';

Expand All @@ -84,28 +90,35 @@ describe('Query Resolvers', async () => {
let zkAppKeypair: Keypair;
let zkApp: HelloWorld;

console.log('Describe ', 'Query Resolvers');

before(async () => {
setNetworkConfig();
console.log('Before');
try {
setNetworkConfig();

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

zkAppKeypair = await Lightnet.acquireKeyPair();
senderKeypair = await Lightnet.acquireKeyPair();
zkApp = await deployContract(
zkAppKeypair,
senderKeypair,
/* fundNewAccount = */ false
);
zkAppKeypair = await Lightnet.acquireKeyPair();
senderKeypair = await Lightnet.acquireKeyPair();
zkApp = await deployContract(
zkAppKeypair,
senderKeypair,
/* fundNewAccount = */ false
);
} catch (error) {
console.error(error);
}
});

after(async () => {
Expand Down Expand Up @@ -198,7 +211,7 @@ describe('Query Resolvers', async () => {
});
});

describe('Actions', async () => {
describe.only('Actions', async () => {
test('Fetching actions with a valid address should not throw', async () => {
assert.doesNotThrow(async () => {
await executor({
Expand Down Expand Up @@ -253,5 +266,45 @@ describe('Query Resolvers', async () => {
const lastAction = actions[actions.length - 1];
assert.strictEqual(lastAction.actionData.length, 3);
});

describe('Actions from different accounts', async () => {
const sendersCount = 5;
const actionsCount = 3;
const senders: Keypair[] = [];

before(async () => {
for (let i = 0; i < sendersCount; i++) {
senders.push(await Lightnet.acquireKeyPair());
}
});

test('Emitting actions from many accounts should be fetchable in o1js', async () => {
await emitActionsFromMultipleSenders(zkApp, senders, {
numberOfEmits: actionsCount,
});

await Mina.fetchActions(zkApp.address); // This line will throw if actions do not reproduce the correct action hash
assert(true);
});

test('Fetched actions have order metadata', async () => {
const results = await executor({
variables: {
input: {
address: zkApp.address,
},
},
document: parse(`${actionsQuery}`),
});
const actions: Actions = results.data.actions;
for (const block of actions) {
const actionData = block.actionData;
for (const action of actionData) {
assert.ok(action.transactionInfo.sequenceNumber);
assert(action.transactionInfo.zkappAccountUpdateIds.length > 0);
}
}
});
});
});
});
Loading

0 comments on commit d8f0b60

Please sign in to comment.