Angel \”Java\” Lopez on Blog

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

2 Comments »

  1. […] Previous Post Next Post […]

    Pingback by Building A Blockchain (8) | Angel "Java" Lopez on Blog — May 16, 2016 @ 10:33 am

  2. […] Previous Post […]

    Pingback by Building A Blockchain (10) | Angel "Java" Lopez on Blog — May 23, 2016 @ 8:46 am


RSS feed for comments on this post. TrackBack URI

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: