diff --git a/beacon_chain/beacon_node.nim b/beacon_chain/beacon_node.nim index 656d4a76e..bb1083665 100644 --- a/beacon_chain/beacon_node.nim +++ b/beacon_chain/beacon_node.nim @@ -99,8 +99,8 @@ proc init*(T: type BeaconNode, conf: BeaconNodeConf): Future[BeaconNode] {.async else: try: result.networkMetadata = Json.loadFile(conf.network, NetworkMetadata) - except: - fail "Failed to load network metadata: ", getCurrentExceptionMsg() + except SerializationError as err: + fail "Failed to load network metadata: \n", err.formatMsg(conf.network) var metadataErrorMsg = "" diff --git a/beacon_chain/eth2_network.nim b/beacon_chain/eth2_network.nim index 5c9bf03b3..44f2b1199 100644 --- a/beacon_chain/eth2_network.nim +++ b/beacon_chain/eth2_network.nim @@ -144,18 +144,6 @@ else: const networkKeyFilename = "privkey.protobuf" - proc writeValue*(writer: var JsonWriter, value: PeerID) {.inline.} = - writer.writeValue value.pretty - - proc readValue*(reader: var JsonReader, value: var PeerID) {.inline.} = - value = PeerID.init reader.readValue(string) - - proc writeValue*(writer: var JsonWriter, value: MultiAddress) {.inline.} = - writer.writeValue $value - - proc readValue*(reader: var JsonReader, value: var MultiAddress) {.inline.} = - value = MultiAddress.init reader.readValue(string) - proc init*(T: type BootstrapAddr, str: string): T = Json.decode(str, PeerInfo) diff --git a/beacon_chain/libp2p_backend.nim b/beacon_chain/libp2p_backend.nim index 78a8eb0ef..436e6e715 100644 --- a/beacon_chain/libp2p_backend.nim +++ b/beacon_chain/libp2p_backend.nim @@ -1,12 +1,12 @@ import - options, macros, algorithm, tables, + macros, algorithm, tables, std_shims/[macros_shim, tables_shims], chronos, chronicles, libp2p/daemon/daemonapi, faststreams/output_stream, serialization, - eth/p2p/p2p_protocol_dsl, - ssz + json_serialization/std/options, eth/p2p/p2p_protocol_dsl, + libp2p_json_serialization, ssz export - daemonapi, p2pProtocol + daemonapi, p2pProtocol, libp2p_json_serialization type Eth2Node* = ref object of RootObj @@ -506,7 +506,7 @@ proc p2pProtocolBackendImpl*(p: P2PProtocol): Backend = try: debug "INCOMING CONNECTION", `peerVar` `await` `handshakeProcName`(`peerVar`, `streamVar`) - debug "HANSHAKE COMPLETED", `peerVar` + debug "HANDSHAKE COMPLETED", `peerVar` except SerializationError as err: debug "Failed to decode message", err = err.formatMsg(""), diff --git a/beacon_chain/libp2p_json_serialization.nim b/beacon_chain/libp2p_json_serialization.nim new file mode 100644 index 000000000..181f84f6f --- /dev/null +++ b/beacon_chain/libp2p_json_serialization.nim @@ -0,0 +1,15 @@ +import libp2p/daemon/daemonapi, json_serialization +export json_serialization + +proc writeValue*(writer: var JsonWriter, value: PeerID) {.inline.} = + writer.writeValue value.pretty + +proc readValue*(reader: var JsonReader, value: var PeerID) {.inline.} = + value = PeerID.init reader.readValue(string) + +proc writeValue*(writer: var JsonWriter, value: MultiAddress) {.inline.} = + writer.writeValue $value + +proc readValue*(reader: var JsonReader, value: var MultiAddress) {.inline.} = + value = MultiAddress.init reader.readValue(string) + diff --git a/beacon_chain/libp2p_spec_backend.nim b/beacon_chain/libp2p_spec_backend.nim index 98084114e..548864f46 100644 --- a/beacon_chain/libp2p_spec_backend.nim +++ b/beacon_chain/libp2p_spec_backend.nim @@ -2,10 +2,10 @@ import tables, deques, options, algorithm, std_shims/[macros_shim, tables_shims], ranges/ptr_arith, chronos, chronicles, serialization, faststreams/input_stream, eth/async_utils, eth/p2p/p2p_protocol_dsl, libp2p/daemon/daemonapi, - ssz + libp2p_json_serialization, ssz export - daemonapi, p2pProtocol, serialization, ssz + daemonapi, p2pProtocol, serialization, ssz, libp2p_json_serialization const # Compression nibble diff --git a/tests/test_ssz.nim b/tests/test_ssz.nim index 7bd0f7463..b5d8b7369 100644 --- a/tests/test_ssz.nim +++ b/tests/test_ssz.nim @@ -71,12 +71,12 @@ suite "Simple serialization": ser.len() == 3 SSZ.decode(ser, v.type) == v - SSZ.roundripTest [1, 2, 3] - SSZ.roundripTest @[1, 2, 3] - SSZ.roundripTest SigKey.random().getKey() - SSZ.roundripTest BeaconBlock( + SSZ.roundtripTest [1, 2, 3] + SSZ.roundtripTest @[1, 2, 3] + SSZ.roundtripTest SigKey.random().getKey() + SSZ.roundtripTest BeaconBlock( slot: 42.Slot, signature: sign(SigKey.random(), 0'u64, "")) - SSZ.roundripTest BeaconState(slot: 42.Slot) + SSZ.roundtripTest BeaconState(slot: 42.Slot) # suite "Tree hashing": # # TODO The test values are taken from an earlier version of SSZ and have