Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
rockbmb committed Oct 19, 2024
1 parent 52f6914 commit 1034398
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 42 deletions.
4 changes: 2 additions & 2 deletions packages/networks/src/chains/polkadot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ const getInitStorages = () => ({
Account: [[[defaultAccounts.alice.address], { providers: 1, data: { free: 10 * 1e12 } }]],
},
ParasDisputes: {
// those can makes block building super slow
// these can makes block building super slow
$removePrefix: ['disputes'],
},
Dmp: {
// clear existing dmp to avoid impact test result
// clear existing dmp to avoid impacting test result
$removePrefix: ['downwardMessageQueues'],
},
})
Expand Down
9 changes: 9 additions & 0 deletions packages/tests/people/polkadot/authority.e2e.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { describe } from 'vitest'

import { peoplePolkadot, polkadot } from '@e2e-test/networks/chains'

import { addIdentityThenKill } from '../shared.js'

describe('Polkadot people chain: Adding a username authority (as root from the relay chain) works', async () => {
await addIdentityThenKill(polkadot, peoplePolkadot)
})
184 changes: 144 additions & 40 deletions packages/tests/people/shared.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BN } from 'bn.js'
import { assert } from 'vitest'

import { ApiPromise } from '@polkadot/api'
import { Chain, defaultAccounts } from '@e2e-test/networks'
import { ITuple } from '@polkadot/types/types'
import { Option, Vec, u128, u32 } from '@polkadot/types'
Expand All @@ -10,9 +11,24 @@ import {
PalletIdentityRegistrarInfo,
PalletIdentityRegistration,
} from '@polkadot/types/lookup'
import { aliceRegistrar } from '@e2e-test/networks/chains'
import { setupNetworks } from '@e2e-test/shared'

/**
* Identity to be used in tests.
*/
const identity = {
email: { Raw: '[email protected]' },
legal: { Raw: 'FirstName LastName' },
matrix: { Raw: '@test:test_server.io' },
twitter: { Raw: '@test_twitter' },
github: { Raw: 'test_github' },
discord: { Raw: 'test_discord' },
web: { Raw: 'http://test.te/me' },
image: { Raw: 'test' },
display: { Raw: 'Test Display' },
pgpFingerprint: 'a1b2c3d4e5f6g7h8i9j1',
}

/**
* Test the process of
* 1. setting an identity,
Expand All @@ -28,19 +44,6 @@ export async function setIdentityThenRequestAndProvideJudgement<
>(peopleChain: Chain<TCustom, TInitStorages>) {
const [peopleClient] = await setupNetworks(peopleChain)

const identity = {
email: { raw: '[email protected]' },
legal: { raw: 'FirstName LastName' },
matrix: { raw: '@test:test_server.io' },
twitter: { Raw: '@test_twitter' },
github: { Raw: 'test_github' },
discord: { Raw: 'test_discord' },
web: { Raw: 'http://test.te/me' },
image: { raw: 'test' },
display: { raw: 'Test Display' },
pgpFingerprint: 'a1b2c3d4e5f6g7h8i9j1',
}

const querier = peopleClient.api.query
const txApi = peopleClient.api.tx

Expand All @@ -58,6 +61,7 @@ export async function setIdentityThenRequestAndProvideJudgement<
const registrationInfo: PalletIdentityRegistration = identityInfoReply.unwrap()[0]
const identityInfo = registrationInfo.info

assert(identityInfo.eq(identity))
console.log(identityInfo.toHuman())
assert(
registrationInfo.judgements.isEmpty,
Expand Down Expand Up @@ -142,19 +146,6 @@ export async function setIdentityThenRequesThenCancelThenClear<
>(peopleChain: Chain<TCustom, TInitStorages>) {
const [peopleClient] = await setupNetworks(peopleChain)

const identity = {
email: { raw: '[email protected]' },
legal: { raw: 'FirstName LastName' },
matrix: { raw: '@test:test_server.io' },
twitter: { Raw: '@test_twitter' },
github: { Raw: 'test_github' },
discord: { Raw: 'test_discord' },
web: { Raw: 'http://test.te/me' },
image: { raw: 'test' },
display: { raw: 'Test Display' },
pgpFingerprint: 'a1b2c3d4e5f6g7h8i9j1',
}

const querier = peopleClient.api.query
const txApi = peopleClient.api.tx

Expand Down Expand Up @@ -247,19 +238,6 @@ export async function setIdentityThenAddSubsThenRemove<
>(peopleChain: Chain<TCustom, TInitStorages>) {
const [peopleClient] = await setupNetworks(peopleChain)

const identity = {
email: { raw: '[email protected]' },
legal: { raw: 'FirstName LastName' },
matrix: { raw: '@test:test_server.io' },
twitter: { Raw: '@test_twitter' },
github: { Raw: 'test_github' },
discord: { Raw: 'test_discord' },
web: { Raw: 'http://test.te/me' },
image: { raw: 'test' },
display: { raw: 'Test Display' },
pgpFingerprint: 'a1b2c3d4e5f6g7h8i9j1',
}

const querier = peopleClient.api.query
const txApi = peopleClient.api.tx

Expand Down Expand Up @@ -505,3 +483,129 @@ export async function addRegistrarViaRelayAsRoot<
assert(registrarsAfterParaBlock.length === 3)
assertionHelper(registrarsAfterParaBlock, 'Registrars after parachain block differ from expected')
}

async function sendXcmFromRelay(
relayClient: { api: ApiPromise },
encodedChainCallData: `0x${string}`,
paraId: number,
requireWeightAtMost = { proofSize: '10000', refTime: '100000000' },
) {
// Destination of the XCM message sent from the relay chain to the parachain via `xcmPallet`
const dest = {
V4: {
parents: 0,
interior: {
X1: [
{
Parachain: paraId,
},
],
},
},
}

// The message being sent to the parachain, containing a call to be executed in the parachain:
// an origin-restricted extrinsic from the `identity` pallet, to be executed as a `SuperUser`.
const message = {
V4: [
{
UnpaidExecution: {
weightLimit: 'Unlimited',
checkOrigin: null,
},
},
{
Transact: {
call: {
encoded: encodedChainCallData,
},
originKind: 'SuperUser',
requireWeightAtMost,
},
},
],
}

const xcmTx = relayClient.api.tx.xcmPallet.send(dest, message)
const encodedRelayCallData = xcmTx.method.toHex()

/**
* Execution of XCM call via RPC `dev_setStorage`
*/

const number = (await relayClient.api.rpc.chain.getHeader()).number.toNumber()

await relayClient.api.rpc('dev_setStorage', {
scheduler: {
agenda: [
[
[number + 1],
[
{
call: {
Inline: encodedRelayCallData,
},
origin: {
system: 'Root',
},
},
],
],
],
},
})
}

/**
* Test the process of setting up an identity, then forcibly removing it.
*
* It uses the parachain's relay to send an XCM message forcing execution of the normally gated
* `kill_identity` call as `SuperUser`.
*
* @param relayChain Relay chain on which the test will be run: Polkadot or Kusama.
* Must have `xcmpPallet` available.
* @param peopleChain People parachain whose registrars will be modified and asserted upon.
*/
export async function addIdentityThenKill<
TCustom extends Record<string, unknown> | undefined,
TInitStoragesRelay extends Record<string, Record<string, any>> | undefined,
TInitStoragesPara extends Record<string, Record<string, any>> | undefined,
>(relayChain: Chain<TCustom, TInitStoragesRelay>, peopleChain: Chain<TCustom, TInitStoragesPara>) {
/**
* Setup relay and parachain clients
*/

const [relayClient, peopleClient] = await setupNetworks(relayChain, peopleChain)

const querier = peopleClient.api.query
const txApi = peopleClient.api.tx

/**
* Set Bob's on-chain identity
*/

const setIdTx = txApi.identity.setIdentity(identity)
await setIdTx.signAndSend(defaultAccounts.bob)

await peopleClient.chain.newBlock()

const identityInfoReply = await querier.identity.identityOf(defaultAccounts.bob.address)
const registrationInfo: PalletIdentityRegistration = identityInfoReply.unwrap()[0]
const identityInfo = registrationInfo.info
assert(identityInfo.eq(identity))

/**
* Kill identity forcibly
*/

const killIdentityTx = peopleClient.api.tx.identity.killIdentity(defaultAccounts.bob.address)
const encodedPeopleChainCallData = killIdentityTx.method.toHex()

sendXcmFromRelay(relayClient, encodedPeopleChainCallData, 1004)

await relayClient.chain.newBlock()
await peopleClient.chain.newBlock()

const bobIdentity = await peopleClient.api.query.identity.identityOf(defaultAccounts.bob.address)
console.log(bobIdentity.isNone)
}

0 comments on commit 1034398

Please sign in to comment.