-
Notifications
You must be signed in to change notification settings - Fork 270
/
Blockchain.js
144 lines (125 loc) · 3.06 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const Block = require("./Block.js");
const crypto = require("crypto");
class Blockchain {
constructor() {
this.blockchain = [Block.genesis];
this.difficulty = 3;
}
get() {
return this.blockchain;
}
get latestBlock() {
return this.blockchain[this.blockchain.length - 1];
}
isValidHashDifficulty(hash) {
for (var i = 0; i < hash.length; i++) {
if (hash[i] !== "0") {
break;
}
}
return i >= this.difficulty;
}
calculateHashForBlock(block) {
const { index, previousHash, timestamp, data, nonce } = block;
return this.calculateHash(
index,
previousHash,
timestamp,
data,
nonce
);
}
calculateHash(index, previousHash, timestamp, data, nonce) {
return crypto
.createHash("sha256")
.update(index + previousHash + timestamp + data + nonce)
.digest("hex");
}
mine(data) {
const newBlock = this.generateNextBlock(data);
try {
this.addBlock(newBlock);
} catch(err) {
throw err;
}
}
generateNextBlock(data) {
const nextIndex = this.latestBlock.index + 1;
const previousHash = this.latestBlock.hash;
let timestamp = new Date().getTime();
let nonce = 0;
let nextHash = this.calculateHash(
nextIndex,
previousHash,
timestamp,
data,
nonce
);
while (!this.isValidHashDifficulty(nextHash)) {
nonce = nonce + 1;
timestamp = new Date().getTime();
nextHash = this.calculateHash(
nextIndex,
previousHash,
timestamp,
data,
nonce
);
}
const nextBlock = new Block(
nextIndex,
previousHash,
timestamp,
data,
nextHash,
nonce
);
return nextBlock;
}
addBlock(newBlock) {
if (this.isValidNextBlock(newBlock, this.latestBlock)) {
this.blockchain.push(newBlock);
} else {
throw "Error: Invalid block";
}
}
isValidNextBlock(nextBlock, previousBlock) {
const nextBlockHash = this.calculateHashForBlock(nextBlock);
if (previousBlock.index + 1 !== nextBlock.index) {
return false;
} else if (previousBlock.hash !== nextBlock.previousHash) {
return false;
} else if (nextBlockHash !== nextBlock.hash) {
return false;
} else if (!this.isValidHashDifficulty(nextBlockHash)) {
return false;
} else {
return true;
}
}
isValidChain(chain) {
if (JSON.stringify(chain[0]) !== JSON.stringify(Block.genesis)) {
return false;
}
const tempChain = [chain[0]];
for (let i = 1; i < chain.length; i = i + 1) {
if (this.isValidNextBlock(chain[i], tempChain[i - 1])) {
tempChain.push(chain[i]);
} else {
return false;
}
}
return true;
}
isChainLonger(chain) {
return chain.length > this.blockchain.length;
}
replaceChain(newChain) {
if (this.isValidChain(newChain) && this.isChainLonger(newChain)) {
this.blockchain = JSON.parse(JSON.stringify(newChain));
} else {
throw "Error: invalid chain";
}
}
}
module.exports = Blockchain;