DEV Community

Callis Ezenwaka
Callis Ezenwaka

Posted on • Updated on

Understanding Blockchain using JavaScript Class

Introduction

This tutorial will demonstrate how to simulate and run a blockchain system using JavaScript. Often times, whenever blockchain and to some extent smart contract is described, what comes to minds is an opaque black-box. It could be that the predominant language used for writing most smart contract codes contributes to this.

This piece will show a detailed step-by-step approach to implementing a smart contract using Node.js and vanilla JavaScript. A test suite will also be added to test the whole application. The source code for the article can be found in this repository. Let's get started!

Here, we have three parameters, two arrays representing initialBalances and transactions and a third integer representing the blockSize. We are going to create a blockchain smart contract that includes all valid pending transactions in the order in which they are given. And provide functionality to get the balance of a specific account on the blockchain.

Installation of Dependencies

First, clone the above repository and run npm install and skip over to the Implementation section below. If you are otherwise coding along, initialize your node.js project by running npm init -y. Then, create two files blockchain.js and blockchain.test.js. Install the following dependencies by running npm i sha1 chai mocha.

The files in the directory should look like the below image:

File directory
Then add the following codes to the blockchain.js file blockchain.js

Save the file and add the following code to the blockchain.test.js file blockchain.test.js

Save the file also and open the package.json file. Here, you need to make minor changes to the script commands. First, the following code to the script part of the package.json:

    "start": "node blockchain.js",
    "test": "mocha --timeout 10000"
Enter fullscreen mode Exit fullscreen mode

The --timeout 10000 will ensure that all the test will pass during testing.

Implementation

There are two interfaces viz: init and getAccountBalance. But before that, let's define some parameters.

  const balances = [200, 250, 500];
  const transactions = [[0, 1, 50], [1, 2, 80], [1, 0, 100], [1, 2, 600], [2, 0, 150], [1, 0, 50], [2, 1, 60], [0, 1, 55], [1, 2, 40], [1, 0, 70]];
  const blockSize = 3;
  const index = 1;
  const pendingTransactions = [[0,1,50], [1,2,80]];
  const prevBlockHash = '548bc92f827bd41ad97243cc3ca4b765f8c68a2c';
Enter fullscreen mode Exit fullscreen mode

We are going to execute the code and run the test. Run the following code sequentially inside the root directory of the project (Ignore the undefined output after executing any of the commands):

  1. Activate the Node REPL with node

  2. Initialize the constructor by running const Blockchain = require('./blockchain');

  3. Instantiate an object by running const blockchain = new Blockchain();

  4. Initialize an object by running blockchain.init(balances, transactions, blockSize);

  5. Retrieve account balance by running blockchain.getAccountBalance(index);

The Node REPL should look like the following image if everything checks out:
Node REPL

Testing the Codes

Exit the Node REPL and return to the root directory. Run the test by executing npm test ./blockchain.test.js. The output should look like below image:

Testing output

You have successfully implemented and tested a blockchain code using JavaScript. You can take some time to go through the code in order to understand how each part of the code interact with each other.

If you like the article, do like and share with friends.

Top comments (0)