.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/consensus/pos/plot_02_nothing_at_stake.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_pos_plot_02_nothing_at_stake.py: Nothing at stake: why proof of stake needs penalties (Buterin 2014) =================================================================== A proof-of-work miner must divide its hashrate between competing forks; a proof-of-stake validator can sign every fork at no cost and collect the reward whichever one wins. If everyone does that, forks never resolve. Buterin's Slasher proposal made such double-signing punishable: evidence of two conflicting signatures destroys the validator's deposit. What to look for ---------------- Without a penalty, signing both forks pays more than any honest choice. Once the penalty exceeds the reward lost by picking one side, picking a side wins. Every modern proof-of-stake protocol has slashing. The history behind this experiment: :doc:`/history/consensus_breakthroughs`. .. GENERATED FROM PYTHON SOURCE LINES 22-24 Payoffs without slashing ------------------------ .. GENERATED FROM PYTHON SOURCE LINES 24-33 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np import blockchainkit as bk free = bk.consensus.fork_voting_payoffs(0.6, reward=1.0, penalty=0.0) print("no penalty:", free) assert free["both"] > free["A"] > free["B"] .. rst-class:: sphx-glr-script-out .. code-block:: none no penalty: {'A': 0.6, 'B': 0.4, 'both': 1.0} .. GENERATED FROM PYTHON SOURCE LINES 34-36 Payoffs as the penalty grows ---------------------------- .. GENERATED FROM PYTHON SOURCE LINES 36-55 .. code-block:: Python penalties = np.linspace(0, 1.5, 61) rows = [bk.consensus.fork_voting_payoffs(0.6, 1.0, p) for p in penalties] fig, ax = plt.subplots(figsize=(7, 4)) for strategy, color in (("A", "#2563eb"), ("B", "#64748b"), ("both", "#dc2626")): ax.plot(penalties, [r[strategy] for r in rows], color=color, label=f"vote {strategy}") crossover = 1.0 - 0.6 ax.axvline(crossover, color="black", linestyle=":", label="penalty = reward lost by choosing") ax.set( xlabel="slashing penalty", ylabel="expected payoff", title="Slashing makes choosing a fork the best response", ) ax.legend() fig.tight_layout() assert ( bk.consensus.fork_voting_payoffs(0.6, 1.0, 0.5)["A"] > bk.consensus.fork_voting_payoffs(0.6, 1.0, 0.5)["both"] ) .. image-sg:: /api/gallery/consensus/pos/images/sphx_glr_plot_02_nothing_at_stake_001.png :alt: Slashing makes choosing a fork the best response :srcset: /api/gallery/consensus/pos/images/sphx_glr_plot_02_nothing_at_stake_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 56-62 Exercise -------- Slashing only works while the deposit can still be taken. Explain the long-range attack: why could keys of validators who withdrew long ago be used to forge an alternative history, and what "weak subjectivity" asks new nodes to trust? .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.183 seconds) .. _sphx_glr_download_api_gallery_consensus_pos_plot_02_nothing_at_stake.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/pos/plot_02_nothing_at_stake.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_02_nothing_at_stake.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_nothing_at_stake.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_nothing_at_stake.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_