.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/consensus/agreement/plot_02_ben_or.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_02_ben_or.py: Randomized consensus: agreeing by flipping coins (Ben-Or 1983) ============================================================== Michael Ben-Or showed that asynchronous processes can reach agreement despite crashes if they may flip coins. In each round they exchange values, propose any value held by a strict majority, and adopt a proposal they see, or flip a coin if they see none. A decision follows once enough processes propose the same value. What to look for ---------------- Against a scheduler that delivers messages to keep processes split, the protocol still decides: eventually the coins all land the same way. The number of rounds is random, with an exponential tail. The history behind this experiment: :doc:`/history/consensus_breakthroughs`. See :doc:`/exercises/consensus` for a worked solution to the exercise. .. GENERATED FROM PYTHON SOURCE LINES 23-25 Decide despite an adversarial scheduler --------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 25-33 .. code-block:: Python import matplotlib.pyplot as plt import blockchainkit as bk run = bk.consensus.ben_or([0, 0, 1, 1], faults=1, seed=1983) assert run.decided and len(set(run.decisions.values())) == 1 print(f"decided {set(run.decisions.values())} after {run.rounds} rounds") .. rst-class:: sphx-glr-script-out .. code-block:: none decided {1} after 3 rounds .. GENERATED FROM PYTHON SOURCE LINES 34-36 Unanimous inputs decide in one round ------------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 36-38 .. code-block:: Python assert bk.consensus.ben_or([1, 1, 1, 1], faults=1).rounds == 1 .. GENERATED FROM PYTHON SOURCE LINES 39-41 How many rounds? ---------------- .. GENERATED FROM PYTHON SOURCE LINES 41-54 .. code-block:: Python rounds = [bk.consensus.ben_or([0, 0, 1, 1], faults=1, seed=s).rounds for s in range(400)] assert all(bk.consensus.ben_or([0, 1, 0, 1], faults=1, seed=s).decided for s in range(50)) fig, ax = plt.subplots(figsize=(7, 3.5)) ax.hist(rounds, bins=range(1, max(rounds) + 2), color="#2563eb", edgecolor="white") ax.axvline(sum(rounds) / len(rounds), color="black", linestyle="--", label="mean") ax.set( xlabel="rounds to decide (4 processes, adversarial delivery)", ylabel="runs", title="Termination with probability one", ) ax.legend() fig.tight_layout() .. image-sg:: /api/gallery/consensus/agreement/images/sphx_glr_plot_02_ben_or_001.png :alt: Termination with probability one :srcset: /api/gallery/consensus/agreement/images/sphx_glr_plot_02_ben_or_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 55-60 Exercise -------- The adversary here stalls unless all four coins agree, which happens with probability 2/16 per round. Derive the expected number of rounds and compare it with the histogram's mean. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.279 seconds) .. _sphx_glr_download_api_gallery_consensus_agreement_plot_02_ben_or.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_02_ben_or.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_02_ben_or.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_ben_or.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_ben_or.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_