2021-03-23 22:50:18 +00:00
|
|
|
# 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.
|
|
|
|
|
|
|
|
import
|
|
|
|
stew/results,
|
|
|
|
chronicles,
|
|
|
|
presto,
|
2021-08-03 15:17:11 +00:00
|
|
|
./rest_utils,
|
2021-03-23 22:50:18 +00:00
|
|
|
../beacon_node_common
|
|
|
|
|
|
|
|
logScope: topics = "rest_eventapi"
|
|
|
|
|
|
|
|
proc validateEventTopics(events: seq[EventTopic]): Result[EventTopics,
|
|
|
|
cstring] =
|
|
|
|
const NonUniqueError = cstring("Event topics must be unique")
|
|
|
|
var res: set[EventTopic]
|
|
|
|
for item in events:
|
|
|
|
case item
|
|
|
|
of EventTopic.Head:
|
|
|
|
if EventTopic.Head in res:
|
|
|
|
return err(NonUniqueError)
|
|
|
|
res.incl(EventTopic.Head)
|
|
|
|
of EventTopic.Block:
|
|
|
|
if EventTopic.Block in res:
|
|
|
|
return err(NonUniqueError)
|
|
|
|
res.incl(EventTopic.Block)
|
|
|
|
of EventTopic.Attestation:
|
|
|
|
if EventTopic.Attestation in res:
|
|
|
|
return err(NonUniqueError)
|
|
|
|
res.incl(EventTopic.Attestation)
|
|
|
|
of EventTopic.VoluntaryExit:
|
|
|
|
if EventTopic.VoluntaryExit in res:
|
|
|
|
return err(NonUniqueError)
|
|
|
|
res.incl(EventTopic.VoluntaryExit)
|
|
|
|
of EventTopic.FinalizedCheckpoint:
|
|
|
|
if EventTopic.FinalizedCheckpoint in res:
|
|
|
|
return err(NonUniqueError)
|
|
|
|
res.incl(EventTopic.FinalizedCheckpoint)
|
|
|
|
of EventTopic.ChainReorg:
|
|
|
|
if EventTopic.ChainReorg in res:
|
|
|
|
return err(NonUniqueError)
|
|
|
|
res.incl(EventTopic.ChainReorg)
|
|
|
|
if res == {}:
|
|
|
|
err("Empty topics list")
|
|
|
|
else:
|
|
|
|
ok(res)
|
|
|
|
|
|
|
|
proc installEventApiHandlers*(router: var RestRouter, node: BeaconNode) =
|
|
|
|
router.api(MethodGet, "/api/eth/v1/events") do (
|
|
|
|
topics: seq[EventTopic]) -> RestApiResponse:
|
2021-03-29 10:59:39 +00:00
|
|
|
# TODO (cheatfate): This call is not fully implemented yet, because there
|
|
|
|
# missing infrastructure to raise/catch global events (eventbus).
|
2021-03-23 22:50:18 +00:00
|
|
|
let eventTopics =
|
|
|
|
block:
|
|
|
|
if topics.isErr():
|
|
|
|
return RestApiResponse.jsonError(Http400, "Invalid topics value",
|
|
|
|
$topics.error())
|
|
|
|
let res = validateEventTopics(topics.get())
|
|
|
|
if res.isErr():
|
|
|
|
return RestApiResponse.jsonError(Http400, "Invalid topics value",
|
|
|
|
$res.error())
|
|
|
|
res.get()
|
|
|
|
|
|
|
|
return RestApiResponse.jsonError(Http500, "Not implemented yet")
|
2021-04-13 10:19:31 +00:00
|
|
|
|
|
|
|
router.redirect(
|
|
|
|
MethodGet,
|
|
|
|
"/eth/v1/events",
|
|
|
|
"/api/eth/v1/events"
|
|
|
|
)
|