Angel \”Java\” Lopez on Blog

March 28, 2016

Building A Blockchain (2)

Previous Post
Next Post

In the past days, I wrote a first implementation of a blockchain, using TDD (Test-Driven Development). Simplicity guides my decision: the blockchain is in-memory, and blocks are identified by number and a hash. Two blocks with number 42 are differente blocks if they have different hashes. A block has the parent block hash (the parent block number is deduced, it’s the block number minus one). A genesis block has number 0, and null parent hash. In this way, I have all the ingredients to build a blockchain, from genesis block to best block, chaining the blocks using their numbers and hashes.

I have a class BlockChain (yes, I prefer the mixed case name). There is a method to add a new block to the blockchain. It controls the new block has as parent the current best block in the blockchain.

But there are other nodes, that send other blocks, that can or cannot be added to the blockchain. How to process those blocks? I collect the blocks that cannot be added to the current blockchain in other objects, I call them blockbranch:

In the second blockbranch, parent block for block 41 is unknown, so, the blockbranch is waiting for the arrival of that block. The first blockbranch is connected to blockchain, as an alternative branch. But is has a height less than blockchain height.

A block branch has one or more consecutive blocks. The blocks are not part of the current blockchain. But they are proto-blockchain.

In the second blockbranch of the above figure, parent block for block 41 is unknown, so, the blockbranch is waiting for the arrival of that block. The first blockbranch is connected to blockchain, as an alternative branch. But is has a height less than blockchain height.

When I have sufficient blocks in a block branch (maybe, connect the bottom of the blockbranch to an existing block in another blockbranch or in the current blockchain), and I can build a list of blocks from genesis to the block in block branch, the branch is a candidate to be the new blockchain. Suppose a new block arrives:

The new block can be added to the second block branch, and it has an existing parent in the current blockchain. So, the block branch now has a complete path of block from genesis.

If the block branch is valid (applying their blocks to a known valid state at end of main block 39), and its height is greater than the current blockchain, the block branch is promoted to be the new blockchain:

The process works even if the new blocks arrives in random order. To manage the creation, growth, and promotion of blockchain and related blockbranches, I have a separate object, of class BlockProcessor, in charge of all this orchestration. The processor receives the new blocks, and send them to the corresponding blockchain or branch. Then, it can detect any new connection between branches, and the formation of branches that can be promoted to blockchains.

In the next post: details of a DSL (Domain Specific Language) I’m using to test different scenarios for the block processor.

Stay tuned!

Angel “Java” Lopez
http://www.ajlopez.com
http://twitter.com/ajlopez

March 21, 2016

Building a Blockchain (1)

Filed under: Bitcoin, Blockchain, C Sharp, Ethereum, Open Source Projects — ajlopez @ 8:50 am

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:

http://delicious.com/ajlopez/bitcoin
http://delicious.com/ajlopez/ethereum
http://delicious.com/ajlopez/blockchain

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):

https://github.com/ajlopez/BlockchainSharp

Stay tuned!

Angel “Java” Lopez
http://www.ajlopez.com
http://twitter.com/ajlopez

March 6, 2016

New Month’s Resolutions: March 2016

Filed under: C Sharp, Crystal, JavaScript, NodeJs, Open Source Projects — ajlopez @ 4:01 pm

A new month starts, time to write the new resolutions, but first, review the previous ones.

– Improve AjGenesisNode-Express [pending]
– Work on CrysJS [complete] see repo
– Work on CrysSharp [pending]
– Work on Memolap [pending]
– Work on SimpleMemolap [pending]
– Improve my SimpleGA samples [pending]
– Improve SharpGo [complete] see repo
– Improve Husky [pending]
– Work on GoLin [pending]
– Improve ImmuO [complete] see repo
– More ReactJS samples [pending]
– Improve Aktores [pending]
– Improve ErlSharp [pending]

It was a month to start a new job, an very interesting project related to Ethereum. Additionally, I worked on:

– Improve RuScript [complete] see repo
– Start EthSharp, Ethereum-like node in C# [complete] see repo
– Start EthSharpVm, Ethereum-like virtual machine, in C# [complete] see repo
– Improve ElixirJS [complete] see repo
– Improve SimpleForth [complete] see repo
– Start CrLisp [complete] see repo

My new resolutions:

– Improve AjGenesisNode-Express
– Work on CrysJS
– Work on CrysSharp
– Improve my SimpleGA samples
– Work on SharpGo
– Work on EthSharp

Stay tuned!

Angel “Java” Lopez
http://www.ajlopez.com
http://twitter.com/ajlopez

February 7, 2016

New Month’s Resolutions: February 2016

Filed under: C Sharp, JavaScript, NodeJs, Open Source Projects — ajlopez @ 6:20 pm

Before declaring my new resolutions, first let review the previous month’s ones:

– Work on SimpleGA trader samples [pending]
– Work on SimpleDatabase [pending]
– Work on SimpleMemolap [complete] see repo
– Work on Memolap [complete] see repo
– Improve AjGenesisNode [pending]
– Improve SharpGo [complete] see repo
– Improve Husky [complete] see repo
– Work on GoLin [complete] see repo

Also, I was working on:

– Created SRedux, a Redux-like library [complete] see repo
– Created ImmuO, immutable objects in JavaScript [complete] see repo
– Started CrysSharp, Crystal language in C# [complete] see repo
– Started CrysJS, Crystal language in JavaScript [complete] see repo
– Add ReactJS samples to JavaScript samples [complete] see repo
– Improve ElixirJS [complete] see repo
– Created ReduMan, reducer manager in JavaScript [complete] see repo
– Created RkStreams, reactive streams in JavaScript [complete] see repo

My new resolutions:

– Improve AjGenesisNode-Express
– Work on CrysJS
– Work on CrysSharp
– Work on Memolap
– Work on SimpleMemolap
– Improve my SimpleGA samples
– Improve SharpGo
– Improve Husky
– Work on GoLin
– Improve ImmuO
– More ReactJS samples
– Improve Aktores
– Improve ErlSharp

Keep tuned!

Angel “Java” Lopez
@ajlopez

January 5, 2016

New Month’s Resolutions: January 2016

Filed under: C Sharp, JavaScript, NodeJs, Open Source Projects — ajlopez @ 9:34 am

A new year started, it’s time to write the new month’s resolutions. But first, let review the past month’s ones:

– Prepare a FinTech-related talk [pending]
– Work on Husky [pending]
– Work on SharpGo [complete] see repo
– Improve AjGenesis for Node [complete] see repo
– Publish AjLispJava [pending]
– Work on Golin [pending]
– Work on RkModel [pending]
– Improve SimpleDT [complete] see repo

Additionally I was working on:

– Improving Memolap [complete] see repo
– Improving SimpleMemolap [complete] see repo
– Improving ElixirJS [complete] see repo
– Improving SimpleDatabase [complete] see repo
– Improving SimpleLisp [complete] see repo
– Starting SimpleTasks [complete] see repo
– Publishing a new version of SimpleAsync [complete] see repo
– Starting MoneyPool [complete] see repo
– Starting and Publishing MongoRepo [complete] see repo
– Refactoring SimpleGA and adding a new traders sample [complete] see repo
– Updating AjGenesisNode-Entity to a new version [complete] see repo
– Updating AjGenesisNode-Model to a new version [complete] see repo
– Updating AjGenesisNode-Express to a new version [complete] see repo
– Simplifying SimpleRules and adding a DSL [complete] see repo
– Improving OStore and publishing a new version [complete] see repo
– Adding JSON support to SimpleDSL [complete] see repo
– Adding recursive option to SimpleUnit [complete] see repo
– Starting BTraderLib [complete] see repo

A lot of work! My new month’s resolutions:

– Work on SimpleGA trader samples
– Work on SimpleDatabase
– Work on SimpleMemolap
– Work on Memolap
– Improve AjGenesisNode
– Improve SharpGo
– Improve Husky
– Work on GoLin

Stay tuned!

Angel “Java” Lopez
http://www.ajlopez.com
http://twitter.com/ajlopez

December 5, 2015

New Month’s Resolutions: December 2015

Filed under: C Sharp, Java, JavaScript, NodeJs, Open Source Projects — ajlopez @ 6:35 pm

The last month of the year have started. Time to review past month’s resolutions:

– Work on SJonT [partial] see repo
– Work on SparkSharp [complete] see repo
– Work on Husky [complete] see repo
– Work on MessiJS [pending]
– Work on ElixirJS [complete] see repo
– Work on GStones [pending]
– Work on SharpGo [complete] see repo
– Prepare a NodeJS one-day course [complete]
– Prepare a FinTech-related talk [partial]

Additionally, I worked on:

– Start MeliLib, access to Mercado Libre API using NodeJS [complete] see repo
– Start RkModel, reactive model [complete] see repo
– Start Golin, Lisp interpreter in Go [complete] see repo
– Improve AjGenesisNode-Express [complete] see repo
– Start Go samples [complete] see repo
– Improve AjGenesisNode-Entity [complete] see repo
– Improve AjGenesisNode-Model [complete] see repo
– Improve AjGenesisNode using SimpleJson [complete] see repo
– Start GccSamples [complete] see repo
– Improve SimpleJson [complete] see repo
– Improve AjLispJava [complete] see repo

My new month’s resolutions:

– Prepare a FinTech-related talk
– Work on Husky
– Work on SharpGo
– Improve AjGenesis for Node
– Publis AjLispJava
– Work on Golin
– Work on RkModel
– Improve SimpleDT

Stay tuned!

Angel “Java” Lopez
http://www.ajlopez.com
http://twitter.com/ajlopez

November 4, 2015

New Month’s Resolutions: November 2015

Filed under: C Sharp, JavaScript, NodeJs, Open Source Projects — ajlopez @ 8:48 am

A new month has started. Time to review the resolutions for previous month and write down the new ones.

– Work on Husky [complete] see repo
– Work on SimpleJson [complete] see repo
– Work on GStones [pending]
– Work on ErlSharp [pending]
– Work on ElixirJS [complete] see repo
– Work on OStore [pending]
– Work on SharpGo [pending]

Additionally I work on:

– Start SJsonT [complete] see repo
– Improve SparkSharp [complete] see repo
– Start MessiJS [complete] see repo

My new month’s resolutions:

– Work on SJonT
– Work on SparkSharp
– Work on Husky
– Work on MessiJS
– Work on ElixirJS
– Work on GStones
– Work on SharpGo
– Prepare a NodeJS one-day course
– Prepare a FinTech-related talk

Stay tuned!

Angel “Java” Lopez
http://www.ajlopez.com
http://twitter.com/ajlopez

October 5, 2015

New Month’s Resolutions: October 2015

Filed under: .NET, C Sharp, JavaScript, NodeJs, Open Source Projects — ajlopez @ 9:58 am

It’s a new month, time to write down the new resolutions, and to review the previous month ones:

– Work on ElixirJS [complete] see repo
– Work on ErlSharp [complete] see repo
– Work on MathelSharp [complete] see repo
– Work on MathelJS [complete] see repo
– Work on GStones [complete] see repo
– Work on OStore [pending]
– Work on SparkSharp [pending]

In addition, I worked on:

– Start Husky, Haskell-like interpreter in C# [complete] see repo
– Start SimpleJson, JSON model from many files [complete] see repo
– Improve SharpGo [complete] see repo

My new resolutions:

– Work on Husky
– Work on SimpleJson
– Work on GStones
– Work on ErlSharp
– Work on ElixirJS
– Work on OStore
– Work on SharpGo

Stay tuned!

Angel “Java” Lopez
http://www.ajlopez.com
http://twitter.com/ajlopez

September 5, 2015

New Month’s Resolutions: September 2015

Filed under: .NET, C Sharp, JavaScript, NodeJs, Open Source Projects — ajlopez @ 9:45 pm

The Spring season is near, at Southern Hemisphere. Time to write my new month’s resolutions, and to review past month ones:

– Work on MathelSharp [complete] see repo
– Work on SparkSharp [pending]
– Work on MathelJS [complete] see repo
– Work on Messi [pending]
– Work on renaming, refactor AjErl, to ErlSharp [complete] see repo
– Work on OStore [complete] see repo
– Work on DynApp [pending]

Additionally, I worked on:

– Start GStones, Gobstones language implementation in JavaScript [complete] see repo
– Improve SharpGo [complete] see repo
– Improve BScript [complete] see repo
– Work on ElixirJS [complete] see repo

My new month’s resolutions:

– Work on ElixirJS
– Work on ErlSharp
– Work on MathelSharp
– Work on MathelJS
– Work on GStones
– Work on OStore
– Work on SparkSharp

Stay tuned!

Angel “Java” Lopez
http://www.ajlopez.com
http://twitter.com/ajlopez

August 4, 2015

New Month’s Resolutions: August 2015

Filed under: .NET, C Sharp, JavaScript, Open Source Projects — ajlopez @ 9:08 am

It was an exciting month. Time to review my July’s resolutions and to write the new ones.

– Continue work on SparkSharp [pending]
– Continue work on BScript [complete] see repo
– Continue work on DynApp [pending]
– Give a talk about TDD [complete]
– Continue work on SharpMongo [complete] see repo
– Continue work on NRiak [pending]
– Continue work on Elixir samples [pending]
– Continue work on Android samples [pending]

Additionally, I was working on:

– Started MathelSharp, mathematical library in C# [complete] see repo
– Started ElixirJS, Elixir language implemented in JavaScript [complete] see repo
Improved SimpleLists [complete] see repo
– Started MathelJS, mathematical library in JavaScript [complete] see repo
– Improved SharpGo [complete] see repo
– Improved Complexo [complete] see repo
– Improved OStore [complete] see repo
– Improved Messi [complete] see repo
– Improved AcquarellaJS [complete] see repo
– Improved SimpleTPL [complete] see repo

My new resolutions are:

– Work on MathelSharp
– Work on SparkSharp
– Work on MathelJS
– Work on Messi
– Work on renaming, refactor AjErl
– Work on OStore
– Work on DynApp

Stay tuned!

Angel “Java” Lopez
http://www.ajlopez.com
http://twitter.com/ajlopez

« Newer PostsOlder Posts »

The Shocking Blue Green Theme. Create a free website or blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 73 other followers