-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(launch-local-docker): P-273 Detect block height more reliably
- Loading branch information
1 parent
ff22b32
commit a31c3cb
Showing
3 changed files
with
78 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import '@polkadot/api-augment'; | ||
import { ApiPromise, Keyring, WsProvider } from '@polkadot/api'; | ||
import type { VoidFn } from '@polkadot/api/types'; | ||
import type { ISubmittableResult } from '@polkadot/types/types'; | ||
import { loadConfig } from '../utils'; | ||
|
||
const FINALIZED_BLOCKS_COUNT = 1; | ||
const TIMEOUT_MIN = 1000 * 60 * 3; // 1min | ||
|
||
/** | ||
* Connects to the parachain via the config file | ||
* and waits for `FINALIZED_BLOCKS_COUNT` blocks to | ||
* get finalized. | ||
* | ||
* It times out after `TIMEOUT_MIN` min. | ||
*/ | ||
(async () => { | ||
const config = loadConfig(); | ||
let timeout: NodeJS.Timeout; | ||
let count = 0; | ||
let unsub: VoidFn; | ||
let api: ApiPromise; | ||
|
||
const provider = new WsProvider(config.parachain_ws); | ||
|
||
console.log(`Connecting to parachain ${config.parachain_ws}`); | ||
|
||
timeout = global.setTimeout(async () => { | ||
|
||
if (typeof unsub === 'function') unsub(); | ||
|
||
if (api) api.disconnect(); | ||
provider.on('disconnected', () => { | ||
console.log(`\nno block production detected after ${TIMEOUT_MIN}min, you might want to check it manually. Quit now`); | ||
process.exit(1); | ||
}); | ||
}, TIMEOUT_MIN); | ||
|
||
api = await ApiPromise.create({ | ||
provider: provider, | ||
}); | ||
|
||
unsub = await api.rpc.chain.subscribeFinalizedHeads(async (head) => { | ||
const blockNumber = head.number.toNumber(); | ||
console.log(`Parachain finalized block #${blockNumber} with hash ${head.hash}`); | ||
count += 1; | ||
|
||
if (blockNumber >= 1 && count >= FINALIZED_BLOCKS_COUNT) { | ||
unsub(); | ||
if (timeout) global.clearTimeout(timeout) | ||
|
||
await api.disconnect(); | ||
provider.on('disconnected', () => { | ||
console.log('Done.'); | ||
process.exit(0); | ||
}); | ||
} | ||
}) | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters