This section gives basics of blockchain on a code level. Readers who are well-acquainted with coding can review the code given here from the “Further reading” (hackr.io, modified) section, as well as take a look at Multichain platform for creating your own blockchain. For the rest of us, we will go through the code step by step, clarifying coding concepts when needed. In this example, we use Python code (see Further reading for the code itself), but the same type of structure can be built with any currently used programming language.
To begin our journey to the code itself, a few words on the general structure of any code. Usually, the first thing that is defined in a code are subfunctions, which make a certain process happen. After subfunctions, the actual code begins, usually called the main of the code. The main uses the subfunctions to operate efficiently and clearly. The code ends when the main ends, e.g., after giving the result of the computation needed. Therefore, we begin by reviewing the main of the coded blockchain.
The code is short, even simplistic, and does not utilize all the functionalities of a blockchain described during this course. However, it fulfills the purpose of creating blocks and adding them together as a chain.
Main of the code:
# Genesis block is created here
genesisBlock = createGenesisBlock("A paid 50€ to B")
This line creates the first block, usually called as the genesis block, which initializes the blockchain. The code calls a subfunction named createGenesisBlock, and we will review this particular subfunction after going through the main of the code. Note that lines marked with “#” are comments and not ran code.
After creating the genesis block, the code creates a hash for that block in order to refer to it. Again, a subfunction is called, this time getTransactionHash, which saves the hash needed, this time for the genesis block just created. In addition, the code prints on the user interface the text “Genesis block hash is the following:”, after which it prints the hash. This is how the code looks like:
# Hash of the genesis block is printed here
genesisBlockHash = getTransactionHash(genesisBlock)
print "Genesis block hash is the following:", genesisBlockHash
Then the code adds another block to a chain, where there for now is only the genesis block. For this to happen, the code calls a subfunction createBlock, with the necessary information to be stored into the block. This is how it is done in the example:
# A regular block is created here
blockOne = createBlock("B paid 10€ to C, A paid 5€ to E", genesisBlockHash)
To finish the code, with one genesis block and one added block, the code prints the added block’s hash after saving it. This is done exactly the same way as for the genesis block, and the hash of the added block is printed just below the hash of the genesis block.
# The hash of the regular block, blockOne, is printed here
blockOneHash = getTransactionHash(blockOne)
print "Block One hash is the following:", blockOneHash
Here the code ends. To understand how the blocks are created and how their hashes are established we need to look at the subfunctions createGenesisBlock, createBlock and getTransactionHash, which are used in the code above.
Let us first take a look at createGenesisBlock subfunction. It is given in the code as
# This function creates the first block, called genesis block, to the blockchain
def createGenesisBlock(transactions):
return createBlock(transactions, 0)
We can see that the genesis block is actually just a normal block with no parent. The code for creating a normal block is:
# This function creates a block to the blockchain
def createBlock(transactions, parentHash):
blockHash = hash((transactions, parentHash))
return (parentHash, transactions, blockHash)
Which creates a block with some transactions and a hash value and a reference to its parent (here the genesis block). Hash value is created with Python ready-made function called hash.
Last, we review the getTransactionHash subfunction, which takes a block values as an input and should return the value of the hash created with the ready-made function. The code is here:
def getTransactionHash(block):
return block[2]
The return value is what the subfunction gives out when it is called. We can find that the “block” is actually an entity in the memory, with the first value (block[0]) being the parent hash, the second value (block[1]) being the transaction data and the third value (block[2]) being the hash value of the transactions and the parent hash (in the case of the genesis block, there is no parent hash, and it is given the value zero). This hash value is being returned by getTransactionHash subfunction with the command return block[2].
These are the other values of the “block” and the subfunctions to return them.
def getParentHash(block):
return block[0]
def getTransactions(block):
return block[1]
The whole code (published by hackr.io, here modified) is printed here and can be found also from the link in the Further reading Section.
def getParentHash(block):
return block[0]
def getTransactions(block):
return block[1]
def getTransactionHash(block):
return block[2]
# This function creates a block to the blockchain
def createBlock(transactions, parentHash):
blockHash = hash((transactions, parentHash))
return (parentHash, transactions, blockHash)
# This function creates the first block, called genesis block, to the blockchain
def createGenesisBlock(transactions):
return createBlock(transactions, 0)
# Genesis block is created here
genesisBlock = createGenesisBlock("A paid 50€ to B")
# Hash of the genesis block is printed here
genesisBlockHash = getTransactionHash(genesisBlock)
print "Genesis block hash is the following:", genesisBlockHash
# A regular block is created here
blockOne = createBlock("B paid 10€ to C, A paid 5€ to E", genesisBlockHash)
# The hash of the regular block, blockOne, is printed here
blockOneHash = getTransactionHash(blockOne)
print "Block One hash is the following:", blockOneHash
Besides coding your own blockchain application from scratch as above, there are also faster ways to do your own blockchain application. One prominent way to do this is to use Multichain, which is a command prompt based (meaning non-graphical) application for creating and maintaining a peer-to-peer blockchain on multiple computers. The commands given for a blockchain to operate are quite simple when the connection between the computers belonging to a network is first established. Multichain is a versatile tool and it has been used in many instances, e.g., to create a voting system which enables laptops to join the network and vote on matters passed as bills by the peers (see Bistarelli et al. 2019).
For code, from which the example was modified from, see: https://hackr.io/blog/blockchain-programming-beginners-guide
Multichain: https://www.multichain.com/
For more in-depth description of coding a blockchain, see: https://hackernoon.com/learn-blockchains-by-building-one-117428612f46