Skip to content

Commit 17f044d

Browse files
committed
rest version
1 parent 34b9253 commit 17f044d

14 files changed

+258
-73
lines changed

beacon_chain/rpc/beacon_rest_api.nim

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,11 @@ proc publishBlock*(body: phase0.SignedBeaconBlock): RestPlainResponse {.
956956
meth: MethodPost.}
957957
## https://ethereum.github.io/eth2.0-APIs/#/Beacon/publishBlock
958958

959+
proc getBlock*(block_id: BlockIdent): RestResponse[DataRestSignedBeaconBlock] {.
960+
rest, endpoint: "/api/eth/v1/beacon/blocks/{block_id}",
961+
meth: MethodGet.}
962+
## https://ethereum.github.io/eth2.0-APIs/#/Beacon/getBlock
963+
959964
proc getStateValidator*(state_id: StateIdent,
960965
validator_id: ValidatorIdent
961966
): RestResponse[DataRestValidator] {.
@@ -971,7 +976,43 @@ proc getStateValidators*(state_id: StateIdent,
971976
meth: MethodGet.}
972977
## https://ethereum.github.io/eth2.0-APIs/#/Beacon/getStateValidators
973978

979+
proc getBlockHeaders*(slot: Option[Slot], parent_root: Option[Eth2Digest]): RestResponse[DataRestBlockHeaders] {.
980+
rest, endpoint: "/api/eth/v1/beacon/headers",
981+
meth: MethodGet.}
982+
## https://ethereum.github.io/eth2.0-APIs/#/Beacon/getBlockHeaders
983+
984+
proc getBlockHeader*(block_id: BlockIdent): RestResponse[DataRestBlockHeaders] {.
985+
rest, endpoint: "/api/eth/v1/beacon/headers/{block_id}",
986+
meth: MethodGet.}
987+
## https://ethereum.github.io/eth2.0-APIs/#/Beacon/getBlockHeader
988+
989+
proc getPoolAttestations*(
990+
slot: Option[Slot],
991+
committee_index: Option[CommitteeIndex]): RestResponse[DataRestPoolAttestations] {.
992+
rest, endpoint: "/api/eth/v1/beacon/pool/attestations",
993+
meth: MethodGet.}
994+
## https://ethereum.github.io/eth2.0-APIs/#/Beacon/getPoolAttestations
995+
974996
proc submitPoolAttestations*(body: seq[Attestation]): RestPlainResponse {.
975997
rest, endpoint: "/eth/v1/beacon/pool/attestations",
976998
meth: MethodPost.}
977999
## https://ethereum.github.io/eth2.0-APIs/#/Beacon/submitPoolAttestations
1000+
1001+
proc getPoolAttesterSlashings*(): RestResponse[DataRestPoolAttesterSlashings] {.
1002+
rest, endpoint: "/api/eth/v1/beacon/pool/attester_slashings",
1003+
meth: MethodGet.}
1004+
## https://ethereum.github.io/eth2.0-APIs/#/Beacon/getPoolAttesterSlashings
1005+
1006+
proc getPoolProposerSlashings*(): RestResponse[DataRestPoolProposerSlashings] {.
1007+
rest, endpoint: "/api/eth/v1/beacon/pool/proposer_slashings",
1008+
meth: MethodGet.}
1009+
## https://ethereum.github.io/eth2.0-APIs/#/Beacon/getPoolProposerSlashings
1010+
1011+
proc getPoolVoluntaryExits*(): RestResponse[DataRestPoolVoluntaryExits] {.
1012+
rest, endpoint: "/api/eth/v1/beacon/pool/voluntary_exits",
1013+
meth: MethodGet.}
1014+
## https://ethereum.github.io/eth2.0-APIs/#/Beacon/getPoolVoluntaryExits
1015+
1016+
proc getStateFinalityCheckpoints*(state_id: StateIdent): RestResponse[DataRestBeaconStateFinalityCheckpoints] {.
1017+
rest, endpoint: "/api/eth/v1/beacon/states/{state_id}/finality_checkpoints",
1018+
meth: MethodGet.}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import
2+
presto/client,
3+
"."/[
4+
beacon_rest_api, config_rest_api, debug_rest_api,
5+
eth2_json_rest_serialization, event_rest_api, nimbus_rest_api,
6+
node_rest_api, validator_rest_api
7+
]
8+
9+
export
10+
client,
11+
beacon_rest_api, config_rest_api, debug_rest_api,
12+
eth2_json_rest_serialization, event_rest_api, nimbus_rest_api,
13+
node_rest_api, validator_rest_api

beacon_chain/rpc/debug_rest_api.nim

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,8 @@ proc installDebugApiHandlers*(router: var RestRouter, node: BeaconNode) =
4242
"/eth/v1/debug/beacon/heads",
4343
"/api/eth/v1/debug/beacon/heads"
4444
)
45+
46+
proc getDebugChainHeads*(): RestResponse[DataRestDebugChainHeads] {.
47+
rest, endpoint: "/eth/v1/debug/beacon/heads",
48+
meth: MethodGet.}
49+
## https://ethereum.github.io/eth2.0-APIs/#/Beacon/getDebugChainHeads

beacon_chain/rpc/eth2_json_rest_serialization.nim

Lines changed: 98 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import
2-
std/[typetraits],
2+
std/[json, typetraits],
33
stew/[results, base10, byteutils, endians2],
44
chronicles, presto,
55
faststreams/[outputs],
@@ -10,7 +10,7 @@ import
1010
../spec/[crypto, digest, datatypes/phase0, eth2_apis/callsigs_types],
1111
../ssz/merkleization,
1212
rest_utils
13-
export json_serialization
13+
export json_serialization, callsigs_types
1414

1515
Json.createFlavor RestJson
1616

@@ -60,14 +60,45 @@ type
6060
status*: string
6161
validator*: Validator
6262

63-
RestVersion* = object
63+
RestBlockHeader* = object
64+
slot*: Slot
65+
proposer_index*: ValidatorIndex
66+
parent_root*: Eth2Digest
67+
state_root*: Eth2Digest
68+
body_root*: Eth2Digest
69+
70+
RestSignedBlockHeader* = object
71+
message*: RestBlockHeader
72+
signature*: ValidatorSig
73+
74+
RestBlockHeaderInfo* = object
75+
root*: Eth2Digest
76+
canonical*: bool
77+
header*: RestSignedBlockHeader
78+
79+
RestNodeVersion* = object
6480
version*: string
6581

6682
RestSyncInfo* = object
6783
head_slot*: Slot
6884
sync_distance*: uint64
6985
is_syncing*: bool
7086

87+
RestChainHead* = object
88+
root*: Eth2Digest
89+
slot*: Slot
90+
91+
RestMetadata* = object
92+
seq_number*: string
93+
attnets*: string
94+
95+
RestNetworkIdentity* = object
96+
peer_id*: string
97+
enr*: string
98+
p2p_addresses*: seq[string]
99+
discovery_addresses*: seq[string]
100+
metadata*: RestMetadata
101+
71102
RestConfig* = object
72103
CONFIG_NAME*: string
73104
MAX_COMMITTEES_PER_SLOT*: uint64
@@ -143,33 +174,66 @@ type
143174
DataEnclosedObject*[T] = object
144175
data*: T
145176

177+
DataMetaEnclosedObject*[T] = object
178+
data*: T
179+
meta*: JsonNode
180+
146181
DataRootEnclosedObject*[T] = object
147182
dependent_root*: Eth2Digest
148183
data*: T
149184

185+
DataRestAttestation* = DataEnclosedObject[Attestation]
186+
DataRestAttestationData* = DataEnclosedObject[AttestationData]
187+
DataRestAttesterDuties* = DataRootEnclosedObject[seq[RestAttesterDuty]]
188+
DataRestBeaconBlock* = DataEnclosedObject[phase0.BeaconBlock]
150189
DataRestBeaconGenesis* = DataEnclosedObject[RestBeaconGenesis]
190+
DataRestBeaconStateFinalityCheckpoints* = DataEnclosedObject[BeaconStatesFinalityCheckpointsTuple]
191+
DataRestBlockHeaders* = DataEnclosedObject[RestBlockHeaderInfo]
192+
DataRestConfig* = DataEnclosedObject[RestConfig]
193+
DataRestDebugChainHeads* = DataEnclosedObject[seq[RestChainHead]]
151194
DataRestFork* = DataEnclosedObject[Fork]
195+
DataRestNetworkIdentity* = DataEnclosedObject[RestNetworkIdentity]
196+
DataRestNodeVersion* = DataEnclosedObject[RestNodeVersion]
197+
DataRestPeers* = DataMetaEnclosedObject[seq[NodePeerTuple]]
198+
DataRestPoolAttestations* = DataEnclosedObject[seq[Attestation]]
199+
DataRestPoolAttesterSlashings* = DataEnclosedObject[seq[AttesterSlashing]]
200+
DataRestPoolProposerSlashings* = DataEnclosedObject[seq[ProposerSlashing]]
201+
DataRestPoolVoluntaryExits* = DataEnclosedObject[seq[VoluntaryExit]]
152202
DataRestProposerDuties* = DataRootEnclosedObject[seq[RestProposerDuty]]
153-
DataRestAttesterDuties* = DataRootEnclosedObject[seq[RestAttesterDuty]]
154-
DataRestBeaconBlock* = DataEnclosedObject[phase0.BeaconBlock]
155-
DataRestAttestationData* = DataEnclosedObject[AttestationData]
156-
DataRestAttestation* = DataEnclosedObject[Attestation]
203+
DataRestSignedBeaconBlock* = DataEnclosedObject[phase0.SignedBeaconBlock]
157204
DataRestSyncInfo* = DataEnclosedObject[RestSyncInfo]
158205
DataRestValidator* = DataEnclosedObject[RestValidator]
159206
DataRestValidatorList* = DataEnclosedObject[seq[RestValidator]]
160-
DataRestVersion* = DataEnclosedObject[RestVersion]
161-
DataRestConfig* = DataEnclosedObject[RestConfig]
162207

163208
EncodeTypes* = phase0.SignedBeaconBlock
164209
EncodeArrays* = seq[ValidatorIndex] | seq[Attestation] |
165210
seq[SignedAggregateAndProof] | seq[RestCommitteeSubscription]
166211

167-
DecodeTypes* = DataRestBeaconGenesis | DataRestFork | DataRestProposerDuties |
168-
DataRestAttesterDuties | DataRestBeaconBlock |
169-
DataRestAttestationData | DataRestAttestation |
170-
DataRestSyncInfo | DataRestValidator |
171-
DataRestValidatorList | DataRestVersion |
172-
DataRestConfig | RestGenericError | RestAttestationError
212+
DecodeTypes* =
213+
DataRestAttestation |
214+
DataRestAttestationData |
215+
DataRestAttesterDuties |
216+
DataRestBeaconBlock |
217+
DataRestBeaconGenesis |
218+
DataRestBeaconStateFinalityCheckpoints |
219+
DataRestBlockHeaders |
220+
DataRestConfig |
221+
DataRestDebugChainHeads |
222+
DataRestFork |
223+
DataRestNetworkIdentity |
224+
DataRestNodeVersion |
225+
DataRestPeers |
226+
DataRestPoolAttestations |
227+
DataRestPoolAttesterSlashings |
228+
DataRestPoolProposerSlashings |
229+
DataRestPoolVoluntaryExits |
230+
DataRestProposerDuties |
231+
DataRestSignedBeaconBlock |
232+
DataRestSyncInfo |
233+
DataRestValidator |
234+
DataRestValidatorList |
235+
RestAttestationError |
236+
RestGenericError
173237

174238
proc jsonResponseWRoot*(t: typedesc[RestApiResponse],
175239
data: auto,
@@ -620,12 +684,10 @@ proc decodeBytes*[T: DecodeTypes](t: typedesc[T], value: openarray[byte],
620684
contentType: string): RestResult[T] =
621685
case contentType
622686
of "application/json":
623-
let res =
624-
try:
625-
RestJson.decode(value, T)
626-
except SerializationError:
627-
return err("Serialization error")
628-
ok(res)
687+
try:
688+
ok RestJson.decode(value, T)
689+
except SerializationError as exc:
690+
err("Serialization error")
629691
else:
630692
err("Content-Type not supported")
631693

@@ -667,3 +729,18 @@ proc encodeString*(value: StateIdent): RestResult[string] =
667729
ok("finalized")
668730
of StateIdentType.Justified:
669731
ok("justified")
732+
733+
proc encodeString*(value: BlockIdent): RestResult[string] =
734+
case value.kind
735+
of BlockQueryKind.Slot:
736+
ok(Base10.toString(uint64(value.slot)))
737+
of BlockQueryKind.Root:
738+
ok(hexOriginal(value.root.data))
739+
of BlockQueryKind.Named:
740+
case value.value
741+
of BlockIdentType.Head:
742+
ok("head")
743+
of BlockIdentType.Genesis:
744+
ok("genesis")
745+
of BlockIdentType.Finalized:
746+
ok("finalized")

beacon_chain/rpc/node_rest_api.nim

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,12 +292,29 @@ proc installNodeApiHandlers*(router: var RestRouter, node: BeaconNode) =
292292
"/api/eth/v1/node/health"
293293
)
294294

295+
proc getPeers*(
296+
state: seq[PeerStateKind],
297+
direction: seq[PeerDirectKind]): RestResponse[DataRestPeers] {.
298+
rest, endpoint: "/eth/v1/node/peers",
299+
meth: MethodGet.}
300+
## https://ethereum.github.io/eth2.0-APIs/#/Node/getPeers
301+
295302
proc getSyncingStatus*(): RestResponse[DataRestSyncInfo] {.
296303
rest, endpoint: "/eth/v1/node/syncing",
297304
meth: MethodGet.}
298305
## https://ethereum.github.io/eth2.0-APIs/#/Node/getSyncingStatus
299306

300-
proc getVersion*(): RestResponse[DataRestVersion] {.
307+
proc getNetworkIdentity*(): RestResponse[DataRestNetworkIdentity] {.
308+
rest, endpoint: "/eth/v1/node/identity",
309+
meth: MethodGet.}
310+
## https://ethereum.github.io/eth2.0-APIs/#/Node/getNetworkIdentity
311+
312+
proc getNodeVersion*(): RestResponse[DataRestNodeVersion] {.
301313
rest, endpoint: "/eth/v1/node/version",
302314
meth: MethodGet.}
303315
## https://ethereum.github.io/eth2.0-APIs/#/Node/getNodeVersion
316+
317+
proc getHealth*(): RestPlainResponse {.
318+
rest, endpoint: "/eth/v1/node/health",
319+
meth: MethodGet.}
320+
## https://ethereum.github.io/eth2.0-APIs/#/Node/getHealth

beacon_chain/rpc/rest_utils.nim

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,17 @@ proc decodeString*(t: typedesc[PeerStateKind],
419419
else:
420420
err("Incorrect peer's state value")
421421

422+
proc encodeString*(value: PeerStateKind): Result[string, cstring] =
423+
case value
424+
of PeerStateKind.Disconnected:
425+
ok("disconnected")
426+
of PeerStateKind.Connecting:
427+
ok("connecting")
428+
of PeerStateKind.Connected:
429+
ok("connected")
430+
of PeerStateKind.Disconnecting:
431+
ok("disconnecting")
432+
422433
proc decodeString*(t: typedesc[PeerDirectKind],
423434
value: string): Result[PeerDirectKind, cstring] =
424435
case value
@@ -429,6 +440,13 @@ proc decodeString*(t: typedesc[PeerDirectKind],
429440
else:
430441
err("Incorrect peer's direction value")
431442

443+
proc encodeString*(value: PeerDirectKind): Result[string, cstring] =
444+
case value
445+
of PeerDirectKind.Inbound:
446+
ok("inbound")
447+
of PeerDirectKind.Outbound:
448+
ok("outbound")
449+
432450
proc decodeString*(t: typedesc[EventTopic],
433451
value: string): Result[EventTopic, cstring] =
434452
case value
@@ -579,6 +597,15 @@ proc init*(t: typedesc[StateIdent], v: Slot): StateIdent =
579597
proc init*(t: typedesc[StateIdent], v: Eth2Digest): StateIdent =
580598
StateIdent(kind: StateQueryKind.Root, root: v)
581599

600+
proc init*(t: typedesc[BlockIdent], v: BlockIdentType): BlockIdent =
601+
BlockIdent(kind: BlockQueryKind.Named, value: v)
602+
603+
proc init*(t: typedesc[BlockIdent], v: Slot): BlockIdent =
604+
BlockIdent(kind: BlockQueryKind.Slot, slot: v)
605+
606+
proc init*(t: typedesc[BlockIdent], v: Eth2Digest): BlockIdent =
607+
BlockIdent(kind: BlockQueryKind.Root, root: v)
608+
582609
proc init*(t: typedesc[ValidatorIdent], v: ValidatorIndex): ValidatorIdent =
583610
ValidatorIdent(kind: ValidatorQueryKind.Index, index: RestValidatorIndex(v))
584611

ngui/attestationlist.nim

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import
22
std/[tables],
33
NimQml,
4-
../beacon_chain/spec/[helpers, eth2_apis/beacon_rpc_client],
4+
../beacon_chain/rpc/beacon_rest_client,
5+
../beacon_chain/spec/[datatypes, helpers],
56
../beacon_chain/ssz/merkleization,
67
./objecttablemodel, ./utils
78

ngui/blockmodel.nim

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import NimQml
22

33
import
4-
std/[sequtils, json, times],
4+
std/[sequtils, times],
55
NimQml,
6-
../beacon_chain/eth2_json_rpc_serialization,
6+
../beacon_chain/rpc/beacon_rest_client,
77
../beacon_chain/spec/[datatypes, crypto],
88
./attestationlist, ./depositlist, ./utils
99

@@ -59,16 +59,16 @@ QtObject:
5959
proc randao_reveal*(self: BlockModel): string {.slot.} = toDisplayHex(self.blck.message.body.randao_reveal)
6060
QtProperty[string] randao_reveal: read = randao_reveal
6161

62-
proc eth1_data*(self: BlockModel): string {.slot.} = (%*self.blck.message.body.eth1_data).pretty()
62+
proc eth1_data*(self: BlockModel): string {.slot.} = RestJson.encode(self.blck.message.body.eth1_data, pretty=true)
6363
QtProperty[string] eth1_data: read = eth1_data
6464

6565
proc graffiti*(self: BlockModel): string {.slot.} = $self.blck.message.body.graffiti
6666
QtProperty[string] graffiti: read = graffiti
6767

68-
proc proposer_slashings*(self: BlockModel): string {.slot.} = (%*self.blck.message.body.proposer_slashings.asSeq()).pretty()
68+
proc proposer_slashings*(self: BlockModel): string {.slot.} = RestJson.encode(self.blck.message.body.proposer_slashings, pretty=true)
6969
QtProperty[string] proposer_slashings: read = proposer_slashings
7070

71-
proc attester_slashings*(self: BlockModel): string {.slot.} = (%*self.blck.message.body.attester_slashings.asSeq()).pretty()
71+
proc attester_slashings*(self: BlockModel): string {.slot.} = RestJson.encode(self.blck.message.body.attester_slashings.asSeq(), pretty=true)
7272
QtProperty[string] attester_slashings: read = attester_slashings
7373

7474
proc attestations*(self: BlockModel): QVariant {.slot.} = newQVariant(self.attestationsx)
@@ -77,7 +77,7 @@ QtObject:
7777
proc deposits*(self: BlockModel): QVariant {.slot.} = newQVariant(self.depositsx)
7878
QtProperty[QVariant] deposits: read = deposits
7979

80-
proc voluntary_exits*(self: BlockModel): string {.slot.} = (%*self.blck.message.body.voluntary_exits.asSeq()).pretty()
80+
proc voluntary_exits*(self: BlockModel): string {.slot.} = RestJson.encode(self.blck.message.body.voluntary_exits.asSeq(), pretty=true)
8181
QtProperty[string] voluntary_exits: read = voluntary_exits
8282

8383
proc signature*(self: BlockModel): string {.slot.} = toDisplayHex(self.blck.signature)

ngui/depositlist.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import
22
std/[tables],
33
NimQml,
4-
../beacon_chain/spec/[eth2_apis/beacon_rpc_client],
4+
../beacon_chain/spec/datatypes,
55
./objecttablemodel, ./utils
66

77
type

0 commit comments

Comments
 (0)