.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/consensus/agreement/plot_03_flp_impossibility.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_consensus_agreement_plot_03_flp_impossibility.py: FLP: no deterministic consensus in an asynchronous network (1985) ================================================================= Fischer, Lynch and Paterson proved that no deterministic protocol can always reach agreement in an asynchronous system where even one process may crash. The adversary does not need to crash anyone: it only chooses the order in which messages arrive, and can keep the system undecided forever. What to look for ---------------- Ben-Or's protocol with its coin replaced by a fixed rule, here "process p adopts p mod 2", never decides against a scheduler that keeps every process from seeing a strict majority: the values stay split two against two in every round. Give the processes real coins and the same scheduler loses. FLP says *every* deterministic protocol has some such schedule. Practical protocols escape it with randomness (Ben-Or) or timing assumptions (partial synchrony). The history behind this experiment: :doc:`/history/consensus_breakthroughs`. .. GENERATED FROM PYTHON SOURCE LINES 25-27 A deterministic protocol, stalled forever ----------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 27-40 .. code-block:: Python import matplotlib.pyplot as plt import blockchainkit as bk def parity(process, round_): return process % 2 stalled = bk.consensus.ben_or([0, 0, 1, 1], faults=1, coin=parity, max_rounds=500) assert not stalled.decided and stalled.rounds == 500 print("adversarial schedule, deterministic rule: no decision after 500 rounds") .. rst-class:: sphx-glr-script-out .. code-block:: none adversarial schedule, deterministic rule: no decision after 500 rounds .. GENERATED FROM PYTHON SOURCE LINES 41-43 Change one ingredient at a time ------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 43-57 .. code-block:: Python cases = { "deterministic rule": bk.consensus.ben_or([0, 0, 1, 1], faults=1, coin=parity, max_rounds=500), "random coin": bk.consensus.ben_or([0, 0, 1, 1], faults=1, seed=3, max_rounds=500), } for label, run in cases.items(): print(label, "->", "decided" if run.decided else "stalled", f"({run.rounds} rounds)") assert [run.decided for run in cases.values()] == [False, True] fig, ax = plt.subplots(figsize=(7, 3.2)) ax.bar(cases.keys(), [run.rounds for run in cases.values()], color=["#dc2626", "#16a34a"]) ax.set( ylabel="rounds run (500 = gave up)", title="Same adversarial schedule, deterministic vs random" ) fig.tight_layout() .. image-sg:: /api/gallery/consensus/agreement/images/sphx_glr_plot_03_flp_impossibility_001.png :alt: Same adversarial schedule, deterministic vs random :srcset: /api/gallery/consensus/agreement/images/sphx_glr_plot_03_flp_impossibility_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none deterministic rule -> stalled (500 rounds) random coin -> decided (21 rounds) .. GENERATED FROM PYTHON SOURCE LINES 58-64 Exercise -------- Try ``coin=lambda p, r: r % 2`` (every process adopts the same bit). It decides against this scheduler. Why does that not contradict FLP? (The theorem promises a stalling schedule for every deterministic protocol, not that this particular scheduler finds it.) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.113 seconds) .. _sphx_glr_download_api_gallery_consensus_agreement_plot_03_flp_impossibility.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/consensus/agreement/plot_03_flp_impossibility.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_03_flp_impossibility.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_03_flp_impossibility.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_03_flp_impossibility.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_