2020-11-27 22:16:13 +00:00
|
|
|
# beacon_chain
|
|
|
|
# Copyright (c) 2018-2020 Status Research & Development GmbH
|
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2020-04-22 05:53:02 +00:00
|
|
|
{.push raises: [Defect].}
|
|
|
|
{.pragma: raisesssz, raises: [Defect, IOError, MalformedSszError, SszSizeMismatchError].}
|
|
|
|
|
2019-11-01 15:14:34 +00:00
|
|
|
import
|
2020-11-27 22:16:13 +00:00
|
|
|
std/[strutils, parseutils],
|
2020-05-06 12:31:05 +00:00
|
|
|
stew/objects, faststreams/outputs, json_serialization/writer,
|
2019-11-09 10:46:34 +00:00
|
|
|
../spec/datatypes,
|
2020-06-03 13:52:02 +00:00
|
|
|
./bytes_reader, ./types, ./navigator, ./spec_types
|
2019-11-01 15:14:34 +00:00
|
|
|
|
2019-11-09 10:46:34 +00:00
|
|
|
export
|
2020-06-03 13:52:02 +00:00
|
|
|
bytes_reader, navigator, types
|
2019-11-09 10:46:34 +00:00
|
|
|
|
2019-11-01 15:14:34 +00:00
|
|
|
type
|
|
|
|
ObjKind = enum
|
|
|
|
Record
|
|
|
|
Indexable
|
|
|
|
LeafValue
|
|
|
|
|
|
|
|
FieldInfo = ref object
|
|
|
|
name: string
|
|
|
|
fieldType: TypeInfo
|
|
|
|
navigator: proc (m: MemRange): MemRange {. gcsafe
|
|
|
|
noSideEffect
|
2020-04-22 05:53:02 +00:00
|
|
|
raisesssz }
|
2019-11-01 15:14:34 +00:00
|
|
|
TypeInfo = ref object
|
|
|
|
case kind: ObjKind
|
|
|
|
of Record:
|
|
|
|
fields: seq[FieldInfo]
|
|
|
|
of Indexable:
|
|
|
|
elemType: TypeInfo
|
|
|
|
navigator: proc (m: MemRange, idx: int): MemRange {. gcsafe
|
|
|
|
noSideEffect
|
2020-04-22 05:53:02 +00:00
|
|
|
raisesssz }
|
2019-11-01 15:14:34 +00:00
|
|
|
else:
|
|
|
|
discard
|
|
|
|
|
|
|
|
jsonPrinter: proc (m: MemRange,
|
2020-04-09 18:36:00 +00:00
|
|
|
outStream: OutputStream,
|
2020-04-22 05:53:02 +00:00
|
|
|
pretty: bool) {.gcsafe, raisesssz.}
|
2019-11-01 15:14:34 +00:00
|
|
|
|
|
|
|
DynamicSszNavigator* = object
|
|
|
|
m: MemRange
|
|
|
|
typ: TypeInfo
|
|
|
|
|
2020-04-22 05:53:02 +00:00
|
|
|
proc jsonPrinterImpl[T](m: MemRange, outStream: OutputStream, pretty: bool) {.raisesssz.} =
|
2019-11-01 15:14:34 +00:00
|
|
|
var typedNavigator = sszMount(m, T)
|
|
|
|
var jsonWriter = init(JsonWriter, outStream, pretty)
|
|
|
|
# TODO: it should be possible to serialize the navigator object
|
|
|
|
# without dereferencing it (to avoid the intermediate value).
|
|
|
|
writeValue(jsonWriter, typedNavigator[])
|
|
|
|
|
|
|
|
func findField(fields: seq[FieldInfo], name: string): FieldInfo =
|
|
|
|
# TODO: Replace this with a binary search?
|
|
|
|
# Will it buy us anything when there are only few fields?
|
|
|
|
for field in fields:
|
|
|
|
if field.name == name:
|
|
|
|
return field
|
|
|
|
|
2020-04-22 05:53:02 +00:00
|
|
|
func indexableNavigatorImpl[T](m: MemRange, idx: int): MemRange {.raisesssz.} =
|
2019-11-01 15:14:34 +00:00
|
|
|
var typedNavigator = sszMount(m, T)
|
|
|
|
getMemRange(typedNavigator[idx])
|
|
|
|
|
|
|
|
func fieldNavigatorImpl[RecordType; FieldType;
|
2020-04-22 05:53:02 +00:00
|
|
|
fieldName: static string](m: MemRange): MemRange {.raisesssz.} =
|
2019-11-01 15:14:34 +00:00
|
|
|
# TODO: Make sure this doesn't fail with a Defect when
|
|
|
|
# navigating to an inactive field in a case object.
|
|
|
|
var typedNavigator = sszMount(m, RecordType)
|
|
|
|
getMemRange navigateToField(typedNavigator, fieldName, FieldType)
|
|
|
|
|
|
|
|
func genTypeInfo(T: type): TypeInfo {.gcsafe.}
|
|
|
|
|
|
|
|
proc typeInfo*(T: type): TypeInfo =
|
|
|
|
let res {.global.} = genTypeInfo(T)
|
|
|
|
|
|
|
|
# TODO This will be safer if the RTTI object use only manually
|
|
|
|
# managed memory, but the `fields` sequence right now make
|
|
|
|
# things harder. We'll need to switch to a different seq type.
|
|
|
|
{.gcsafe, noSideEffect.}: res
|
|
|
|
|
|
|
|
func genTypeInfo(T: type): TypeInfo =
|
2019-11-09 10:46:34 +00:00
|
|
|
mixin toSszType, enumAllSerializedFields
|
2020-04-22 23:35:55 +00:00
|
|
|
type SszType = type toSszType(declval T)
|
2019-11-09 10:46:34 +00:00
|
|
|
result = when type(SszType) isnot T:
|
|
|
|
TypeInfo(kind: LeafValue)
|
|
|
|
elif T is object:
|
2019-11-01 15:14:34 +00:00
|
|
|
var fields: seq[FieldInfo]
|
|
|
|
enumAllSerializedFields(T):
|
|
|
|
fields.add FieldInfo(name: fieldName,
|
|
|
|
fieldType: typeInfo(FieldType),
|
|
|
|
navigator: fieldNavigatorImpl[T, FieldType, fieldName])
|
|
|
|
TypeInfo(kind: Record, fields: fields)
|
|
|
|
elif T is seq|array:
|
|
|
|
TypeInfo(kind: Indexable,
|
|
|
|
elemType: typeInfo(ElemType(T)),
|
|
|
|
navigator: indexableNavigatorImpl[T])
|
|
|
|
else:
|
|
|
|
TypeInfo(kind: LeafValue)
|
|
|
|
|
|
|
|
result.jsonPrinter = jsonPrinterImpl[T]
|
|
|
|
|
2020-04-22 05:53:02 +00:00
|
|
|
func `[]`*(n: DynamicSszNavigator, idx: int): DynamicSszNavigator {.raisesssz.} =
|
2019-11-01 15:14:34 +00:00
|
|
|
doAssert n.typ.kind == Indexable
|
|
|
|
DynamicSszNavigator(m: n.typ.navigator(n.m, idx), typ: n.typ.elemType)
|
|
|
|
|
|
|
|
func navigate*(n: DynamicSszNavigator, path: string): DynamicSszNavigator {.
|
2020-04-22 05:53:02 +00:00
|
|
|
raises: [Defect, KeyError, IOError, MalformedSszError, SszSizeMismatchError, ValueError] .} =
|
2019-11-01 15:14:34 +00:00
|
|
|
case n.typ.kind
|
|
|
|
of Record:
|
|
|
|
let fieldInfo = n.typ.fields.findField(path)
|
|
|
|
if fieldInfo == nil:
|
|
|
|
raise newException(KeyError, "Unrecogned field name: " & path)
|
|
|
|
return DynamicSszNavigator(m: fieldInfo.navigator(n.m),
|
|
|
|
typ: fieldInfo.fieldType)
|
|
|
|
of Indexable:
|
|
|
|
var idx: int
|
|
|
|
let consumed = parseInt(path, idx)
|
|
|
|
if consumed == 0 or idx < 0:
|
2019-11-09 10:46:34 +00:00
|
|
|
raise newException(KeyError, "Indexing should be done with natural numbers")
|
2019-11-01 15:14:34 +00:00
|
|
|
return n[idx]
|
|
|
|
else:
|
|
|
|
doAssert false, "Navigation should be terminated once you reach a leaf value"
|
|
|
|
|
2019-11-09 10:46:34 +00:00
|
|
|
template navigatePathImpl(nav, iterabalePathFragments: untyped) =
|
|
|
|
result = nav
|
|
|
|
for pathFragment in iterabalePathFragments:
|
|
|
|
if pathFragment.len == 0:
|
|
|
|
continue
|
|
|
|
result = result.navigate(pathFragment)
|
|
|
|
if result.typ.kind == LeafValue:
|
|
|
|
return
|
|
|
|
|
|
|
|
func navigatePath*(n: DynamicSszNavigator, path: string): DynamicSszNavigator {.
|
2020-04-22 05:53:02 +00:00
|
|
|
raises: [Defect, IOError, ValueError, MalformedSszError, SszSizeMismatchError] .} =
|
2019-11-09 10:46:34 +00:00
|
|
|
navigatePathImpl n, split(path, '/')
|
|
|
|
|
2020-10-28 18:35:31 +00:00
|
|
|
func navigatePath*(n: DynamicSszNavigator, path: openArray[string]): DynamicSszNavigator {.
|
2020-04-22 05:53:02 +00:00
|
|
|
raises: [Defect, IOError, ValueError, MalformedSszError, SszSizeMismatchError] .} =
|
2019-11-09 10:46:34 +00:00
|
|
|
navigatePathImpl n, path
|
|
|
|
|
|
|
|
func init*(T: type DynamicSszNavigator,
|
2020-10-28 18:35:31 +00:00
|
|
|
bytes: openArray[byte], Navigated: type): T =
|
2019-11-09 10:46:34 +00:00
|
|
|
T(m: MemRange(startAddr: unsafeAddr bytes[0], length: bytes.len),
|
|
|
|
typ: typeInfo(Navigated))
|
2019-11-01 15:14:34 +00:00
|
|
|
|
2020-04-22 05:53:02 +00:00
|
|
|
proc writeJson*(n: DynamicSszNavigator, outStream: OutputStream, pretty = true) {.raisesssz.} =
|
2019-11-01 15:14:34 +00:00
|
|
|
n.typ.jsonPrinter(n.m, outStream, pretty)
|
|
|
|
|
2020-04-22 05:53:02 +00:00
|
|
|
func toJson*(n: DynamicSszNavigator, pretty = true): string {.raisesssz.} =
|
2020-04-09 18:36:00 +00:00
|
|
|
var outStream = memoryOutput()
|
|
|
|
{.noSideEffect.}:
|
|
|
|
# We are assuming that there are no side-effects here
|
|
|
|
# because we are using a `memoryOutput`. The computed
|
|
|
|
# side-effects are coming from the fact that the dynamic
|
|
|
|
# dispatch mechanisms used in faststreams may be reading
|
|
|
|
# from a file or a network device.
|
|
|
|
writeJson(n, outStream, pretty)
|
2019-11-01 15:14:34 +00:00
|
|
|
outStream.getOutput(string)
|