Skip to content

Commit

Permalink
fix unsafe block height error (#447)
Browse files Browse the repository at this point in the history
* fix unsafe block height error

* add regression test
  • Loading branch information
qiweiii authored Oct 17, 2023
1 parent e337727 commit dc6626e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
4 changes: 4 additions & 0 deletions packages/chopsticks/src/plugins/new-block/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ export const rpc = async (context: Context, params: [NewBlockParams]) => {

let finalHash: string | undefined

if (unsafeBlockHeight < now) {
throw new ResponseError(1, 'unsafeBlockHeight must be greater than current block height')
}

for (let i = 0; i < finalCount; i++) {
const block = await context.chain
.newBlock({
Expand Down
4 changes: 0 additions & 4 deletions packages/core/src/blockchain/block-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,6 @@ export const buildBlock = async (
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: newBlockNumber,
Expand Down
9 changes: 9 additions & 0 deletions packages/e2e/src/build-block.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,17 @@ describe.runIf(process.env.CI).each([
storage && (await ws.send('dev_setStorage', [storage]))
const blockNumber = chain.head.number
const unsafeBlockHeight = blockNumber + 100

// unsafeBlockHeight works
await ws.send('dev_newBlock', [{ count: 2, unsafeBlockHeight }])
expect(chain.head.number).eq(unsafeBlockHeight + 1)

// unsafeBlockHeight using earlier block throw error but won't crash
await expect(ws.send('dev_newBlock', [{ unsafeBlockHeight: blockNumber - 1 }])).rejects.toThrowError(
'1: unsafeBlockHeight must be greater than current block height',
)
expect(chain.head.number).eq(unsafeBlockHeight + 1)

await teardown()
})
})

0 comments on commit dc6626e

Please sign in to comment.