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,
|
|
|
|
stew/[bitseqs, endians2, objects, bitseqs], serialization/testing/tracing,
|
2019-07-03 07:35:05 +00:00
|
|
|
../spec/[digest, datatypes], ./types
|
|
|
|
|
2020-01-24 18:35:15 +00:00
|
|
|
const
|
|
|
|
maxListAllocation = 1 * 1024 * 1024 * 1024 # 1 GiB
|
|
|
|
|
|
|
|
template setOutputSize[R, T](a: var array[R, T], length: int) =
|
2019-07-03 07:35:05 +00:00
|
|
|
if length != a.len:
|
|
|
|
raise newException(MalformedSszError, "SSZ input of insufficient size")
|
|
|
|
|
2020-04-22 05:53:02 +00:00
|
|
|
proc setOutputSize[T](s: var seq[T], length: int) {.inline, raisesssz.} =
|
2020-01-24 18:35:15 +00:00
|
|
|
if sizeof(T) * length > maxListAllocation:
|
|
|
|
raise newException(MalformedSszError, "SSZ list size is too large to fit in memory")
|
|
|
|
s.setLen length
|
|
|
|
|
2020-04-22 05:53:02 +00:00
|
|
|
proc setOutputSize(s: var string, length: int) {.inline, raisesssz.} =
|
2020-01-24 18:35:15 +00:00
|
|
|
if length > maxListAllocation:
|
|
|
|
raise newException(MalformedSszError, "SSZ string is too large to fit in memory")
|
|
|
|
s.setLen length
|
|
|
|
|
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-04-22 05:53:02 +00:00
|
|
|
func fromSszBytes*(T: type SomeInteger, 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-01-24 18:35:15 +00:00
|
|
|
if data.len < sizeof(result):
|
|
|
|
raise newException(MalformedSszError, "SSZ input of insufficient size")
|
|
|
|
|
2020-03-05 00:29:27 +00:00
|
|
|
T.fromBytesLE(data)
|
2019-07-03 07:35:05 +00:00
|
|
|
|
2020-04-22 05:53:02 +00:00
|
|
|
func fromSszBytes*(T: type bool, data: openarray[byte]): T {.raisesssz.} =
|
2019-07-03 07:35:05 +00:00
|
|
|
# TODO: spec doesn't say what to do if the value is >1 - we'll use the C
|
|
|
|
# definition for now, but maybe this should be a parse error instead?
|
2020-05-06 12:31:05 +00:00
|
|
|
if data.len == 0 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-04-22 05:53:02 +00:00
|
|
|
func fromSszBytes*(T: type Eth2Digest, data: openarray[byte]): T {.raisesssz.} =
|
2020-01-24 18:35:15 +00:00
|
|
|
if data.len < sizeof(result.data):
|
|
|
|
raise newException(MalformedSszError, "SSZ input of insufficient size")
|
2019-07-03 07:35:05 +00:00
|
|
|
copyMem(result.data.addr, unsafeAddr data[0], sizeof(result.data))
|
|
|
|
|
|
|
|
template fromSszBytes*(T: type Slot, bytes: openarray[byte]): Slot =
|
|
|
|
Slot fromSszBytes(uint64, bytes)
|
|
|
|
|
|
|
|
template fromSszBytes*(T: type Epoch, bytes: openarray[byte]): Epoch =
|
|
|
|
Epoch fromSszBytes(uint64, bytes)
|
|
|
|
|
2020-05-11 18:08:52 +00:00
|
|
|
func fromSszBytes*(T: type ForkDigest, bytes: openarray[byte]): T {.raisesssz.} =
|
|
|
|
if bytes.len < sizeof(result):
|
|
|
|
raise newException(MalformedSszError, "SSZ input of insufficient size")
|
|
|
|
copyMem(result.addr, unsafeAddr bytes[0], sizeof(result))
|
|
|
|
|
|
|
|
func fromSszBytes*(T: type Version, bytes: openarray[byte]): T {.raisesssz.} =
|
|
|
|
if bytes.len < sizeof(result):
|
|
|
|
raise newException(MalformedSszError, "SSZ input of insufficient size")
|
|
|
|
copyMem(result.addr, unsafeAddr bytes[0], sizeof(result))
|
|
|
|
|
2020-04-22 05:53:02 +00:00
|
|
|
template fromSszBytes*(T: type enum, bytes: openarray[byte]): auto =
|
2019-07-03 07:35:05 +00:00
|
|
|
T fromSszBytes(uint64, bytes)
|
|
|
|
|
|
|
|
template fromSszBytes*(T: type BitSeq, bytes: openarray[byte]): auto =
|
|
|
|
BitSeq @bytes
|
|
|
|
|
2020-05-21 13:21:29 +00:00
|
|
|
func fromSszBytes*(T: type BitList, bytes: openarray[byte]): auto {.raisesssz.} =
|
2020-04-26 16:26:53 +00:00
|
|
|
if bytes.len == 0:
|
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.11.1/ssz/simple-serialize.md#bitlistn
|
|
|
|
# "An additional 1 bit is added to the end, at index e where e is the
|
|
|
|
# length of the bitlist (not the limit), so that the length in bits will
|
|
|
|
# also be known."
|
|
|
|
# It's not possible to have a literally 0-byte (raw) Bitlist.
|
|
|
|
# https://github.com/status-im/nim-beacon-chain/issues/931
|
|
|
|
raise newException(MalformedSszError, "SSZ input Bitlist too small")
|
2020-05-21 13:21:29 +00:00
|
|
|
T @bytes
|
2019-07-03 07:35:05 +00:00
|
|
|
|
2020-04-22 05:53:02 +00:00
|
|
|
func fromSszBytes*[N](T: type BitArray[N], bytes: openarray[byte]): T {.raisesssz.} =
|
2020-01-29 00:40:27 +00:00
|
|
|
# A bit vector doesn't have a marker bit, but we'll use the helper from
|
|
|
|
# nim-stew to determine the position of the leading (marker) bit.
|
|
|
|
# If it's outside the BitArray size, we have an overflow:
|
|
|
|
if bitsLen(bytes) > N - 1:
|
|
|
|
raise newException(MalformedSszError, "SSZ bit array overflow")
|
|
|
|
copyMem(addr result.bytes[0], unsafeAddr bytes[0], bytes.len)
|
|
|
|
|
2020-04-15 01:24:13 +00:00
|
|
|
proc `[]`[T, U, V](s: openArray[T], x: HSlice[U, V]) {.error:
|
|
|
|
"Please don't use openarray's [] as it allocates a result sequence".}
|
|
|
|
|
2020-04-22 05:53:02 +00:00
|
|
|
func readSszValue*(input: openarray[byte], T: type): T {.raisesssz.} =
|
2019-07-03 07:35:05 +00:00
|
|
|
mixin fromSszBytes, toSszType
|
|
|
|
|
2020-01-24 18:35:15 +00:00
|
|
|
type T {.used.} = type(result)
|
2019-07-03 07:35:05 +00:00
|
|
|
|
2019-11-06 10:36:05 +00:00
|
|
|
template readOffset(n: int): int {.used.}=
|
2020-04-15 01:24:13 +00:00
|
|
|
int fromSszBytes(uint32, input.toOpenArray(n, n + offsetSize - 1))
|
2019-07-03 07:35:05 +00:00
|
|
|
|
2020-05-18 17:49:22 +00:00
|
|
|
when result is List:
|
2019-07-03 07:35:05 +00:00
|
|
|
type ElemType = type result[0]
|
|
|
|
result = T readSszValue(input, seq[ElemType])
|
2020-01-24 18:35:15 +00:00
|
|
|
|
2020-05-21 13:21:29 +00:00
|
|
|
elif result is seq|openarray|array:
|
2019-07-03 07:35:05 +00:00
|
|
|
type ElemType = type result[0]
|
|
|
|
when ElemType is byte|char:
|
2020-01-24 18:35:15 +00:00
|
|
|
result.setOutputSize input.len
|
2019-09-27 16:48:12 +00:00
|
|
|
if input.len > 0:
|
|
|
|
copyMem(addr result[0], unsafeAddr input[0], input.len)
|
2019-07-03 07:35:05 +00:00
|
|
|
|
|
|
|
elif isFixedSize(ElemType):
|
|
|
|
const elemSize = fixedPortionSize(ElemType)
|
|
|
|
if input.len mod elemSize != 0:
|
|
|
|
var ex = new SszSizeMismatchError
|
|
|
|
ex.deserializedType = cstring typetraits.name(T)
|
|
|
|
ex.actualSszSize = input.len
|
|
|
|
ex.elementSize = elemSize
|
|
|
|
raise ex
|
2020-01-24 18:35:15 +00:00
|
|
|
result.setOutputSize input.len div elemSize
|
2019-07-03 07:35:05 +00:00
|
|
|
trs "READING LIST WITH LEN ", result.len
|
|
|
|
for i in 0 ..< result.len:
|
|
|
|
trs "TRYING TO READ LIST ELEM ", i
|
|
|
|
let offset = i * elemSize
|
2020-04-15 01:24:13 +00:00
|
|
|
result[i] = readSszValue(input.toOpenArray(offset, offset + elemSize - 1), ElemType)
|
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
|
|
|
|
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-01-24 18:35:15 +00:00
|
|
|
result.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")
|
|
|
|
elif nextOffset > input.len:
|
|
|
|
raise newException(MalformedSszError, "SSZ list element offset points past the end of the input")
|
2019-08-05 00:00:49 +00:00
|
|
|
else:
|
2020-04-15 01:24:13 +00:00
|
|
|
result[i - 1] = readSszValue(input.toOpenArray(offset, nextOffset - 1), ElemType)
|
2019-07-03 07:35:05 +00:00
|
|
|
offset = nextOffset
|
|
|
|
|
2020-04-15 01:24:13 +00:00
|
|
|
result[resultLen - 1] = readSszValue(input.toOpenArray(offset, input.len - 1), ElemType)
|
2019-07-03 07:35:05 +00:00
|
|
|
|
2020-01-29 00:40:27 +00:00
|
|
|
elif result is SomeInteger|bool|enum|BitArray:
|
|
|
|
trs "READING BASIC TYPE ", type(result).name, " input=", input.len
|
|
|
|
result = fromSszBytes(type(result), input)
|
|
|
|
trs "RESULT WAS ", repr(result)
|
|
|
|
|
2019-07-03 07:35:05 +00:00
|
|
|
elif result is object|tuple:
|
2020-01-24 18:35:15 +00:00
|
|
|
const minimallyExpectedSize = fixedPortionSize(T)
|
|
|
|
if input.len < minimallyExpectedSize:
|
|
|
|
raise newException(MalformedSszError, "SSZ input of insufficient size")
|
|
|
|
|
2019-07-03 07:35:05 +00:00
|
|
|
enumInstanceSerializedFields(result, fieldName, field):
|
|
|
|
const boundingOffsets = T.getFieldBoundingOffsets(fieldName)
|
|
|
|
trs "BOUNDING OFFSET FOR FIELD ", fieldName, " = ", boundingOffsets
|
|
|
|
|
2020-04-29 20:12:07 +00:00
|
|
|
type FieldType = type field
|
2020-04-22 23:35:55 +00:00
|
|
|
type SszType = type toSszType(declval FieldType)
|
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
|
|
|
|
startOffset = readOffset(boundingOffsets[0])
|
|
|
|
endOffset = if boundingOffsets[1] == -1: input.len
|
|
|
|
else: readOffset(boundingOffsets[1])
|
|
|
|
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")
|
|
|
|
elif endOffset > input.len:
|
|
|
|
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:
|
|
|
|
when type(FieldType) is type(SszType):
|
|
|
|
trs "READING NATIVE ", fieldName, ": ", name(SszType)
|
2020-04-29 20:12:07 +00:00
|
|
|
field = readSszValue(
|
2020-04-22 23:35:55 +00:00
|
|
|
input.toOpenArray(startOffset, endOffset - 1),
|
|
|
|
SszType)
|
2019-07-03 07:35:05 +00:00
|
|
|
trs "READING COMPLETE ", fieldName
|
2020-04-22 23:35:55 +00:00
|
|
|
|
2020-05-18 17:49:22 +00:00
|
|
|
elif FieldType is List:
|
|
|
|
# TODO
|
|
|
|
# The `typeof(field)` coercion below is required to deal with a Nim
|
|
|
|
# bug. For some reason, Nim gets confused about the type of the list
|
|
|
|
# returned from the `readSszValue` function. This could be a generics
|
|
|
|
# caching issue caused by the use of distinct types. Such an issue
|
|
|
|
# would be very scary in general, but in this particular situation
|
|
|
|
# it shouldn't matter, because the different flavours of `List[T, N]`
|
|
|
|
# won't produce different serializations.
|
|
|
|
field = typeof(field) readSszValue(
|
2020-04-22 23:35:55 +00:00
|
|
|
input.toOpenArray(startOffset, endOffset - 1),
|
|
|
|
FieldType)
|
|
|
|
|
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-04-22 23:35:55 +00:00
|
|
|
FieldType,
|
|
|
|
input.toOpenArray(startOffset, endOffset - 1))
|
2019-07-03 07:35:05 +00:00
|
|
|
|
|
|
|
else:
|
|
|
|
unsupported T
|