|
| 1 | +import { hexToU8a } from '@polkadot/util'; |
1 | 2 | import type { IntegrationTestContext } from './common-types';
|
2 | 3 |
|
3 |
| -import { FrameSystemEventRecord } from 'parachain-api'; |
| 4 | +import { |
| 5 | + AddressOrPair, |
| 6 | + ApiPromise, |
| 7 | + ApiTypes, |
| 8 | + FrameSystemEventRecord, |
| 9 | + Keyring, |
| 10 | + SubmittableExtrinsic, |
| 11 | +} from 'parachain-api'; |
4 | 12 |
|
5 | 13 | // for DI-test
|
6 | 14 | export const subscribeToEventsWithExtHash = async (
|
@@ -46,3 +54,111 @@ export const subscribeToEventsWithExtHash = async (
|
46 | 54 | });
|
47 | 55 | });
|
48 | 56 | };
|
| 57 | + |
| 58 | +// for II-test |
| 59 | +export const subscribeToEvents = async ( |
| 60 | + section: string, |
| 61 | + method: string, |
| 62 | + api: ApiPromise |
| 63 | +): Promise<FrameSystemEventRecord[]> => { |
| 64 | + return new Promise<FrameSystemEventRecord[]>((resolve, reject) => { |
| 65 | + let blocksToScan = 15; |
| 66 | + const unsubscribe = api.rpc.chain.subscribeNewHeads(async (blockHeader) => { |
| 67 | + const shiftedApi = await api.at(blockHeader.hash); |
| 68 | + |
| 69 | + const allBlockEvents = await shiftedApi.query.system.events(); |
| 70 | + const allExtrinsicEvents = allBlockEvents.filter(({ phase }) => phase.isApplyExtrinsic); |
| 71 | + |
| 72 | + const matchingEvent = allExtrinsicEvents.filter(({ event, phase }) => { |
| 73 | + return event.section === section && event.method === method; |
| 74 | + }); |
| 75 | + |
| 76 | + if (matchingEvent.length == 0) { |
| 77 | + blocksToScan -= 1; |
| 78 | + if (blocksToScan < 1) { |
| 79 | + reject(new Error(`timed out listening for event ${section}.${method}`)); |
| 80 | + (await unsubscribe)(); |
| 81 | + } |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + resolve(matchingEvent); |
| 86 | + (await unsubscribe)(); |
| 87 | + }); |
| 88 | + }); |
| 89 | +}; |
| 90 | + |
| 91 | +export async function waitForBlock(api: ApiPromise, blockNumber: number, blocksToCheck = 5) { |
| 92 | + let count = 0; |
| 93 | + |
| 94 | + return new Promise<void>((resolve, reject) => { |
| 95 | + const unsubscribe = api.rpc.chain.subscribeNewHeads(async (header) => { |
| 96 | + console.log(`Chain is at block: #${header.number}`); |
| 97 | + |
| 98 | + if (header.number.toNumber() === blockNumber) { |
| 99 | + (await unsubscribe)(); |
| 100 | + resolve(); |
| 101 | + } |
| 102 | + |
| 103 | + if (++count === blocksToCheck) { |
| 104 | + (await unsubscribe)(); |
| 105 | + reject(new Error(`Timeout: Block #${blockNumber} not reached within ${blocksToCheck} blocks.`)); |
| 106 | + } |
| 107 | + }); |
| 108 | + }); |
| 109 | +} |
| 110 | + |
| 111 | +export async function setAliceAsAdmin(api: ApiPromise) { |
| 112 | + // Get keyring of Alice, who is also the sudo in dev chain spec |
| 113 | + const keyring = new Keyring({ type: 'sr25519' }); |
| 114 | + const alice = keyring.addFromUri('//Alice'); |
| 115 | + |
| 116 | + const tx = await sudoWrapperGC(api, api.tx.teebag.setAdmin('esqZdrqhgH8zy1wqYh1aLKoRyoRWLFbX9M62eKfaTAoK67pJ5')); |
| 117 | + |
| 118 | + console.log(`Setting Alice as Admin for Teebag`); |
| 119 | + return signAndSend(tx, alice); |
| 120 | +} |
| 121 | + |
| 122 | +export function signAndSend(tx: SubmittableExtrinsic<ApiTypes>, account: AddressOrPair) { |
| 123 | + return new Promise<{ block: string }>(async (resolve, reject) => { |
| 124 | + await tx.signAndSend(account, (result) => { |
| 125 | + console.log(`Current status is ${result.status}`); |
| 126 | + if (result.status.isInBlock) { |
| 127 | + console.log(`Transaction included at blockHash ${result.status.asInBlock}`); |
| 128 | + } else if (result.status.isFinalized) { |
| 129 | + console.log(`Transaction finalized at blockHash ${result.status.asFinalized}`); |
| 130 | + resolve({ |
| 131 | + block: result.status.asFinalized.toString(), |
| 132 | + }); |
| 133 | + } else if (result.status.isInvalid) { |
| 134 | + reject(`Transaction is ${result.status}`); |
| 135 | + } |
| 136 | + }); |
| 137 | + }); |
| 138 | +} |
| 139 | + |
| 140 | +// After removing the sudo module, we use `EnsureRootOrHalfCouncil` instead of `Sudo`, |
| 141 | +// and there are only two council members in litmus-dev/rococo-dev/litentry-dev. |
| 142 | +// So only `propose` is required, no vote. |
| 143 | +// |
| 144 | +// TODO: support to send the `vote extrinsic`, if the number of council members is greater than 2. |
| 145 | +export async function sudoWrapperGC(api: ApiPromise, tx: SubmittableExtrinsic<ApiTypes>) { |
| 146 | + const chain = (await api.rpc.system.chain()).toString().toLowerCase(); |
| 147 | + if (chain != 'rococo-dev') { |
| 148 | + const threshold = api.createType('Compact<u32>', 1); |
| 149 | + const call = api.createType('Call', tx); |
| 150 | + return api.tx.council.propose(threshold, call, api.createType('Compact<u32>', tx.length)); |
| 151 | + } else { |
| 152 | + return api.tx.sudo.sudo(tx); |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +export async function setScheduledEnclave(api: ApiPromise, block: number, mrenclave: string) { |
| 157 | + const keyring = new Keyring({ type: 'sr25519' }); |
| 158 | + const alice = keyring.addFromUri('//Alice'); |
| 159 | + |
| 160 | + const tx = api.tx.teebag.setScheduledEnclave('Identity', block, hexToU8a(`0x${mrenclave}`)); |
| 161 | + |
| 162 | + console.log('Schedule Enclave Extrinsic sent'); |
| 163 | + return signAndSend(tx, alice); |
| 164 | +} |
0 commit comments