Ask any of my friends or colleagues and they will tell you I have a slightly unhealthy obsession with Bitcoin. Whilst true to a certain degree, the actual obsession is around Blockchain, the underlying technology which allows the transactions of cryptocurrency. There are still some uncertainties about whether the likes of Bitcoin, Ethereum and other digital currencies are here to stay, one thing for sure that is, is Blockchain. So what actually is Blockchain?
In summary, Blockchain is a digital ledger that records transactions chronologically and publicly on a decentralized network. To elaborate further, data is stored in containers called Blocks and everytime a transaction happens a new Block is linked to the previous one securely using cryptography. Each Block contains data such as a timestamp, information about the transaction, a hash code and a pointer to the previous Block’s hash code. Storing transactions and data in this manner provides a more efficient and secure way of recording information, and once recorded, the data in any given Block cannot be altered without subsequently altering all other Blocks within the chain allowing for a single source of truth.
There are a number of blogs online which go into great depth about the Blockchain process and also the possible effect this may have in the future of the construction industry so I won’t go into much more detail here. It truly is a very fascinating subject though and I encourage anyone looking for some evening reading to delve into it all. In our industry I can definitely see the application of Blockchain changing a number of key areas some already mentioned and others yet to be talked about publicly. Maybe I’ll save my thoughts on this for another post in the future.
In IronPython there is a module called hashlib which allows an interface to many different secure hash and message digest algorithms. It will be this that I use to generate the FIPS secure algorithm SHA256 for use as part of the Block’s hash code later. To import this I first need to import sys and append the system path ‘C:\Program Files (x86)\IronPython 2.7\Lib’ before I import hashlib. The other library I will need other than the common clr is the datetime library so I can build a timestamp for each Block.
sys.path.append(r’C:\Program Files (x86)\IronPython 2.7\Lib’)
import datetime as date
With my libraries referenced correctly the first step is to build a Block definition. The Block in this instance will contain an Index, a Timestamp, the Data, a Hash, and the Previous Hash. These become my inputs of the Block definition as they will eventually be supplied as part of the output of the previous Block, once we begin to create the Chain through looping. For clarity in understanding the outputs of each Block, I prefixed each item with a title.
def Block(index, timestamp, data, hash, previousHash):
return “Index: ” + index.ToString(), “Timestamp: ” + timestamp.ToString(), “Data: ” + data.ToString(), “Hash: ” + hash.ToString(), “Previous Hash: ” + previousHash.ToString()
Every Blockchain must start with a hardcoded Block, which is often referred to as a Genesis Block. This allows the next Block in the chain and the first created by a transaction to have a previous Block to reference. We can easily define a Genesis Block as another definition. I define the index of the Genesis Block to be 0 and use the datetime.now() method to derive a timestamp. To create the hash we use the hashlib.sha256() method combining the information within the index, timestamp and data to generate this. The SHA (Secure Hash Algorithm) is one of a number of cryptographic hash functions and serves as a signature for text or data. The SHA-256 is one of the strongest hash functions available and it generates an almost-unique, fixed size 256-bit (32-byte) hash. This hash can only be replicated using the exact same data used to create it making alterations to the Block impossible. As this Genesis Block is hardcoded we can bypass the previous hash.
index = 0
timestamp = date.datetime.now()
data = “Genesis Block”
hash = hashlib.sha256(index.ToString() + timestamp.ToString() + data.ToString()).hexdigest()
previousHash = “”
return Block(index, timestamp, data, hash, previousHash)
The next step is to define how the Next Block in the sequence is created. The input of this is the previous Block and this definition should return a new Block as the output. It’s within this process we can determine the inputs required as part of the Block definition created above. The index of the Next Block will always be +1 from the previous Block’s index. I did some initial string manipulation to handle removing the titles prior to the addition. The timestamp is created by using the datetime.now() method as above and the data I am using for each Block is just some arbitrary text for this example. Again, the hash is constructed in the same manner as it was within the Genesis Block, but this time we can also reference the previous hash from the last Block.
str = lastBlock
rep = str.replace(” “,””)
split = rep.Split(“:”)
thisIndex = float(split) + 1
thisTimestamp = date.datetime.now()
thisData = “block” + thisIndex.ToString() + “data”
thisHash = hashlib.sha256(thisIndex.ToString() + thisTimestamp.ToString() + thisData.ToString() + lastBlock.ToString()).hexdigest()
str2 = lastBlock
rep2 = str2.replace(” “,””)
split2 = rep2.Split(“:”)
thisPreviousHash = split2
return Block(thisIndex, thisTimestamp, thisData, thisHash, thisPreviousHash)
With all our definitions created we can build a simple for loop in Python to begin building the required Blocks. First, I define a variable as count allowing the user to define how many Blocks they wish to create. Before initiating the loop we construct an instance of GenesisBlock() and assign it to the variable blockchain. Due to the output of the GenesisBlock being a tuple the previousBlock variable becomes blockchain. We then create an empty list to store all the Blocks within the Blockchain in and store this under the variable blockList. The loop itself builds the NextBlock using the input variables of the previousBlock. In the first instance of the loop this variable is the GenesisBlock as previously defined. Once the NextBlock method is executed the resulting Block is appended to the blockList before the variable of the previousBlock is overridden with the output. The loop then steps through the next iteration of the process, with the recently created NextBlock now becoming the previousBlock. This looping process continues through the range of iterations from 0 to our desired count we specified.
count = IN
blockchain = [GenesisBlock()]
previousBlock = blockchain
blockList = 
for i in range(0, count):
addBlock = NextBlock(previousBlock)
previousBlock = addBlock
OUT = blockList
This is a very basic example of implementation and the process in creating the hash has nothing to do with either ‘Mining’ or ‘Proof of Work’. Alongside this, the next steps would be to add a server layer that enables changes to the chain to be tracked through multiple machines making the system truly decentralised. Stay tuned!
If you are interested in collaborating on this in the future please feel free to get in contact.