From f0bf0570d1ee191124e26943c34da00b9f76c963 Mon Sep 17 00:00:00 2001 From: Zahary Karadjov Date: Mon, 25 Mar 2019 01:36:40 +0200 Subject: [PATCH] Support compiling with json logging; Drop package_visible_types --- eth.nimble | 1 - eth/common/eth_types_json_serialization.nim | 8 +- eth/p2p/discovery.nim | 3 +- eth/p2p/kademlia.nim | 2 +- eth/p2p/private/p2p_types.nim | 1 - eth/p2p/rlpx.nim | 3 +- eth/p2p/rlpx_protocols/les/flow_control.nim | 12 +- .../rlpx_protocols/les/private/les_types.nim | 160 +++++++++--------- eth/p2p/rlpx_protocols/les_protocol.nim | 5 +- 9 files changed, 100 insertions(+), 95 deletions(-) diff --git a/eth.nimble b/eth.nimble index 40a53da..495bf8e 100644 --- a/eth.nimble +++ b/eth.nimble @@ -11,7 +11,6 @@ requires "nim >= 0.19.0", "byteutils", "secp256k1", "rocksdb", - "package_visible_types", "chronos", "chronicles", "std_shims" diff --git a/eth/common/eth_types_json_serialization.nim b/eth/common/eth_types_json_serialization.nim index 7b9f986..f6b7d9e 100644 --- a/eth/common/eth_types_json_serialization.nim +++ b/eth/common/eth_types_json_serialization.nim @@ -1,5 +1,5 @@ import - times, + times, net, json_serialization, nimcrypto/hash, eth_types proc writeValue*(w: var JsonWriter, a: MDigest) {.inline.} = @@ -8,6 +8,12 @@ proc writeValue*(w: var JsonWriter, a: MDigest) {.inline.} = proc readValue*(r: var JsonReader, a: var MDigest) {.inline.} = a = fromHex(type(a), r.readValue(string)) +proc writeValue*(w: var JsonWriter, a: Port) {.inline.} = + w.writeValue uint16(a) + +proc readValue*(r: var JsonReader, a: var Port) {.inline.} = + a = Port r.readValue(uint16) + proc writeValue*(w: var JsonWriter, value: StUint) {.inline.} = w.writeValue $value diff --git a/eth/p2p/discovery.nim b/eth/p2p/discovery.nim index d8e812a..6d948b9 100644 --- a/eth/p2p/discovery.nim +++ b/eth/p2p/discovery.nim @@ -10,7 +10,8 @@ import times, - chronos, eth/[keys, rlp], stint, nimcrypto, chronicles, + chronos, stint, nimcrypto, chronicles, + eth/common/eth_types_json_serialization, eth/[keys, rlp], kademlia, enode export diff --git a/eth/p2p/kademlia.nim b/eth/p2p/kademlia.nim index 0e19446..5ac8329 100644 --- a/eth/p2p/kademlia.nim +++ b/eth/p2p/kademlia.nim @@ -10,7 +10,7 @@ import tables, hashes, times, algorithm, sets, sequtils, random, - chronos, chronicles, eth/keys, stint, nimcrypto, + chronos, eth/keys, chronicles, stint, nimcrypto, enode export sets # TODO: This should not be needed, but compilation fails otherwise diff --git a/eth/p2p/private/p2p_types.nim b/eth/p2p/private/p2p_types.nim index a4cd381..ef148e6 100644 --- a/eth/p2p/private/p2p_types.nim +++ b/eth/p2p/private/p2p_types.nim @@ -1,6 +1,5 @@ import deques, tables, - package_visible_types, eth/[rlp, keys], chronos, eth/common/eth_types, ../enode, ../kademlia, ../discovery, ../options, ../rlpxcrypt diff --git a/eth/p2p/rlpx.nim b/eth/p2p/rlpx.nim index e154095..c17a438 100644 --- a/eth/p2p/rlpx.nim +++ b/eth/p2p/rlpx.nim @@ -35,8 +35,7 @@ var template allProtocols*: auto = {.gcsafe.}: gProtocols template devp2pInfo: auto = {.gcsafe.}: gDevp2pInfo -proc `$`*(p: Peer): string {.inline.} = - $p.remote +chronicles.formatIt(Peer): $(it.remote) proc disconnect*(peer: Peer, reason: DisconnectionReason, notifyOtherPeer = true) {.gcsafe, async.} diff --git a/eth/p2p/rlpx_protocols/les/flow_control.nim b/eth/p2p/rlpx_protocols/les/flow_control.nim index 67f828f..3fbe85d 100644 --- a/eth/p2p/rlpx_protocols/les/flow_control.nim +++ b/eth/p2p/rlpx_protocols/les/flow_control.nim @@ -78,9 +78,9 @@ proc currentRequestsCosts*(network: LesNetwork, if b < 0: b = 0 - result.add ReqCostInfo.init(msgId = msg.id, - baseCost = ReqCostInt(b * 2), - reqCost = ReqCostInt(m * 2)) + result.add ReqCostInfo(msgId: msg.id, + baseCost: ReqCostInt(b * 2), + reqCost: ReqCostInt(m * 2)) proc persistMessageStats*(db: AbstractChainDB, network: LesNetwork) = @@ -278,9 +278,11 @@ proc canMakeRequest(peer: var LesPeer, maxCost: int): (LesTime, float64) = template getRequestCost(peer: LesPeer, localOrRemote: untyped, msgId, costQuantity: int): ReqCostInt = - template msgCostInfo: untyped = peer.`localOrRemote ReqCosts`[msgId] + let + baseCost = peer.`localOrRemote ReqCosts`[msgId].baseCost + reqCost = peer.`localOrRemote ReqCosts`[msgId].reqCost - min(msgCostInfo.baseCost + msgCostInfo.reqCost * costQuantity, + min(baseCost + reqCost * costQuantity, peer.`localOrRemote FlowState`.bufLimit) proc trackOutgoingRequest*(network: LesNetwork, peer: LesPeer, diff --git a/eth/p2p/rlpx_protocols/les/private/les_types.nim b/eth/p2p/rlpx_protocols/les/private/les_types.nim index 9288e5c..272682c 100644 --- a/eth/p2p/rlpx_protocols/les/private/les_types.nim +++ b/eth/p2p/rlpx_protocols/les/private/les_types.nim @@ -1,107 +1,105 @@ import hashes, tables, sets, - package_visible_types, eth/common/eth_types -packageTypes: - type - AnnounceType* = enum - None, - Simple, - Signed, - Unspecified +type + AnnounceType* = enum + None, + Simple, + Signed, + Unspecified - ReqCostInfo = object - msgId: int - baseCost, reqCost: ReqCostInt + ReqCostInfo* = object + msgId*: int + baseCost*, reqCost*: ReqCostInt - FlowControlState = object - bufValue, bufLimit: int - minRecharge: int - lastUpdate: LesTime + FlowControlState* = object + bufValue*, bufLimit*: int + minRecharge*: int + lastUpdate*: LesTime - StatsRunningAverage = object - sumX, sumY, sumXX, sumXY: float64 - count: int + StatsRunningAverage* = object + sumX*, sumY*, sumXX*, sumXY*: float64 + count*: int - LesPeer* = ref object - isServer*: bool - isClient*: bool - announceType*: AnnounceType + LesPeer* = ref object + isServer*: bool + isClient*: bool + announceType*: AnnounceType - bestDifficulty*: DifficultyInt - bestBlockHash*: KeccakHash - bestBlockNumber*: BlockNumber + bestDifficulty*: DifficultyInt + bestBlockHash*: KeccakHash + bestBlockNumber*: BlockNumber - hasChainSince: HashOrNum - hasStateSince: HashOrNum - relaysTransactions: bool + hasChainSince*: HashOrNum + hasStateSince*: HashOrNum + relaysTransactions*: bool - # The variables below are used to implement the flow control - # mechanisms of LES from our point of view as a server. - # They describe how much load has been generated by this - # particular peer. - reqCount: int # How many outstanding requests are there? - # - rechargingPower: int # Do we give this peer any extra priority - # (implemented as a faster recharning rate) - # 100 is the default. You can go higher and lower. - # - isRecharging: bool # This is true while the peer is not making - # any requests - # - reqCostGradient: int # Measures the speed of recharging or accumulating - # "requests cost" at any given moment. - # - reqCostVal: int # The accumulated "requests cost" - # - rechargingEndsAt: int # When will recharging end? - # (the buffer of the Peer will be fully restored) - # - lastRechargeTime: LesTime # When did we last update the recharging parameters - # - startReqCostVal: int # TODO + # The variables below are used to implement the flow control + # mechanisms of LES from our point of view as a server. + # They describe how much load has been generated by this + # particular peer. + reqCount*: int # How many outstanding requests are there? + # + rechargingPower*: int # Do we give this peer any extra priority + # (implemented as a faster recharning rate) + # 100 is the default. You can go higher and lower. + # + isRecharging*: bool # This is true while the peer is not making + # any requests + # + reqCostGradient*: int # Measures the speed of recharging or accumulating + # "requests cost" at any given moment. + # + reqCostVal*: int # The accumulated "requests cost" + # + rechargingEndsAt*: int # When will recharging end? + # (the buffer of the Peer will be fully restored) + # + lastRechargeTime*: LesTime # When did we last update the recharging parameters + # + startReqCostVal*: int # TODO - remoteFlowState: FlowControlState - remoteReqCosts: seq[ReqCostInfo] + remoteFlowState*: FlowControlState + remoteReqCosts*: seq[ReqCostInfo] - # The next variables are used to limit ourselves as a client in order to - # not violate the control-flow requirements of the remote LES server. + # The next variables are used to limit ourselves as a client in order to + # not violate the control-flow requirements of the remote LES server. - pendingReqs: Table[int, ReqCostInt] - pendingReqsCost: int + pendingReqs*: Table[int, ReqCostInt] + pendingReqsCost*: int - localFlowState: FlowControlState - localReqCosts: seq[ReqCostInfo] + localFlowState*: FlowControlState + localReqCosts*: seq[ReqCostInfo] - LesNetwork* = ref object - peers: HashSet[LesPeer] - messageStats: seq[StatsRunningAverage] - ourAnnounceType*: AnnounceType + LesNetwork* = ref object + peers*: HashSet[LesPeer] + messageStats*: seq[StatsRunningAverage] + ourAnnounceType*: AnnounceType - # The fields below are relevant when serving data. - bufferLimit: int - minRechargingRate: int + # The fields below are relevant when serving data. + bufferLimit*: int + minRechargingRate*: int - reqCostSum, maxReqCostSum: ReqCostInt - reqCount, maxReqCount: int - sumWeigth: int + reqCostSum*, maxReqCostSum*: ReqCostInt + reqCount*, maxReqCount*: int + sumWeigth*: int - rechargingRate: int - totalRechargedUnits: int - totalRechargingPower: int + rechargingRate*: int + totalRechargedUnits*: int + totalRechargingPower*: int - lastUpdate: LesTime + lastUpdate*: LesTime - KeyValuePair = object - key: string - value: Blob + KeyValuePair* = object + key*: string + value*: Blob - HandshakeError = object of Exception + HandshakeError* = object of Exception - LesTime = int # this is in milliseconds - BufValueInt = int - ReqCostInt = int + LesTime* = int # this is in milliseconds + BufValueInt* = int + ReqCostInt* = int template hash*(peer: LesPeer): Hash = hash(cast[pointer](peer)) diff --git a/eth/p2p/rlpx_protocols/les_protocol.nim b/eth/p2p/rlpx_protocols/les_protocol.nim index a5d62fc..9c273fa 100644 --- a/eth/p2p/rlpx_protocols/les_protocol.nim +++ b/eth/p2p/rlpx_protocols/les_protocol.nim @@ -15,7 +15,8 @@ import ../rlpx, ../kademlia, ../private/p2p_types, ../blockchain_utils, les/private/les_types, les/flow_control -les_types.forwardPublicTypes +export + les_types const lesVersion = 2'u @@ -179,7 +180,7 @@ p2pProtocol les(version = lesVersion, lesNetwork = peer.networkState template `=>`(k, v: untyped): untyped = - KeyValuePair.init(key = k, value = rlp.encode(v)) + KeyValuePair(key: k, value: rlp.encode(v)) var lesProperties = @[ keyProtocolVersion => lesVersion,