-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblockchain.js
More file actions
51 lines (42 loc) · 1.65 KB
/
blockchain.js
File metadata and controls
51 lines (42 loc) · 1.65 KB
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
const Block=require("./block");
const cryptoHash=require("./crypto-hash");
class Blockchain{
constructor(){
this.chain=[Block.genesis()];
}
addBlock({data}){
const newBlock=Block.minedBlock({ lastblock:this.chain[this.chain.length-1], data });
this.chain.push(newBlock);
}
// validation this is where crypto validation is
static isValidChain(chain){
//checking if the first chain is genesis
if(JSON.stringify(chain[0]) !== JSON.stringify(Block.genesis)){
return false;
}
// validating each of the element in the chain by looing and meeting conditions
for (let i = 1; i < chain.length; i++) {
const {timestamp, lastHash, hash, nonce, difficulty, data} = chain[i];
const actualLastHash=chain[i-1].hash;
const lastDifficulty=chain[i-1].difficulty
if(lastHash !== actualLastHash) return false;
const validatedHash=cryptoHash(timestamp,lastHash,data,nonce,difficulty);
if(hash !==validatedHash) return false;
if (Math.abs(lastDifficulty-difficulty)>1) return false;
}
return true;
};
replaceChain(chain){
if (chain.length <= this.chain.length){
console.error('the incoming chain must be longer');
return;
}
if(!Blockchain.isValidChain(chain)){
console.error('the incoming chain must be valid');
return;
}
console.log('replacing the chain with', chain);
this.chain=chain;
}
}
module.exports=Blockchain;