From dc6626e41108e36d06abeeed5377205b56c934b6 Mon Sep 17 00:00:00 2001 From: Qiwei Yang Date: Wed, 18 Oct 2023 07:47:12 +0800 Subject: [PATCH] fix unsafe block height error (#447) * fix unsafe block height error * add regression test --- packages/chopsticks/src/plugins/new-block/index.ts | 4 ++++ packages/core/src/blockchain/block-builder.ts | 4 ---- packages/e2e/src/build-block.test.ts | 9 +++++++++ 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/packages/chopsticks/src/plugins/new-block/index.ts b/packages/chopsticks/src/plugins/new-block/index.ts index 673c416d..97d81019 100644 --- a/packages/chopsticks/src/plugins/new-block/index.ts +++ b/packages/chopsticks/src/plugins/new-block/index.ts @@ -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({ diff --git a/packages/core/src/blockchain/block-builder.ts b/packages/core/src/blockchain/block-builder.ts index ad3f982d..8238ff5a 100644 --- a/packages/core/src/blockchain/block-builder.ts +++ b/packages/core/src/blockchain/block-builder.ts @@ -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, diff --git a/packages/e2e/src/build-block.test.ts b/packages/e2e/src/build-block.test.ts index 27c496ea..576e73af 100644 --- a/packages/e2e/src/build-block.test.ts +++ b/packages/e2e/src/build-block.test.ts @@ -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() }) })