Skip to content

Commit

Permalink
Remove duplicated e2store code
Browse files Browse the repository at this point in the history
  • Loading branch information
kdeme committed Feb 8, 2024
1 parent d8096be commit f78f20c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 75 deletions.
77 changes: 8 additions & 69 deletions fluffy/eth_data/era1.nim
Original file line number Diff line number Diff line change
Expand Up @@ -61,72 +61,6 @@ type
startNumber*: uint64
offsets*: seq[int64] # Absolute positions in file

template lenu64(x: untyped): untyped =
uint64(len(x))

## Following procs are more e2s specific and copied from e2store.nim
## TODO: Split up e2store.nim between e2s and era1 specific parts and reuse
## e2s code.

proc toString(v: IoErrorCode): string =
try: ioErrorMsg(v)
except Exception as e: raiseAssert e.msg

proc append(f: IoHandle, data: openArray[byte]): Result[void, string] =
if (? writeFile(f, data).mapErr(toString)) != data.len.uint:
return err("could not write data")
ok()

proc appendHeader(f: IoHandle, typ: Type, dataLen: int): Result[int64, string] =
if dataLen.uint64 > uint32.high:
return err("entry does not fit 32-bit length")

let start = ? getFilePos(f).mapErr(toString)

? append(f, typ)
? append(f, toBytesLE(dataLen.uint32))
? append(f, [0'u8, 0'u8])

ok(start)

proc checkBytesLeft(f: IoHandle, expected: int64): Result[void, string] =
let size = ? getFileSize(f).mapErr(toString)
if expected > size:
return err("Record extends past end of file")

let pos = ? getFilePos(f).mapErr(toString)
if expected > size - pos:
return err("Record extends past end of file")

ok()

proc readFileExact(f: IoHandle, buf: var openArray[byte]): Result[void, string] =
if (? f.readFile(buf).mapErr(toString)) != buf.len().uint:
return err("missing data")
ok()

proc readHeader(f: IoHandle): Result[Header, string] =
var buf: array[10, byte]
? readFileExact(f, buf.toOpenArray(0, 7))

var
typ: Type
discard typ.copyFrom(buf)

# Conversion safe because we had only 4 bytes of length data
let len = (uint32.fromBytesLE(buf.toOpenArray(2, 5))).int64

# No point reading these..
if len > int.high(): return err("header length exceeds int.high")

# Must have at least that much data, or header is invalid
? f.checkBytesLeft(len)

ok(Header(typ: typ, len: int(len)))

## Following types & procs are era1 specific

type
Era1* = distinct uint64 # Period of 8192 blocks (not an exact time unit)

Era1Group* = object
Expand All @@ -135,9 +69,14 @@ type
# As stated, not really a time unit but nevertheless, need the borrows
ethTimeUnit Era1

# Note: appendIndex, appendRecord and readIndex for BlockIndex are only
# different from its consensus layer counter parts because of usage of slot vs
# blockNumber. In practise, they do the same thing.
template lenu64(x: untyped): untyped =
uint64(len(x))

# Note: appendIndex, appendRecord and readIndex for BlockIndex are very similar
# to its consensus layer counter parts. The difference lies in the naming of
# slots vs block numbers and there is different behavior for the first era
# (first slot) and the last era (era1 ends at merge block).

proc appendIndex*(
f: IoHandle, startNumber: uint64, offsets: openArray[int64]):
Result[int64, string] =
Expand Down
6 changes: 1 addition & 5 deletions fluffy/eth_data/history_data_ssz_e2s.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Nimbus - Portal Network
# Copyright (c) 2022-2023 Status Research & Development GmbH
# Copyright (c) 2022-2024 Status Research & Development GmbH
# Licensed and distributed under either of
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
Expand All @@ -15,10 +15,6 @@ import

export results

proc toString*(v: IoErrorCode): string =
try: ioErrorMsg(v)
except Exception as e: raiseAssert e.msg

# Reading SSZ data from files

proc readAccumulator*(file: string): Result[FinishedAccumulator, string] =
Expand Down
2 changes: 1 addition & 1 deletion vendor/nimbus-eth2

0 comments on commit f78f20c

Please sign in to comment.