MCPcopy
hub / github.com/Savjee/SavjeeCoin / isChainValid

Method isChainValid

src/blockchain.js:300–329  ·  view source on GitHub ↗

* Loops over all the blocks in the chain and verify if they are properly * linked together and nobody has tampered with the hashes. By checking * the blocks it also verifies the (signed) transactions inside of them. * * @returns {boolean}

()

Source from the content-addressed store, hash-verified

298 * @returns {boolean}
299 */
300 isChainValid() {
301 // Check if the Genesis block hasn't been tampered with by comparing
302 // the output of createGenesisBlock with the first block on our chain
303 const realGenesis = JSON.stringify(this.createGenesisBlock());
304
305 if (realGenesis !== JSON.stringify(this.chain[0])) {
306 return false;
307 }
308
309 // Check the remaining blocks on the chain to see if there hashes and
310 // signatures are correct
311 for (let i = 1; i < this.chain.length; i++) {
312 const currentBlock = this.chain[i];
313 const previousBlock = this.chain[i - 1];
314
315 if (previousBlock.hash !== currentBlock.previousHash) {
316 return false;
317 }
318
319 if (!currentBlock.hasValidTransactions()) {
320 return false;
321 }
322
323 if (currentBlock.hash !== currentBlock.calculateHash()) {
324 return false;
325 }
326 }
327
328 return true;
329 }
330}
331
332module.exports.Blockchain = Blockchain;

Callers 2

main.jsFile · 0.80
blockchain.test.jsFile · 0.80

Calls 3

createGenesisBlockMethod · 0.95
hasValidTransactionsMethod · 0.80
calculateHashMethod · 0.45

Tested by

no test coverage detected