Skip to content

Commit

Permalink
test: add test to compare block heights
Browse files Browse the repository at this point in the history
  • Loading branch information
boray committed Jan 11, 2025
1 parent c285396 commit ce09256
Showing 1 changed file with 67 additions and 39 deletions.
106 changes: 67 additions & 39 deletions tests/resolvers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
Keypair,
emitActionsFromMultipleSenders,
emitMultipleFieldsEvents,
fetchNetworkState,
randomStruct,
} from '../zkapp/utils.js';
import { HelloWorld, TestStruct } from '../zkapp/contract.js';
Expand All @@ -37,21 +38,22 @@ import {
ActionOutput,
EventData,
EventOutput,
NetworkStateOutput,
MaxBlockHeightInfo,
Maybe,
} from 'src/resolvers-types.js';

interface ExecutorResult {
data:
| {
events: Array<EventOutput>;
}
| {
actions: Array<ActionOutput>;
}
| {
block: MaxBlockHeightInfo;
};
| {
events: Array<EventOutput>;
}
| {
actions: Array<ActionOutput>;
}
| {
networkState: NetworkStateOutput;
};
}

interface EventQueryResult extends ExecutorResult {
Expand All @@ -66,9 +68,9 @@ interface ActionQueryResult extends ExecutorResult {
};
}

interface BlockQueryResult extends ExecutorResult {
interface NetworkQueryResult extends ExecutorResult {
data: {
block: MaxBlockHeightInfo;
networkState: NetworkStateOutput;
};
}

Expand Down Expand Up @@ -131,12 +133,14 @@ query getActions($input: ActionFilterOptionsInput!) {
}
`;

const blockQuery = `
const networkQuery = `
query maxBlockHeightInfo {
block {
canonicalMaxBlockHeight
pendingMaxBlockHeight
networkState {
maxBlockHeight {
canonicalMaxBlockHeight
pendingMaxBlockHeight
}
}
}
`;

Expand All @@ -145,15 +149,15 @@ const PG_CONN = 'postgresql://postgres:postgres@localhost:5432/archive ';

interface ExecutorResult {
data:
| {
events: Array<EventOutput>;
}
| {
actions: Array<ActionOutput>;
}
| {
block: MaxBlockHeightInfo;
};
| {
events: Array<EventOutput>;
}
| {
actions: Array<ActionOutput>;
}
| {
networkState: NetworkStateOutput;
};
}

interface EventQueryResult extends ExecutorResult {
Expand All @@ -168,9 +172,9 @@ interface ActionQueryResult extends ExecutorResult {
};
}

interface BlockQueryResult extends ExecutorResult {
interface NetworkQueryResult extends ExecutorResult {
data: {
block: MaxBlockHeightInfo;
networkState: NetworkStateOutput;
};
}

Expand Down Expand Up @@ -202,10 +206,10 @@ describe('Query Resolvers', async () => {
})) as EventQueryResult;
}

async function executeBlockQuery(): Promise<BlockQueryResult> {
async function executeNetworkStateQuery(): Promise<NetworkQueryResult> {
return (await executor({
document: parse(`${blockQuery}`),
})) as BlockQueryResult;
document: parse(`${networkQuery}`),
})) as NetworkQueryResult;
}

before(async () => {
Expand Down Expand Up @@ -543,23 +547,47 @@ describe('Query Resolvers', async () => {
});
});

describe("Block", async () => {
let blockResponse: MaxBlockHeightInfo;
let results: BlockQueryResult;
describe("NetworkState", () => {
let blockResponse: NetworkStateOutput;
let results: NetworkQueryResult;

before(async () => {
results = await executeNetworkStateQuery();
blockResponse = results.data.networkState;
});

test("Fetching the max block height should return the max block height", async () => {
results = await executeNetworkStateQuery();
blockResponse = results.data.networkState;
assert.ok(blockResponse.maxBlockHeight.canonicalMaxBlockHeight > 0);
assert.ok(blockResponse.maxBlockHeight.pendingMaxBlockHeight > 0);
});

test("Fetching the max block height should not throw", async () => {
assert.doesNotThrow(async () => {
await executeBlockQuery();
await executeNetworkStateQuery();
});
});

test("Fetching the max block height should return the max block height", async () => {
results = await executeBlockQuery();
blockResponse = results.data.block;
assert.ok(blockResponse.canonicalMaxBlockHeight > 0);
assert.ok(blockResponse.pendingMaxBlockHeight > 0);
describe("Fetch max block height from mina node", () => {
let blockResponse: NetworkStateOutput;
let results: NetworkQueryResult;
let fetchedBlockchainLength: number;

before(async () => {
results = await executeNetworkStateQuery();
blockResponse = results.data.networkState;
fetchedBlockchainLength = await fetchNetworkState(zkApp, senderKeypair);
});

test("Fetched max block height from archive node should match with the one from mina node", () => {
console.log("fetchedBlockchainLength:", fetchedBlockchainLength);
console.log("blockResponse.maxBlockHeight.pendingMaxBlockHeight:", blockResponse.maxBlockHeight.pendingMaxBlockHeight);
console.log(fetchedBlockchainLength == blockResponse.maxBlockHeight.pendingMaxBlockHeight);
assert.strictEqual(blockResponse.maxBlockHeight.pendingMaxBlockHeight, fetchedBlockchainLength);
});

});

});

});
Expand Down

0 comments on commit ce09256

Please sign in to comment.