In this guide, we will go through some of the more major Blockchain coding.
The blockchain technology is incredibly fascinating. It won’t be far-fetched to think of a future that will be built entirely on it. So, what do you need to learn in order to start developing on the blockchain? Which languages will give you the edge? Let’s get started on blockchain coding!
Programming Blockchain Problems
Before we begin, let’s check out some of the challenges that a blockchain developer faces. Creating and maintaining a public blockchain is not easy because of a number of reasons.
(Before we continue, a huge shoutout to David Schwartz for his keynote address regarding C++ use in blockchain software development in CPPCON 2016.)
Blockchain Coding
Blockchain Coding Difficulty Challenge #1: Security
Blockchains, as David Schwartz puts it, should be fortresses. Firstly, the code is public and open for all to see. Anyone can look over the code and check for bugs and vulnerabilities. However, unlike other open code resources, the downside of finding vulnerabilities on blockchain code is massive. Any programmer can hack in and get away with potentially millions and millions of dollars. Because of these legitimate security concerns, development on blockchain is usually very slow.
Blockchain Coding Difficulty Challenge #2: Resource Management
It is important to keep pace with the network. You cannot fall too far behind and not keep up with all the network demands. You should be well equipped to handle remote and local queries.
Blockchain Coding Difficulty Challenge #3: Performance
The blockchain must always perform at its highest possible capabilities, but for that to happen the language chosen must be extremely versatile. The thing is that there are certain tasks in the blockchain which are parallelizable whilst there are some tasks that can’t be done in parallel.
A good example of a “parallelizable” task is digital signature verification. All that you need for signature verification is the key, transaction, and signature. With just three data you can conduct verifications in a parallelized manner.
However, not all the functions on a blockchain should be done that way. Think of transaction execution itself. Multiple transactions can’t be executed in parallel; it needs to be done one at a time to avoid errors like double spends. Some languages are good at parallel operations while some are good in non-parallel operations.
Blockchain Coding Difficulty Challenge #4: Isolation
What is deterministic behavior?
- If A + B = C, then no matter what the circumstances, A+B will always be equal to C. That is called deterministic behavior.
- Hash functions are deterministic, meaning A’s hash will always be H(A).
So, in blockchain development, all transaction operations must be deterministic. You cannot have a transaction that behaves one way and then behaves another way the next day. Similarly, you cannot have smart contracts that work in two different ways in two different machines.
The only solution to this is isolation. Basically you isolate your smart contracts and transactions from non-deterministic elements.
So, we have discussed the main problems that blockchain developers face. Now let’s finally check out some of the languages that the developers can use to code on the blockchain.
Blockchain Coding Language #1: C++
First and foremost, let’s start with the granddaddy of them all, the evergreen C++. C++ was created by Bjarne Stroustrup as an extension of the C language. The Language was designed to have the flexibility and efficiency of the C but with some major differences. The biggest difference between C and C++ is that while C is process-oriented, C++ is object-oriented.
What this means is that, in C++, the data and functions are wrapped into one neat little package called “objects” which means that once an object is created, it can easily be called and reused in other programs, which greatly reduces coding time.
Let’s look at the simplest C++ program in the world. The “Hello World” program:
#include <iostream.h> main() { cout << "Hello World!"; return 0; }
This code will print” Hello World!”
So, why do people still use C++ for coding? Surely there are way more glamorous languages now, why do people still insist on going back to C++? Why is the bitcoin blockchain coded on C++?
Well, as it happens, C++ has certain features that make it very appealing. (Shout out Peter Wiulle and David Schwartz for the following explanation).
Feature #1: Memory Control
Remember what we said earlier about the challenges of blockchain development? Not only should blockchains be secured fortresses but they should have effective resource management as well. A blockchain is supposed to interact with a lot of untrusted endpoints while still giving quick service to any and all nodes.
This quick and prompt service is critical for the success of a cryptocurrency like bitcoin. Remember, they are all based on the principle of “consensus”, all nodes on the network must accept and reject the exact same blocks, or else there could be a fork in the chain.
In order to satisfy all these demands and perform at the highest level, you need tight and complete control over CPU and memory usage. C++ gives that to its users.
Feature #2: Threading
As we have discussed before, one of the main challenges of the blockchain programming is the integration of tasks that parallelize well and the tasks that don’t parallelize. Most languages specialize in one, however, C++’s threading ability is good enough to handle both parallel and non-parallel tasks. A thread is a set of instructions that can be executed simultaneously. Not only does C++ allow for superb multithreading facilities with effective inter-thread communication, but it also optimizes single-thread performance.
Feature #3: Move Semantics
One of the most interesting aspects of C++ is move semantics. Move semantics provides a way for the contents to be moved between objects rather than be copied outright. Let’s check out the differences between copy semantics and move semantics. (Following data are taken from Peter Alexander’s answer in “Stackoverflow”).
Copy Semantics:
- assert(b == c);
- a = b;
- assert(a == b && b == c);
So what is happening here? The value of b goes into a and b remains unchanged at the end of the whole thing.
Now, consider this.
Move Semantics:
- assert( b = = c);
- move (a,b);
- assert (a = =c );
What is happening here?
Can you see the difference between the two blocks of codes?
When we are using the move semantics, the value of “b” need not be unchanged. That is the difference between copy semantics and move semantics. The biggest advantage of move semantics is that you can get copies of certain data only when you need them, which greatly decreases redundancy in the code and gives a huge performance boost. So as you can see, this efficient memory management and high performance are both desirable for the blockchain.
Feature #4: Compile Time Polymorphism
What is polymorphism?
Remember when we called C++ an “object-oriented programming (OOP) language”? Polymorphism happens to be an OOP property. Using polymorphism, you use a particular feature in more than one way. In C++ polymorphism can be used in two ways:
- Compile-time polymorphism.
- Run time polymorphism.
Over here, we will only be focusing on compile-time polymorphism. There are two ways that C++ implements compile-time polymorphism:
- Function Overloading.
- Operator Overloading.
Function Overloading:
Function overloading is when you have many functions of the same name but with different parameter intake.
Consider this program:
#include <bits/stdc++.h> using namespace std; class A { void func (int x) //first instance of the function takes only one integer value { cout<<x<<endl; } void func (double x) //second instance of the function takes only one double value { cout<<x<<endl; } void func (int x, int y) //third instance of the function takes two integer values { cout<<x=y<<endl; } } int main() { A obj1 //making one object of the class A //now we are going to call the functions obj1.func(2); obj1.func(2.65); obj1.func(2,5); return 0; }
Now when you run this function the output will be:
- 2
- 2.65
- 7
So, as you can see, the same function func() was used in 3 different ways.
Operator Overloading:
In C++ the same operator can have more than one meaning.
- Eg. “+” can be used both for mathematical addition and for concatenation.
- Concatenation basically means taking two strings and combining them as one.
- So 3+4 = 7.
AND
- Block+geeks = Blockgeeks.
- The same operator did two different functions, this is operator overloading.
Compile-time polymorphism helps a lot in blockchain development. It helps in putting responsibilities separately in various functions and, in turn, boosting the performance of the whole system.
Feature #5: Code Isolation
C++ has namespace features which can be imported from one program to another. Namespace helps in avoiding name collisions. Also, since C++ has classes, it can act as boundaries between various APIs and help in making clear separation.
A class in C++ is a user defined type or data structure declared with keyword class that has data and functions as its members. You can access the functions declared in the class by declaring objects of that particular class.
Feature #6: Maturity
The language is both mature and regularly updated. There are at least 3 solid compilers, as David Schwartz says, and the new features are aimed at solving real issues. Debuggers and analytical tools of all kinds are available for everything from performance profiling to automatic detection of issues of all kinds. This means the language is constantly growing to incorporate newer and better features.
Because of the above features, Satoshi Nakamoto chose C++ to be the base language of the bitcoin source code.
Blockchain Coding Language #2: Javascript
Up next we have Javascript.
Along with HTML and CSS it is one of the three core technologies in World Wide Web Content Production. Javascript is usually used to create highly interactive webpages. So, now we will see how to create a very simple blockchain using Javascript. Huge shoutout to savjee.be for the content in this section.
Suppose, we want to create a simple blockchain in Javascript. Before we do so, there are certain things that we need to address.
What is a blockchain and how exactly does it work…code-wise?
A blockchain is basically a chain of blocks which contain data. It is basically a glorified linked list. However, what makes it so special? A blockchain is immutable. Meaning, once a data goes inside a block, it can never be changed. How does a blockchain attain immutability? It is because of a simple but ingenious mechanism called “hashing”. Check out the diagram below:
Each block is connected to the previous block via a hash pointer which contains the hash of the previous block. So, how does this make the chain immutable?
One of the most fascinating properties of cryptographic hash functions is that if you even change the input by a little bit, it can greatly affect the output hash. Eg. Check this out:
Just changing the first “T” from upper to lower case drastically changed the output hash so much.
So, how does this affect the blockchain?
Each block is connected to the previous one via the hash pointer. So, if someone were to tamper the data in a block, it would change the hash drastically and as a result, end up affecting the whole chain (as all the blocks are linked). This would freeze up the chain which is an impossibility and hence the blocks remain unchanged.
So, how do we make a block? What does a simple block consist of? In our simple cryptocoin that we are going to make (Let’s call it “BlockGeeksCoin”), each block will have the following pieces of information:
- Index: To know the block number.
- Timestamp: To know the time of creation.
- Data: The data inside the block.
- Previous Hash: The hash of the previous block.
- Hash: The Hash of the current block.
Before we continue. You need to understand certain terms that we are going to use in our program:
- this: The “this” keyword is invoked inside a function and enables you to access the values inside a specific object that calls that particular function.
- Constructor: A constructor is a special function which can help create and initialize an object within a class. Each class is restricted to only one constructor.
Now that that’s done, let’s start making our block.
Creating the Block
const SHA256 = require("crypto-js/sha256"); class Block { constructor(index, timestamp, data, previousHash = '') { this.index = index; this.previousHash = previousHash; this.timestamp = timestamp; this.data = data; this.hash = this.calculateHash(); } calculateHash() { return SHA256(this.index + this.previousHash + this.timestamp + JSON.stringify(this.data)).toString(); } }
Code Analysis
Ok, so this right here is out block. So, in the first line of the code we called the crypto-js library because the sha256 hash function is not available in JavaScript.
Next, we invoked a constructor inside the class to call for objects which will have certain values. The thing that probably catches your eye is the calculateHash() function. Let’s see what exactly is it doing.
In a block, we take all the contents and hash them to get the hash of that particular block. We are using the JSON.stringify function to turn the data of the block into string to hash it.
Ok, so we have the block ready and good to go. Now let’s connect the blocks together into a blockchain.
Creating the blockchain
class Blockchain { //Section 1 Genesis block creation constructor() { this.chain = [this.createGenesisBlock()]; } createGenesisBlock() { return new Block(0, "01/01/2017", "Genesis block", "0"); } //section 2 adding new blocks getLatestBlock() { return this.chain[this.chain.length - 1]; } addBlock(newBlock) { newBlock.previousHash = this.getLatestBlock().hash; newBlock.hash = newBlock.calculateHash(); this.chain.push(newBlock); } //section 3 validating the chain isChainValid() { for (let i = 1; i < this.chain.length; i++) { const currentBlock = this.chain[i]; const previousBlock = this.chain[i - 1]; if (currentBlock.hash !== currentBlock.calculateHash()) { return false; } if (currentBlock.previousHash !== previousBlock.hash) { return false; } } return true; } }
Code Analysis
Ok, so a lot of things are going on in the chain above, let’s break it down to sections.
Section 1: The Genesis Block
What is the genesis block?
The genesis block is the first block of the blockchain, and the reason why it is special is that while every bock points to the block previous to it, the genesis block doesn’t point at anything. So, the moment a new chain is created, the genesis block is invoked immediately. Also, you can see a “createGenesisBlock()” function wherein we have given the data of the block manually:
createGenesisBlock()
{
return new Block(0, “01/01/2017”, “Genesis block”, “0”);
}
Now that we have built the genesis block, let’s build the rest of the chain.
Section 2: Adding The Blocks
Firstly, we will need to know what the last block in the blockchain currently is. For that we use the getLatestBlock() function.
getLatestBlock() { return this.chain[this.chain.length - 1]; } Now that we have determined the latest block, let’s see how we are going to add new blocks. addBlock(newBlock) { newBlock.previousHash = this.getLatestBlock().hash; newBlock.hash = newBlock.calculateHash(); this.chain.push(newBlock); }
So, what is happening here? How are we adding the blocks? How are we checking if the given block is valid or not?
Remember the contents of a block?
A block has the hash of the previous block right?
So, what we are going to do here is simple. Compare the previousHash value of the new block with the hash value of the latest block.
If these two values match, then this means that the new block is legit and it gets added to the blockchain.
Section 3: Validating the Chain
Now, we need to check that nobody has been messing with our blockchain and that everything is stable.
We are using the “for” loop to go from block 1 to the last block. Genesis block is block 0.
for (let i = 1; i < this.chain.length; i++) { const currentBlock = this.chain[i]; const previousBlock = this.chain[i - 1]; In this part of the code we are defining two terms, current block and previous block. And now we are simply going to find the hash of these two values. if (currentBlock.hash !== currentBlock.calculateHash()) { return false; } if (currentBlock.previousHash !== previousBlock.hash) { return false; } } return true; }
If the “previousHash” of the current block is not equal to the “Hash” of the previous block, then this function will return False, else it will return True.
Using the blockchain
Now, we are going to finally use the blockchain to create our BlockGeeksCoin.
- let BlockGeeksCoin = new Blockchain();
- BlockGeeksCoin.addBlock(new Block(1, “20/07/2017”, { amount: 4 }));
- BlockGeeksCoin.addBlock(new Block(2, “20/07/2017”, { amount: 8 }));
And that’s it!
So what happened here?
We created a new cryptocurrency based on the blockchain and named it BlockGeeksCoin. By invoking this new object, I activated the constructor, which in turn created the Genesis block automatically.
We simply added two more blocks to it and gave them some data.
It is that simple.
(Thank you savjee.be for the amazing and simple explanation.)
Blockchain Coding Language #3: Python
Guido van Rossum, a Dutch programmer, created Python back in 1991. Python is based on a simple philosophy: Simplicity and Minimalism. One of the more notable ways that they incorporated simplicity into their language is by using white spaces to signify code blocks instead of curly brackets or keywords. Let’s see what this means.
Let’s check out a simple “hello world” program.
print(‘Hello, world!’)
Yup, that’s it!
Compare that to the C++ “hello world” program.
See how less complicated it is in comparison? How about we do something a little more complicated? Let’s say we are adding two numbers and printing the result.
num1 = 1.5
num2 = 6.3
sum = float(num1) + float(num2)
print(‘The sum of {0} and {1} is {2}’.format(num1, num2, sum))
And that’s it.
The output of this program will be:
- The sum of 1.5 and 6.3 is 7.8
So, let’s up the ante. How are we going to program an entire blockchain using Python? The following data and code is taken from Gerald Nash’s article in Medium.
Creating the block
Firstly, let’s make our block:
import hashlib as hasher
class Block: def __init__(self, index, timestamp, data, previous_hash): self.index = index self.timestamp = timestamp self.data = data self.previous_hash = previous_hash self.hash = self.hash_block() def hash_block(self): sha = hasher.sha256() sha.update(str(self.index) + str(self.timestamp) + str(self.data) + str(self.previous_hash)) return sha.hexdigest()
Code Analysis
We are starting off by importing the hash library to use the SHA 256 hash finctions (quite like Javascript).
Just like before, the block has the same value:
- Index.
- Timestamp.
- Data.
- Previous hash.
- Hash.
Once against, we are filling up the hash values via a function, same as before.
Creating the genesis block
Now, let’s create the Genesis block:
import datetime as date
def create_genesis_block():
return Block(0, date.datetime.now(), “Genesis Block”, “0”)
Code Analysis
We have imported datetime to put in the timestamp.
We simply generated the genesis block and manually given it some data to work with. The previous hash value is “0” because it is pointing to no other block.
Creating the rest of the blocks
Now let’s define how the subsequent blocks will be created.
def next_block(last_block): this_index = last_block.index + 1 this_timestamp = date.datetime.now() this_data = "Hey! I'm block " + str(this_index) this_hash = last_block.hash return Block(this_index, this_timestamp, this_data, this_hash)
Code Analysis
So, how are we going to be determining the values of each and every piece of data inside each and every block?
The block index is simply the index of the last block + 1.
The timestamp is the current date and time.
The data of the block is a simple message: “Hey! I’m block <index number>”.
Hash we are calculating using the function we definted earlier.
And ultimately, we are returning all these values to the block.
Creating the blockchain
Finally, let’s create the blockchain.
blockchain = [create_genesis_block()] previous_block = blockchain[0] num_of_blocks_to_add = 15 for i in range(0, num_of_blocks_to_add): block_to_add = next_block(previous_block) blockchain.append(block_to_add) previous_block = block_to_add # Tell everyone about it! print "Block #{} has been added to the blockchain!".format(block_to_add.index) print "Hash: {}n".format(block_to_add.hash)
Code Analysis
Firstly, we create the genesis block and give its value to “previous_block”.
Then we determine how many blocks to add, in this example we are going with 15.
So we are running a loop that goes till 15 and adds each and every block to the blockchain. At the end of the look, we are printing which number block has been added to the blockchain via showing their index number. Plus, we are printing the Hash as well.
This is what the output will look like:
Obviously in both this and the javascript you could add more complicated features like Proof Of Work. If you want to learn how to implement that then it is highly recommended to go through Gerald Nash’s article. But for now, you at least know how to create a simple blockchain in Python.
Language #4: Solidity
Finally, we come to Solidity. For anyone who wants learn how to make DAPPs (Decentralized Applications) or get into the ICO game, learning Solidity is an absolute must. We already have a detailed guide on it which you can read here. However, here we are going to give you a basic overview. Solidity was developed by Gavin Wood, Christian Reitwiessner, Alex Beregszaszi, Yoichi Hirai and several former ethereum core contributors to enable writing smart contracts on blockchain platforms such as Ethereum.
Solidity is a purposefully slimmed down, loosely-typed language with a syntax very similar to ECMAScript (Javascript). There are some key points to remember from the Ethereum Design Rationale document, namely that we are working within a stack-and-memory model with a 32-byte instruction word size, the EVM (Ethereum Virtual Machine) gives us access to the program “stack” which is like a register space where we can also stick memory addresses to make the Program Counter loop/jump (for sequential program control), an expandable temporary “memory” and a more permanent “storage” which is actually written into the permanent blockchain, and most importantly, the EVM requires total determinism within the smart contracts.
So, before we continue, let’s checkout a basic Solidity contract example. (Codes taken from github).
Let’s run a simple while loop in solidity:
contract BasicIterator { address creator; // reserve one "address"-type spot uint8[10] integers; // reserve a chunk of storage for 10 8-bit unsigned integers in an array function BasicIterator() { creator = msg.sender; uint8 x = 0; //Section 1: Assigning values while(x < integers.length) { integers[x] = x; x++; } } function getSum() constant returns (uint) { uint8 sum = 0; uint8 x = 0; //Section 2: Adding the integers in an array. while(x < integers.length) { sum = sum + integers[x]; x++; } return sum; } // Section 3: Killing the contract function kill() { if (msg.sender == creator) { suicide(creator); } } }
So, let’s analyze.
Section 1: Assigning Values
In the first step, we are filling up an array called “integers” which takes in 10 8-bit unsigned integers. The way we are doing it is via a while loop. Let’s look at what is happening inside the while loop.
while(x < integers.length) {
integers[x] = x;
x++;
}
Remember, we have already assigned a value of “0” to the integer x. The while loop goes from 0 to integers.length. Integers.length is a function that returns the max capacity of the array. So, if we decided that an array will have 10 integers, arrayname.length will return a value of 10. In the loop above, the value of x goes from 0 – 9 (<10) and assigns the value of itself to the integers array as well. So, at the end of the loop, integers will have the following value:
0,1,2,3,4,5,6,7,8,9.
Section 2: Adding the array content
Inside the getSum() function we are going to add up the contents of the array itself. The way is going to do it is by repeating the same while loop as above and using the variable “sum” to add the contents of the array.
Section 3: Killing the contract
This function kills the contract and sends the remaining funds in the contract back to the contract creator.
When asked about what was the inspiration and motivation behind creating solidity, Dr. Gavin Woods said this:
“It [Solidity] was meant to be a sophisticated tool for developing contracts that could ultimately give both developers and users good information on what the code did. To help this along, I devised NatSpec, a contract-friendly documentation format, and made that a first-class citizen in Solidity. I also proposed a formal proofing language subset (not yet implemented) in order to maximise the kinds of correctness guarantees that could be made.
I introduced events as a first-class citizen into the Solidity language in order to provide a nice abstraction for LOGs similar in form to function calls. Inspiration for that came from the Qt meta-object system’s “signals”.
One later feature that Christian R. and I figured out together was function modifiers; that allows attributes placed as part of a function signature to make some modifications to the apparent function body. Being a very declarative means of expression, it’s an idiom that falls nicely into the contract-oriented programming space.”
Blockchain Coding: Conclusion
In this article, we have only covered 4 languages for blockchain coding that are used in developing in and around the blockchain. In reality, there are many many more languages that you can potentially use (Java, Go). If you are a programmer, then the possibilities for you are truly endless. As the world becomes more and more decentralized and blockchain becomes more and more mainstream, the future for you is definitely limitless.