Skip to content

Commit 501de91

Browse files
authored
Add tests for PreImage (#470)
Add tests which verify PreImage behavior: - 1) A standard account registers (notes) and unregisters (unnotes) a preimage - 2) A manager account requests and unrequests a preimage hash - 3) A manager account requests a preimage hash once then unrequests it multiple times - 4) A manager account requests and unrequests the hash of a previously-registered preimage - 5) A standard account should not be able to register and unregister preimage hashes Issue: #453
1 parent 6a3e945 commit 501de91

8 files changed

Lines changed: 400 additions & 85 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`Kusama Asset Hub PreImage > Kusama Asset Hub PreImage > preimage tests > preimage single note and unnote test > note 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 single note and unnote test > unnote preimage events 1`] = `
16+
[
17+
{
18+
"data": {
19+
"hash_": "(hash)",
20+
},
21+
"method": "Cleared",
22+
"section": "preimage",
23+
},
24+
]
25+
`;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { assetHubKusama } from '@e2e-test/networks/chains'
2+
import { basePreimageE2ETests, registerTestTree, type TestConfig } from '@e2e-test/shared'
3+
4+
const testConfig: TestConfig = {
5+
testSuiteName: 'Kusama Asset Hub PreImage',
6+
addressEncoding: 2,
7+
blockProvider: 'NonLocal',
8+
asyncBacking: 'Enabled',
9+
}
10+
11+
registerTestTree(basePreimageE2ETests(assetHubKusama, testConfig))

packages/polkadot/src/__snapshots__/polkadot.governance.e2e.test.ts.snap

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

3-
exports[`Polkadot Governance > preimage tests > preimage submission, query and removal works > note preimage events 1`] = `
4-
[
5-
{
6-
"data": {
7-
"hash_": "(hash)",
8-
},
9-
"method": "Noted",
10-
"section": "preimage",
11-
},
12-
]
13-
`;
14-
15-
exports[`Polkadot Governance > preimage tests > preimage submission, query and removal works > unnote preimage events 1`] = `
16-
[
17-
{
18-
"data": {
19-
"hash_": "(hash)",
20-
},
21-
"method": "Cleared",
22-
"section": "preimage",
23-
},
24-
]
25-
`;
26-
273
exports[`Polkadot Governance > referenda tests > referendum lifecycle test - submission, decision deposit, various voting should all work > cancelling referendum with signed origin 1`] = `
284
[
295
{
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`Polkadot PreImage > Polkadot PreImage > preimage tests > preimage single note and unnote test > note preimage events 1`] = `
4+
[
5+
{
6+
"data": {
7+
"hash_": "(hash)",
8+
},
9+
"method": "Noted",
10+
"section": "preimage",
11+
},
12+
]
13+
`;
14+
15+
exports[`Polkadot PreImage > Polkadot PreImage > preimage tests > preimage single note and unnote test > unnote preimage events 1`] = `
16+
[
17+
{
18+
"data": {
19+
"hash_": "(hash)",
20+
},
21+
"method": "Cleared",
22+
"section": "preimage",
23+
},
24+
]
25+
`;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { polkadot } from '@e2e-test/networks/chains'
2+
import { basePreimageE2ETests, type RelayTestConfig, registerTestTree } from '@e2e-test/shared'
3+
4+
const testConfig: RelayTestConfig = {
5+
testSuiteName: 'Polkadot PreImage',
6+
addressEncoding: 0,
7+
blockProvider: 'Local',
8+
}
9+
10+
registerTestTree(basePreimageE2ETests(polkadot, testConfig))

packages/shared/src/governance.ts

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -874,56 +874,6 @@ export async function referendumLifecycleKillTest<
874874
})
875875
}
876876

877-
/**
878-
* Test the registering, querying and unregistering a preimage; in this test, a `spend_local`
879-
* treasury call.
880-
*/
881-
export async function preimageTest<
882-
TCustom extends Record<string, unknown> | undefined,
883-
TInitStorages extends Record<string, Record<string, any>> | undefined,
884-
>(chain: Chain<TCustom, TInitStorages>) {
885-
const [client] = await setupNetworks(chain)
886-
887-
const encodedProposal = client.api.tx.treasury.spendLocal(1e10, devAccounts.bob.address).method
888-
const preimageTx = client.api.tx.preimage.notePreimage(encodedProposal.toHex())
889-
const preImageEvents = await sendTransaction(preimageTx.signAsync(devAccounts.alice))
890-
891-
await client.dev.newBlock()
892-
893-
await checkEvents(preImageEvents, 'preimage').toMatchSnapshot('note preimage events')
894-
895-
/**
896-
* Query noted preimage
897-
*/
898-
899-
let preimage = await client.api.query.preimage.preimageFor([
900-
encodedProposal.hash.toHex(),
901-
encodedProposal.encodedLength,
902-
])
903-
904-
assert(preimage.isSome)
905-
expect(preimage.unwrap().toHex()).toBe(encodedProposal.toHex())
906-
907-
/**
908-
* Unnote preimage with the same account that had previously noted it
909-
*/
910-
911-
const unnotePreimageTx = client.api.tx.preimage.unnotePreimage(encodedProposal.hash.toHex())
912-
const unnotePreImageEvents = await sendTransaction(unnotePreimageTx.signAsync(devAccounts.alice))
913-
914-
await client.dev.newBlock()
915-
916-
await checkEvents(unnotePreImageEvents, 'preimage').toMatchSnapshot('unnote preimage events')
917-
918-
/**
919-
* Query unnoted preimage, and verify it is absent
920-
*/
921-
922-
preimage = await client.api.query.preimage.preimageFor([encodedProposal.hash.toHex(), encodedProposal.encodedLength])
923-
924-
assert(preimage.isNone)
925-
}
926-
927877
export function baseGovernanceE2ETests<
928878
TCustom extends Record<string, unknown> | undefined,
929879
TInitStorages extends Record<string, Record<string, any>> | undefined,
@@ -948,17 +898,6 @@ export function baseGovernanceE2ETests<
948898
},
949899
],
950900
},
951-
{
952-
kind: 'describe',
953-
label: 'preimage tests',
954-
children: [
955-
{
956-
kind: 'test',
957-
label: 'preimage submission, query and removal works',
958-
testFn: async () => await preimageTest(chain),
959-
},
960-
],
961-
},
962901
],
963902
}
964903
}

packages/shared/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export * from './multisig.proxy.js'
1010
export * from './nomination-pools.js'
1111
export * from './people.js'
1212
export * from './postAhmFiltering.js'
13+
export * from './preimage.js'
1314
export * from './proxy.js'
1415
export * from './scheduler.js'
1516
export * from './setup.js'

0 commit comments

Comments
 (0)