.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/network/relay/plot_01_inv_getdata.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code or to run this example in your browser via JupyterLite. .. rst-class:: sphx-glr-example-title .. _sphx_glr_api_gallery_network_relay_plot_01_inv_getdata.py: Announce, then fetch: Bitcoin's inv/getdata relay (Nakamoto 2009) ================================================================= The simplest relay floods: every peer forwards a new block over each of its links, so a peer with eight neighbors may receive it eight times. The first Bitcoin client instead *announces*: it sends a small ``inv`` message carrying the block's hash, and sends the block only to peers that ask for it with ``getdata``. Each peer downloads each block once. What to look for ---------------- Announcing cuts the traffic by roughly the average degree, and the saving grows as peers keep more connections. The price is latency: each hop now needs three one-way messages (inv, getdata, block) instead of one, which is the delay that compact blocks later attacked. The history behind this experiment: :doc:`/history/network_breakthroughs`. .. GENERATED FROM PYTHON SOURCE LINES 23-25 A 1 MB block on a random network of 500 peers --------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 25-42 .. code-block:: Python import matplotlib.pyplot as plt import blockchainkit as bk size = 1_000_000 degrees = [4, 8, 16, 32] flood, announce = [], [] for d in degrees: graph = bk.network.watts_strogatz(500, d, 1.0, seed=1) assert graph.is_connected() flood.append(bk.network.relay_cost(graph, size=size)) announce.append(bk.network.relay_cost(graph, size=size, mode="announce")) for d, f, a in zip(degrees, flood, announce, strict=True): print(f"degree {d:2d}: flood {f.bytes / 1e6:6.0f} MB, announce {a.bytes / 1e6:4.0f} MB") assert all(a.bytes < f.bytes / (d / 2) for d, f, a in zip(degrees, flood, announce, strict=True)) assert all(a.completion == 3 * f.completion for f, a in zip(flood, announce, strict=True)) .. rst-class:: sphx-glr-script-out .. code-block:: none degree 4: flood 1501 MB, announce 499 MB degree 8: flood 3501 MB, announce 499 MB degree 16: flood 7501 MB, announce 499 MB degree 32: flood 15501 MB, announce 500 MB .. GENERATED FROM PYTHON SOURCE LINES 43-47 Check the count against the simulator ------------------------------------- Every message the simulator hands to a link is counted, including the duplicates that receivers discard. .. GENERATED FROM PYTHON SOURCE LINES 47-65 .. code-block:: Python graph = bk.network.watts_strogatz(500, 8, 1.0, seed=1) network = bk.network.SimulatedNetwork.from_graph(graph) network.broadcast("0", b"block") network.run() assert network.messages_sent == bk.network.relay_cost(graph, size=size).messages print(network.messages_sent, "full-block copies sent to reach", len(network.deliveries), "peers") fig, (left, right) = plt.subplots(1, 2, figsize=(10, 4)) left.plot(degrees, [f.bytes / 1e9 for f in flood], "o-", label="flood") left.plot(degrees, [a.bytes / 1e9 for a in announce], "s-", label="inv/getdata") left.set(xlabel="links per peer", ylabel="GB sent network-wide", title="Traffic for one block") left.legend() right.plot(degrees, [f.completion for f in flood], "o-", label="flood") right.plot(degrees, [a.completion for a in announce], "s-", label="inv/getdata") right.set(xlabel="links per peer", ylabel="one-way latencies", title="Time to reach every peer") right.legend() fig.tight_layout() .. image-sg:: /api/gallery/network/relay/images/sphx_glr_plot_01_inv_getdata_001.png :alt: Traffic for one block, Time to reach every peer :srcset: /api/gallery/network/relay/images/sphx_glr_plot_01_inv_getdata_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 3501 full-block copies sent to reach 500 peers .. GENERATED FROM PYTHON SOURCE LINES 66-71 Exercise -------- Bitcoin relays transactions the same way. Transactions are about 250 bytes, while an ``inv`` costs 61 bytes. Does announcing still pay off, and how would batching many hashes into one ``inv`` change the answer? .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.921 seconds) .. _sphx_glr_download_api_gallery_network_relay_plot_01_inv_getdata.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../../../lite/lab/index.html?path=api/gallery/network/relay/plot_01_inv_getdata.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_inv_getdata.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_inv_getdata.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_inv_getdata.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_