diff --git a/packages/chopsticks/src/plugins/new-block/index.ts b/packages/chopsticks/src/plugins/new-block/index.ts index d84e0ddb..680ffe96 100644 --- a/packages/chopsticks/src/plugins/new-block/index.ts +++ b/packages/chopsticks/src/plugins/new-block/index.ts @@ -2,7 +2,7 @@ import { Handler, ResponseError } from '../../rpc/shared' import { defaultLogger } from '../../logger' export const rpc: Handler = async (context, [param]) => { - const { count, to, hrmp, ump, dmp, transactions } = param || {} + const { count, to, hrmp, ump, dmp, transactions, unsafeBlockHeight } = param || {} const now = context.chain.head.number const diff = to ? to - now : count const finalCount = diff > 0 ? diff : 1 @@ -16,6 +16,7 @@ export const rpc: Handler = async (context, [param]) => { horizontalMessages: hrmp, upwardMessages: ump, downwardMessages: dmp, + unsafeBlockHeight: i === 0 ? unsafeBlockHeight : undefined, }) .catch((error) => { throw new ResponseError(1, error.toString()) diff --git a/packages/core/src/blockchain/block-builder.ts b/packages/core/src/blockchain/block-builder.ts index 561a672d..dc9d9250 100644 --- a/packages/core/src/blockchain/block-builder.ts +++ b/packages/core/src/blockchain/block-builder.ts @@ -52,7 +52,7 @@ const getNewSlot = (digest: RawBabePreDigest, slotNumber: number) => { return digest.toJSON() } -export const newHeader = async (head: Block) => { +export const newHeader = async (head: Block, unsafeBlockHeight?: number) => { const meta = await head.meta const parentHeader = await head.header @@ -91,7 +91,7 @@ export const newHeader = async (head: Block) => { const header = meta.registry.createType
('Header', { parentHash: head.hash, - number: head.number + 1, + number: unsafeBlockHeight ?? head.number + 1, stateRoot: '0x0000000000000000000000000000000000000000000000000000000000000000', extrinsicsRoot: '0x0000000000000000000000000000000000000000000000000000000000000000', digest: { @@ -147,17 +147,23 @@ export const buildBlock = async ( extrinsics: HexString[], ump: Record, onApplyExtrinsicError: (extrinsic: HexString, error: TransactionValidityError) => void, + unsafeBlockHeight?: number, ): Promise<[Block, HexString[]]> => { const registry = await head.registry - const header = await newHeader(head) + const header = await newHeader(head, unsafeBlockHeight) + const newBlockNumber = header.number.toNumber() + + if (newBlockNumber < head.number) { + throw new Error('unsafeBlockHeight is not allowed to be less than current block number') + } logger.info( { - number: head.number + 1, + number: newBlockNumber, extrinsicsCount: extrinsics.length, umpCount: Object.keys(ump).length, }, - `Try building block #${(head.number + 1).toLocaleString()}`, + `Try building block #${newBlockNumber.toLocaleString()}`, ) let layer: StorageLayer | undefined diff --git a/packages/core/src/blockchain/txpool.ts b/packages/core/src/blockchain/txpool.ts index d0bb3924..e273dfaf 100644 --- a/packages/core/src/blockchain/txpool.ts +++ b/packages/core/src/blockchain/txpool.ts @@ -34,6 +34,7 @@ export interface BuildBlockParams { upwardMessages: Record horizontalMessages: Record transactions: HexString[] + unsafeBlockHeight?: number } export class TxPool { @@ -171,6 +172,7 @@ export class TxPool { const upwardMessages = params?.upwardMessages || { ...this.#ump } const downwardMessages = params?.downwardMessages || this.#dmp.splice(0) const horizontalMessages = params?.horizontalMessages || { ...this.#hrmp } + const unsafeBlockHeight = params?.unsafeBlockHeight if (!params?.upwardMessages) { for (const id of Object.keys(this.#ump)) { delete this.#ump[id] @@ -186,6 +188,7 @@ export class TxPool { upwardMessages, downwardMessages, horizontalMessages, + unsafeBlockHeight, }) } @@ -231,6 +234,7 @@ export class TxPool { (extrinsic, error) => { this.event.emit(APPLY_EXTRINSIC_ERROR, [extrinsic, error]) }, + params.unsafeBlockHeight, ) for (const extrinsic of pendingExtrinsics) { this.#pool.push({ extrinsic, signer: await this.#getSigner(extrinsic) }) diff --git a/packages/e2e/src/build-block.test.ts b/packages/e2e/src/build-block.test.ts index bfc4e886..1f1c631e 100644 --- a/packages/e2e/src/build-block.test.ts +++ b/packages/e2e/src/build-block.test.ts @@ -40,4 +40,14 @@ describe.each([ expect(chain.head.number).eq(blockNumber + 2) await teardown() }) + + it.runIf(process.env.CI)('build block using unsafeBlockHeight', async () => { + const { chain, ws, teardown } = await setup() + storage && (await ws.send('dev_setStorage', [storage])) + const blockNumber = chain.head.number + const unsafeBlockHeight = blockNumber + 100 + await ws.send('dev_newBlock', [{ count: 2, unsafeBlockHeight }]) + expect(chain.head.number).eq(unsafeBlockHeight + 1) + await teardown() + }) })