Skip to content

Commit

Permalink
add unsafeBlockHeight (#387)
Browse files Browse the repository at this point in the history
* add unsageBlockHeight

* add unsafeBlockHeight in test

* Update packages/core/src/blockchain/block-builder.ts

Co-authored-by: Xiliang Chen <xlchen1291@gmail.com>

* fix unsafeBlockHeight test

---------

Co-authored-by: Xiliang Chen <xlchen1291@gmail.com>
  • Loading branch information
qiweiii and xlc authored Sep 5, 2023
1 parent d81a435 commit c8e1c21
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
3 changes: 2 additions & 1 deletion packages/chopsticks/src/plugins/new-block/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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())
Expand Down
16 changes: 11 additions & 5 deletions packages/core/src/blockchain/block-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -91,7 +91,7 @@ export const newHeader = async (head: Block) => {

const header = meta.registry.createType<Header>('Header', {
parentHash: head.hash,
number: head.number + 1,
number: unsafeBlockHeight ?? head.number + 1,
stateRoot: '0x0000000000000000000000000000000000000000000000000000000000000000',
extrinsicsRoot: '0x0000000000000000000000000000000000000000000000000000000000000000',
digest: {
Expand Down Expand Up @@ -147,17 +147,23 @@ export const buildBlock = async (
extrinsics: HexString[],
ump: Record<number, HexString[]>,
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
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/blockchain/txpool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface BuildBlockParams {
upwardMessages: Record<number, HexString[]>
horizontalMessages: Record<number, HorizontalMessage[]>
transactions: HexString[]
unsafeBlockHeight?: number
}

export class TxPool {
Expand Down Expand Up @@ -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]
Expand All @@ -186,6 +188,7 @@ export class TxPool {
upwardMessages,
downwardMessages,
horizontalMessages,
unsafeBlockHeight,
})
}

Expand Down Expand Up @@ -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) })
Expand Down
10 changes: 10 additions & 0 deletions packages/e2e/src/build-block.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})

0 comments on commit c8e1c21

Please sign in to comment.