Skip to content

Commit 9246af8

Browse files
committed
Add tests for PreImage (continued)
Add additional tests which verify PreImage behavior: This is a continuation of the PR: #470. Also address outstanding comments from the original PR.
1 parent 501de91 commit 9246af8

File tree

3 files changed

+610
-67
lines changed

3 files changed

+610
-67
lines changed

packages/shared/src/helpers/index.ts

Lines changed: 91 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type { ApiPromise } from '@polkadot/api'
77
import { decodeAddress, encodeAddress } from '@polkadot/keyring'
88
import type { KeyringPair } from '@polkadot/keyring/types'
99
import type { EventRecord } from '@polkadot/types/interfaces'
10-
import type { PalletStakingValidatorPrefs } from '@polkadot/types/lookup'
10+
import type { FrameSystemAccountInfo, PalletStakingValidatorPrefs } from '@polkadot/types/lookup'
1111
import type { IsEvent } from '@polkadot/types/metadata/decorate/types'
1212
import type { AnyTuple, Codec, IEvent } from '@polkadot/types/types'
1313
import type { HexString } from '@polkadot/util/types'
@@ -107,6 +107,79 @@ export type BlockProvider = 'Local' | 'NonLocal'
107107
/** Whether async backing is enabled or disabled on the querying parachain. */
108108
export type AsyncBacking = 'Enabled' | 'Disabled'
109109

110+
/**
111+
* Given a PJS client and a call, modify the `scheduler` pallet's `agenda` storage to execute the list of extrinsics
112+
* in the next block.
113+
*
114+
* The calls can be either inline calls or lookup calls, which in the latter case *must* have been noted
115+
* in the storage of the chain's `preimage` pallet with a `notePreimage` extrinsic.
116+
*
117+
* @param blockProvider Whether the calls are being scheduled on a chain that uses a local or nonlocal block provider.
118+
* This chain's runtime *must* have the scheduler pallet available.
119+
*/
120+
export async function scheduleCallListWithOrigin(
121+
client: {
122+
api: ApiPromise
123+
dev: {
124+
setStorage: (values: StorageValues, blockHash?: string) => Promise<any>
125+
}
126+
},
127+
calls: {
128+
call:
129+
| { Inline: any }
130+
| {
131+
Lookup: {
132+
hash: any
133+
len: any
134+
}
135+
}
136+
origin: any
137+
}[],
138+
blockProvider: BlockProvider = 'Local',
139+
) {
140+
const scheduledBlock = await match(blockProvider)
141+
.with('Local', async () => (await client.api.rpc.chain.getHeader()).number.toNumber() + 1)
142+
.with('NonLocal', async () =>
143+
((await client.api.query.parachainSystem.lastRelayChainBlockNumber()) as any).toNumber(),
144+
)
145+
.exhaustive()
146+
147+
const agenda = calls.map(({ call, origin }) => [
148+
[scheduledBlock],
149+
[
150+
{
151+
call,
152+
origin: origin,
153+
},
154+
],
155+
])
156+
157+
await client.dev.setStorage({
158+
Scheduler: {
159+
agenda: agenda,
160+
},
161+
})
162+
}
163+
164+
/**
165+
* Given a PJS client and a list of inline calls with the same origin, modify the `scheduler`
166+
* pallet's `agenda` storage to execute the extrinsic in the next block.
167+
*/
168+
export async function scheduleInlineCallListWithSameOrigin(
169+
client: {
170+
api: ApiPromise
171+
dev: {
172+
setStorage: (values: StorageValues, blockHash?: string) => Promise<any>
173+
}
174+
},
175+
encodedCall: HexString[],
176+
origin: any,
177+
blockProvider: BlockProvider = 'Local',
178+
) {
179+
const callList = encodedCall.map((call) => ({ call: { Inline: call }, origin }))
180+
await scheduleCallListWithOrigin(client, callList, blockProvider)
181+
}
182+
110183
/**
111184
* Given a PJS client and a call, modify the `scheduler` pallet's `agenda` storage to execute the extrinsic in the next
112185
* block.
@@ -135,28 +208,7 @@ export async function scheduleCallWithOrigin(
135208
origin: any,
136209
blockProvider: BlockProvider = 'Local',
137210
) {
138-
const scheduledBlock = await match(blockProvider)
139-
.with('Local', async () => (await client.api.rpc.chain.getHeader()).number.toNumber() + 1)
140-
.with('NonLocal', async () =>
141-
((await client.api.query.parachainSystem.lastRelayChainBlockNumber()) as any).toNumber(),
142-
)
143-
.exhaustive()
144-
145-
await client.dev.setStorage({
146-
Scheduler: {
147-
agenda: [
148-
[
149-
[scheduledBlock],
150-
[
151-
{
152-
call,
153-
origin: origin,
154-
},
155-
],
156-
],
157-
],
158-
},
159-
})
211+
await scheduleCallListWithOrigin(client, [{ call, origin }], blockProvider)
160212
}
161213

162214
/**
@@ -487,6 +539,22 @@ export function sortAddressesByBytes(addresses: string[], addressEncoding: numbe
487539
.map((bytes) => encodeAddress(bytes, addressEncoding))
488540
}
489541

542+
/**
543+
* Get the free funds of an account.
544+
*/
545+
export async function getFreeFunds(client: Client<any, any>, address: any): Promise<number> {
546+
const account = (await client.api.query.system.account(address)) as FrameSystemAccountInfo
547+
return account.data.free.toNumber()
548+
}
549+
550+
/**
551+
* Get the reserved funds of an account.
552+
*/
553+
export async function getReservedFunds(client: Client<any, any>, address: any): Promise<number> {
554+
const account = (await client.api.query.system.account(address)) as FrameSystemAccountInfo
555+
return account.data.reserved.toNumber()
556+
}
557+
490558
/**
491559
* Configuration for relay chain tests.
492560
*/

packages/shared/src/multisig.proxy.ts

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { type Client, setupBalances, setupNetworks, verifyPureProxy } from '@e2e
55

66
import type { KeyringPair } from '@polkadot/keyring/types'
77
import type { AccountId32 } from '@polkadot/types/interfaces/runtime'
8-
import type { FrameSystemAccountInfo } from '@polkadot/types/lookup'
98
import type { U8aFixed } from '@polkadot/types-codec'
109
import { encodeAddress } from '@polkadot/util-crypto'
1110

@@ -16,6 +15,8 @@ import {
1615
check,
1716
checkEvents,
1817
getBlockNumber,
18+
getFreeFunds,
19+
getReservedFunds,
1920
sortAddressesByBytes,
2021
type TestConfig,
2122
} from './helpers/index.js'
@@ -86,22 +87,6 @@ async function getAndVerifyMultisigEventData(
8687
return [multisigAddress, multisigExtrinsicIndex, multisigCallHash]
8788
}
8889

89-
/**
90-
* Get the free funds of an account.
91-
*/
92-
async function getFreeFunds(client: Client<any, any>, address: any): Promise<number> {
93-
const account = (await client.api.query.system.account(address)) as FrameSystemAccountInfo
94-
return account.data.free.toNumber()
95-
}
96-
97-
/**
98-
* Get the reserved funds of an account.
99-
*/
100-
async function getReservedFunds(client: Client<any, any>, address: any): Promise<number> {
101-
const account = (await client.api.query.system.account(address)) as FrameSystemAccountInfo
102-
return account.data.reserved.toNumber()
103-
}
104-
10590
/**
10691
* Get the costs for creating a number of proxies.
10792
*/

0 commit comments

Comments
 (0)