Building a Blockchain (1)

Next Post

In the past month, I started to work at Rootstock dev team, a very interesting project involving smart contracts over distributed ledgers. In recent weeks, I studied about Bitcoin, Ethereum, and cryptocurrencies. I read many papers, books, and reviewed some code implementation. My links:

I want to describe the essential elements of a distributed blockchain, and its importance. I hope to write source code implementing those ideas, in a simple way, grasping what is the core of all implementations.

A blockchain is a list of blocks, starting from the first one, called the genesis block:

A block has information. In crypto-currencies, that information is usually called transactions:

A transaction describe, in such domains, a transference of crypto-currency, named bitcoins or ether or whatever. But the essence is: a transaction is a piece of information that describe the change of world state.

There is an initial world state after the appearance of the genesis block, and each block, having 0 or more transactions (and, usually, a block finalization implicit transaction), ALTERS the world state:

The transactions should be valid: no transaction is allowed to transform the world state into an invalid one. A typical example of an invalid transaction: one than transfer crypto-currencies from an inexistent account, or from an account with insufficient funds.

The system is not running in one dedicated server. Instead, a network of independent machines is running the software, called the nodes, running the client software (“client” name is a bit confused, because implies the existent of a “server”, but no: each node is the client of other nodes).

Nodes are connected to some other nodes, and the network could have hundreds or thousands of nodes.

There are new transactions that are injected in the system, using specialized software. A node can received a new transaction, and send it to its neighbors:

Some specialized nodes, having all the resources to validate and execute transactions, generates new blocks, containing zero or more transactions. In bitcoin and similar systems, there are economics incentives for such block producers, called miners. The miners gain crypto-currencies, for each created block, and collecting fees from mined transactions.

When a miner (say, N2) produces a new block, it sends it to their neighbor nodes, eventually reaching the whole network:

Many nodes keep the full blockchain, and at receiving a new block, they adds it to its own version of the blockchain, if the block is valid. But sometimes, there are many competing blocks to be added:

And one part of the network could have a blockchain different from the blockchain of other nodes:

In these cases, there is an algorithm to reach consensus. Once the consensus is reached, the blockchain is a distributed one (many nodes have its content and world state), and the mined transactions are accepted in the distributed version.

There are many details to be discussed:

– When a block is valid?
– When a transaction is valid?
– How to generate a transaction? (a transaction moving value from one account to another cannot be generated by anyone, only for the giving account owners)
– How to reach consensus?
– How to store the blockchain?
– How to store the state after each block transactions execution?
– How to transmit the transactions and mined blocks from node to node?

And I want to write some example code, giving a minimal and essential. I created a C# class library. It is being writing using TDD (Test-Driven Development):

Stay tuned!

Angel “Java” Lopez

2 thoughts on “Building a Blockchain (1)

  1. Pingback: Building A Blockchain (2) | Angel "Java" Lopez on Blog

  2. Pingback: Programming for RSK | Angel ”Java” Lopez on Blog

Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s