Skip to content

Commit 2a774e5

Browse files
authored
vm: remove backfill of block hashes on 2935 activation (#3478)
1 parent e31a65b commit 2a774e5

File tree

2 files changed

+22
-36
lines changed

2 files changed

+22
-36
lines changed

packages/vm/src/runBlock.ts

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -493,8 +493,6 @@ export async function accumulateParentBlockHash(
493493
)
494494
const historyServeWindow = this.common.param('vm', 'historyServeWindow')
495495

496-
const forkTime = this.common.eipTimestamp(2935)
497-
498496
// getAccount with historyAddress will throw error as witnesses are not bundeled
499497
// but we need to put account so as to query later for slot
500498
try {
@@ -524,28 +522,6 @@ export async function accumulateParentBlockHash(
524522
}
525523
await putBlockHash(this, parentHash, currentBlockNumber - BIGINT_1)
526524

527-
// in stateless execution parentBlock is not in blockchain but in chain's blockCache
528-
// need to move the blockCache to the blockchain, in any case we can ignore forkblock
529-
// which is where we need this code segment
530-
try {
531-
const parentBlock = await this.blockchain.getBlock(parentHash)
532-
533-
// If on the fork block, store the old block hashes as well
534-
if (forkTime !== null && parentBlock.header.timestamp < forkTime) {
535-
// forkTime could be null in test fixtures
536-
let ancestor = parentBlock
537-
for (let i = 0; i < Number(historyServeWindow) - 1; i++) {
538-
if (ancestor.header.number === BIGINT_0) {
539-
break
540-
}
541-
542-
ancestor = await this.blockchain.getBlock(ancestor.header.parentHash)
543-
await putBlockHash(this, ancestor.hash(), ancestor.header.number)
544-
}
545-
}
546-
// eslint-disable-next-line no-empty
547-
} catch (_e) {}
548-
549525
// do cleanup if the code was not deployed
550526
await this.evm.journal.cleanup()
551527
}

packages/vm/test/api/EIPs/eip-2935-historical-block-hashes.spec.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,9 @@ describe('EIP 2935: historical block hashes', () => {
211211
})
212212
it('should ensure blocks older than 256 blocks can be retrieved from the history contract', async () => {
213213
// Test: build a chain with 256+ blocks and then retrieve BLOCKHASH of the genesis block and block 1
214-
const blocksActivation = 256 // This ensures that block 0 - 255 all get stored into the hash contract
214+
const blocksActivation = 256 // This ensures that only block 255 gets stored into the hash contract
215215
// More than blocks activation to build, so we can ensure that we can also retrieve block 0 or block 1 hash at block 300
216-
const blocksToBuild = 300
216+
const blocksToBuild = 500
217217
const commonGetHistoryServeWindow = eip2935ActiveAtCommon(0)
218218
commonGetHistoryServeWindow.setEIPs([2935])
219219
const common = eip2935ActiveAtCommon(blocksActivation)
@@ -225,23 +225,23 @@ describe('EIP 2935: historical block hashes', () => {
225225
validateConsensus: false,
226226
})
227227
const vm = await VM.create({ common, blockchain })
228-
let parentBlock = await vm.blockchain.getBlock(0)
228+
let lastBlock = await vm.blockchain.getBlock(0)
229229
for (let i = 1; i <= blocksToBuild; i++) {
230-
parentBlock = await (
230+
lastBlock = await (
231231
await vm.buildBlock({
232-
parentBlock,
232+
parentBlock: lastBlock,
233233
blockOpts: {
234234
putBlockIntoBlockchain: false,
235235
setHardfork: true,
236236
},
237237
headerData: {
238-
timestamp: parentBlock.header.number + BIGINT_1,
238+
timestamp: lastBlock.header.number + BIGINT_1,
239239
},
240240
})
241241
).build()
242-
await vm.blockchain.putBlock(parentBlock)
242+
await vm.blockchain.putBlock(lastBlock)
243243
await vm.runBlock({
244-
block: parentBlock,
244+
block: lastBlock,
245245
generate: true,
246246
skipHeaderValidation: true,
247247
setHardfork: true,
@@ -263,14 +263,24 @@ describe('EIP 2935: historical block hashes', () => {
263263
historyAddress,
264264
setLengthLeft(bigIntToBytes(BigInt(i) % historyServeWindow), 32)
265265
)
266+
267+
// we will evaluate on lastBlock where 7709 is active and BLOCKHASH
268+
// will look from the state if within 256 window
266269
const ret = await vm.evm.runCall({
267270
// Code: RETURN the BLOCKHASH of block i
268271
// PUSH(i) BLOCKHASH PUSH(32) MSTORE PUSH(64) PUSH(0) RETURN
269272
// Note: need to return a contract with starting zero bytes to avoid non-deployable contracts by EIP 3540
270273
data: hexToBytes('0x61' + i.toString(16).padStart(4, '0') + '4060205260406000F3'),
271-
block: parentBlock,
274+
block: lastBlock,
272275
})
273-
if (i <= blocksToBuild - 1 && i >= blocksToBuild - Number(historyServeWindow)) {
276+
277+
// contract will only have hashes between blocksActivation -1 and blocksToBuild -1 thresholded by
278+
// historyServeWindow window
279+
if (
280+
i >= blocksActivation - 1 &&
281+
i <= blocksToBuild - 1 &&
282+
i >= blocksToBuild - Number(historyServeWindow)
283+
) {
274284
assert.ok(equalsBytes(setLengthLeft(storage, 32), block.hash()))
275285
if (i >= blocksToBuild - 256) {
276286
assert.ok(equalsBytes(ret.execResult.returnValue, setLengthLeft(block.hash(), 64)))
@@ -294,8 +304,8 @@ describe('EIP 2935: historical block hashes', () => {
294304
{ common }
295305
)
296306

297-
// should be able to resolve blockhash via contract code
298-
for (const i of [0, 1, blocksActivation, blocksToBuild - 1]) {
307+
// should be able to resolve blockhash via contract code but from the blocksActivation -1 onwards
308+
for (const i of [blocksActivation - 1, blocksActivation, blocksToBuild - 1]) {
299309
const blockHashi = await testBlockhashContract(vm, block, BigInt(i))
300310
const blocki = await blockchain.getBlock(i)
301311
assert.ok(equalsBytes(blockHashi, blocki.hash()))

0 commit comments

Comments
 (0)