@@ -211,9 +211,9 @@ describe('EIP 2935: historical block hashes', () => {
211
211
} )
212
212
it ( 'should ensure blocks older than 256 blocks can be retrieved from the history contract' , async ( ) => {
213
213
// 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
215
215
// 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
217
217
const commonGetHistoryServeWindow = eip2935ActiveAtCommon ( 0 )
218
218
commonGetHistoryServeWindow . setEIPs ( [ 2935 ] )
219
219
const common = eip2935ActiveAtCommon ( blocksActivation )
@@ -225,23 +225,23 @@ describe('EIP 2935: historical block hashes', () => {
225
225
validateConsensus : false ,
226
226
} )
227
227
const vm = await VM . create ( { common, blockchain } )
228
- let parentBlock = await vm . blockchain . getBlock ( 0 )
228
+ let lastBlock = await vm . blockchain . getBlock ( 0 )
229
229
for ( let i = 1 ; i <= blocksToBuild ; i ++ ) {
230
- parentBlock = await (
230
+ lastBlock = await (
231
231
await vm . buildBlock ( {
232
- parentBlock,
232
+ parentBlock : lastBlock ,
233
233
blockOpts : {
234
234
putBlockIntoBlockchain : false ,
235
235
setHardfork : true ,
236
236
} ,
237
237
headerData : {
238
- timestamp : parentBlock . header . number + BIGINT_1 ,
238
+ timestamp : lastBlock . header . number + BIGINT_1 ,
239
239
} ,
240
240
} )
241
241
) . build ( )
242
- await vm . blockchain . putBlock ( parentBlock )
242
+ await vm . blockchain . putBlock ( lastBlock )
243
243
await vm . runBlock ( {
244
- block : parentBlock ,
244
+ block : lastBlock ,
245
245
generate : true ,
246
246
skipHeaderValidation : true ,
247
247
setHardfork : true ,
@@ -263,14 +263,24 @@ describe('EIP 2935: historical block hashes', () => {
263
263
historyAddress ,
264
264
setLengthLeft ( bigIntToBytes ( BigInt ( i ) % historyServeWindow ) , 32 )
265
265
)
266
+
267
+ // we will evaluate on lastBlock where 7709 is active and BLOCKHASH
268
+ // will look from the state if within 256 window
266
269
const ret = await vm . evm . runCall ( {
267
270
// Code: RETURN the BLOCKHASH of block i
268
271
// PUSH(i) BLOCKHASH PUSH(32) MSTORE PUSH(64) PUSH(0) RETURN
269
272
// Note: need to return a contract with starting zero bytes to avoid non-deployable contracts by EIP 3540
270
273
data : hexToBytes ( '0x61' + i . toString ( 16 ) . padStart ( 4 , '0' ) + '4060205260406000F3' ) ,
271
- block : parentBlock ,
274
+ block : lastBlock ,
272
275
} )
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
+ ) {
274
284
assert . ok ( equalsBytes ( setLengthLeft ( storage , 32 ) , block . hash ( ) ) )
275
285
if ( i >= blocksToBuild - 256 ) {
276
286
assert . ok ( equalsBytes ( ret . execResult . returnValue , setLengthLeft ( block . hash ( ) , 64 ) ) )
@@ -294,8 +304,8 @@ describe('EIP 2935: historical block hashes', () => {
294
304
{ common }
295
305
)
296
306
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 ] ) {
299
309
const blockHashi = await testBlockhashContract ( vm , block , BigInt ( i ) )
300
310
const blocki = await blockchain . getBlock ( i )
301
311
assert . ok ( equalsBytes ( blockHashi , blocki . hash ( ) ) )
0 commit comments