Skip to content

Commit

Permalink
dev_newBlock support relayParentNumber (#832)
Browse files Browse the repository at this point in the history
* dev_newBlock support relayParentNumber

* fix

* fix

* fix test

* fix

* fix

* update test
  • Loading branch information
xlc authored Oct 1, 2024
1 parent ac102d4 commit 97d77eb
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ export class SetValidationData implements InherentProvider {
validationData: {
...extrinsic.validationData,
relayParentStorageRoot: trieRootHash,
relayParentNumber: extrinsic.validationData.relayParentNumber + relaySlotIncrease,
relayParentNumber: params.relayParentNumber ?? extrinsic.validationData.relayParentNumber + relaySlotIncrease,
},
relayChainState: {
trieNodes: nodes,
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/blockchain/txpool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export interface BuildBlockParams {
transactions: HexString[]
unsafeBlockHeight?: number
relayChainStateOverrides?: [HexString, HexString | null][]
relayParentNumber?: number
}

export class TxPool {
Expand Down Expand Up @@ -179,6 +180,7 @@ export class TxPool {
const horizontalMessages = params?.horizontalMessages || { ...this.#hrmp }
const unsafeBlockHeight = params?.unsafeBlockHeight
const relayChainStateOverrides = params?.relayChainStateOverrides
const relayParentNumber = params?.relayParentNumber
if (!params?.upwardMessages) {
for (const id of Object.keys(this.#ump)) {
delete this.#ump[id]
Expand All @@ -198,6 +200,7 @@ export class TxPool {
horizontalMessages,
unsafeBlockHeight,
relayChainStateOverrides,
relayParentNumber,
})

// with the latest message queue, messages could be processed in the upcoming block
Expand Down
11 changes: 8 additions & 3 deletions packages/core/src/rpc/dev/new-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const schema = z.object({
transactions: z.array(zHex).min(1).optional(),
unsafeBlockHeight: z.number().optional(),
relayChainStateOverrides: z.array(z.tuple([zHex, z.union([zHex, z.null()])])).optional(),
relayParentNumber: z.number().optional(),
})

type Params = z.infer<typeof schema>
Expand Down Expand Up @@ -70,6 +71,10 @@ export interface NewBlockParams {
* Build block using a custom relay chain state
*/
relayChainStateOverrides: Params['relayChainStateOverrides']
/**
* Build block using a custom relay parent number
*/
relayParentNumber: Params['relayParentNumber']
}

/**
Expand Down Expand Up @@ -111,9 +116,8 @@ export interface NewBlockParams {
* ```
*/
export const dev_newBlock = async (context: Context, [params]: [NewBlockParams]) => {
const { count, to, hrmp, ump, dmp, transactions, unsafeBlockHeight, relayChainStateOverrides } = schema.parse(
params || {},
)
const { count, to, hrmp, ump, dmp, transactions, unsafeBlockHeight, relayChainStateOverrides, relayParentNumber } =
schema.parse(params || {})
const now = context.chain.head.number
const diff = to ? to - now : count
const finalCount = diff !== undefined ? Math.max(diff, 1) : 1
Expand All @@ -132,6 +136,7 @@ export const dev_newBlock = async (context: Context, [params]: [NewBlockParams])
downwardMessages: dmp,
unsafeBlockHeight: i === 0 ? unsafeBlockHeight : undefined,
relayChainStateOverrides: relayChainStateOverrides,
relayParentNumber: relayParentNumber,
})
.catch((error) => {
throw new ResponseError(1, error.toString())
Expand Down
14 changes: 14 additions & 0 deletions packages/e2e/src/build-parachain-block.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,18 @@ describe('override-relay-state-proof', async () => {

await teardown()
})

it('newBlock with relayParentNumber', async () => {
const { dev, api, teardown } = await networks.acala()

await dev.newBlock({ relayParentNumber: 14355209 })
const block = await api.rpc.chain.getBlock()
const setValidationData = block.block.extrinsics
.find(({ method }) => method.method == 'setValidationData')
?.method.toJSON().args.data

expect(setValidationData.validationData.relayParentNumber).to.be.eq(14355209)

await teardown()
})
})
1 change: 1 addition & 0 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
},
"dependencies": {
"@acala-network/chopsticks": "workspace:*",
"@acala-network/chopsticks-core": "workspace:*",
"@polkadot/api": "^12.3.1",
"@polkadot/api-base": "^12.3.1",
"@polkadot/keyring": "^13.0.2",
Expand Down
3 changes: 2 additions & 1 deletion packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { Codec } from '@polkadot/types/types'
import { Config } from '@acala-network/chopsticks/schema/index.js'
import { HexString } from '@polkadot/util/types'
import { Keyring, createTestKeyring } from '@polkadot/keyring'
import { NewBlockParams } from '@acala-network/chopsticks-core/rpc/dev/new-block.js'
import { SubmittableExtrinsic } from '@polkadot/api-base/types'

const logger = defaultLogger.child({ name: 'utils' })
Expand Down Expand Up @@ -96,7 +97,7 @@ export const setupContextWithConfig = async ({ timeout, ...config }: SetupConfig
ws,
api,
dev: {
newBlock: (param?: { count?: number; to?: number; unsafeBlockHeight?: number }): Promise<string> => {
newBlock: (param?: Partial<NewBlockParams>): Promise<string> => {
return ws.send('dev_newBlock', [param])
},
setStorage: (values: StorageValues, blockHash?: string) => {
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ __metadata:
resolution: "@acala-network/chopsticks-utils@workspace:packages/utils"
dependencies:
"@acala-network/chopsticks": "workspace:*"
"@acala-network/chopsticks-core": "workspace:*"
"@polkadot/api": "npm:^12.3.1"
"@polkadot/api-base": "npm:^12.3.1"
"@polkadot/keyring": "npm:^13.0.2"
Expand Down

0 comments on commit 97d77eb

Please sign in to comment.