October 30th, 2017 Posted by Blockchain, computation, Download, Dynamo, Python, Revit, Tools 4 comments on “BUILDING A BLOCKCHAIN IN DYNAMO & PYTHON”

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.

Now you have a basic understanding of the principle, I will explain how I created and implement my own Blockchain from within Dynamo. Again, there are several good blogs online which outline how to build the simplest form of a Blockchain but the one I used for reference was the article below. The author demonstrates the process in JavaScript so I thought I would mimic the process in Python.

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.

import clr
import sys
sys.path.append(r’C:\Program Files (x86)\IronPython 2.7\Lib’)
import hashlib
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 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.

def GenesisBlock():
index = 0
timestamp =
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 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.

def NextBlock(lastBlock):
str = lastBlock[0]
rep = str.replace(” “,””)
split = rep.Split(“:”)
thisIndex = float(split[1]) + 1
thisTimestamp =
thisData = “block” + thisIndex.ToString() + “data”
thisHash = hashlib.sha256(thisIndex.ToString() + thisTimestamp.ToString() + thisData.ToString() + lastBlock[3].ToString()).hexdigest()
str2 = lastBlock[3]
rep2 = str2.replace(” “,””)
split2 = rep2.Split(“:”)
thisPreviousHash = split2[1]
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[0]. 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[0]

blockchain = [GenesisBlock()]
previousBlock = blockchain[0]

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.

Tags: , , , ,


Ryan Cameron says:

Very interested in this concept!

Arjan Peeters says:

Nice. really interested in the next step(s)!

Arjan Peeters says:

Maybe intruduse a class in python. and use it to create blocks. And please use the IFCguid as ID/index?

rupal says:

Hi nice article and also helpful. We are also Blockchain Development Company,
if you are looking for Blockchain services we are here to give you the best solution.

Leave a Reply

Your email address will not be published.This is a required field!

<small>You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes:<br> <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>