Blockchain Work Principles

Blockchain architecture attracts a lot of developers with its durability, fault-tolerance, and attack resistance. Blockchain can be implemented in almost any programming language, but some are highly recommended due to their simplicity and flexibility: Python, JavaScript, and GoLang. There have been a lot of blockchain implementations written on them, especially on Python and GoLang, due to Python’s simplicity. By the way, GoLang was chosen by Ethereum for its core.

As blockchain is still considered a young technology, there are not many blockchain developers working on Android or IOS. This feature has both positive and negative sides: it becomes an issue due to lack of experiential exchange, and every blockchain developer must consider him/herself a pioneer in a new and growing sphere.

Ready to Face the Challenge?

Before we start diving deep into writing blockchain code, let’s figure out some of the challenges in creating and maintaining the blockchain:

1. Security

Blockchain technology claims safety and security as one of its biggest advantages. The code of each blockchain (Ethereum, Bitcoin, EOS, and Cardano) is open-source, which means that any developer can see it, check for bugs, make improvements, make a fork, and present as his or her own new technology.

2. Pick up the pace!

Although blockchain technology is relatively young and needs development and expansion, its growth is speeding up in terms of geometric progression and new implementations. If you want to develop a blockchain, you should keep up with all new functions and current news.

3. Scalability

The blockchain is decentralized by nature, so it doesn’t require a third party to verify transactions. Instead, before the transaction is executed, each node has to prove it and save its copy. This makes the verification process time-consuming.

4. Which consensus to choose?

When a transaction appears, network participants must validate it. The number of validations needed for confirmation is determined by the blockchain, and may vary. The transaction won’t be marked as successful until all participants have come to a common decision. That is what consensus type stands for. There is Proof of Work (PoW) and Proof of Stake (PoS) to choose from, and each has its own set of advantages and disadvantages.

Proof of Work (PoW) is a consensus protocol that comes to action when a miner solves a mathematical problem to verify a transaction. As soon as he/she succeeds, a reward comes in the form of a coin to his/her wallet. The biggest advantages of PoW are:

It is stable (however, this advantage makes the confirmation process slower than in PoS).

It is difficult and expensive to hack.

Disadvantages of PoW to consider:

It is quite expensive to implement (due to the need for a high number of nodes to prevent the network from being compromised).

It requires an extremely powerful computer.

Proof of Stake (PoS) consensus features a completely different logic. When a transaction comes into the memory pool on the blockchain, the nodes verify it. But the order in which they will do this is defined by the consensus of the stake. A stake is considered to be the wallet balance of a node. For example, node A has 10 coins, and node B has 20. Node B will most likely be chosen to verify the transaction first.

The benefit that distinguishes PoS consensus is:

there is no need for extensive computational power.

This means that you can even use your mobile phone to confirm transactions.

The biggest disadvantage of the PoS type of consensus is that “the rich” are more likely to get even richer.

Choosing a type of consensus for your blockchain is rather difficult. Our Applicature specialists would be glad to help you with this issue, and will take your preferences into account.

Don’t get us wrong — we are not trying to persuade you that blockchain holds more disadvantages than advantages. Decentralized applications are, indeed, our future — for many reasons. The issues mentioned here are simply the flipside of everything the blockchain has to offer.

Essential to Know

Before starting to write your own blockchain, you need to know some of the essentials you will face while coding.

Blocks

Blockchain is a public ledger that stores data in so-called blocks. One block followed by another creates a chain of blocks. Each block has a specific hash.

The table below shows the structure of one block:

To maintain a concise chain, the hash of the previous block should be mentioned in the following block. Then the block goes through uploading onto the blockchain. To do this, a developer has to solve an NP-class problem. This process is called mining.

Transactions

There is also the notion of reward on the blockchain. This means that the miner who found the solution that fits the block gets the reward in coins.

He or she may manage money as he/she wishes, including transferring them to other miners (accounts). Money transfers are registered on the blockchain as transactions. Anyone can see any transaction that has ever been made, as they are not encrypted. This is why blockchain is a public ledger that brings transparency to many fields.

Hashes

Basically, a hash is a bunch of numbers, similar to what is illustrated below:

In a practical sense, it is a fingerprint of some digital data. If you type your name, it will have a certain hash in the code, but if you add your surname, the hash will change. Whenever you come back and type your name only again, you will get exactly the previous hash. Thus, a hash is a unique key to any information not dependent upon its longitude or weight.

Programming Languages

If you know at least one of the programming languages, you will be able to develop using all the others, due to the fact that you can find a lot of open-source solutions written on them. To develop blockchain quickly and simply, choose from the following languages: Python, Java, JavaScript, C++ GoLang.

Python is considered to be rather simple. It is an interpreted object-oriented programming language. When reading Python code, it might seem like reading English, which means this programming language is easy to learn and use.

Java is also an object-oriented and interpreted programming language, which is relatively simple to learn.

One of the peculiarities of Java is that it is strongly typed, which means it is capable of intensive error-checking. Any system with Java Virtual Machine implementation can read any applications written in Java.

Despite their similar-sounding names, Java and JavaScript are not really similar in their characteristics and usage. JavaScript is an object-based and case-sensitive scripting language. One crucial characteristic of JavaScript is that you can make new functions using the function command.

The main difference between C++ and the previously-mentioned programming languages is that C++ is mostly used for large projects due to its extended number of data types, functions, controls, and decision-making statements.

You are likely to already know the first characteristic of GoLang. Right! It’s an object-oriented programming language. But what distinguishes it from C++, for example, is that it’s faster. Thus, a huge program written in GoLang can be compiled in several seconds.

Blockchain from the Inside

The concept that blockchain technology offers to fields like finance or the supply chain is rather fascinating, but haven’t you been wondering how it looks from the inside? What do the transaction records look like on the blockchain?

Fortunately, you’ve got Applicature to introduce you to the full picture.

Now that you are fully prepared in terms of the theoretical material, we are ready to show you how to code your blockchain from scratch on Python. We’ve chosen it due to its simplicity and the availability of the appropriate libraries.

Open your IDE, create a new Python file, and follow the instructions:

Add All Needed Libraries

To create your own blockchain, you need libraries for HTTP, hash, time, and JSON. Just copy/paste the following ‘imports’:

Create a Class Block

Now it’s time for some OOP coding. As mentioned earlier, blockchain “under the hood” is made up of a chain of blocks, and is distributed. So we need to define the primary entity named ‘Blockchain’ by framing it in a class with properties like ‘current_transactions’ (transaction pool), ‘chain’(storage of blocks), and ‘nodes’ (storage of nodes):

Add a Node

Then we need to add functionality to the setup node. To the parameter address, we assign the HTTP address of the node (e.g. ‘http://192.168.0.5:5000'):

Validation

The next step will be determining functionality and the validity of the chain inside the blockchain node:

Check if the block proof is valid:

Add Your First Block

Now we’ve come to the most interesting point: creating the block. First, we call the new_block function. To the parameter ‘previous_hash’ we assign the hash of the previous block. If it is the first one, the hash will consist only of zeros. The proof parameter requires a block existence proof.

Add a Transaction

If you are the happy owner of cryptocurrency assets, you will probably want to transfer them to someone. By the way, a transaction is the smallest structural unit produced by blockchain. Let’s define functionality for the creation of transactions.

There are several parameters that are necessary to add: sender address, recipient address, number of the tokens you would like to send, and the index of the block that will hold the data about this transaction.

When you add the needed number of blocks to your blockchain, it will look as follows:

Congrats! You’ve created your very first (or not) blockchain. You can also get additional support from the specialists at Applicature.