.. _Extended9Blockchain:

.. raw:: html

   <script>ODSA.SETTINGS.DISP_MOD_COMP = true;ODSA.SETTINGS.MODULE_NAME = "Extended9Blockchain";ODSA.SETTINGS.MODULE_LONG_NAME = "Extended Example: Blockchain Proof-of-Work";ODSA.SETTINGS.MODULE_CHAPTER = "Parallel and Distributed Systems"; ODSA.SETTINGS.BUILD_DATE = "2021-06-01 12:51:47"; ODSA.SETTINGS.BUILD_CMAP = false;JSAV_OPTIONS['lang']='en';JSAV_EXERCISE_OPTIONS['code']='java';</script>


.. |--| unicode:: U+2013   .. en dash
.. |---| unicode:: U+2014  .. em dash, trimming surrounding whitespace
   :trim:


.. This file is part of the OpenCSF eTextbook project. It was
.. auto-generated by scripts from the OpenDSA eTextbook project.
.. See https://OpenCSF.org for more details. OpenCSF is distributed
.. under a Creative Commons Attribution-NonCommercial 4.0 International
.. License (see http://creativecommons.org/licenses/by-nc/4.0/),
.. Copyright (c) 2019-2021 by Michael S. Kirkpatrick. OpenDSA is
.. distributed under an MIT open source license, Copyright (c) 2012-2021
.. by the OpenDSA Project Contributors.

.. avmetadata::
   :author: Michael S. Kirkpatrick
   :requires:
   :satisfies: 
   :topic: 

Extended Example: Blockchain Proof-of-Work
==========================================

A blockchain is a sequence of messages that create a verifiable ordering of
events. Blockchains can be used in a variety of applications, such as
cryptocurrency (Bitcoin) or other distributed database ledgers. One key idea
behind a blockchain is that new entries are difficult to generate. A common
technique is to require a proof-of-work calculation, such as finding a string
that produces a cryptographic hash value with several leading 0s.

Consider the example output below. Each line contains a message index (0, 1, 2,
...) followed by the previous line's hash value (``0000...``, ``f74d...``, and so on), a
timestamp (omitted for brevity), then a log message ("Genesis Block"). The line
then ends with an integer value (called a *nonce*), such as ``0000``, ``22d75``, and
``6ea`` as shown. The entire line ending at the nonce is then used as input to a
cryptographic hash function. If the output of the hash does not begin with 0s,
the nonce is increased and the line is re-hashed. For instance, the second line
required 142,709 (``0x22d75``) attempts before the hash output began with 0s. Once
this entry is found, it can be added to the blockchain.

.. codeinclude:: ParallelDistributed/Blockchain.txt
   :linenos: true

The second field in each line is the previous line's hash output, which serves
the purpose of declaring that the new line is the only legitimate successor of
the previous line. That is, each line explicitly ties itself to the previous
line by including the previous line's hash in the input. Note that the first
line, often called a *Genesis block*, is special, as it does not follow any
entry. As such, a nonce of 0 is often used for this value and the hash value
does not need to begin with 0s. In this extended example, we use SHA-1 as a
cryptographic hash function simply to make the search more efficient. **In
practice, SHA-1 is considered insecure and should not be use for applications
like this**.

.. codeinclude:: ParallelDistributed/ExtEx-9.c
   :linenos: true


