remove `updateRoot` param from SSZ
call `readSszBytes` directly to skip root updates
This commit is contained in:
parent
66cb18d69b
commit
c7d4659d32
|
@ -7,8 +7,9 @@
|
|||
|
||||
{.push raises: [Defect].}
|
||||
|
||||
# Import this module to get access to `SSZ.encode` and `SSZ.decode` for spec types
|
||||
|
||||
# This module exports SSZ.encode and SSZ.decode for spec types - don't use
|
||||
# ssz_serialization directly! To bypass root updates, use `readSszBytes`
|
||||
# without involving SSZ!
|
||||
import
|
||||
./ssz_codec,
|
||||
../ssz/ssz_serialization,
|
||||
|
@ -38,3 +39,13 @@ template readSszBytes*(
|
|||
template readSszBytes*(
|
||||
data: openArray[byte], val: var altair.TrustedSignedBeaconBlock, updateRoot = true) =
|
||||
readAndUpdateRoot(data, val, updateRoot)
|
||||
|
||||
template readSszBytes*(
|
||||
data: openArray[byte], val: var auto, updateRoot: bool) =
|
||||
readSszValue(data, val)
|
||||
|
||||
func readSszBytes(T: type, data: openArray[byte], updateRoot = true): T {.
|
||||
raises: [Defect, MalformedSszError, SszSizeMismatchError].} =
|
||||
var res: T
|
||||
readSszBytes(data, res, updateRoot)
|
||||
res
|
||||
|
|
|
@ -23,7 +23,6 @@ export
|
|||
type
|
||||
SszReader* = object
|
||||
stream: InputStream
|
||||
updateRoot: bool
|
||||
|
||||
SszWriter* = object
|
||||
stream: OutputStream
|
||||
|
@ -47,9 +46,8 @@ template sizePrefixed*[TT](x: TT): untyped =
|
|||
SizePrefixed[T](x)
|
||||
|
||||
proc init*(T: type SszReader,
|
||||
stream: InputStream,
|
||||
updateRoot: bool = true): T =
|
||||
T(stream: stream, updateRoot: updateRoot)
|
||||
stream: InputStream): T =
|
||||
T(stream: stream)
|
||||
|
||||
proc writeFixedSized(s: var (OutputStream|WriteCursor), x: auto) {.raises: [Defect, IOError].} =
|
||||
mixin toSszType
|
||||
|
@ -222,21 +220,22 @@ proc writeValue*[T](w: var SszWriter, x: SizePrefixed[T]) {.raises: [Defect, IOE
|
|||
let length = toBytes(uint64(w.stream.pos - initPos), Leb128)
|
||||
cursor.finalWrite length.toOpenArray()
|
||||
|
||||
proc readValue*(r: var SszReader, val: var auto) {.raises: [Defect, MalformedSszError, SszSizeMismatchError, IOError].} =
|
||||
proc readValue*(r: var SszReader, val: var auto) {.
|
||||
raises: [Defect, MalformedSszError, SszSizeMismatchError, IOError].} =
|
||||
mixin readSszBytes
|
||||
type T = type val
|
||||
when isFixedSize(T):
|
||||
const minimalSize = fixedPortionSize(T)
|
||||
if r.stream.readable(minimalSize):
|
||||
readSszBytes(r.stream.read(minimalSize), val, r.updateRoot)
|
||||
readSszBytes(r.stream.read(minimalSize), val)
|
||||
else:
|
||||
raise newException(MalformedSszError, "SSZ input of insufficient size")
|
||||
else:
|
||||
# TODO(zah) Read the fixed portion first and precisely measure the
|
||||
# size of the dynamic portion to consume the right number of bytes.
|
||||
readSszBytes(r.stream.read(r.stream.len.get), val, r.updateRoot)
|
||||
readSszBytes(r.stream.read(r.stream.len.get), val)
|
||||
|
||||
proc readSszBytes*[T](data: openArray[byte], val: var T, updateRoot = true) {.
|
||||
proc readSszBytes*[T](data: openArray[byte], val: var T) {.
|
||||
raises: [Defect, MalformedSszError, SszSizeMismatchError].} =
|
||||
# Overload `readSszBytes` to perform custom operations on T after
|
||||
# deserialization
|
||||
|
|
|
@ -42,6 +42,11 @@ suite "Specific field types":
|
|||
t.message == decoded.message
|
||||
t.root == decoded.root
|
||||
|
||||
t = default(type t)
|
||||
readSszBytes(encoded, t, false)
|
||||
check:
|
||||
t.root == Eth2Digest()
|
||||
|
||||
testit(phase0.SignedBeaconBlock)
|
||||
testit(phase0.TrustedSignedBeaconBlock)
|
||||
testit(altair.SignedBeaconBlock)
|
||||
|
|
Loading…
Reference in New Issue