From 2fca9465e7ffa152d5ddee0e26b9905c64fc8ef8 Mon Sep 17 00:00:00 2001 From: Zahary Karadjov Date: Thu, 15 Aug 2019 18:00:12 +0200 Subject: [PATCH] Fix the build with RLPx --- beacon_chain/gossipsub_protocol.nim | 2 +- beacon_chain/spec/crypto.nim | 44 +++++++++++++++-------------- beacon_chain/spec/datatypes.nim | 27 +++++++++++++----- beacon_chain/sync_protocol.nim | 4 +++ 4 files changed, 48 insertions(+), 29 deletions(-) diff --git a/beacon_chain/gossipsub_protocol.nim b/beacon_chain/gossipsub_protocol.nim index a7d13de52..47db96048 100644 --- a/beacon_chain/gossipsub_protocol.nim +++ b/beacon_chain/gossipsub_protocol.nim @@ -29,7 +29,7 @@ proc tryEmitting(peer: Peer, topic: string, msgId: string, msg: string): Future[void] {.gcsafe.} p2pProtocol GossipSub(version = 1, - shortName = "gss", + rlpxName = "gss", peerState = GossipSubPeer, networkState = GossipSubNetwork): # This is a very barebones emulation of the GossipSub protocol diff --git a/beacon_chain/spec/crypto.nim b/beacon_chain/spec/crypto.nim index 0ddbcdce7..82bcb9d49 100644 --- a/beacon_chain/spec/crypto.nim +++ b/beacon_chain/spec/crypto.nim @@ -46,9 +46,9 @@ import sequtils, - stew/objects, hashes, eth/rlp, nimcrypto/utils, + stew/objects, hashes, nimcrypto/utils, blscurve, json_serialization, - digest + ../version, digest export json_serialization @@ -257,28 +257,30 @@ else: proc newPrivKey*(): ValidatorPrivKey = SigKey.random() -# RLP serialization (TODO: remove if no longer necessary) -when ValidatorPubKey is BlsValue: - proc append*(writer: var RlpWriter, value: ValidatorPubKey) = - writer.append if value.kind == Real: value.blsValue.getBytes() - else: value.blob -else: - proc append*(writer: var RlpWriter, value: ValidatorPubKey) = - writer.append value.getBytes() +when networkBackend == rlpxBackend: + import eth/rlp -proc read*(rlp: var Rlp, T: type ValidatorPubKey): T {.inline.} = - result fromBytes(T, rlp.toBytes) + when ValidatorPubKey is BlsValue: + proc append*(writer: var RlpWriter, value: ValidatorPubKey) = + writer.append if value.kind == Real: value.blsValue.getBytes() + else: value.blob + else: + proc append*(writer: var RlpWriter, value: ValidatorPubKey) = + writer.append value.getBytes() -when ValidatorSig is BlsValue: - proc append*(writer: var RlpWriter, value: ValidatorSig) = - writer.append if value.kind == Real: value.blsValue.getBytes() - else: value.blob -else: - proc append*(writer: var RlpWriter, value: ValidatorSig) = - writer.append value.getBytes() + proc read*(rlp: var Rlp, T: type ValidatorPubKey): T {.inline.} = + result.initFromBytes rlp.toBytes.toOpenArray -proc read*(rlp: var Rlp, T: type ValidatorSig): T {.inline.} = - let bytes = fromBytes(T, rlp.toBytes) + when ValidatorSig is BlsValue: + proc append*(writer: var RlpWriter, value: ValidatorSig) = + writer.append if value.kind == Real: value.blsValue.getBytes() + else: value.blob + else: + proc append*(writer: var RlpWriter, value: ValidatorSig) = + writer.append value.getBytes() + + proc read*(rlp: var Rlp, T: type ValidatorSig): T {.inline.} = + result.initFromBytes rlp.toBytes.toOpenArray proc writeValue*(writer: var JsonWriter, value: VerKey) {.inline.} = writer.writeValue($value) diff --git a/beacon_chain/spec/datatypes.nim b/beacon_chain/spec/datatypes.nim index f04448736..a78306055 100644 --- a/beacon_chain/spec/datatypes.nim +++ b/beacon_chain/spec/datatypes.nim @@ -19,8 +19,8 @@ import macros, hashes, math, json, strutils, - stew/[byteutils, bitseqs], chronicles, eth/[common, rlp], - ../ssz/types, ./crypto, ./digest + stew/[byteutils, bitseqs], chronicles, eth/common, + ../version, ../ssz/types, ./crypto, ./digest # TODO Data types: # Presently, we're reusing the data types from the serialization (uint64) in the @@ -71,6 +71,8 @@ const template maxSize*(n: int) {.pragma.} type + Bytes = seq[byte] + ValidatorIndex* = range[0'u32 .. 0xFFFFFF'u32] # TODO: wrap-around Shard* = uint64 Gwei* = uint64 @@ -388,6 +390,16 @@ type data*: BeaconState root*: Eth2Digest # hash_tree_root (not signing_root!) +when networkBackend == rlpxBackend: + import eth/rlp/bitseqs as rlpBitseqs + export read, append + + proc read*(rlp: var Rlp, T: type BitList): T {.inline.} = + T rlp.read(BitSeq) + + proc append*(writer: var RlpWriter, value: BitList) = + writer.append BitSeq(value) + template foreachSpecType*(op: untyped) = ## These are all spec types that will appear in network messages ## and persistent consensus data. This helper template is useful @@ -486,11 +498,12 @@ template ethTimeUnit(typ: type) {.dirty.} = proc `%`*(x: typ): JsonNode {.borrow.} # Serialization - proc read*(rlp: var Rlp, T: type typ): typ {.inline.} = - typ(rlp.read(uint64)) + when networkBackend == rlpxBackend: + proc read*(rlp: var Rlp, T: type typ): typ {.inline.} = + typ(rlp.read(uint64)) - proc append*(writer: var RlpWriter, value: typ) = - writer.append uint64(value) + proc append*(writer: var RlpWriter, value: typ) = + writer.append uint64(value) proc writeValue*(writer: var JsonWriter, value: typ) = writeValue(writer, uint64 value) @@ -593,4 +606,4 @@ static: import nimcrypto, json_serialization export json_serialization -export writeValue, readValue, append, read +export writeValue, readValue diff --git a/beacon_chain/sync_protocol.nim b/beacon_chain/sync_protocol.nim index 73ab214bd..a4321b6f3 100644 --- a/beacon_chain/sync_protocol.nim +++ b/beacon_chain/sync_protocol.nim @@ -4,6 +4,10 @@ import spec/[datatypes, crypto, digest, helpers], eth/rlp, beacon_node_types, eth2_network, beacon_chain_db, block_pool, time, ssz +when networkBackend == rlpxBackend: + import eth/rlp/options as rlpOptions + template libp2pProtocol*(name: string, version: int) {.pragma.} + type ValidatorSetDeltaFlags {.pure.} = enum Activation = 0