-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblockchain.js
67 lines (59 loc) · 1.61 KB
/
blockchain.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const Block = require('./block');
const cryptoHash = require('./crypto.hash');
class Blockchain {
constructor() {
this.chain = [Block.genesisBlock()];
}
// ADD A BLOCK TO THE CHAIN
addBlock({ data }) {
const lastBlock = this.chain[this.chain.length - 1];
const newBlock = Block.mineBlock({
lastBlock,
data
});
this.chain.push(newBlock);
}
// VALIDATE A CHAIN
static isValidChain(chain) {
if (JSON.stringify(chain[0]) !== JSON.stringify(Block.genesisBlock())) {
return false;
}
// loop through the chain
for (let i = 1; i < chain.length; i++) {
const { timestamp, lastHash, hash, data, nonce, difficulty } = chain[i];
const realLastHash = chain[i - 1].hash;
const lastDifficulty = chain[i - 1].difficulty;
if (lastHash !== realLastHash) {
return false;
}
const validHash = cryptoHash(
timestamp,
lastHash,
data,
nonce,
difficulty
);
if (hash !== validHash) {
return false;
}
if (Math.abs(lastDifficulty - difficulty) > 1) {
return false;
}
}
return true;
}
// REPLACE THE CHAIN WITH NEW LONGER CHAIN
replaceChain(chain) {
if (chain.length <= this.chain.length) {
console.error('The incoming chain must be longer than the current chain');
return;
}
if (!Blockchain.isValidChain(chain)) {
console.error('The incoming chain must be valid');
return;
}
console.log('The current chain is being replaced by the new chain', chain);
this.chain = chain;
}
}
module.exports = Blockchain;