Skip to content

Commit

Permalink
dev_newBlock support relayParentNumber
Browse files Browse the repository at this point in the history
  • Loading branch information
xlc committed Oct 1, 2024
1 parent ac102d4 commit 3129819
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 3 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
10 changes: 9 additions & 1 deletion 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,7 +116,9 @@ export interface NewBlockParams {
* ```
*/
export const dev_newBlock = async (context: Context, [params]: [NewBlockParams]) => {
const { count, to, hrmp, ump, dmp, transactions, unsafeBlockHeight, relayChainStateOverrides } = schema.parse(
const {
count, to, hrmp, ump, dmp, transactions, unsafeBlockHeight, relayChainStateOverrides, relayParentNumber
} = schema.parse(
params || {},
)
const now = context.chain.head.number
Expand All @@ -132,6 +139,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
10 changes: 10 additions & 0 deletions packages/e2e/src/dev.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ describe('dev rpc', async () => {
expect(newBlockNumber2).toBe(blockNumber + 5)
})

it('newBlock with relayParentNumber', async () => {
await dev.newBlock({ relayParentNumber: 14355200 })
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(14355200)
})

it('timeTravel', async () => {
const date = 'Jan 1, 2023'
const timestamp = await dev.timeTravel(date)
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

0 comments on commit 3129819

Please sign in to comment.