@@ -209,6 +209,17 @@ func endNumber*(era: Era1): uint64 =
209209func endNumber * (blockIdx: BlockIndex ): uint64 =
210210 blockIdx.startNumber + blockIdx.offsets.lenu64 () - 1
211211
212+ func era * (blockNumber: uint64 ): Era1 =
213+ Era1 (blockNumber div MaxEra1Size )
214+
215+ func offsetsLen (startNumber: uint64 ): int =
216+ # For the era where the merge happens the era files only holds the blocks
217+ # until the merge block so the offsets length needs to be adapted too.
218+ if startNumber.era () >= mergeBlockNumber.era ():
219+ int ((mergeBlockNumber) mod MaxEra1Size )
220+ else :
221+ MaxEra1Size
222+
212223proc toCompressedRlpBytes (item: auto ): seq [byte ] =
213224 snappy.encodeFramed (rlp.encode (item))
214225
@@ -226,7 +237,7 @@ proc init*(
226237 ok (Era1Group (
227238 blockIndex: BlockIndex (
228239 startNumber: startNumber,
229- offsets: newSeq [int64 ](8192 . int64 )
240+ offsets: newSeq [int64 ](startNumber. offsetsLen () )
230241 )))
231242
232243proc update * (
@@ -306,10 +317,8 @@ proc open*(_: type Era1File, name: string): Result[Era1File, string] =
306317 ? f[].setFilePos (blockIdxPos, SeekPosition .SeekCurrent ).mapErr (ioErrorMsg)
307318
308319 let blockIdx = ? f[].readBlockIndex ()
309- # Note: Could do an additional offset.len check here by calculating what it
310- # should be based on mergeBlockNumber. It is however not necessary as the
311- # accumulator root will fail if to many blocks are added (it will take a bit
312- # longer though).
320+ if blockIdx.offsets.len () != blockIdx.startNumber.offsetsLen ():
321+ return err (" Block index length invalid" )
313322
314323 let res = Era1File (handle: f, blockIdx: blockIdx)
315324 reset (f)
0 commit comments