Building the chainIsValid method

Let's start building the consensus algorithm by creating a new method called chainIsValid. This method will validate whether or not a chain is legitimate. Let's get started with building this method:

  1. In the blockchain.js file, after the proofOfWork method, let's define the method as follows:
Blockchain.prototype.chainIsValid = function() {}
  1. Now, this method will take in a blockchain as an argument, and will return to us whether the blockchain is valid or not: 
Blockchain.prototype.chainIsValid = function(blockchain) {}

We're going to use this chainIsValid method to validate the other chains inside of the network when we are comparing them to the chain that is hosted on the current node. In order to ...

Get Learn Blockchain Programming with JavaScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.