Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

era: speed up index reading #2202

Merged
merged 1 commit into from
May 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 18 additions & 17 deletions fluffy/eth_data/era1.nim
Original file line number Diff line number Diff line change
Expand Up @@ -99,30 +99,35 @@ proc appendRecord(f: IoHandle, index: BlockIndex): Result[int64, string] =
f.appendIndex(index.startNumber, index.offsets)

proc readBlockIndex*(f: IoHandle): Result[BlockIndex, string] =
var
buf: seq[byte]
pos: int

let
startPos = ?f.getFilePos().mapErr(toString)
fileSize = ?f.getFileSize().mapErr(toString)
header = ?f.readHeader()
header = ?f.readRecord(buf)

if header.typ != E2BlockIndex:
return err("not an index")
if header.len < 16:
if buf.len < 16:
return err("index entry too small")
if header.len mod 8 != 0:
if buf.len mod 8 != 0:
return err("index length invalid")

var buf: array[8, byte]
?f.readFileExact(buf)
let
blockNumber = uint64.fromBytesLE(buf)
count = header.len div 8 - 2
blockNumber = uint64.fromBytesLE(buf.toOpenArray(pos, pos + 7))
count = buf.len div 8 - 2
pos += 8

# technically not an error, but we'll throw this sanity check in here..
if blockNumber > int32.high().uint64:
return err("fishy block number")

var offsets = newSeqUninitialized[int64](count)
for i in 0 ..< count:
?f.readFileExact(buf)

let
offset = uint64.fromBytesLE(buf)
offset = uint64.fromBytesLE(buf.toOpenArray(pos, pos + 7))
absolute =
if offset == 0:
0'i64
Expand All @@ -131,17 +136,13 @@ proc readBlockIndex*(f: IoHandle): Result[BlockIndex, string] =
cast[int64](cast[uint64](startPos) + offset)

if absolute < 0 or absolute > fileSize:
return err("Invalid offset")
return err("invalid offset")
offsets[i] = absolute
pos += 8

?f.readFileExact(buf)
if uint64(count) != uint64.fromBytesLE(buf):
if uint64(count) != uint64.fromBytesLE(buf.toOpenArray(pos, pos + 7)):
return err("invalid count")

# technically not an error, but we'll throw this sanity check in here..
if blockNumber > int32.high().uint64:
return err("fishy block number")

ok(BlockIndex(startNumber: blockNumber, offsets: offsets))

proc skipRecord*(f: IoHandle): Result[void, string] =
Expand Down
Loading