.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/consensus/nakamoto/plot_02_ghost.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_nakamoto_plot_02_ghost.py: GHOST: follow the heaviest subtree (Sompolinsky and Zohar 2013) =============================================================== When blocks are fast relative to network delay, honest miners often extend the same parent at once, and the longest chain discards their side blocks. That wasted work no longer protects the chain. GHOST (Greedy Heaviest Observed SubTree) counts it: at each fork, follow the child whose whole subtree carries the most work. What to look for ---------------- A single long branch wins the longest-chain rule, while a shorter but bushier branch, with more honest work behind it, wins under GHOST. Ethereum's proof-of-work chain used a GHOST variant, with uncle rewards. The history behind this experiment: :doc:`/history/consensus_breakthroughs`. .. GENERATED FROM PYTHON SOURCE LINES 22-24 Build a fork ------------ .. GENERATED FROM PYTHON SOURCE LINES 24-47 .. code-block:: Python import blockchainkit as bk from blockchainkit.structures.visualizers import plot_block_tree genesis = bk.consensus.mine(bk.structures.Block(difficulty=3)).block chain = bk.structures.Blockchain(genesis) def extend(parent, timestamp): block = bk.structures.Block( parent.hash, height=parent.height + 1, timestamp=timestamp, difficulty=3 ) block = bk.consensus.mine(block).block chain.add(block) return block a = extend(genesis, 1) a = extend(a, 2) a = extend(a, 3) # Branch A: three blocks in a row. b = extend(genesis, 4) b_children = [extend(b, 10 + i) for i in range(3)] # Branch B: one block, three children. .. GENERATED FROM PYTHON SOURCE LINES 48-50 Two fork-choice rules, two answers ---------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 50-63 .. code-block:: Python ghost = bk.consensus.ghost_tip(chain) assert chain.tip == a and ghost in b_children print("longest chain picks height", chain.tip.height, "; GHOST picks height", ghost.height) work_a = bk.consensus.subtree_work( chain, chain.blocks[chain.blocks[a.previous_hash].previous_hash].hash ) work_b = bk.consensus.subtree_work(chain, b.hash) print(f"subtree work: branch A {work_a}, branch B {work_b}") ax = plot_block_tree(chain) ax.set_title("Longest chain (blue) versus GHOST's heaviest subtree (branch B)") ax.figure.tight_layout() .. image-sg:: /api/gallery/consensus/nakamoto/images/sphx_glr_plot_02_ghost_001.png :alt: Longest chain (blue) versus GHOST's heaviest subtree (branch B) :srcset: /api/gallery/consensus/nakamoto/images/sphx_glr_plot_02_ghost_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none longest chain picks height 3 ; GHOST picks height 2 subtree work: branch A 24, branch B 32 .. GENERATED FROM PYTHON SOURCE LINES 64-69 Exercise -------- With a 12-second block time, a large share of Ethereum's blocks were uncles. Why would the longest-chain rule have let an attacker with less than half the hashrate win more often? .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.123 seconds) .. _sphx_glr_download_api_gallery_consensus_nakamoto_plot_02_ghost.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/nakamoto/plot_02_ghost.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_02_ghost.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_ghost.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_ghost.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_