Skip to content

Commit

Permalink
Better wait for block import in tests (#2314)
Browse files Browse the repository at this point in the history
* Better wait for block import in tests

* adds temp fix for tracing

* Better TODO clean up

* testing better prettier report

* typo

* prettier
  • Loading branch information
Alan Sapede authored May 25, 2023
1 parent 027b9df commit 0d02150
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 25 deletions.
10 changes: 9 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,15 @@ jobs:
with:
node-version: 18.x
- name: Check with Prettier
run: npx prettier --check --ignore-path .prettierignore '**/*.(yml|js|ts|json)'
run: |
npx prettier --check --ignore-path .prettierignore '**/*.(yml|js|ts|json)' \
|| (git diff --quiet \
|| (echo 'Unable to show a diff because there are unstaged changes'; false) \
&& (npx prettier --ignore-path \
.prettierignore '**/*.(yml|js|ts|json)' -w --loglevel silent \
&& git --no-pager diff; git restore .) && false)
npx prettier --check --ignore-path .prettierignore '**/*.(yml|js|ts|json)'
check-cargo-toml-format:
name: "Check Cargo.toml files format"
Expand Down
54 changes: 30 additions & 24 deletions tests/util/setup-dev-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,33 +199,39 @@ export function describeDevMoonbeam(

const { parentHash, finalize } = options;

// TODO: Removes this whole check once Frontier support block import wait for
// create block. (cc @tgmichel)

// We are now listening to the eth block too. The main reason is because the Ethereum
// ingestion in Frontier is asynchronous, and can sometime be slightly delayed. This
// generates some race condition if we don't wait for it.

let expectedBlockNumber: number = (await subProvider.eth.getBlockNumber()) + 1;
const ethCheckPromise = new Promise<void>((resolve) => {
const ethBlockSub = subProvider.eth
.subscribe("newBlockHeaders", function (error, result) {
if (!error) {
return;
}
console.error(error);
})
.on("data", function (blockHeader) {
// unsubscribes the subscription once we get the right block
if (blockHeader.number != expectedBlockNumber) {
debug(
`Received unexpected block: ${blockHeader.number} ` +
`(expected: ${expectedBlockNumber})`
);
return;
}
ethBlockSub.unsubscribe();
resolve();
})
.on("error", console.error);
});
// We don't use the blockNumber because some tests are doing "re-org" which would make
// the new block number not to be the expected one.
let currentBlockHash = (await subProvider.eth.getBlock("latest")).hash;
const ethCheckPromise = parentHash
? Promise.resolve()
: new Promise<void>((resolve) => {
const ethBlockSub = subProvider.eth
.subscribe("newBlockHeaders", function (error, result) {
if (!error) {
return;
}
console.error(error);
})
.on("data", function (blockHeader) {
// unsubscribes the subscription once we get the right block
if (blockHeader.hash == currentBlockHash) {
debug(
`Received same block [${blockHeader.number}] hash: ${blockHeader.hash} ` +
`(previous: ${currentBlockHash})`
);
return;
}
ethBlockSub.unsubscribe();
resolve();
})
.on("error", console.error);
});

const blockResult = await createAndFinalizeBlock(context.polkadotApi, parentHash, finalize);

Expand Down

0 comments on commit 0d02150

Please sign in to comment.