From fe107abb56814779de4a3771ce48abc4bf472266 Mon Sep 17 00:00:00 2001 From: Ferenc Beres Date: Tue, 28 Feb 2023 16:31:49 +0100 Subject: [PATCH] reformatting with black --- ethp2psim/adversary.py | 17 +++++++++-------- ethp2psim/protocols.py | 19 +++++++++++-------- ipython/EthP2PSimExamples.ipynb | 13 +++++++++++-- 3 files changed, 31 insertions(+), 18 deletions(-) diff --git a/ethp2psim/adversary.py b/ethp2psim/adversary.py index b40dca7..0433f4f 100644 --- a/ethp2psim/adversary.py +++ b/ethp2psim/adversary.py @@ -413,11 +413,12 @@ class OnionRoutingAdversary(Adversary): Optional list of nodes that can be set to be adversaries instead of randomly selecting them. seed: int (optional) Random seed (disabled by default) - + References ---------- Domokos M. Kelen, Istvan Andras Seres, Ferenc Beres, Andras A. Benczur, Integrated Onion Routing for Peer-to-Peer Validator Privacy in the Ethereum Network, https://info.ilab.sztaki.hu/~kdomokos/OnionRoutingP2PEthereumPrivacy.pdf """ + def __init__( self, protocol: Protocol, @@ -524,10 +525,10 @@ class OnionRoutingAdversary(Adversary): while len(queue) > 0: u, v, t, step = queue.popleft() v, t, step = step_back(u, v, t, step) - #print("step:", v, t, step) + # print("step:", v, t, step) is_adv = v in self.nodes events = prev_events if is_adv else next_events - #print(events) + # print(events) valid = False if (len(events) > 0) and v in events: candidates, timestamps = events[v] @@ -540,7 +541,7 @@ class OnionRoutingAdversary(Adversary): queue.append((u, v, t, step)) else: predictions.append((v, t, step)) - #print("preds:", predictions) + # print("preds:", predictions) return predictions def predict_msg_source(self, estimator: str = "first_reach") -> pd.DataFrame: @@ -554,11 +555,11 @@ class OnionRoutingAdversary(Adversary): * first_reach: the node from whom the adversary first heard the message is assigned 1.0 probability while every other node receives zero. * first_sent: the node that sent the message the earliest to the receiver * dummy: the probability is divided equally between non-adversary nodes. - + Examples -------- In this small example, the adversary reconstructs the channel originating from node 9 from the two adversarial nodes (7 and 1). - + >>> from ethp2psim.network import * >>> from ethp2psim.message import Message >>> from ethp2psim.simulator import Simulator @@ -576,9 +577,9 @@ class OnionRoutingAdversary(Adversary): >>> # the adversary could reconstruct the channel from node 1 and 7 >>> predictions.loc[msgs[0].mid, 9] # and it predicted node 9 to be the originator 1.0 - + Next, we evaluate a simulation with 20 random messages. - + >>> from ethp2psim.network import * >>> from ethp2psim.message import Message >>> from ethp2psim.simulator import * diff --git a/ethp2psim/protocols.py b/ethp2psim/protocols.py index 40f2e1f..3280b30 100644 --- a/ethp2psim/protocols.py +++ b/ethp2psim/protocols.py @@ -387,7 +387,7 @@ class DandelionPlusPlusProtocol(DandelionProtocol): class OnionRoutingProtocol(BroadcastProtocol): """ - Message propagation is first based on an anonymity phase that is followed by a spreading phase. During the anonymity phase the messages are propagated on pre-selected channels that contain multiple relayer nodes. The message source uses Onion Routing to encrypt the message with the public keys of relayer nodes. When an encrypted message reaches the last relayer in a given chain then it is broadcasted as a cleartext message. This is the start of the spreading phase. + Message propagation is first based on an anonymity phase that is followed by a spreading phase. During the anonymity phase the messages are propagated on pre-selected channels that contain multiple relayer nodes. The message source uses Onion Routing to encrypt the message with the public keys of relayer nodes. When an encrypted message reaches the last relayer in a given chain then it is broadcasted as a cleartext message. This is the start of the spreading phase. Parameters ---------- @@ -402,8 +402,8 @@ class OnionRoutingProtocol(BroadcastProtocol): Examples -------- - In this small example, we show how you can access encrypted channel(s) for each originator. - + In this small example, we show how you can access encrypted channel(s) for each originator. + >>> from .network import * >>> seed = 42 >>> nw_generator = NodeWeightGenerator("random", seed) @@ -432,12 +432,15 @@ class OnionRoutingProtocol(BroadcastProtocol): self._num_relayers = num_relayers self._tor_network = {} self.change_anonimity_graph() - + def __repr__(self): - return "OnionRoutingProtocol(num_relayers=%i, num_channels=%i, broadcast_mode=%s)" % ( - self.num_relayers, - self.num_channels, - self.broadcast_mode, + return ( + "OnionRoutingProtocol(num_relayers=%i, num_channels=%i, broadcast_mode=%s)" + % ( + self.num_relayers, + self.num_channels, + self.broadcast_mode, + ) ) @property diff --git a/ipython/EthP2PSimExamples.ipynb b/ipython/EthP2PSimExamples.ipynb index 86e66e8..9837152 100644 --- a/ipython/EthP2PSimExamples.ipynb +++ b/ipython/EthP2PSimExamples.ipynb @@ -275,7 +275,11 @@ "metadata": {}, "outputs": [], "source": [ - "from ethp2psim.protocols import BroadcastProtocol, DandelionPlusPlusProtocol, OnionRoutingProtocol\n", + "from ethp2psim.protocols import (\n", + " BroadcastProtocol,\n", + " DandelionPlusPlusProtocol,\n", + " OnionRoutingProtocol,\n", + ")\n", "from ethp2psim.adversary import Adversary, OnionRoutingAdversary\n", "import pandas as pd\n", "\n", @@ -486,7 +490,12 @@ "metadata": {}, "outputs": [], "source": [ - "fig = px.box(results_df[~results_df[\"protocol\"].apply(lambda x: \"OnionRouting\" in x)], x=\"adversary_ratio\", y=\"entropy\", color=\"protocol\")\n", + "fig = px.box(\n", + " results_df[~results_df[\"protocol\"].apply(lambda x: \"OnionRouting\" in x)],\n", + " x=\"adversary_ratio\",\n", + " y=\"entropy\",\n", + " color=\"protocol\",\n", + ")\n", "fig.show()" ] },