2020-04-22 05:53:02 +00:00
|
|
|
{.push raises: [Defect].}
|
|
|
|
{.pragma: raisesssz, raises: [Defect, MalformedSszError, SszSizeMismatchError].}
|
|
|
|
|
2019-07-03 07:35:05 +00:00
|
|
|
import
|
2020-03-05 00:29:27 +00:00
|
|
|
typetraits, options,
|
2020-06-03 13:52:02 +00:00
|
|
|
stew/[bitops2, endians2, objects], serialization/testing/tracing,
|
2020-07-16 13:16:51 +00:00
|
|
|
../spec/[digest, datatypes], ./types, ./spec_types, ./merkleization
|
2019-07-03 07:35:05 +00:00
|
|
|
|
2020-05-22 19:55:00 +00:00
|
|
|
template raiseIncorrectSize(T: type) =
|
|
|
|
const typeName = name(T)
|
|
|
|
raise newException(MalformedSszError,
|
|
|
|
"SSZ " & typeName & " input of incorrect size")
|
|
|
|
|
2020-01-24 18:35:15 +00:00
|
|
|
template setOutputSize[R, T](a: var array[R, T], length: int) =
|
2019-07-03 07:35:05 +00:00
|
|
|
if length != a.len:
|
2020-05-22 19:55:00 +00:00
|
|
|
raiseIncorrectSize a.type
|
2020-01-24 18:35:15 +00:00
|
|
|
|
2020-05-25 14:42:01 +00:00
|
|
|
proc setOutputSize(list: var List, length: int) {.raisesssz.} =
|
2020-05-22 19:55:00 +00:00
|
|
|
if int64(length) > list.maxLen:
|
|
|
|
raise newException(MalformedSszError, "SSZ list maximum size exceeded")
|
|
|
|
list.setLen length
|
2020-01-24 18:35:15 +00:00
|
|
|
|
2019-07-03 07:35:05 +00:00
|
|
|
# fromSszBytes copies the wire representation to a Nim variable,
|
|
|
|
# assuming there's enough data in the buffer
|
2020-10-28 18:35:31 +00:00
|
|
|
func fromSszBytes*(T: type UintN, data: openArray[byte]): T {.raisesssz.} =
|
2019-07-03 07:35:05 +00:00
|
|
|
## Convert directly to bytes the size of the int. (e.g. ``uint16 = 2 bytes``)
|
|
|
|
## All integers are serialized as **little endian**.
|
2020-05-22 19:55:00 +00:00
|
|
|
if data.len != sizeof(result):
|
|
|
|
raiseIncorrectSize T
|
2020-01-24 18:35:15 +00:00
|
|
|
|
2020-03-05 00:29:27 +00:00
|
|
|
T.fromBytesLE(data)
|
2019-07-03 07:35:05 +00:00
|
|
|
|
2020-10-28 18:35:31 +00:00
|
|
|
func fromSszBytes*(T: type bool, data: openArray[byte]): T {.raisesssz.} =
|
2020-05-27 11:36:02 +00:00
|
|
|
# Strict: only allow 0 or 1
|
2020-05-22 19:55:00 +00:00
|
|
|
if data.len != 1 or byte(data[0]) > byte(1):
|
2020-01-24 18:35:15 +00:00
|
|
|
raise newException(MalformedSszError, "invalid boolean value")
|
|
|
|
data[0] == 1
|
2019-07-03 07:35:05 +00:00
|
|
|
|
2020-10-28 18:35:31 +00:00
|
|
|
func fromSszBytes*(T: type Eth2Digest, data: openArray[byte]): T {.raisesssz.} =
|
2020-05-22 19:55:00 +00:00
|
|
|
if data.len != sizeof(result.data):
|
|
|
|
raiseIncorrectSize T
|
2019-07-03 07:35:05 +00:00
|
|
|
copyMem(result.data.addr, unsafeAddr data[0], sizeof(result.data))
|
|
|
|
|
2020-10-28 18:35:31 +00:00
|
|
|
func fromSszBytes*(T: type GraffitiBytes, data: openArray[byte]): T {.raisesssz.} =
|
2020-06-29 17:30:19 +00:00
|
|
|
if data.len != sizeof(result):
|
|
|
|
raiseIncorrectSize T
|
|
|
|
copyMem(result.addr, unsafeAddr data[0], sizeof(result))
|
|
|
|
|
2020-11-04 21:52:47 +00:00
|
|
|
template fromSszBytes*(T: type Slot, bytes: openArray[byte]): T =
|
|
|
|
T fromSszBytes(uint64, bytes)
|
2019-07-03 07:35:05 +00:00
|
|
|
|
2020-11-04 21:52:47 +00:00
|
|
|
template fromSszBytes*(T: type Epoch, bytes: openArray[byte]): T =
|
|
|
|
T fromSszBytes(uint64, bytes)
|
2019-07-03 07:35:05 +00:00
|
|
|
|
2020-10-28 18:35:31 +00:00
|
|
|
func fromSszBytes*(T: type ForkDigest, bytes: openArray[byte]): T {.raisesssz.} =
|
2020-05-22 19:55:00 +00:00
|
|
|
if bytes.len != sizeof(result):
|
|
|
|
raiseIncorrectSize T
|
2020-05-11 18:08:52 +00:00
|
|
|
copyMem(result.addr, unsafeAddr bytes[0], sizeof(result))
|
|
|
|
|
2020-10-28 18:35:31 +00:00
|
|
|
func fromSszBytes*(T: type Version, bytes: openArray[byte]): T {.raisesssz.} =
|
2020-05-22 19:55:00 +00:00
|
|
|
if bytes.len != sizeof(result):
|
|
|
|
raiseIncorrectSize T
|
2020-05-11 18:08:52 +00:00
|
|
|
copyMem(result.addr, unsafeAddr bytes[0], sizeof(result))
|
|
|
|
|
2020-10-28 18:35:31 +00:00
|
|
|
template fromSszBytes*(T: type BitSeq, bytes: openArray[byte]): auto =
|
2019-07-03 07:35:05 +00:00
|
|
|
BitSeq @bytes
|
|
|
|
|
2020-04-15 01:24:13 +00:00
|
|
|
proc `[]`[T, U, V](s: openArray[T], x: HSlice[U, V]) {.error:
|
2020-10-28 18:35:31 +00:00
|
|
|
"Please don't use openArray's [] as it allocates a result sequence".}
|
2020-04-15 01:24:13 +00:00
|
|
|
|
2020-05-22 19:55:00 +00:00
|
|
|
template checkForForbiddenBits(ResulType: type,
|
2020-10-28 18:35:31 +00:00
|
|
|
input: openArray[byte],
|
2020-07-13 14:44:58 +00:00
|
|
|
expectedBits: static int64) =
|
2020-05-22 19:55:00 +00:00
|
|
|
## This checks if the input contains any bits set above the maximum
|
|
|
|
## sized allowed. We only need to check the last byte to verify this:
|
|
|
|
const bitsInLastByte = (expectedBits mod 8)
|
|
|
|
when bitsInLastByte != 0:
|
|
|
|
# As an example, if there are 3 bits expected in the last byte,
|
|
|
|
# we calculate a bitmask equal to 11111000. If the input has any
|
|
|
|
# raised bits in range of the bitmask, this would be a violation
|
|
|
|
# of the size of the BitArray:
|
|
|
|
const forbiddenBitsMask = byte(byte(0xff) shl bitsInLastByte)
|
|
|
|
|
|
|
|
if (input[^1] and forbiddenBitsMask) != 0:
|
|
|
|
raiseIncorrectSize ResulType
|
|
|
|
|
2020-10-28 18:35:31 +00:00
|
|
|
func readSszValue*[T](input: openArray[byte],
|
2020-09-04 06:35:10 +00:00
|
|
|
val: var T, updateRoot: bool = true) {.raisesssz.} =
|
2019-07-03 07:35:05 +00:00
|
|
|
mixin fromSszBytes, toSszType
|
|
|
|
|
2020-06-09 11:44:40 +00:00
|
|
|
template readOffsetUnchecked(n: int): uint32 {.used.}=
|
|
|
|
fromSszBytes(uint32, input.toOpenArray(n, n + offsetSize - 1))
|
2019-07-03 07:35:05 +00:00
|
|
|
|
2020-05-22 19:55:00 +00:00
|
|
|
template readOffset(n: int): int {.used.} =
|
|
|
|
let offset = readOffsetUnchecked(n)
|
2020-06-09 11:44:40 +00:00
|
|
|
if offset > input.len.uint32:
|
2020-05-22 19:55:00 +00:00
|
|
|
raise newException(MalformedSszError, "SSZ list element offset points past the end of the input")
|
2020-06-09 11:44:40 +00:00
|
|
|
int(offset)
|
2020-05-22 19:55:00 +00:00
|
|
|
|
|
|
|
#when result is List:
|
|
|
|
# result.setOutputSize input.len
|
|
|
|
# readOpenArray(toSeq result, input)
|
|
|
|
|
|
|
|
#elif result is array:
|
|
|
|
# result.checkOutputSize input.len
|
|
|
|
# readOpenArray(result, input)
|
2020-01-24 18:35:15 +00:00
|
|
|
|
2020-05-27 15:04:43 +00:00
|
|
|
when val is BitList:
|
2020-05-22 19:55:00 +00:00
|
|
|
if input.len == 0:
|
|
|
|
raise newException(MalformedSszError, "Invalid empty SSZ BitList value")
|
|
|
|
|
2020-05-28 10:58:25 +00:00
|
|
|
# Since our BitLists have an in-memory representation that precisely
|
|
|
|
# matches their SSZ encoding, we can deserialize them as regular Lists:
|
2020-05-27 15:04:43 +00:00
|
|
|
const maxExpectedSize = (val.maxLen div 8) + 1
|
2020-05-28 10:58:25 +00:00
|
|
|
type MatchingListType = List[byte, maxExpectedSize]
|
|
|
|
|
|
|
|
when false:
|
|
|
|
# TODO: Nim doesn't like this simple type coercion,
|
|
|
|
# we'll rely on `cast` for now (see below)
|
|
|
|
readSszValue(input, MatchingListType val)
|
|
|
|
else:
|
|
|
|
static:
|
|
|
|
# As a sanity check, we verify that the coercion is accepted by the compiler:
|
|
|
|
doAssert MatchingListType(val) is MatchingListType
|
|
|
|
readSszValue(input, cast[ptr MatchingListType](addr val)[])
|
2020-05-22 19:55:00 +00:00
|
|
|
|
2020-05-27 15:04:43 +00:00
|
|
|
let resultBytesCount = len bytes(val)
|
2020-05-22 19:55:00 +00:00
|
|
|
|
2020-05-27 15:04:43 +00:00
|
|
|
if bytes(val)[resultBytesCount - 1] == 0:
|
2020-05-22 19:55:00 +00:00
|
|
|
raise newException(MalformedSszError, "SSZ BitList is not properly terminated")
|
|
|
|
|
|
|
|
if resultBytesCount == maxExpectedSize:
|
2020-05-28 10:58:25 +00:00
|
|
|
checkForForbiddenBits(T, input, val.maxLen + 1)
|
2020-05-22 19:55:00 +00:00
|
|
|
|
2020-05-28 16:34:59 +00:00
|
|
|
elif val is HashList:
|
|
|
|
readSszValue(input, val.data)
|
|
|
|
val.hashes.setLen(0)
|
|
|
|
val.growHashes()
|
|
|
|
|
|
|
|
elif val is HashArray:
|
|
|
|
readSszValue(input, val.data)
|
|
|
|
for h in val.hashes.mitems():
|
|
|
|
clearCache(h)
|
|
|
|
|
2020-05-27 15:04:43 +00:00
|
|
|
elif val is List|array:
|
|
|
|
type E = type val[0]
|
2020-06-09 11:44:40 +00:00
|
|
|
|
2020-05-27 11:36:02 +00:00
|
|
|
when E is byte:
|
2020-05-27 15:04:43 +00:00
|
|
|
val.setOutputSize input.len
|
2019-09-27 16:48:12 +00:00
|
|
|
if input.len > 0:
|
2020-05-27 15:04:43 +00:00
|
|
|
copyMem(addr val[0], unsafeAddr input[0], input.len)
|
2019-07-03 07:35:05 +00:00
|
|
|
|
2020-05-27 11:36:02 +00:00
|
|
|
elif isFixedSize(E):
|
|
|
|
const elemSize = fixedPortionSize(E)
|
2019-07-03 07:35:05 +00:00
|
|
|
if input.len mod elemSize != 0:
|
|
|
|
var ex = new SszSizeMismatchError
|
2020-05-28 10:58:25 +00:00
|
|
|
ex.deserializedType = cstring typetraits.name(T)
|
2019-07-03 07:35:05 +00:00
|
|
|
ex.actualSszSize = input.len
|
|
|
|
ex.elementSize = elemSize
|
|
|
|
raise ex
|
2020-05-27 15:04:43 +00:00
|
|
|
val.setOutputSize input.len div elemSize
|
|
|
|
trs "READING LIST WITH LEN ", val.len
|
|
|
|
for i in 0 ..< val.len:
|
2019-07-03 07:35:05 +00:00
|
|
|
trs "TRYING TO READ LIST ELEM ", i
|
|
|
|
let offset = i * elemSize
|
2020-05-27 15:04:43 +00:00
|
|
|
readSszValue(input.toOpenArray(offset, offset + elemSize - 1), val[i])
|
2019-07-03 07:35:05 +00:00
|
|
|
trs "LIST READING COMPLETE"
|
|
|
|
|
|
|
|
else:
|
|
|
|
if input.len == 0:
|
|
|
|
# This is an empty list.
|
|
|
|
# The default initialization of the return value is fine.
|
|
|
|
return
|
2020-01-24 18:35:15 +00:00
|
|
|
elif input.len < offsetSize:
|
|
|
|
raise newException(MalformedSszError, "SSZ input of insufficient size")
|
2019-07-03 07:35:05 +00:00
|
|
|
|
|
|
|
var offset = readOffset 0
|
|
|
|
trs "GOT OFFSET ", offset
|
2020-06-09 11:44:40 +00:00
|
|
|
|
2019-07-03 07:35:05 +00:00
|
|
|
let resultLen = offset div offsetSize
|
|
|
|
trs "LEN ", resultLen
|
2020-04-23 19:39:23 +00:00
|
|
|
|
|
|
|
if resultLen == 0:
|
|
|
|
# If there are too many elements, other constraints detect problems
|
|
|
|
# (not monotonically increasing, past end of input, or last element
|
|
|
|
# not matching up with its nextOffset properly)
|
|
|
|
raise newException(MalformedSszError, "SSZ list incorrectly encoded of zero length")
|
|
|
|
|
2020-05-27 15:04:43 +00:00
|
|
|
val.setOutputSize resultLen
|
2019-07-03 07:35:05 +00:00
|
|
|
for i in 1 ..< resultLen:
|
|
|
|
let nextOffset = readOffset(i * offsetSize)
|
2020-01-24 18:35:15 +00:00
|
|
|
if nextOffset <= offset:
|
|
|
|
raise newException(MalformedSszError, "SSZ list element offsets are not monotonically increasing")
|
2019-08-05 00:00:49 +00:00
|
|
|
else:
|
2020-05-27 15:04:43 +00:00
|
|
|
readSszValue(input.toOpenArray(offset, nextOffset - 1), val[i - 1])
|
2019-07-03 07:35:05 +00:00
|
|
|
offset = nextOffset
|
|
|
|
|
2020-05-27 15:04:43 +00:00
|
|
|
readSszValue(input.toOpenArray(offset, input.len - 1), val[resultLen - 1])
|
2019-07-03 07:35:05 +00:00
|
|
|
|
2020-05-22 19:55:00 +00:00
|
|
|
# TODO: Should be possible to remove BitArray from here
|
2020-08-21 18:50:46 +00:00
|
|
|
elif val is UintN|bool:
|
2020-05-28 12:36:37 +00:00
|
|
|
trs "READING BASIC TYPE ", typetraits.name(T), " input=", input.len
|
|
|
|
val = fromSszBytes(T, input)
|
2020-05-27 15:04:43 +00:00
|
|
|
trs "RESULT WAS ", repr(val)
|
|
|
|
|
|
|
|
elif val is BitArray:
|
|
|
|
if sizeof(val) != input.len:
|
2020-05-28 10:58:25 +00:00
|
|
|
raiseIncorrectSize(T)
|
|
|
|
checkForForbiddenBits(T, input, val.bits)
|
2020-05-27 15:04:43 +00:00
|
|
|
copyMem(addr val.bytes[0], unsafeAddr input[0], input.len)
|
|
|
|
|
|
|
|
elif val is object|tuple:
|
2020-06-09 11:44:40 +00:00
|
|
|
let inputLen = uint32 input.len
|
|
|
|
const minimallyExpectedSize = uint32 fixedPortionSize(T)
|
|
|
|
|
|
|
|
if inputLen < minimallyExpectedSize:
|
2020-01-24 18:35:15 +00:00
|
|
|
raise newException(MalformedSszError, "SSZ input of insufficient size")
|
|
|
|
|
2020-05-27 15:04:43 +00:00
|
|
|
enumInstanceSerializedFields(val, fieldName, field):
|
2020-05-28 10:58:25 +00:00
|
|
|
const boundingOffsets = getFieldBoundingOffsets(T, fieldName)
|
2019-07-03 07:35:05 +00:00
|
|
|
trs "BOUNDING OFFSET FOR FIELD ", fieldName, " = ", boundingOffsets
|
|
|
|
|
2020-05-25 14:42:01 +00:00
|
|
|
# type FieldType = type field # buggy
|
|
|
|
# For some reason, Nim gets confused about the alias here. This could be a
|
|
|
|
# generics caching issue caused by the use of distinct types. Such an
|
|
|
|
# issue is very scary in general.
|
|
|
|
# The bug can be seen with the two List[uint64, N] types that exist in
|
|
|
|
# the spec, with different N.
|
|
|
|
|
|
|
|
type SszType = type toSszType(declval type(field))
|
2019-07-03 07:35:05 +00:00
|
|
|
|
|
|
|
when isFixedSize(SszType):
|
|
|
|
const
|
|
|
|
startOffset = boundingOffsets[0]
|
|
|
|
endOffset = boundingOffsets[1]
|
|
|
|
trs "FIXED FIELD ", startOffset, "-", endOffset
|
|
|
|
else:
|
|
|
|
let
|
2020-05-22 19:55:00 +00:00
|
|
|
startOffset = readOffsetUnchecked(boundingOffsets[0])
|
2020-06-09 11:44:40 +00:00
|
|
|
endOffset = if boundingOffsets[1] == -1: inputLen
|
2020-05-22 19:55:00 +00:00
|
|
|
else: readOffsetUnchecked(boundingOffsets[1])
|
2020-05-29 19:06:35 +00:00
|
|
|
|
|
|
|
when boundingOffsets.isFirstOffset:
|
|
|
|
if startOffset != minimallyExpectedSize:
|
|
|
|
raise newException(MalformedSszError, "SSZ object dynamic portion starts at invalid offset")
|
|
|
|
|
2019-07-03 07:35:05 +00:00
|
|
|
trs "VAR FIELD ", startOffset, "-", endOffset
|
2020-01-27 17:20:06 +00:00
|
|
|
if startOffset > endOffset:
|
2020-01-24 18:35:15 +00:00
|
|
|
raise newException(MalformedSszError, "SSZ field offsets are not monotonically increasing")
|
2020-06-09 11:44:40 +00:00
|
|
|
elif endOffset > inputLen:
|
2020-01-24 18:35:15 +00:00
|
|
|
raise newException(MalformedSszError, "SSZ field offset points past the end of the input")
|
2020-04-17 09:00:47 +00:00
|
|
|
elif startOffset < minimallyExpectedSize:
|
2020-04-16 19:21:28 +00:00
|
|
|
raise newException(MalformedSszError, "SSZ field offset points outside bounding offsets")
|
2019-07-03 07:35:05 +00:00
|
|
|
|
|
|
|
# TODO The extra type escaping here is a work-around for a Nim issue:
|
2020-05-25 14:42:01 +00:00
|
|
|
when type(field) is type(SszType):
|
2019-07-03 07:35:05 +00:00
|
|
|
trs "READING NATIVE ", fieldName, ": ", name(SszType)
|
2020-04-22 23:35:55 +00:00
|
|
|
|
2020-05-25 14:42:01 +00:00
|
|
|
# TODO passing in `FieldType` instead of `type(field)` triggers a
|
|
|
|
# bug in the compiler
|
2020-05-27 15:04:43 +00:00
|
|
|
readSszValue(
|
2020-06-09 11:44:40 +00:00
|
|
|
input.toOpenArray(int(startOffset), int(endOffset - 1)),
|
2020-05-27 15:04:43 +00:00
|
|
|
field)
|
2020-05-25 14:42:01 +00:00
|
|
|
trs "READING COMPLETE ", fieldName
|
2019-07-03 07:35:05 +00:00
|
|
|
else:
|
|
|
|
trs "READING FOREIGN ", fieldName, ": ", name(SszType)
|
2020-04-29 20:12:07 +00:00
|
|
|
field = fromSszBytes(
|
2020-05-25 14:42:01 +00:00
|
|
|
type(field),
|
2020-06-09 11:44:40 +00:00
|
|
|
input.toOpenArray(int(startOffset), int(endOffset - 1)))
|
2019-07-03 07:35:05 +00:00
|
|
|
|
2020-07-16 13:16:51 +00:00
|
|
|
when val is SignedBeaconBlock | TrustedSignedBeaconBlock:
|
2020-09-04 06:35:10 +00:00
|
|
|
if updateRoot:
|
|
|
|
val.root = hash_tree_root(val.message)
|
2019-07-03 07:35:05 +00:00
|
|
|
else:
|
2020-05-28 10:58:25 +00:00
|
|
|
unsupported T
|