split SSZ and JSON parsing logic in LC handler (#5055)
Use separate functions per format when parsing LC data from REST. This allows to process events from the eventstream more directly, as they are always JSON not SSZ. And also makes the code cleaner.
This commit is contained in:
parent
54cc7bb7a1
commit
6671965fd9
|
@ -38,19 +38,11 @@ func checkForkConsistency(
|
|||
consensusFork: ConsensusFork) {.raises: [RestError].} =
|
||||
obj.checkForkConsistency(cfg, Opt[ConsensusFork].ok(consensusFork))
|
||||
|
||||
proc decodeHttpLightClientObject[T: SomeForkedLightClientObject](
|
||||
func decodeSszLightClientObject[T: SomeForkedLightClientObject](
|
||||
x: typedesc[T],
|
||||
data: seq[byte],
|
||||
contentType: Opt[ContentTypeData],
|
||||
data: openArray[byte],
|
||||
consensusFork: ConsensusFork,
|
||||
cfg: RuntimeConfig): T {.raises: [RestError].} =
|
||||
let mediaTypeRes = decodeMediaType(contentType)
|
||||
if mediaTypeRes.isErr:
|
||||
raise newException(RestError, mediaTypeRes.error)
|
||||
template mediaType: auto = mediaTypeRes.get
|
||||
|
||||
return
|
||||
if mediaType == OctetStreamMediaType:
|
||||
try:
|
||||
withLcDataFork(lcDataForkAtConsensusFork(consensusFork)):
|
||||
when lcDataFork > LightClientDataFork.None:
|
||||
|
@ -64,30 +56,47 @@ proc decodeHttpLightClientObject[T: SomeForkedLightClientObject](
|
|||
except SszError as exc:
|
||||
raiseRestDecodingBytesError(cstring("Malformed data: " & $exc.msg))
|
||||
|
||||
elif mediaType == ApplicationJsonMediaType:
|
||||
let objRes = decodeBytes(T, data, contentType)
|
||||
proc decodeJsonLightClientObject[T: SomeForkedLightClientObject](
|
||||
x: typedesc[T],
|
||||
data: openArray[byte],
|
||||
consensusFork: Opt[ConsensusFork],
|
||||
cfg: RuntimeConfig): T {.raises: [RestError].} =
|
||||
let objRes = decodeBytes(T, data, Opt.none(ContentTypeData))
|
||||
if objRes.isErr:
|
||||
raiseRestDecodingBytesError(objRes.error)
|
||||
template obj: auto = objRes.get
|
||||
obj.checkForkConsistency(cfg, consensusFork)
|
||||
obj
|
||||
|
||||
proc decodeHttpLightClientObject*[T: SomeForkedLightClientObject](
|
||||
x: typedesc[T],
|
||||
data: openArray[byte],
|
||||
mediaType: MediaType,
|
||||
consensusFork: ConsensusFork,
|
||||
cfg: RuntimeConfig): T {.raises: [RestError].} =
|
||||
if mediaType == OctetStreamMediaType:
|
||||
x.decodeSszLightClientObject(data, consensusFork, cfg)
|
||||
elif mediaType == ApplicationJsonMediaType:
|
||||
x.decodeJsonLightClientObject(data, Opt.some(consensusFork), cfg)
|
||||
else:
|
||||
raise newException(RestError, "Unsupported content-type")
|
||||
|
||||
proc decodeHttpLightClientObjects[S: seq[SomeForkedLightClientObject]](
|
||||
x: typedesc[S],
|
||||
data: seq[byte],
|
||||
proc decodeHttpLightClientObject[T: SomeForkedLightClientObject](
|
||||
x: typedesc[T],
|
||||
data: openArray[byte],
|
||||
contentType: Opt[ContentTypeData],
|
||||
cfg: RuntimeConfig,
|
||||
forkDigests: ref ForkDigests): S {.raises: [RestError].} =
|
||||
consensusFork: ConsensusFork,
|
||||
cfg: RuntimeConfig): T {.raises: [RestError].} =
|
||||
let mediaTypeRes = decodeMediaType(contentType)
|
||||
if mediaTypeRes.isErr:
|
||||
raise newException(RestError, mediaTypeRes.error)
|
||||
template mediaType: auto = mediaTypeRes.get
|
||||
x.decodeHttpLightClientObject(data, mediaTypeRes.get, consensusFork, cfg)
|
||||
|
||||
return
|
||||
if mediaType == OctetStreamMediaType:
|
||||
proc decodeSszLightClientObjects[S: seq[SomeForkedLightClientObject]](
|
||||
x: typedesc[S],
|
||||
data: openArray[byte],
|
||||
cfg: RuntimeConfig,
|
||||
forkDigests: ref ForkDigests): S {.raises: [RestError].} =
|
||||
let l = data.len
|
||||
var
|
||||
res: S
|
||||
|
@ -140,8 +149,12 @@ proc decodeHttpLightClientObjects[S: seq[SomeForkedLightClientObject]](
|
|||
raiseRestDecodingBytesError(cstring("Malformed data: " & $exc.msg))
|
||||
res
|
||||
|
||||
elif mediaType == ApplicationJsonMediaType:
|
||||
let objsRes = decodeBytes(S, data, contentType)
|
||||
proc decodeJsonLightClientObjects[S: seq[SomeForkedLightClientObject]](
|
||||
x: typedesc[S],
|
||||
data: openArray[byte],
|
||||
cfg: RuntimeConfig,
|
||||
forkDigests: ref ForkDigests): S {.raises: [RestError].} =
|
||||
let objsRes = decodeBytes(S, data, Opt.none(ContentTypeData))
|
||||
if objsRes.isErr:
|
||||
raiseRestDecodingBytesError(objsRes.error)
|
||||
template objs: auto = objsRes.get
|
||||
|
@ -149,9 +162,30 @@ proc decodeHttpLightClientObjects[S: seq[SomeForkedLightClientObject]](
|
|||
obj.checkForkConsistency(cfg)
|
||||
objs
|
||||
|
||||
proc decodeHttpLightClientObjects*[S: seq[SomeForkedLightClientObject]](
|
||||
x: typedesc[S],
|
||||
data: openArray[byte],
|
||||
mediaType: MediaType,
|
||||
cfg: RuntimeConfig,
|
||||
forkDigests: ref ForkDigests): S {.raises: [RestError].} =
|
||||
if mediaType == OctetStreamMediaType:
|
||||
x.decodeSszLightClientObjects(data, cfg, forkDigests)
|
||||
elif mediaType == ApplicationJsonMediaType:
|
||||
x.decodeJsonLightClientObjects(data, cfg, forkDigests)
|
||||
else:
|
||||
raise newException(RestError, "Unsupported content-type")
|
||||
|
||||
proc decodeHttpLightClientObjects[S: seq[SomeForkedLightClientObject]](
|
||||
x: typedesc[S],
|
||||
data: openArray[byte],
|
||||
contentType: Opt[ContentTypeData],
|
||||
cfg: RuntimeConfig,
|
||||
forkDigests: ref ForkDigests): S {.raises: [RestError].} =
|
||||
let mediaTypeRes = decodeMediaType(contentType)
|
||||
if mediaTypeRes.isErr:
|
||||
raise newException(RestError, mediaTypeRes.error)
|
||||
x.decodeHttpLightClientObjects(data, mediaTypeRes.get, cfg, forkDigests)
|
||||
|
||||
proc getLightClientBootstrapPlain(
|
||||
block_root: Eth2Digest): RestHttpResponseRef {.
|
||||
rest, endpoint: "/eth/v1/beacon/light_client/bootstrap/{block_root}",
|
||||
|
|
Loading…
Reference in New Issue