Build your own block chain in 15 minutes on Node-RED using Node.js, JavaScript, Cloudant/CouchDB on a free IBM Cloud account…

Screen Shot 2016-08-17 at 09.17.36

Note: To do the tutorial you need a free Bluemix (IBM PaaS Cloud) account. You can obtain one here and the raw file (JSON) for this NodeRED flow is here.

Tutorial Objective

In this exercise you will generate a small block-chain implementation on your own. All you need is a free IBM Cloud account and some very basic programming skills (JavaScript). After you’ve completed the tutorial you’ll have a complete in-depth view how block-chains work.

Refresher – How Bitcoin consensus works

Screen Shot 2016-08-17 at 09.44.26

Let’s consider a transaction (or any arbitrary string) you want to securely store on a block chain.

Screen Shot 2016-08-17 at 09.44.34

In order to do this (so that the block chain accepts your transaction) you have to find (compute) a cryptographic token – a nonce – matching your (any only your) transaction (string).

Screen Shot 2016-08-17 at 09.50.40So in Bitcoin this is done by applying the sha256 hash function twice and…

Screen Shot 2016-08-17 at 09.55.06.png

…try different nonce values until you find a hash value starting with a number of d zeros. In Bitcoin d is called difficulty and is varied to achieve a constant block rate of 10 minutes. In other words, the chance of finding a nonce that generates a hash value for your transaction with d leading zeros decreases with increasing d. Therefore the computational power needed to find a valid bitcoin block also increases with d. Since the aggregated computational power of all bitcoin peers varies (but continuously increases) parameter d is automatically adjusted to obtain a constant block rate of one block every 10 minutes.

Screen Shot 2016-07-27 at 14.49.14.pngSo congratulations, you’ve understood the so called “proof-of-work” algorithm. Since finding a nonce is computationally intensive and validating if a transaction+nonce creates a hash code with d starting zeros can be done in milliseconds you have the asymetric cryptographic property that creating a transaction is complex but validating a transaction is easy.

It is interesting to know that the Bitcoin peer-to-peer network currently has a computing power of 19881622 Petaflops – in contrast the fastest supercomputer in the world only has 93014.6 Teraflops.

Let’s create our own hashing algorithm in JavaScript

Since JavaScript out of the box doesn’t provide access to the SHA256 hash function we’ll implement one on our own as formally described as follows:

Screen Shot 2016-07-27 at 14.49.15.png

Don’t be scared, basically what this algorithm does is to take the ASCII value of each character in the string and sum it up. Afterward the same is done for the same string but with the last character removed – and so on – recursively – until there are no more characters left.

So this can be easily implemented in JavaScript:

Screen Shot 2016-08-17 at 11.32.07.png

Let’s turn this hash function into a proof-of-work algorithm

Screen Shot 2016-08-17 at 10.52.22.png

So we will use this hash function in order to create a proof-of-work algorithm by just hashing our string together with a nonce and calculating a modulo operation. We change the nonce until we find one leading to zero. In JavaScript this will look something like this (note that msg.payload.transaction accesses the JSON property within NodeRED using OGNL):

Screen Shot 2016-08-17 at 11.35.08.png

So once we’ve calculated our nonce we are done, the only thing is to pour this into some sort of WebService that we don’t have to install anything on our machine. This can be done very easily using NodeRED:

Screen Shot 2016-07-27 at 14.49.16

This basically creates an HTTPS endpoint on your NodeRED instance listening to the /createTransaction endpoint URL. The only parameter you have to provide is “transaction” – the actual transaction string – and it will return the generated nonce and the computational cost – this is implemented in the JavaScript function in the middle and looks like this:

Untitled

Now just run a little test and see whether it works: Untitled

Submitting, verifying and storing a transaction into Cloudant/CouchDB

Since most block-chains are peer-to-peer networks we will simulating this by creating another HTTPS endpoint accepting transactions and corresponding nonce values, verifying them and in case correct, storing them to a data base.

Screen Shot 2016-08-17 at 11.52.47

The “submitTransaction” endpoint takes transaction and nonce as parameters and using the following JavaScript function we’ll check weather the transaction is valid or not and add this finding as status code to the “msg” JSON object flowing through our flow:

Untitled

We split the flow after this function and basically return the status code back to the caller:

Untitled

On the other branch of the spit we use the switch function to decide whether we want to let the message pass through or not based on the status code:

Screen Shot 2016-08-17 at 11.58.31

If accepted, we just store it in Cloudant/CouchDB, here is what’s necessary for doing that:

Screen Shot 2016-08-17 at 11.59.47.png

Pulling all together

Now we have all ingredients we need to implement a block chain consensus protocol. We can generate nonce values from transactions and we can submit, verify and store transactions to a data base. Most of block chains implement a sort of peer-to-peer protocol, but for sake of simplicity we’ll omit this step and hard-code the distribution list of participating peers. So now it’s time to convince a colleague or friend to implement the very same process in his Bluemix account. Screen Shot 2016-08-17 at 12.06.47.png

And guess what? We can implement this just as an additional endpoint within NodeRED. We take transactions and corresponding nonce values on the endpoint “distributeTransaction” and send them to all submitTransaction endpoints of all known peers. Just update the two http request nodes to point to the corresponding endpoints, one has to be yourself because you also wanna store the data in your data base or block chain respectively. In the second node just enter the endpoint of your peer:

Screen Shot 2016-08-17 at 12.09.24

So as an example this might look like this:

Screen Shot 2016-08-17 at 13.08.18.png

So once you’ve hit the publish button again and call “distributeTransaction” with a transaction string and a corresponding nonce value you will see something like this…

Screen Shot 2016-08-17 at 12.13.08

…and in addition to that you should see the transaction being stored in Cloudant/CouchDB:

Screen Shot 2016-08-17 at 15.28.12

Add an optional little user interface

So in case you want to add a little user interface you can do this in NodeRED straightforward, just add another http endpoint and use the template node in order to create the HTML from:

Conclusion

So as you can see implementing a block chain is very easy – at least for a learning and demo purpose. You should now have completely understood the concept of proof-of-work and distributed consensus.

 

Advertisements

Node-RED Blockchain

Image

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s