Angel \”Java\” Lopez on Blog

May 23, 2016

Building A Blockchain (10)

Previous Post

This week, I added more logic to my personal blockchain project in JavaScript:

https://github.com/ajlopez/SimpleBlockchain

I started to implement the blockchain logic using TDD (Test-Driven Development) workflow. A first test creating the blockchain object using a genesis block:

var blockchains = require('../lib/blockchains');
var blocks = require('../lib/blocks');

exports['create blockchain'] = function (test) {
    var genesis = blocks.block();
    var bc = blockchains.blockchain(genesis);
    
    test.ok(bc);
    test.equal(typeof bc, 'object');
    test.equal(bc.bestBlock(), genesis);
};

Then, I wrote a test exercising to add a new block:

exports['add block'] = function (test) {
    var genesis = blocks.block();
    var block = blocks.block(genesis);
    var bc = blockchains.blockchain(genesis);
    
    bc.add(block);
    
    test.equal(bc.bestBlock(), block);
};

The rejection of a block of the same height than the best block in blockchain:

exports['add block same height'] = function (test) {
    var genesis = blocks.block();
    var block = blocks.block(genesis);
    var block2 = blocks.block(genesis);
    
    var bc = blockchains.blockchain(genesis);
    
    bc.add(block);
    bc.add(block2);
    
    test.equal(bc.bestBlock(), block);
};

The adding of a block that is a child of the best block, and the rejection of a block that is not the descendant of the best block:

exports['add block with next height'] = function (test) {
    var genesis = blocks.block();
    var block = blocks.block(genesis);
    var block2 = blocks.block(block);
    
    var bc = blockchains.blockchain(genesis);
    
    bc.add(block);
    bc.add(block2);
    
    test.equal(bc.bestBlock(), block2);
};

exports['add block next height but another parent block'] = function (test) {
    var genesis = blocks.block();
    var block = blocks.block(genesis);
    var block2 = blocks.block(genesis);
    var block3 = blocks.block(block2);
    
    var bc = blockchains.blockchain(genesis);
    
    bc.add(block);
    bc.add(block3);
    
    test.equal(bc.bestBlock(), block);
};

These tests were written one by one, and each test was followed by the simplest implementation that pass the test. This is the spirit of TDD: baby steps, simple implementation, explicit description of expected behavior.

Next post: implementing account states, immutable states, and node communication.

Stay tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

May 16, 2016

Building A Blockchain (9)

Filed under: Bitcoin, Blockchain, Ethereum, JavaScript, NodeJs, Open Source Projects — ajlopez @ 10:32 am

Previous Post
Next Post

You already know my personal blockchain project in C#:

https://github.com/ajlopez/BlockchainSharp

This week I started another implementation, using JavaScript/NodeJS:

https://github.com/ajlopez/SimpleBlockchain

It is interesting to compare a typed language implementation with another without types.

The base concepts are: blocks, transactions, a blockchain, etc. I’m writing the code using TDD (Test-Driven Development) workflow, as usual. The idea is to implement those base entities, and then, write a node program that can communicate with other nodes in the network, exchanging transactions and new mined blocks. My intention is to use JSON messages, instead of byte streams. But the format could be an decision to change. By now, I have some simple behavior implemented, covered by the corresponding tests.

These are my initial block tests:

exports['create genesis block'] = function (test) {
    var block = blocks.block();
    
    test.ok(block);
    test.equal(typeof block, 'object');
    test.equal(block.number, 0);
    test.ok(isHash(block.hash));
    test.ok(isHash(block.parentHash));
    test.equal(block.parentHash, '0x000....000000');
}

exports['create child block'] = function (test) {
    var genesis = blocks.block();
    var block = blocks.block(genesis);
    
    test.ok(block);
    test.equal(typeof block, 'object');
    test.equal(block.number, 1);
    test.ok(isHash(block.hash));
    test.ok(isHash(block.parentHash));
    test.equal(block.parentHash, genesis.hash);
}

exports['create child block with initial data'] = function (test) {
    var genesis = blocks.block();
    var block = blocks.block({ extra: 'hello' }, genesis);
    
    test.ok(block);
    test.equal(typeof block, 'object');
    test.equal(block.number, 1);
    test.ok(isHash(block.hash));
    test.ok(isHash(block.parentHash));
    test.equal(block.parentHash, genesis.hash);
    test.equal(block.extra, 'hello');
}

I wrote these tests one by one, and after each test, I implemented the code that makes the test pass. In this way, I wrote the simplest solution that satisfies the expected behavior. And I can refactor the implementations to a more powerful one without unnoticed breaking changes: every behavior is backed up by a test.

After the base entities, I need some auxiliary entities that allow the efficient process of blocks and transactions. One of this helper entities is a block store: where to save and retrieve blocks using hash, parent hash or number. By now, I have an in memory implementation. Some tests:

exports['create store'] = function (test) {
    var store = stores.blockstore();
    
    test.ok(store);
    test.equal(typeof store, 'object');
};

exports['retrieve unknown block by hash'] = function (test) {
    var store = stores.blockstore();
    
    var block = store.getByHash(utils.hash());
    
    test.equal(block, null);
};

exports['save block and retrieve it by hash'] = function (test) {
    var store = stores.blockstore();
    var hash = utils.hash();
    var block = { hash: hash };
    
    store.save(block);
    
    var result = store.getByHash(hash);
    
    test.ok(result);
    test.equal(result.hash, hash);
};

I will work on both projects: C# project needs some network code, and JavaScript project needs a blockchain implementation. I want to explore an alternative implementation for the blockchain growth. One nice thing about JavaScript implementation is that I don’t need to worry about multithreading issues. I can wrote code that changes the blockchain structure without having to check concurrent access.

Stay tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

May 8, 2016

New Month’s Resolutions: May 2016

Filed under: C Sharp, JavaScript, NodeJs, Open Source Projects, Wang Tiles — ajlopez @ 5:27 pm

Time to write my new month’s resolutions, and review the previous month’s ones:

– Improve WangTiles [complete] see repo
– Improve BlockchainSharp [complete] see repo
– Start Blockchain in JavaScript [complete] see repo
– Work on EthSharp [pending]
– Improve SimpleGA [pending]
– Improve AjGenesisNode-Express [pending]
– Work on CrysJS [pending]
– Work on CrysSharp [pending]

Additionally, I worked on:

– Improve SimpleForth [complete] see repo
– Start WangTilesJS [complete] see repo

My new month’s resolutions are:

– Improve WangTiles
– Improve WangTilesJS
– Improve CrysSharp
– Improve BlockchainSharp
– Improve SimpleBlockchain
– Prepare and give a Bitcoin related talk

Stay tuned!

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

April 9, 2016

New Month’s Resolutions: Aprill 2016

Filed under: Blockchain, C Sharp, Ethereum, JavaScript, NodeJs, Open Source Projects, SimpleForth — ajlopez @ 4:34 pm

A new month started, and it’s time to write the new resolutions. And to review the past month ones:

– Improve AjGenesisNode-Express [pending]
– Work on CrysJS [complete] see repo
– Work on CrysSharp [pending]
– Improve my SimpleGA samples [pending
– Work on SharpGo [pending]
– Work on EthSharp [complete] see repo

Also I worked on:

– Improve SimpleForth in JavaScript [complete] see repo
– Start WangTiles in C# [complete] see repo
– Publish new version of SimpleArgs [complete] see repo
– Start BlockchainSharp in C# [complete] see repo
– Improve SimpleDT samples [complete] see repo
– Move AjGo to GitHub [complete] see repo
– Improve RuScript [complete] see repo
– Add Code Coverate to ethereumjs rlp [complete] see repo
– New Machine Learning in JavaScript presentation [complete] see repo see talk

My new resolutions:

– Improve WangTiles
– Improve BlockchainSharp
– Start Blockchain in JavaScript
– Work on EthSharp
– Improve SimpleGA
– Improve AjGenesisNode-Express
– Work on CrysJS
– Work on CrysSharp

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

Older 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