use correct minimum size when reading block / state headers (#6263)

`sizeof` also includes padding between fields, while SSZ defines
`fixedPortionSize` (on type) or `sszSize` (on value) to denote
required bytes to encode. Switch forked block/state readers to SSZ size.
As blocks/states are much larger than the padding, this doesn't affect
practical use cases but is slightly more correct this way.
This commit is contained in:
Etan Kissling 2024-05-25 07:30:05 +02:00 committed by GitHub
parent de740199f5
commit 0efc81d96d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 10 additions and 10 deletions

View File

@ -1360,19 +1360,19 @@ func readSszForkedHashedBeaconState*(cfg: RuntimeConfig, data: openArray[byte]):
ForkedHashedBeaconState {.raises: [SerializationError].} = ForkedHashedBeaconState {.raises: [SerializationError].} =
## Read a state picking the right fork by first reading the slot from the byte ## Read a state picking the right fork by first reading the slot from the byte
## source ## source
if data.len() < sizeof(BeaconStateHeader): const numHeaderBytes = fixedPortionSize(BeaconStateHeader)
raise (ref MalformedSszError)(msg: "Not enough data for BeaconState header") if data.len() < numHeaderBytes:
raise (ref MalformedSszError)(msg: "Incomplete BeaconState header")
let header = SSZ.decode( let header = SSZ.decode(
data.toOpenArray(0, sizeof(BeaconStateHeader) - 1), data.toOpenArray(0, numHeaderBytes - 1), BeaconStateHeader)
BeaconStateHeader)
# TODO https://github.com/nim-lang/Nim/issues/19357 # TODO https://github.com/nim-lang/Nim/issues/19357
result = readSszForkedHashedBeaconState(cfg, header.slot, data) result = readSszForkedHashedBeaconState(cfg, header.slot, data)
type type
ForkedBeaconBlockHeader = object ForkedBeaconBlockHeader = object
message*: uint32 # message offset message: uint32 # message offset
signature*: ValidatorSig signature: ValidatorSig
slot: Slot # start of BeaconBlock slot: Slot # start of BeaconBlock
func readSszForkedSignedBeaconBlock*( func readSszForkedSignedBeaconBlock*(
@ -1380,11 +1380,11 @@ func readSszForkedSignedBeaconBlock*(
ForkedSignedBeaconBlock {.raises: [SerializationError].} = ForkedSignedBeaconBlock {.raises: [SerializationError].} =
## Helper to read a header from bytes when it's not certain what kind of block ## Helper to read a header from bytes when it's not certain what kind of block
## it is ## it is
if data.len() < sizeof(ForkedBeaconBlockHeader): const numHeaderBytes = fixedPortionSize(ForkedBeaconBlockHeader)
raise (ref MalformedSszError)(msg: "Not enough data for SignedBeaconBlock header") if data.len() < numHeaderBytes:
raise (ref MalformedSszError)(msg: "Incomplete SignedBeaconBlock header")
let header = SSZ.decode( let header = SSZ.decode(
data.toOpenArray(0, sizeof(ForkedBeaconBlockHeader) - 1), data.toOpenArray(0, numHeaderBytes - 1), ForkedBeaconBlockHeader)
ForkedBeaconBlockHeader)
# TODO https://github.com/nim-lang/Nim/issues/19357 # TODO https://github.com/nim-lang/Nim/issues/19357
result = ForkedSignedBeaconBlock( result = ForkedSignedBeaconBlock(