Skip to content

Commit

Permalink
test: added utility function that check if event is emitted in the ne…
Browse files Browse the repository at this point in the history
…xt N blocks to use it for offchain worker testing
  • Loading branch information
Aleksandar Brayanov committed Nov 4, 2024
1 parent d263ed4 commit 3faffe0
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 22 deletions.
47 changes: 26 additions & 21 deletions test/suites/zombie-offchain/test-simple-template-offchain.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { beforeAll, describeSuite, expect } from "@moonwall/cli";
import { getHeaderFromRelay } from "../../util/relayInterface.ts";
import { ApiPromise, Keyring } from "@polkadot/api";
import { signAndSendAndInclude } from "../../util/block.ts";
import { signAndSendAndInclude, isEventEmittedInTheNextBlocks } from "../../util/block.ts";
describeSuite({
id: "ZOF01",
title: "Zombie Offchain Tests",
foundationMethods: "zombie",
testCases: function ({ it, context }) {
let relayApi: ApiPromise;
let container2000Api: ApiPromise;
const baseBlockWaitingInterval: number = 10;

beforeAll(async () => {
relayApi = context.polkadotJs("Tanssi-relay");
Expand All @@ -34,13 +35,14 @@ describeSuite({
test: async function () {
const blockNum = (await container2000Api.rpc.chain.getBlock()).block.header.number.toNumber();
expect(blockNum).to.be.greaterThan(0);
await context.waitBlock(10, "Container2000");

const events = await container2000Api.query.system.events();
const offchainWorkerEvents = events.filter((a) => {
return a.event.method == "SimpleOffchainEvent";
});
expect(offchainWorkerEvents.length).to.be.equal(0);
const isOffchainEventEmitted = await isEventEmittedInTheNextBlocks(
context,
container2000Api,
baseBlockWaitingInterval,
"Container2000",
"SimpleOffchainEvent"
);
expect(isOffchainEventEmitted).to.be.false;
},
});

Expand All @@ -57,23 +59,26 @@ describeSuite({

// Enable off-chain worker test event emission
await signAndSendAndInclude(container2000Api.tx.sudo.sudo(ocwOnTx), alice);
await context.waitBlock(20, "Container2000");

const events = await container2000Api.query.system.events();
const offchainWorkerEvents1 = events.filter((a) => {
return a.event.method == "SimpleOffchainEvent";
});
expect(offchainWorkerEvents1.length).to.be.equal(0);
const isOffchainEventEmitted1 = await isEventEmittedInTheNextBlocks(
context,
container2000Api,
baseBlockWaitingInterval,
"Container2000",
"SimpleOffchainEvent"
);
expect(isOffchainEventEmitted1).to.be.false;

// Disable off-chain worker test event emission
const ocwOffTx = container2000Api.tx.offchainWorker.setOffchainWorker(false);
await signAndSendAndInclude(container2000Api.tx.sudo.sudo(ocwOffTx), alice);
await context.waitBlock(40, "Container2000");

const offchainWorkerEvents2 = events.filter((a) => {
return a.event.method == "SimpleOffchainEvent";
});
expect(offchainWorkerEvents2.length).to.be.equal(0);
const isOffchainEventEmitted2 = await isEventEmittedInTheNextBlocks(
context,
container2000Api,
baseBlockWaitingInterval,
"Container2000",
"SimpleOffchainEvent"
);
expect(isOffchainEventEmitted2).to.be.false;
},
});
},
Expand Down
23 changes: 22 additions & 1 deletion test/util/block.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DevModeContext, expect } from "@moonwall/cli";
import { DevModeContext, expect, ZombieContext } from "@moonwall/cli";
import { filterAndApply } from "@moonwall/util";

import { ApiPromise } from "@polkadot/api";
Expand Down Expand Up @@ -346,3 +346,24 @@ export async function fetchStorageProofFromValidationData(polkadotJs) {
relayStorageProof,
};
}

export async function isEventEmittedInTheNextBlocks(
context: ZombieContext,
api: ApiPromise,
blockCount: number,
chainName: string,
eventName: string
) {
while (blockCount > 0) {
await context.waitBlock(1, chainName);
const currentBlockEvents = await api.query.system.events();
const filteredEvents = currentBlockEvents.filter((a) => {
return a.event.method == eventName;
});
if (filteredEvents.length > 0) {
return true;
}
blockCount--;
}
return false;
}

0 comments on commit 3faffe0

Please sign in to comment.