Skip to content

Commit

Permalink
era: fix verifier at empty slots
Browse files Browse the repository at this point in the history
* avoid returning zero-byte block data to REST/p2p when loading era
files
  • Loading branch information
arnetheduck committed Dec 4, 2023
1 parent 0e5c44b commit a905a8a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
4 changes: 2 additions & 2 deletions beacon_chain/consensus_object_pools/blockchain_dag.nim
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ proc getBlockSSZ*(dag: ChainDAGRef, bid: BlockId, bytes: var seq[byte]): bool =
getBlockSSZ(
dag.era, getStateField(dag.headState, historical_roots).asSeq,
dag.headState.historical_summaries().asSeq,
bid.slot, bytes).isOk)
bid.slot, bytes).isOk() and bytes.len > 0)

proc getBlockSZ*(dag: ChainDAGRef, bid: BlockId, bytes: var seq[byte]): bool =
# Load the snappy-frame-compressed ("SZ") SSZ-encoded data of a block into
Expand All @@ -265,7 +265,7 @@ proc getBlockSZ*(dag: ChainDAGRef, bid: BlockId, bytes: var seq[byte]): bool =
getBlockSZ(
dag.era, getStateField(dag.headState, historical_roots).asSeq,
dag.headState.historical_summaries().asSeq,
bid.slot, bytes).isOk)
bid.slot, bytes).isOk and bytes.len > 0)

proc getForkedBlock*(
dag: ChainDAGRef, bid: BlockId): Opt[ForkedTrustedSignedBeaconBlock] =
Expand Down
26 changes: 23 additions & 3 deletions beacon_chain/era_db.nim
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,23 @@ proc getBlockSZ*(

proc getBlockSSZ*(
f: EraFile, slot: Slot, bytes: var seq[byte]): Result[void, string] =
## Get raw SSZ bytes of the block at the given slot - may overwrite
## `bytes` on error.
##
## Sets `bytes` to an empty seq and returns success if there is no block at
## the given slot, according to the index
var tmp: seq[byte]
? f.getBlockSZ(slot, tmp)

let
len = uncompressedLenFramed(tmp).valueOr:
return err("Cannot read uncompressed length, era file corrupt?")

if len == 0:
# Given slot is empty
reset(bytes)
return ok()

if len > int.high.uint64:
return err("Invalid uncompressed size")

Expand Down Expand Up @@ -298,6 +309,11 @@ proc getBlockSSZ*(
db: EraDB, historical_roots: openArray[Eth2Digest],
historical_summaries: openArray[HistoricalSummary], slot: Slot,
bytes: var seq[byte]): Result[void, string] =
## Get raw SSZ bytes of the block at the given slot - may overwrite
## `bytes` on error.
##
## Sets `bytes` to an empty seq and returns success if there is no block at
## the given slot according to the index
let
f = ? db.getEraFile(historical_roots, historical_summaries, slot.era + 1)

Expand All @@ -307,13 +323,15 @@ proc getBlock*(
db: EraDB, historical_roots: openArray[Eth2Digest],
historical_summaries: openArray[HistoricalSummary], slot: Slot,
root: Opt[Eth2Digest], T: type ForkyTrustedSignedBeaconBlock): Opt[T] =
var tmp: seq[byte]
var bytes: seq[byte]
? db.getBlockSSZ(
historical_roots, historical_summaries, slot, tmp).mapErr(proc(x: auto) = discard)
historical_roots, historical_summaries, slot, bytes).mapConvertErr(void)
if bytes.len() == 0:
return Opt.none(T)

result.ok(default(T))
try:
readSszBytes(tmp, result.get(), updateRoot = root.isNone)
readSszBytes(bytes, result.get(), updateRoot = root.isNone)
if root.isSome():
result.get().root = root.get()
except CatchableError:
Expand Down Expand Up @@ -350,6 +368,8 @@ proc getState*(
state: var ForkedHashedBeaconState): Result[void, string] =
var bytes: seq[byte]
? db.getStateSSZ(historical_roots, historical_summaries, slot, bytes)
if bytes.len() == 0:
return err("State not found")

try:
state = readSszForkedHashedBeaconState(db.cfg, slot, bytes)
Expand Down

0 comments on commit a905a8a

Please sign in to comment.