|
1 | 1 | import
|
2 |
| - std/[typetraits], |
| 2 | + std/[json, typetraits], |
3 | 3 | stew/[results, base10, byteutils, endians2],
|
4 | 4 | chronicles, presto,
|
5 | 5 | faststreams/[outputs],
|
|
10 | 10 | ../spec/[crypto, digest, datatypes/phase0, eth2_apis/callsigs_types],
|
11 | 11 | ../ssz/merkleization,
|
12 | 12 | rest_utils
|
13 |
| -export json_serialization |
| 13 | +export json_serialization, callsigs_types |
14 | 14 |
|
15 | 15 | Json.createFlavor RestJson
|
16 | 16 |
|
|
60 | 60 | status*: string
|
61 | 61 | validator*: Validator
|
62 | 62 |
|
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 |
64 | 80 | version*: string
|
65 | 81 |
|
66 | 82 | RestSyncInfo* = object
|
67 | 83 | head_slot*: Slot
|
68 | 84 | sync_distance*: uint64
|
69 | 85 | is_syncing*: bool
|
70 | 86 |
|
| 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 | + |
71 | 102 | RestConfig* = object
|
72 | 103 | CONFIG_NAME*: string
|
73 | 104 | MAX_COMMITTEES_PER_SLOT*: uint64
|
@@ -143,33 +174,66 @@ type
|
143 | 174 | DataEnclosedObject*[T] = object
|
144 | 175 | data*: T
|
145 | 176 |
|
| 177 | + DataMetaEnclosedObject*[T] = object |
| 178 | + data*: T |
| 179 | + meta*: JsonNode |
| 180 | + |
146 | 181 | DataRootEnclosedObject*[T] = object
|
147 | 182 | dependent_root*: Eth2Digest
|
148 | 183 | data*: T
|
149 | 184 |
|
| 185 | + DataRestAttestation* = DataEnclosedObject[Attestation] |
| 186 | + DataRestAttestationData* = DataEnclosedObject[AttestationData] |
| 187 | + DataRestAttesterDuties* = DataRootEnclosedObject[seq[RestAttesterDuty]] |
| 188 | + DataRestBeaconBlock* = DataEnclosedObject[phase0.BeaconBlock] |
150 | 189 | DataRestBeaconGenesis* = DataEnclosedObject[RestBeaconGenesis]
|
| 190 | + DataRestBeaconStateFinalityCheckpoints* = DataEnclosedObject[BeaconStatesFinalityCheckpointsTuple] |
| 191 | + DataRestBlockHeaders* = DataEnclosedObject[RestBlockHeaderInfo] |
| 192 | + DataRestConfig* = DataEnclosedObject[RestConfig] |
| 193 | + DataRestDebugChainHeads* = DataEnclosedObject[seq[RestChainHead]] |
151 | 194 | 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]] |
152 | 202 | 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] |
157 | 204 | DataRestSyncInfo* = DataEnclosedObject[RestSyncInfo]
|
158 | 205 | DataRestValidator* = DataEnclosedObject[RestValidator]
|
159 | 206 | DataRestValidatorList* = DataEnclosedObject[seq[RestValidator]]
|
160 |
| - DataRestVersion* = DataEnclosedObject[RestVersion] |
161 |
| - DataRestConfig* = DataEnclosedObject[RestConfig] |
162 | 207 |
|
163 | 208 | EncodeTypes* = phase0.SignedBeaconBlock
|
164 | 209 | EncodeArrays* = seq[ValidatorIndex] | seq[Attestation] |
|
165 | 210 | seq[SignedAggregateAndProof] | seq[RestCommitteeSubscription]
|
166 | 211 |
|
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 |
173 | 237 |
|
174 | 238 | proc jsonResponseWRoot*(t: typedesc[RestApiResponse],
|
175 | 239 | data: auto,
|
@@ -620,12 +684,10 @@ proc decodeBytes*[T: DecodeTypes](t: typedesc[T], value: openarray[byte],
|
620 | 684 | contentType: string): RestResult[T] =
|
621 | 685 | case contentType
|
622 | 686 | 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") |
629 | 691 | else:
|
630 | 692 | err("Content-Type not supported")
|
631 | 693 |
|
@@ -667,3 +729,18 @@ proc encodeString*(value: StateIdent): RestResult[string] =
|
667 | 729 | ok("finalized")
|
668 | 730 | of StateIdentType.Justified:
|
669 | 731 | 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") |
0 commit comments