Skip to content

Commit 20f9bf5

Browse files
authored
Add tests for PreImage (continued) (#471)
Add additional tests which verify PreImage behavior: - 1) A manager account requests a preimage hash once then unrequests it multiple times (added additional checks) - 2) Check the noting and unnoting of empty images - 3) Check the noting and unnoting of oversized images - 4) Check the repeated noting and unnoting of the same preimage - 5) Check preimage behavior when requesting, noting, unrequesting and unnoting occur in different orders - 6) Check the "ensure_updated" extrinsic applies fees depending on the update ratio Several utility functions have been moved to the helpers file as they are shared with other test files. This is a continuation of the PR: #470. Also address outstanding comments from the original PR.
1 parent 9e67ecb commit 20f9bf5

File tree

5 files changed

+864
-71
lines changed

5 files changed

+864
-71
lines changed

packages/kusama/src/__snapshots__/assetHubKusama.preimage.e2e.test.ts.snap

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,33 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3+
exports[`Kusama Asset Hub PreImage > Kusama Asset Hub PreImage > preimage tests > preimage empty test > note empty preimage events 1`] = `
4+
[
5+
{
6+
"data": {
7+
"hash_": "(hash)",
8+
},
9+
"method": "Noted",
10+
"section": "preimage",
11+
},
12+
]
13+
`;
14+
15+
exports[`Kusama Asset Hub PreImage > Kusama Asset Hub PreImage > preimage tests > preimage empty test > unnote empty preimage events 1`] = `
16+
[
17+
{
18+
"data": {
19+
"hash_": "(hash)",
20+
},
21+
"method": "Cleared",
22+
"section": "preimage",
23+
},
24+
]
25+
`;
26+
27+
exports[`Kusama Asset Hub PreImage > Kusama Asset Hub PreImage > preimage tests > preimage ensure updated test (fees due) > ensure updated preimage events 1`] = `[]`;
28+
29+
exports[`Kusama Asset Hub PreImage > Kusama Asset Hub PreImage > preimage tests > preimage ensure updated test (no fees due) > ensure updated preimage events 1`] = `[]`;
30+
331
exports[`Kusama Asset Hub PreImage > Kusama Asset Hub PreImage > preimage tests > preimage single note and unnote test > note preimage events 1`] = `
432
[
533
{
@@ -23,3 +51,47 @@ exports[`Kusama Asset Hub PreImage > Kusama Asset Hub PreImage > preimage tests
2351
},
2452
]
2553
`;
54+
55+
exports[`Kusama Asset Hub PreImage > failure tests > preimage oversized test > note oversized preimage events 1`] = `
56+
[
57+
{
58+
"data": {
59+
"hash_": "(hash)",
60+
},
61+
"method": "Noted",
62+
"section": "preimage",
63+
},
64+
]
65+
`;
66+
67+
exports[`Kusama Asset Hub PreImage > failure tests > preimage repeated note and unnote test > note preimage events 1`] = `
68+
[
69+
{
70+
"data": {
71+
"hash_": "(hash)",
72+
},
73+
"method": "Noted",
74+
"section": "preimage",
75+
},
76+
]
77+
`;
78+
79+
exports[`Kusama Asset Hub PreImage > failure tests > preimage repeated note and unnote test > repeat note preimage events 1`] = `[]`;
80+
81+
exports[`Kusama Asset Hub PreImage > failure tests > preimage repeated note and unnote test > repeat unnote preimage events 1`] = `[]`;
82+
83+
exports[`Kusama Asset Hub PreImage > failure tests > preimage repeated note and unnote test > unnote preimage events 1`] = `
84+
[
85+
{
86+
"data": {
87+
"hash_": "(hash)",
88+
},
89+
"method": "Cleared",
90+
"section": "preimage",
91+
},
92+
]
93+
`;
94+
95+
exports[`Kusama Asset Hub PreImage > failure tests > preimage single request and unrequest test as non-root > request preimage events 1`] = `[]`;
96+
97+
exports[`Kusama Asset Hub PreImage > failure tests > preimage single request and unrequest test as non-root > unrequest preimage events 1`] = `[]`;

packages/polkadot/src/__snapshots__/assetHubPolkadot.preimage.e2e.test.ts.snap

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,33 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3+
exports[`Polkadot Asset Hub PreImage > Polkadot Asset Hub PreImage > preimage tests > preimage empty test > note empty preimage events 1`] = `
4+
[
5+
{
6+
"data": {
7+
"hash_": "(hash)",
8+
},
9+
"method": "Noted",
10+
"section": "preimage",
11+
},
12+
]
13+
`;
14+
15+
exports[`Polkadot Asset Hub PreImage > Polkadot Asset Hub PreImage > preimage tests > preimage empty test > unnote empty preimage events 1`] = `
16+
[
17+
{
18+
"data": {
19+
"hash_": "(hash)",
20+
},
21+
"method": "Cleared",
22+
"section": "preimage",
23+
},
24+
]
25+
`;
26+
27+
exports[`Polkadot Asset Hub PreImage > Polkadot Asset Hub PreImage > preimage tests > preimage ensure updated test (fees due) > ensure updated preimage events 1`] = `[]`;
28+
29+
exports[`Polkadot Asset Hub PreImage > Polkadot Asset Hub PreImage > preimage tests > preimage ensure updated test (no fees due) > ensure updated preimage events 1`] = `[]`;
30+
331
exports[`Polkadot Asset Hub PreImage > Polkadot Asset Hub PreImage > preimage tests > preimage single note and unnote test > note preimage events 1`] = `
432
[
533
{
@@ -23,3 +51,47 @@ exports[`Polkadot Asset Hub PreImage > Polkadot Asset Hub PreImage > preimage te
2351
},
2452
]
2553
`;
54+
55+
exports[`Polkadot Asset Hub PreImage > failure tests > preimage oversized test > note oversized preimage events 1`] = `
56+
[
57+
{
58+
"data": {
59+
"hash_": "(hash)",
60+
},
61+
"method": "Noted",
62+
"section": "preimage",
63+
},
64+
]
65+
`;
66+
67+
exports[`Polkadot Asset Hub PreImage > failure tests > preimage repeated note and unnote test > note preimage events 1`] = `
68+
[
69+
{
70+
"data": {
71+
"hash_": "(hash)",
72+
},
73+
"method": "Noted",
74+
"section": "preimage",
75+
},
76+
]
77+
`;
78+
79+
exports[`Polkadot Asset Hub PreImage > failure tests > preimage repeated note and unnote test > repeat note preimage events 1`] = `[]`;
80+
81+
exports[`Polkadot Asset Hub PreImage > failure tests > preimage repeated note and unnote test > repeat unnote preimage events 1`] = `[]`;
82+
83+
exports[`Polkadot Asset Hub PreImage > failure tests > preimage repeated note and unnote test > unnote preimage events 1`] = `
84+
[
85+
{
86+
"data": {
87+
"hash_": "(hash)",
88+
},
89+
"method": "Cleared",
90+
"section": "preimage",
91+
},
92+
]
93+
`;
94+
95+
exports[`Polkadot Asset Hub PreImage > failure tests > preimage single request and unrequest test as non-root > request preimage events 1`] = `[]`;
96+
97+
exports[`Polkadot Asset Hub PreImage > failure tests > preimage single request and unrequest test as non-root > unrequest preimage events 1`] = `[]`;

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 = [
148+
[
149+
[scheduledBlock],
150+
calls.map(({ call, origin }) => ({
151+
call,
152+
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)