2024-02-09 10:13:12 +00:00
|
|
|
# fluffy
|
|
|
|
# Copyright (c) 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).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import
|
|
|
|
std/[strformat, typetraits],
|
2024-02-28 17:31:45 +00:00
|
|
|
results,
|
|
|
|
stew/[endians2, io2, byteutils, arrayops],
|
|
|
|
stint,
|
|
|
|
snappy,
|
2024-11-20 13:07:36 +00:00
|
|
|
eth/common/[headers_rlp, blocks_rlp, receipts_rlp],
|
2024-02-09 10:13:12 +00:00
|
|
|
beacon_chain/spec/beacon_time,
|
|
|
|
ssz_serialization,
|
|
|
|
ncli/e2store,
|
2024-09-23 16:56:28 +00:00
|
|
|
../network/history/validation/historical_hashes_accumulator
|
2024-02-09 10:13:12 +00:00
|
|
|
|
2024-10-04 21:21:26 +00:00
|
|
|
from eth/common/eth_types_rlp import rlpHash
|
2024-02-09 10:13:12 +00:00
|
|
|
from nimcrypto/hash import fromHex
|
2024-06-14 07:31:08 +00:00
|
|
|
from ../../nimbus/utils/utils import calcTxRoot, calcReceiptsRoot
|
2024-02-09 10:13:12 +00:00
|
|
|
|
|
|
|
export e2store.readRecord
|
|
|
|
|
|
|
|
# Implementation of era1 file format as current described in:
|
|
|
|
# https://github.com/ethereum/go-ethereum/pull/26621
|
|
|
|
|
|
|
|
# era1 := Version | block-tuple* | other-entries* | Accumulator | BlockIndex
|
|
|
|
# block-tuple := CompressedHeader | CompressedBody | CompressedReceipts | TotalDifficulty
|
|
|
|
|
|
|
|
# block-index := starting-number | index | index | index ... | count
|
|
|
|
|
|
|
|
# CompressedHeader = { type: 0x03, data: snappyFramed(rlp(header)) }
|
|
|
|
# CompressedBody = { type: 0x04, data: snappyFramed(rlp(body)) }
|
|
|
|
# CompressedReceipts = { type: 0x05, data: snappyFramed(rlp(receipts)) }
|
|
|
|
# TotalDifficulty = { type: 0x06, data: uint256(header.total_difficulty) }
|
|
|
|
# AccumulatorRoot = { type: 0x07, data: hash_tree_root(List(HeaderRecord, 8192)) }
|
|
|
|
# BlockIndex = { type: 0x3266, data: block-index }
|
|
|
|
|
|
|
|
# Important note:
|
|
|
|
# Snappy does not give the same compression result as the implementation used
|
|
|
|
# by go-ethereum for some block headers and block bodies. This means that we
|
|
|
|
# cannot rely on the secondary verification mechanism that is based on doing the
|
|
|
|
# sha256sum of the full era1 files.
|
|
|
|
#
|
|
|
|
|
|
|
|
const
|
|
|
|
# Note: When specification is more official, these could go with the other
|
|
|
|
# E2S types.
|
2024-02-28 17:31:45 +00:00
|
|
|
CompressedHeader* = [byte 0x03, 0x00]
|
|
|
|
CompressedBody* = [byte 0x04, 0x00]
|
2024-02-09 10:13:12 +00:00
|
|
|
CompressedReceipts* = [byte 0x05, 0x00]
|
2024-02-28 17:31:45 +00:00
|
|
|
TotalDifficulty* = [byte 0x06, 0x00]
|
|
|
|
AccumulatorRoot* = [byte 0x07, 0x00]
|
|
|
|
E2BlockIndex* = [byte 0x66, 0x32]
|
2024-02-09 10:13:12 +00:00
|
|
|
|
|
|
|
MaxEra1Size* = 8192
|
|
|
|
|
|
|
|
type
|
|
|
|
BlockIndex* = object
|
|
|
|
startNumber*: uint64
|
|
|
|
offsets*: seq[int64] # Absolute positions in file
|
|
|
|
|
|
|
|
Era1* = distinct uint64 # Period of 8192 blocks (not an exact time unit)
|
|
|
|
|
|
|
|
Era1Group* = object
|
|
|
|
blockIndex*: BlockIndex
|
|
|
|
|
|
|
|
# As stated, not really a time unit but nevertheless, need the borrows
|
|
|
|
ethTimeUnit Era1
|
|
|
|
|
|
|
|
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*(
|
2024-02-28 17:31:45 +00:00
|
|
|
f: IoHandle, startNumber: uint64, offsets: openArray[int64]
|
|
|
|
): Result[int64, string] =
|
2024-02-09 10:13:12 +00:00
|
|
|
let
|
|
|
|
len = offsets.len() * sizeof(int64) + 16
|
2024-02-28 17:31:45 +00:00
|
|
|
pos = ?f.appendHeader(E2BlockIndex, len)
|
2024-02-09 10:13:12 +00:00
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
?f.append(startNumber.uint64.toBytesLE())
|
2024-02-09 10:13:12 +00:00
|
|
|
|
|
|
|
for v in offsets:
|
2024-02-28 17:31:45 +00:00
|
|
|
?f.append(cast[uint64](v - pos).toBytesLE())
|
2024-02-09 10:13:12 +00:00
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
?f.append(offsets.lenu64().toBytesLE())
|
2024-02-09 10:13:12 +00:00
|
|
|
|
|
|
|
ok(pos)
|
|
|
|
|
|
|
|
proc appendRecord(f: IoHandle, index: BlockIndex): Result[int64, string] =
|
|
|
|
f.appendIndex(index.startNumber, index.offsets)
|
|
|
|
|
|
|
|
proc readBlockIndex*(f: IoHandle): Result[BlockIndex, string] =
|
2024-05-22 07:27:23 +00:00
|
|
|
var
|
|
|
|
buf: seq[byte]
|
|
|
|
pos: int
|
|
|
|
|
2024-02-09 10:13:12 +00:00
|
|
|
let
|
2024-02-28 17:31:45 +00:00
|
|
|
startPos = ?f.getFilePos().mapErr(toString)
|
|
|
|
fileSize = ?f.getFileSize().mapErr(toString)
|
2024-05-22 07:27:23 +00:00
|
|
|
header = ?f.readRecord(buf)
|
2024-02-09 10:13:12 +00:00
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
if header.typ != E2BlockIndex:
|
|
|
|
return err("not an index")
|
2024-05-22 07:27:23 +00:00
|
|
|
if buf.len < 16:
|
2024-02-28 17:31:45 +00:00
|
|
|
return err("index entry too small")
|
2024-05-22 07:27:23 +00:00
|
|
|
if buf.len mod 8 != 0:
|
2024-02-28 17:31:45 +00:00
|
|
|
return err("index length invalid")
|
2024-02-09 10:13:12 +00:00
|
|
|
|
|
|
|
let
|
2024-05-22 07:27:23 +00:00
|
|
|
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")
|
2024-02-09 10:13:12 +00:00
|
|
|
|
|
|
|
var offsets = newSeqUninitialized[int64](count)
|
2024-02-28 17:31:45 +00:00
|
|
|
for i in 0 ..< count:
|
2024-02-09 10:13:12 +00:00
|
|
|
let
|
2024-05-22 07:27:23 +00:00
|
|
|
offset = uint64.fromBytesLE(buf.toOpenArray(pos, pos + 7))
|
2024-02-09 10:13:12 +00:00
|
|
|
absolute =
|
2024-02-28 17:31:45 +00:00
|
|
|
if offset == 0:
|
|
|
|
0'i64
|
2024-02-09 10:13:12 +00:00
|
|
|
else:
|
|
|
|
# Wrapping math is actually convenient here
|
|
|
|
cast[int64](cast[uint64](startPos) + offset)
|
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
if absolute < 0 or absolute > fileSize:
|
2024-05-22 07:27:23 +00:00
|
|
|
return err("invalid offset")
|
2024-02-09 10:13:12 +00:00
|
|
|
offsets[i] = absolute
|
2024-05-22 07:27:23 +00:00
|
|
|
pos += 8
|
2024-02-09 10:13:12 +00:00
|
|
|
|
2024-05-22 07:27:23 +00:00
|
|
|
if uint64(count) != uint64.fromBytesLE(buf.toOpenArray(pos, pos + 7)):
|
2024-02-28 17:31:45 +00:00
|
|
|
return err("invalid count")
|
2024-02-09 10:13:12 +00:00
|
|
|
|
|
|
|
ok(BlockIndex(startNumber: blockNumber, offsets: offsets))
|
|
|
|
|
2024-02-15 15:49:22 +00:00
|
|
|
proc skipRecord*(f: IoHandle): Result[void, string] =
|
2024-02-28 17:31:45 +00:00
|
|
|
let header = ?readHeader(f)
|
2024-02-15 15:49:22 +00:00
|
|
|
if header.len > 0:
|
2024-02-28 17:31:45 +00:00
|
|
|
?f.setFilePos(header.len, SeekPosition.SeekCurrent).mapErr(ioErrorMsg)
|
2024-02-15 15:49:22 +00:00
|
|
|
|
|
|
|
ok()
|
|
|
|
|
2024-02-09 10:13:12 +00:00
|
|
|
func startNumber*(era: Era1): uint64 =
|
|
|
|
era * MaxEra1Size
|
|
|
|
|
|
|
|
func endNumber*(era: Era1): uint64 =
|
|
|
|
if (era + 1) * MaxEra1Size - 1'u64 >= mergeBlockNumber:
|
|
|
|
# The incomplete era just before the merge
|
|
|
|
mergeBlockNumber - 1'u64
|
|
|
|
else:
|
|
|
|
(era + 1) * MaxEra1Size - 1'u64
|
|
|
|
|
|
|
|
func endNumber*(blockIdx: BlockIndex): uint64 =
|
|
|
|
blockIdx.startNumber + blockIdx.offsets.lenu64() - 1
|
|
|
|
|
|
|
|
func era*(blockNumber: uint64): Era1 =
|
|
|
|
Era1(blockNumber div MaxEra1Size)
|
|
|
|
|
|
|
|
func offsetsLen(startNumber: uint64): int =
|
|
|
|
# For the era where the merge happens the era files only holds the blocks
|
|
|
|
# until the merge block so the offsets length needs to be adapted too.
|
|
|
|
if startNumber.era() >= mergeBlockNumber.era():
|
|
|
|
int((mergeBlockNumber) mod MaxEra1Size)
|
|
|
|
else:
|
|
|
|
MaxEra1Size
|
|
|
|
|
|
|
|
proc toCompressedRlpBytes(item: auto): seq[byte] =
|
|
|
|
snappy.encodeFramed(rlp.encode(item))
|
|
|
|
|
2024-12-18 12:21:20 +00:00
|
|
|
proc fromCompressedRlpBytes[T](bytes: openArray[byte], v: var T): Result[void, string] =
|
2024-02-09 10:13:12 +00:00
|
|
|
try:
|
2024-12-18 12:21:20 +00:00
|
|
|
v = rlp.decode(decodeFramed(bytes, checkIntegrity = false), T)
|
|
|
|
ok()
|
2024-02-09 10:13:12 +00:00
|
|
|
except RlpError as e:
|
2024-11-20 13:07:36 +00:00
|
|
|
err("Invalid compressed RLP data for " & $T & ": " & e.msg)
|
2024-02-09 10:13:12 +00:00
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
proc init*(T: type Era1Group, f: IoHandle, startNumber: uint64): Result[T, string] =
|
|
|
|
discard ?f.appendHeader(E2Version, 0)
|
2024-02-09 10:13:12 +00:00
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
ok(
|
|
|
|
Era1Group(
|
|
|
|
blockIndex: BlockIndex(
|
|
|
|
startNumber: startNumber, offsets: newSeq[int64](startNumber.offsetsLen())
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
2024-02-09 10:13:12 +00:00
|
|
|
|
|
|
|
proc update*(
|
2024-02-28 17:31:45 +00:00
|
|
|
g: var Era1Group,
|
|
|
|
f: IoHandle,
|
|
|
|
blockNumber: uint64,
|
|
|
|
header, body, receipts, totalDifficulty: openArray[byte],
|
|
|
|
): Result[void, string] =
|
2024-02-09 10:13:12 +00:00
|
|
|
doAssert blockNumber >= g.blockIndex.startNumber
|
|
|
|
|
|
|
|
g.blockIndex.offsets[int(blockNumber - g.blockIndex.startNumber)] =
|
2024-02-28 17:31:45 +00:00
|
|
|
?f.appendRecord(CompressedHeader, header)
|
|
|
|
discard ?f.appendRecord(CompressedBody, body)
|
|
|
|
discard ?f.appendRecord(CompressedReceipts, receipts)
|
|
|
|
discard ?f.appendRecord(TotalDifficulty, totalDifficulty)
|
2024-02-09 10:13:12 +00:00
|
|
|
|
|
|
|
ok()
|
|
|
|
|
|
|
|
proc update*(
|
2024-02-28 17:31:45 +00:00
|
|
|
g: var Era1Group,
|
|
|
|
f: IoHandle,
|
|
|
|
blockNumber: uint64,
|
2024-10-04 21:21:26 +00:00
|
|
|
header: headers.Header,
|
2024-02-28 17:31:45 +00:00
|
|
|
body: BlockBody,
|
|
|
|
receipts: seq[Receipt],
|
|
|
|
totalDifficulty: UInt256,
|
|
|
|
): Result[void, string] =
|
2024-02-09 10:13:12 +00:00
|
|
|
g.update(
|
2024-02-28 17:31:45 +00:00
|
|
|
f,
|
|
|
|
blockNumber,
|
2024-02-09 10:13:12 +00:00
|
|
|
toCompressedRlpBytes(header),
|
|
|
|
toCompressedRlpBytes(body),
|
|
|
|
toCompressedRlpBytes(receipts),
|
2024-02-28 17:31:45 +00:00
|
|
|
totalDifficulty.toBytesLE(),
|
2024-02-09 10:13:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
proc finish*(
|
|
|
|
g: var Era1Group, f: IoHandle, accumulatorRoot: Digest, lastBlockNumber: uint64
|
2024-02-28 17:31:45 +00:00
|
|
|
): Result[void, string] =
|
|
|
|
let accumulatorRootPos = ?f.appendRecord(AccumulatorRoot, accumulatorRoot.data)
|
2024-02-09 10:13:12 +00:00
|
|
|
|
|
|
|
if lastBlockNumber > 0:
|
2024-02-28 17:31:45 +00:00
|
|
|
discard ?f.appendRecord(g.blockIndex)
|
2024-02-09 10:13:12 +00:00
|
|
|
|
|
|
|
# TODO:
|
|
|
|
# This is not something added in current specification of era1.
|
|
|
|
# But perhaps we want to be able to quickly jump to acummulator root.
|
|
|
|
# discard ? f.appendIndex(lastBlockNumber, [accumulatorRootPos])
|
|
|
|
discard accumulatorRootPos
|
|
|
|
|
|
|
|
ok()
|
|
|
|
|
|
|
|
func shortLog*(x: Digest): string =
|
|
|
|
x.data.toOpenArray(0, 3).toHex()
|
|
|
|
|
|
|
|
func era1FileName*(network: string, era: Era1, eraRoot: Digest): string =
|
|
|
|
try:
|
|
|
|
&"{network}-{era.uint64:05}-{shortLog(eraRoot)}.era1"
|
|
|
|
except ValueError as exc:
|
|
|
|
raiseAssert exc.msg
|
|
|
|
|
|
|
|
# Helpers to directly read objects from era1 files
|
|
|
|
# TODO: Might want to var parameters to avoid copying as is done for era files.
|
|
|
|
|
|
|
|
type
|
|
|
|
Era1File* = ref object
|
|
|
|
handle: Opt[IoHandle]
|
2024-03-26 21:27:31 +00:00
|
|
|
blockIdx*: BlockIndex
|
2024-02-09 10:13:12 +00:00
|
|
|
|
2024-02-15 15:49:22 +00:00
|
|
|
BlockTuple* =
|
2024-10-04 21:21:26 +00:00
|
|
|
tuple[header: headers.Header, body: BlockBody, receipts: seq[Receipt], td: UInt256]
|
2024-02-15 15:49:22 +00:00
|
|
|
|
2024-02-09 10:13:12 +00:00
|
|
|
proc open*(_: type Era1File, name: string): Result[Era1File, string] =
|
2024-02-28 17:31:45 +00:00
|
|
|
var f = Opt[IoHandle].ok(?openFile(name, {OpenFlags.Read}).mapErr(ioErrorMsg))
|
2024-02-09 10:13:12 +00:00
|
|
|
|
|
|
|
defer:
|
2024-02-28 17:31:45 +00:00
|
|
|
if f.isSome():
|
|
|
|
discard closeFile(f[])
|
2024-02-09 10:13:12 +00:00
|
|
|
|
|
|
|
# Indices can be found at the end of each era file - we only support
|
|
|
|
# single-era files for now
|
2024-02-28 17:31:45 +00:00
|
|
|
?f[].setFilePos(0, SeekPosition.SeekEnd).mapErr(ioErrorMsg)
|
2024-02-09 10:13:12 +00:00
|
|
|
|
|
|
|
# Last in the file is the block index
|
2024-02-28 17:31:45 +00:00
|
|
|
let blockIdxPos = ?f[].findIndexStartOffset()
|
|
|
|
?f[].setFilePos(blockIdxPos, SeekPosition.SeekCurrent).mapErr(ioErrorMsg)
|
2024-02-09 10:13:12 +00:00
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
let blockIdx = ?f[].readBlockIndex()
|
2024-02-09 10:13:12 +00:00
|
|
|
if blockIdx.offsets.len() != blockIdx.startNumber.offsetsLen():
|
|
|
|
return err("Block index length invalid")
|
|
|
|
|
|
|
|
let res = Era1File(handle: f, blockIdx: blockIdx)
|
|
|
|
reset(f)
|
|
|
|
ok res
|
|
|
|
|
|
|
|
proc close*(f: Era1File) =
|
|
|
|
if f.handle.isSome():
|
|
|
|
discard closeFile(f.handle.get())
|
|
|
|
reset(f.handle)
|
|
|
|
|
2024-02-15 15:49:22 +00:00
|
|
|
proc skipRecord*(f: Era1File): Result[void, string] =
|
|
|
|
doAssert f[].handle.isSome()
|
|
|
|
|
|
|
|
f[].handle.get().skipRecord()
|
|
|
|
|
2024-12-18 12:21:20 +00:00
|
|
|
proc getBlockHeader(f: Era1File, res: var headers.Header): Result[void, string] =
|
2024-02-09 10:13:12 +00:00
|
|
|
var bytes: seq[byte]
|
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
let header = ?f[].handle.get().readRecord(bytes)
|
2024-02-09 10:13:12 +00:00
|
|
|
if header.typ != CompressedHeader:
|
|
|
|
return err("Invalid era file: didn't find block header at index position")
|
|
|
|
|
2024-12-18 12:21:20 +00:00
|
|
|
fromCompressedRlpBytes(bytes, res)
|
2024-02-09 10:13:12 +00:00
|
|
|
|
2024-12-18 12:21:20 +00:00
|
|
|
proc getBlockBody(f: Era1File, res: var BlockBody): Result[void, string] =
|
2024-02-09 10:13:12 +00:00
|
|
|
var bytes: seq[byte]
|
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
let header = ?f[].handle.get().readRecord(bytes)
|
2024-02-09 10:13:12 +00:00
|
|
|
if header.typ != CompressedBody:
|
|
|
|
return err("Invalid era file: didn't find block body at index position")
|
|
|
|
|
2024-12-18 12:21:20 +00:00
|
|
|
fromCompressedRlpBytes(bytes, res)
|
2024-02-09 10:13:12 +00:00
|
|
|
|
2024-12-18 12:21:20 +00:00
|
|
|
proc getReceipts(f: Era1File, res: var seq[Receipt]): Result[void, string] =
|
2024-02-09 10:13:12 +00:00
|
|
|
var bytes: seq[byte]
|
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
let header = ?f[].handle.get().readRecord(bytes)
|
2024-02-09 10:13:12 +00:00
|
|
|
if header.typ != CompressedReceipts:
|
|
|
|
return err("Invalid era file: didn't find receipts at index position")
|
|
|
|
|
2024-12-18 12:21:20 +00:00
|
|
|
fromCompressedRlpBytes(bytes, res)
|
2024-02-09 10:13:12 +00:00
|
|
|
|
|
|
|
proc getTotalDifficulty(f: Era1File): Result[UInt256, string] =
|
|
|
|
var bytes: seq[byte]
|
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
let header = ?f[].handle.get().readRecord(bytes)
|
2024-02-09 10:13:12 +00:00
|
|
|
if header.typ != TotalDifficulty:
|
|
|
|
return err("Invalid era file: didn't find total difficulty at index position")
|
|
|
|
|
|
|
|
if bytes.len != 32:
|
|
|
|
return err("Invalid total difficulty length")
|
|
|
|
|
|
|
|
ok(UInt256.fromBytesLE(bytes))
|
|
|
|
|
2024-12-18 12:21:20 +00:00
|
|
|
proc getNextEthBlock*(f: Era1File, res: var Block): Result[void, string] =
|
Consolidate block type for block processing (#2325)
This PR consolidates the split header-body sequences into a single EthBlock
sequence and cleans up the fallout from that which significantly reduces
block processing overhead during import thanks to less garbage collection
and fewer copies of things all around.
Notably, since the number of headers must always match the number of bodies,
we also get rid of a pointless degree of freedom that in the future could
introduce unnecessary bugs.
* only read header and body from era file
* avoid several unnecessary copies along the block processing way
* simplify signatures, cleaning up unused arguemnts and returns
* use `stew/assign2` in a few strategic places where the generated
nim assignent is slow and add a few `move` to work around poor
analysis in nim 1.6 (will need to be revisited for 2.0)
```
stats-20240607_2223-a814aa0b.csv vs stats-20240608_0714-21c1d0a9.csv
bps_x bps_y tps_x tps_y bpsd tpsd timed
block_number
(498305, 713245] 1,540.52 1,809.73 2,361.58 2775.340189 17.63% 17.63% -14.92%
(713245, 928185] 730.36 865.26 1,715.90 2028.973852 18.01% 18.01% -15.21%
(928185, 1143126] 663.03 789.10 2,529.26 3032.490771 19.79% 19.79% -16.28%
(1143126, 1358066] 393.46 508.05 2,152.50 2777.578119 29.13% 29.13% -22.50%
(1358066, 1573007] 370.88 440.72 2,351.31 2791.896052 18.81% 18.81% -15.80%
(1573007, 1787947] 283.65 335.11 2,068.93 2441.373402 17.60% 17.60% -14.91%
(1787947, 2002888] 287.29 342.11 2,078.39 2474.179448 18.99% 18.99% -15.91%
(2002888, 2217828] 293.38 343.16 2,208.83 2584.77457 17.16% 17.16% -14.61%
(2217828, 2432769] 140.09 167.86 1,081.87 1296.336926 18.82% 18.82% -15.80%
blocks: 1934464, baseline: 3h13m1s, contender: 2h43m47s
bpsd (mean): 19.55%
tpsd (mean): 19.55%
Time (total): -29m13s, -15.14%
```
2024-06-09 14:32:20 +00:00
|
|
|
doAssert not isNil(f) and f[].handle.isSome
|
|
|
|
|
2024-12-18 12:21:20 +00:00
|
|
|
var body: BlockBody
|
|
|
|
?getBlockHeader(f, res.header)
|
|
|
|
?getBlockBody(f, body)
|
|
|
|
|
Consolidate block type for block processing (#2325)
This PR consolidates the split header-body sequences into a single EthBlock
sequence and cleans up the fallout from that which significantly reduces
block processing overhead during import thanks to less garbage collection
and fewer copies of things all around.
Notably, since the number of headers must always match the number of bodies,
we also get rid of a pointless degree of freedom that in the future could
introduce unnecessary bugs.
* only read header and body from era file
* avoid several unnecessary copies along the block processing way
* simplify signatures, cleaning up unused arguemnts and returns
* use `stew/assign2` in a few strategic places where the generated
nim assignent is slow and add a few `move` to work around poor
analysis in nim 1.6 (will need to be revisited for 2.0)
```
stats-20240607_2223-a814aa0b.csv vs stats-20240608_0714-21c1d0a9.csv
bps_x bps_y tps_x tps_y bpsd tpsd timed
block_number
(498305, 713245] 1,540.52 1,809.73 2,361.58 2775.340189 17.63% 17.63% -14.92%
(713245, 928185] 730.36 865.26 1,715.90 2028.973852 18.01% 18.01% -15.21%
(928185, 1143126] 663.03 789.10 2,529.26 3032.490771 19.79% 19.79% -16.28%
(1143126, 1358066] 393.46 508.05 2,152.50 2777.578119 29.13% 29.13% -22.50%
(1358066, 1573007] 370.88 440.72 2,351.31 2791.896052 18.81% 18.81% -15.80%
(1573007, 1787947] 283.65 335.11 2,068.93 2441.373402 17.60% 17.60% -14.91%
(1787947, 2002888] 287.29 342.11 2,078.39 2474.179448 18.99% 18.99% -15.91%
(2002888, 2217828] 293.38 343.16 2,208.83 2584.77457 17.16% 17.16% -14.61%
(2217828, 2432769] 140.09 167.86 1,081.87 1296.336926 18.82% 18.82% -15.80%
blocks: 1934464, baseline: 3h13m1s, contender: 2h43m47s
bpsd (mean): 19.55%
tpsd (mean): 19.55%
Time (total): -29m13s, -15.14%
```
2024-06-09 14:32:20 +00:00
|
|
|
?skipRecord(f) # receipts
|
|
|
|
?skipRecord(f) # totalDifficulty
|
|
|
|
|
2024-12-18 12:21:20 +00:00
|
|
|
res.transactions = move(body.transactions)
|
|
|
|
res.uncles = move(body.uncles)
|
|
|
|
res.withdrawals = move(body.withdrawals)
|
|
|
|
|
|
|
|
ok()
|
Consolidate block type for block processing (#2325)
This PR consolidates the split header-body sequences into a single EthBlock
sequence and cleans up the fallout from that which significantly reduces
block processing overhead during import thanks to less garbage collection
and fewer copies of things all around.
Notably, since the number of headers must always match the number of bodies,
we also get rid of a pointless degree of freedom that in the future could
introduce unnecessary bugs.
* only read header and body from era file
* avoid several unnecessary copies along the block processing way
* simplify signatures, cleaning up unused arguemnts and returns
* use `stew/assign2` in a few strategic places where the generated
nim assignent is slow and add a few `move` to work around poor
analysis in nim 1.6 (will need to be revisited for 2.0)
```
stats-20240607_2223-a814aa0b.csv vs stats-20240608_0714-21c1d0a9.csv
bps_x bps_y tps_x tps_y bpsd tpsd timed
block_number
(498305, 713245] 1,540.52 1,809.73 2,361.58 2775.340189 17.63% 17.63% -14.92%
(713245, 928185] 730.36 865.26 1,715.90 2028.973852 18.01% 18.01% -15.21%
(928185, 1143126] 663.03 789.10 2,529.26 3032.490771 19.79% 19.79% -16.28%
(1143126, 1358066] 393.46 508.05 2,152.50 2777.578119 29.13% 29.13% -22.50%
(1358066, 1573007] 370.88 440.72 2,351.31 2791.896052 18.81% 18.81% -15.80%
(1573007, 1787947] 283.65 335.11 2,068.93 2441.373402 17.60% 17.60% -14.91%
(1787947, 2002888] 287.29 342.11 2,078.39 2474.179448 18.99% 18.99% -15.91%
(2002888, 2217828] 293.38 343.16 2,208.83 2584.77457 17.16% 17.16% -14.61%
(2217828, 2432769] 140.09 167.86 1,081.87 1296.336926 18.82% 18.82% -15.80%
blocks: 1934464, baseline: 3h13m1s, contender: 2h43m47s
bpsd (mean): 19.55%
tpsd (mean): 19.55%
Time (total): -29m13s, -15.14%
```
2024-06-09 14:32:20 +00:00
|
|
|
|
2024-12-18 12:21:20 +00:00
|
|
|
proc getEthBlock*(
|
|
|
|
f: Era1File, blockNumber: uint64, res: var Block
|
|
|
|
): Result[void, string] =
|
Consolidate block type for block processing (#2325)
This PR consolidates the split header-body sequences into a single EthBlock
sequence and cleans up the fallout from that which significantly reduces
block processing overhead during import thanks to less garbage collection
and fewer copies of things all around.
Notably, since the number of headers must always match the number of bodies,
we also get rid of a pointless degree of freedom that in the future could
introduce unnecessary bugs.
* only read header and body from era file
* avoid several unnecessary copies along the block processing way
* simplify signatures, cleaning up unused arguemnts and returns
* use `stew/assign2` in a few strategic places where the generated
nim assignent is slow and add a few `move` to work around poor
analysis in nim 1.6 (will need to be revisited for 2.0)
```
stats-20240607_2223-a814aa0b.csv vs stats-20240608_0714-21c1d0a9.csv
bps_x bps_y tps_x tps_y bpsd tpsd timed
block_number
(498305, 713245] 1,540.52 1,809.73 2,361.58 2775.340189 17.63% 17.63% -14.92%
(713245, 928185] 730.36 865.26 1,715.90 2028.973852 18.01% 18.01% -15.21%
(928185, 1143126] 663.03 789.10 2,529.26 3032.490771 19.79% 19.79% -16.28%
(1143126, 1358066] 393.46 508.05 2,152.50 2777.578119 29.13% 29.13% -22.50%
(1358066, 1573007] 370.88 440.72 2,351.31 2791.896052 18.81% 18.81% -15.80%
(1573007, 1787947] 283.65 335.11 2,068.93 2441.373402 17.60% 17.60% -14.91%
(1787947, 2002888] 287.29 342.11 2,078.39 2474.179448 18.99% 18.99% -15.91%
(2002888, 2217828] 293.38 343.16 2,208.83 2584.77457 17.16% 17.16% -14.61%
(2217828, 2432769] 140.09 167.86 1,081.87 1296.336926 18.82% 18.82% -15.80%
blocks: 1934464, baseline: 3h13m1s, contender: 2h43m47s
bpsd (mean): 19.55%
tpsd (mean): 19.55%
Time (total): -29m13s, -15.14%
```
2024-06-09 14:32:20 +00:00
|
|
|
doAssert not isNil(f) and f[].handle.isSome
|
|
|
|
doAssert(
|
|
|
|
blockNumber >= f[].blockIdx.startNumber and blockNumber <= f[].blockIdx.endNumber,
|
|
|
|
"Wrong era1 file for selected block number",
|
|
|
|
)
|
|
|
|
|
|
|
|
let pos = f[].blockIdx.offsets[blockNumber - f[].blockIdx.startNumber]
|
|
|
|
|
|
|
|
?f[].handle.get().setFilePos(pos, SeekPosition.SeekBegin).mapErr(ioErrorMsg)
|
|
|
|
|
2024-12-18 12:21:20 +00:00
|
|
|
getNextEthBlock(f, res)
|
Consolidate block type for block processing (#2325)
This PR consolidates the split header-body sequences into a single EthBlock
sequence and cleans up the fallout from that which significantly reduces
block processing overhead during import thanks to less garbage collection
and fewer copies of things all around.
Notably, since the number of headers must always match the number of bodies,
we also get rid of a pointless degree of freedom that in the future could
introduce unnecessary bugs.
* only read header and body from era file
* avoid several unnecessary copies along the block processing way
* simplify signatures, cleaning up unused arguemnts and returns
* use `stew/assign2` in a few strategic places where the generated
nim assignent is slow and add a few `move` to work around poor
analysis in nim 1.6 (will need to be revisited for 2.0)
```
stats-20240607_2223-a814aa0b.csv vs stats-20240608_0714-21c1d0a9.csv
bps_x bps_y tps_x tps_y bpsd tpsd timed
block_number
(498305, 713245] 1,540.52 1,809.73 2,361.58 2775.340189 17.63% 17.63% -14.92%
(713245, 928185] 730.36 865.26 1,715.90 2028.973852 18.01% 18.01% -15.21%
(928185, 1143126] 663.03 789.10 2,529.26 3032.490771 19.79% 19.79% -16.28%
(1143126, 1358066] 393.46 508.05 2,152.50 2777.578119 29.13% 29.13% -22.50%
(1358066, 1573007] 370.88 440.72 2,351.31 2791.896052 18.81% 18.81% -15.80%
(1573007, 1787947] 283.65 335.11 2,068.93 2441.373402 17.60% 17.60% -14.91%
(1787947, 2002888] 287.29 342.11 2,078.39 2474.179448 18.99% 18.99% -15.91%
(2002888, 2217828] 293.38 343.16 2,208.83 2584.77457 17.16% 17.16% -14.61%
(2217828, 2432769] 140.09 167.86 1,081.87 1296.336926 18.82% 18.82% -15.80%
blocks: 1934464, baseline: 3h13m1s, contender: 2h43m47s
bpsd (mean): 19.55%
tpsd (mean): 19.55%
Time (total): -29m13s, -15.14%
```
2024-06-09 14:32:20 +00:00
|
|
|
|
2024-12-18 12:21:20 +00:00
|
|
|
proc getNextBlockTuple*(f: Era1File, res: var BlockTuple): Result[void, string] =
|
2024-02-09 10:13:12 +00:00
|
|
|
doAssert not isNil(f) and f[].handle.isSome
|
|
|
|
|
2024-12-18 12:21:20 +00:00
|
|
|
?getBlockHeader(f, res.header)
|
|
|
|
?getBlockBody(f, res.body)
|
|
|
|
?getReceipts(f, res.receipts)
|
|
|
|
res.td = ?getTotalDifficulty(f)
|
2024-02-09 10:13:12 +00:00
|
|
|
|
2024-12-18 12:21:20 +00:00
|
|
|
ok()
|
2024-02-09 10:13:12 +00:00
|
|
|
|
2024-12-18 12:21:20 +00:00
|
|
|
proc getBlockTuple*(
|
|
|
|
f: Era1File, blockNumber: uint64, res: var BlockTuple
|
|
|
|
): Result[void, string] =
|
2024-02-09 10:13:12 +00:00
|
|
|
doAssert not isNil(f) and f[].handle.isSome
|
|
|
|
doAssert(
|
2024-02-28 17:31:45 +00:00
|
|
|
blockNumber >= f[].blockIdx.startNumber and blockNumber <= f[].blockIdx.endNumber,
|
|
|
|
"Wrong era1 file for selected block number",
|
|
|
|
)
|
2024-02-09 10:13:12 +00:00
|
|
|
|
|
|
|
let pos = f[].blockIdx.offsets[blockNumber - f[].blockIdx.startNumber]
|
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
?f[].handle.get().setFilePos(pos, SeekPosition.SeekBegin).mapErr(ioErrorMsg)
|
2024-02-09 10:13:12 +00:00
|
|
|
|
2024-12-18 12:21:20 +00:00
|
|
|
getNextBlockTuple(f, res)
|
2024-02-09 10:13:12 +00:00
|
|
|
|
2024-12-18 12:21:20 +00:00
|
|
|
proc getBlockHeader*(
|
|
|
|
f: Era1File, blockNumber: uint64, res: var headers.Header
|
|
|
|
): Result[void, string] =
|
2024-02-15 15:49:22 +00:00
|
|
|
doAssert not isNil(f) and f[].handle.isSome
|
|
|
|
doAssert(
|
2024-02-28 17:31:45 +00:00
|
|
|
blockNumber >= f[].blockIdx.startNumber and blockNumber <= f[].blockIdx.endNumber,
|
|
|
|
"Wrong era1 file for selected block number",
|
|
|
|
)
|
2024-02-15 15:49:22 +00:00
|
|
|
|
|
|
|
let pos = f[].blockIdx.offsets[blockNumber - f[].blockIdx.startNumber]
|
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
?f[].handle.get().setFilePos(pos, SeekPosition.SeekBegin).mapErr(ioErrorMsg)
|
2024-02-15 15:49:22 +00:00
|
|
|
|
2024-12-18 12:21:20 +00:00
|
|
|
getBlockHeader(f, res)
|
2024-02-15 15:49:22 +00:00
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
proc getTotalDifficulty*(f: Era1File, blockNumber: uint64): Result[UInt256, string] =
|
2024-02-15 15:49:22 +00:00
|
|
|
doAssert not isNil(f) and f[].handle.isSome
|
|
|
|
doAssert(
|
2024-02-28 17:31:45 +00:00
|
|
|
blockNumber >= f[].blockIdx.startNumber and blockNumber <= f[].blockIdx.endNumber,
|
|
|
|
"Wrong era1 file for selected block number",
|
|
|
|
)
|
2024-02-15 15:49:22 +00:00
|
|
|
|
|
|
|
let pos = f[].blockIdx.offsets[blockNumber - f[].blockIdx.startNumber]
|
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
?f[].handle.get().setFilePos(pos, SeekPosition.SeekBegin).mapErr(ioErrorMsg)
|
2024-02-15 15:49:22 +00:00
|
|
|
|
2024-10-04 21:21:26 +00:00
|
|
|
?skipRecord(f) # Header
|
2024-02-15 15:49:22 +00:00
|
|
|
?skipRecord(f) # BlockBody
|
|
|
|
?skipRecord(f) # Receipts
|
|
|
|
getTotalDifficulty(f)
|
|
|
|
|
2024-02-09 10:13:12 +00:00
|
|
|
# TODO: Should we add this perhaps in the Era1File object and grab it in open()?
|
|
|
|
proc getAccumulatorRoot*(f: Era1File): Result[Digest, string] =
|
|
|
|
# Get position of BlockIndex
|
2024-02-28 17:31:45 +00:00
|
|
|
?f[].handle.get().setFilePos(0, SeekPosition.SeekEnd).mapErr(ioErrorMsg)
|
|
|
|
let blockIdxPos = ?f[].handle.get().findIndexStartOffset()
|
2024-02-09 10:13:12 +00:00
|
|
|
|
|
|
|
# Accumulator root is 40 bytes before the BlockIndex
|
|
|
|
let accumulatorRootPos = blockIdxPos - 40 # 8 + 32
|
2024-02-28 17:31:45 +00:00
|
|
|
?f[].handle.get().setFilePos(accumulatorRootPos, SeekPosition.SeekCurrent).mapErr(
|
|
|
|
ioErrorMsg
|
|
|
|
)
|
2024-02-09 10:13:12 +00:00
|
|
|
|
|
|
|
var bytes: seq[byte]
|
2024-02-28 17:31:45 +00:00
|
|
|
let header = ?f[].handle.get().readRecord(bytes)
|
2024-02-09 10:13:12 +00:00
|
|
|
|
|
|
|
if header.typ != AccumulatorRoot:
|
|
|
|
return err("Invalid era file: didn't find accumulator root at index position")
|
|
|
|
|
|
|
|
if bytes.len != 32:
|
|
|
|
return err("invalid accumulator root")
|
|
|
|
|
|
|
|
ok(Digest(data: array[32, byte].initCopyFrom(bytes)))
|
|
|
|
|
2024-07-11 15:42:45 +00:00
|
|
|
proc buildAccumulator*(f: Era1File): Result[EpochRecordCached, string] =
|
2024-02-15 15:49:22 +00:00
|
|
|
let
|
|
|
|
startNumber = f.blockIdx.startNumber
|
|
|
|
endNumber = f.blockIdx.endNumber()
|
|
|
|
|
|
|
|
var headerRecords: seq[HeaderRecord]
|
2024-12-18 12:21:20 +00:00
|
|
|
var header: headers.Header
|
2024-02-28 17:31:45 +00:00
|
|
|
for blockNumber in startNumber .. endNumber:
|
2024-12-18 12:21:20 +00:00
|
|
|
?f.getBlockHeader(blockNumber, header)
|
|
|
|
let totalDifficulty = ?f.getTotalDifficulty(blockNumber)
|
2024-02-15 15:49:22 +00:00
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
headerRecords.add(
|
2024-12-18 12:21:20 +00:00
|
|
|
HeaderRecord(blockHash: header.rlpHash(), totalDifficulty: totalDifficulty)
|
2024-02-28 17:31:45 +00:00
|
|
|
)
|
2024-02-15 15:49:22 +00:00
|
|
|
|
2024-07-11 15:42:45 +00:00
|
|
|
ok(EpochRecordCached.init(headerRecords))
|
2024-02-15 15:49:22 +00:00
|
|
|
|
2024-02-09 10:13:12 +00:00
|
|
|
proc verify*(f: Era1File): Result[Digest, string] =
|
|
|
|
let
|
|
|
|
startNumber = f.blockIdx.startNumber
|
|
|
|
endNumber = f.blockIdx.endNumber()
|
|
|
|
|
|
|
|
var headerRecords: seq[HeaderRecord]
|
2024-12-18 12:21:20 +00:00
|
|
|
var blockTuple: BlockTuple
|
2024-02-28 17:31:45 +00:00
|
|
|
for blockNumber in startNumber .. endNumber:
|
2024-12-18 12:21:20 +00:00
|
|
|
?f.getBlockTuple(blockNumber, blockTuple)
|
2024-02-09 10:13:12 +00:00
|
|
|
let
|
2024-12-18 12:21:20 +00:00
|
|
|
txRoot = calcTxRoot(blockTuple.body.transactions)
|
|
|
|
ommershHash = rlpHash(blockTuple.body.uncles)
|
2024-02-09 10:13:12 +00:00
|
|
|
|
2024-12-18 12:21:20 +00:00
|
|
|
if blockTuple.header.txRoot != txRoot:
|
2024-02-09 10:13:12 +00:00
|
|
|
return err("Invalid transactions root")
|
|
|
|
|
2024-12-18 12:21:20 +00:00
|
|
|
if blockTuple.header.ommersHash != ommershHash:
|
2024-02-09 10:13:12 +00:00
|
|
|
return err("Invalid ommers hash")
|
|
|
|
|
2024-12-18 12:21:20 +00:00
|
|
|
if blockTuple.header.receiptsRoot != calcReceiptsRoot(blockTuple.receipts):
|
2024-02-09 10:13:12 +00:00
|
|
|
return err("Invalid receipts root")
|
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
headerRecords.add(
|
2024-12-18 12:21:20 +00:00
|
|
|
HeaderRecord(
|
|
|
|
blockHash: blockTuple.header.rlpHash(), totalDifficulty: blockTuple.td
|
|
|
|
)
|
2024-02-28 17:31:45 +00:00
|
|
|
)
|
2024-02-09 10:13:12 +00:00
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
let expectedRoot = ?f.getAccumulatorRoot()
|
2024-07-11 15:42:45 +00:00
|
|
|
let accumulatorRoot = getEpochRecordRoot(headerRecords)
|
2024-02-09 10:13:12 +00:00
|
|
|
|
|
|
|
if accumulatorRoot != expectedRoot:
|
|
|
|
err("Invalid accumulator root")
|
|
|
|
else:
|
|
|
|
ok(accumulatorRoot)
|
2024-02-15 15:49:22 +00:00
|
|
|
|
2024-10-04 21:21:26 +00:00
|
|
|
iterator era1BlockHeaders*(f: Era1File): headers.Header =
|
2024-02-15 15:49:22 +00:00
|
|
|
let
|
|
|
|
startNumber = f.blockIdx.startNumber
|
|
|
|
endNumber = f.blockIdx.endNumber()
|
|
|
|
|
2024-12-18 12:21:20 +00:00
|
|
|
var header: headers.Header
|
2024-02-28 17:31:45 +00:00
|
|
|
for blockNumber in startNumber .. endNumber:
|
2024-12-18 12:21:20 +00:00
|
|
|
f.getBlockHeader(blockNumber, header).expect("Header can be read")
|
2024-02-15 15:49:22 +00:00
|
|
|
yield header
|
|
|
|
|
|
|
|
iterator era1BlockTuples*(f: Era1File): BlockTuple =
|
|
|
|
let
|
|
|
|
startNumber = f.blockIdx.startNumber
|
|
|
|
endNumber = f.blockIdx.endNumber()
|
|
|
|
|
2024-12-18 12:21:20 +00:00
|
|
|
var blockTuple: BlockTuple
|
2024-02-28 17:31:45 +00:00
|
|
|
for blockNumber in startNumber .. endNumber:
|
2024-12-18 12:21:20 +00:00
|
|
|
f.getBlockTuple(blockNumber, blockTuple).expect("Block tuple can be read")
|
2024-02-15 15:49:22 +00:00
|
|
|
yield blockTuple
|