-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBlockchain.js
More file actions
394 lines (347 loc) · 10.3 KB
/
Copy pathBlockchain.js
File metadata and controls
394 lines (347 loc) · 10.3 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
const Block = require("./Block.js");
const Transaction = require("./Transaction.js").Transaction;
const Output = require("./Transaction.js").Output;
const crypto = require("crypto");
//const signer = require(("nacl-signature"));
class Blockchain {
constructor() {
this.blockchain = [];
this.transactionPool = [];
this.unusedOutputs = [];
this.blockSize = 16;
this.blockReward = 25;
this.nextID = 0;
this.publicKeys = new Map();
// Difficulty targets
this.updateInterval = 1;
this.difficulty = 1;
this.targetRate = 60;
// Initiate blockchain with Genesis block
let timestamp = Date.now();
let nonce = this.mine(0,0,timestamp,0);
let hash = this.calculateBlockHash(0,0,timestamp,0,nonce);
let genesisBlock = new Block(0,0,timestamp,0,hash,nonce,[]);
this.blockchain.push(genesisBlock);
}
get() {
return this.blockchain;
}
get latestBlock() {
return this.blockchain[this.blockchain.length - 1];
}
// Difficulty Update
updateDifficulty() {
let index = this.blockchain.length-1;
let delta = this.blockchain[index].timestamp - this.blockchain[index-this.updateInterval].timestamp;
let averageRate = delta / this.updateInterval;
let ratio = averageRate / this.targetRate;
this.difficulty /= ratio;
}
// Block Creation
createNextBlock() {
let transactions = this.selectTransactions();
let transactionRoot = this.buildMerkleTree(transactions,this.blockSize);
let index = this.latestBlock.index + 1;
let prevHash = this.latestBlock.hash;
let timestamp = new Date().getTime();
let nonce = this.mine(index,prevHash,timestamp,transactionRoot);
if (nonce < 0) { // Other node found block first
this.updateState(this.latestBlock);
return this.latestBlock;
}
let hash = this.calculateBlockHash(
index,
prevHash,
timestamp,
transactionRoot,
nonce);
const newBlock = new Block(
index,
prevHash,
timestamp,
transactionRoot,
hash,
nonce,
transactions);
// Add new block to chain
this.blockchain.push(newBlock);
let state = this.updateState(newBlock,this.unusedOutputs,this.transactionPool,this.nextID);
this.transactionPool = state.transactionPool;
this.unusedOutputs = state.unusedOutputs;
this.nextID = state.nextID;
// Perform difficulty update
if (((index+1) % this.updateInterval) == 0) {
this.updateDifficulty();
}
//return newBlock;
}
updateState(block,unusedOutputs,transactionPool,nxtID) {
let nextID = nxtID+1;
for (let i = 0; i < block.transactions.length; i++) {
let trans = block.transactions[i];
if (trans.id > nextID) {
nextID = trans.id + 1;
}
let ind = transactionPool.indexOf(trans);
transactionPool.splice(ind,1);
for (let i = 0; i < trans.inputs.length; i++) {
let ind = unusedOutputs.indexOf(trans.inputs[i]);
unusedOutputs.splice(ind,1);
}
trans.outputs.forEach(
output => unusedOutputs.push(output)
);
}
nextID = nextID;
return {transactionPool,unusedOutputs,nextID};
}
selectTransactions() { // Assumes that transaction pool is sorted
let transactionPool = this.transactionPool;
// Automatically add block reward transaction
let transactions = [transactionPool[0]];
transactionPool.shift();
let count = 1;
while (count < this.blockSize && transactionPool.length > 0) {
let transaction = transactionPool[0];
if (this.isValidTransaction(transaction)) {
transactions.push(transaction);
count += 1;
}
transactionPool.shift();
}
// TODO: Store used transactions in temp in case other node found block first
return transactions;
}
isValidTransaction(transaction) {
let i = 0;
let inputValue = 0;
// Check for double spend and valid signatures
if (!transaction.inputs) {
return false;
}
for (i = 0; i < transaction.inputs.length; i++) {
let input = transaction.inputs[i];
let index = this.unusedOutputs.indexOf(input);
if (index < 0) {
return false;
}
if (!this.isValidSignature(input)) {
return false;
}
inputValue += input.value;
}
// Check to ensure that input and output values match
let outputValue = transaction.fee; // Include fee in cost of transaction
for (i = 0; i < transaction.outputs.length; i++) {
outputValue += transaction.outputs[i].value;
}
if (inputValue != outputValue) {
return false;
}
return true;
}
isValidSignature(input) {
let publicKey = this.publicKeys.get(input.address);
let signature = input.signature;
let data = [input.address,input.value].toString();
const verifier = crypto.createVerify("RSA-SHA256");
verifier.write(data);
verifier.end();
const valid = verifier.verify(publicKey,signature,"hex");
return valid;
}
buildMerkleTree(transactions,num) {
if (num == 1) {
return crypto.createHash("sha256").update(JSON.stringify(transactions[0])).digest("hex");
} else {
let newNum = Math.floor(num/2);
let firstHalf = transactions.slice(0,newNum);
let firstHash = this.buildMerkleTree(firstHalf,newNum);
if (transactions.length > newNum) {
let secondHalf = transactions.slice(newNum,transactions.length);
let secondHash = this.buildMerkleTree(secondHalf,newNum);
return crypto.createHash("sha256").update(firstHash+secondHash).digest("hex");
} else {
return firstHash;
}
}
}
// Hash Functions
calcHash(input) {
return crypto.createHash("sha256").update(input).digest("hex");
}
createTransactionHash(id,inputs,outputs) {
let input = JSON.stringify([id,inputs,outputs]);
return this.calcHash(input);
}
createOutputHash(output) {
let input = JSON.stringify(output);
return this.calcHash(input);
}
// Transaction Creation
buildTransaction(inputs,outputs,fee,publicKey) {
let id = this.nextID;
this.nextID += 1;
let hash = this.createTransactionHash(id,inputs,outputs);
const trans = new Transaction(id,hash,inputs,outputs,fee);
if (this.isValidTransaction(trans,publicKey)) {
return trans;
} else {
return false;
}
}
createRewardTransaction(addr) {
const out = new Output(addr,this.blockReward);
let id = this.nextID;
this.nextID += 1;
let hash = this.createTransactionHash(id,[],[out]);
return new Transaction(id,hash,[],[out],0);
}
insertTransaction(trans) {
for (let i = 0; i < this.transactionPool.length; i++) {
if (trans.fee > this.transactionPool[i].fee) {
this.transactionPool.splice(i,0,trans);
return;
}
}
this.transactionPool.push(trans);
}
// Proof of Work Consensus
mine(index,previousHash,timestamp,transactionRoot) {
let nonce = 0;
let hash = this.calculateBlockHash(index,previousHash,timestamp,transactionRoot,nonce);
while (!this.checkHashDifficulty(hash)) {
if (index > 0) {
let externalBlock = this.checkExternalBlock(index);
if (externalBlock) {
nonce = -1;
return nonce;
}
}
nonce += 1;
hash = this.calculateBlockHash(index,previousHash,timestamp,transactionRoot,nonce);
}
return nonce;
}
calculateBlockHash(index,prevHash,timestamp,data,nonce) {
let input = JSON.stringify([index,prevHash,timestamp,data,nonce]);
return this.calcHash(input);
}
calculateHashOfBlock(block) {
return this.calculateBlockHash(block.index,block.previousHash,block.timestamp,block.data,block.nonce);
}
checkHashDifficulty(hash) {
let target = Math.ceil(Math.pow(2,256-this.difficulty));
if (parseInt(hash,16) > target) {
return false;
} else {
return true;
}
}
checkExternalBlock(index) {
if (index < this.latestBlock.index) {
return true;
} else {
return false;
}
}
// Blockchain Updates
validateBlock(prevBlock,block,unusedOutputs) {
let hash = this.calculateHashOfBlock(block);
console.log("Hash calculated");
let merkleRoot = this.buildMerkleTree(block.transactions,this.blockSize);
console.log("Merkle calculated");
if (block.index != prevBlock.index + 1) {
console.log("Index incorrect");
return false;
} else if (block.previousHash != prevBlock.hash) {
console.log("Prev hash incorrect");
return false;
} else if (block.hash != hash) {
console.log("Hash incorrect");
return false;
} else if(!this.checkHashDifficulty(hash)) {
console.log("Difficulty incorrect");
return false;
} else if(block.data != merkleRoot) {
console.log("Merkle incorrect");
return false;
}
console.log("Initial arguments valid");
for (let i = 1; i < block.transactions.length; i++) {
if (!this.isValidTransaction(block.transactions[i])) {
console.log("Transaction "+i+" invalid.");
return false;
}
}
return true;
}
getExternalBlock() {
return null;
// TODO: Read block from socket
}
addExternalBlock(block) {
if (this.validateBlock(this.latestBlock,block)) {
this.blockchain.push(block);
return true;
} else {
return false;
}
}
fullTransactionPool(chain) {
let pool = []
for (let i = 0; i < chain.length; i++) {
pool = pool.concat(chain[i].transactions);
}
return pool;
}
mergeTransactionPools(newchain) {
let oldPool = this.fullTransactionPool(this.blockchain);
let newPool = this.fullTransactionPool(newchain);
let unsent = []
for (let i = 0; i < oldPool.length; i++) {
let trans = oldPool[i];
ind = newPool.indexOf(trans);
if (ind < 0) {
unsent.push(trans);
newPool.push(trans);
}
}
return [newPool,unsent];
}
// TODO: Optimize to only look at divergent blocks
isValidChain(newchain,transactionPool) {
let unusedOutputs = [];
let nextID = 0;
console.log("Validating chain");
//if (JSON.stringify(newchain[0]) !== JSON.stringify(this.blockchain[0])) {
// return false; // Invalid genesis block
//}
for (let i = 1; i < newchain.length; i++) {
console.log("Validating block "+i);
if (!this.validateBlock(newchain[i-1],newchain[i],unusedOutputs)) {
return false;
}
let state = this.updateState(newchain[i],unusedOutputs,transactionPool,nextID);
unusedOutputs = state.unusedOutputs;
transactionPool = state.transactionPool;
nextID = state.nextID;
}
console.log("Chain valid.");
this.unusedOutputs = unusedOutputs;
return true
}
replaceChain(newchain) { // Longest Chain Rule
if (newchain.length > this.blockchain.length) {
let pools = this.mergeTransactionPools(newchain);
let transactionPool = pools[0];
let unsent = pools[1];
if (this.isValidChain(newchain,transactionPool)) {
this.blockchain = newchain;
return [true,unsent];
}
return [false,unsent];
}
}
}
module.exports = Blockchain;