-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblockchain.js
165 lines (136 loc) · 3.98 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
const crypto = require('crypto');
const request = require('request');
const url = require('url');
/* Blockchain */
class Blockchain {
constructor() {
this.chain = [];
this.current_transactions = [];
this.nodes = new Set();
//Create the genesis block (it does not have predecessor)
this.createBlock(1, 100);
}
/**
* Creates a new Block in the Blockchain
*
* @param {int} proof The proof given by the Proof of Work algorithm
* @param {string} previousHash Hash of previous Block (optional)
*/
createBlock(proof, previousHash = '') {
const block = {
'index': this.chain.length + 1,
'timestamp': Date.now(),
'transactions': this.current_transactions,
proof,
previousHash,
};
this.current_transactions = [];
this.chain.push(block);
const lastBlockIndex = this.getLastBlock().index;
return lastBlockIndex + 1;
}
/**
* Creates a new transaction to go into the next mined block.
* @param sender string Address of the Sender
* @param recipient strin Address of the Recipient
* @param amount int Amount
* @returns int The index of the Block that will hold this transaction
*/
createTrancaction(sender, recipient, amount) {
this.current_transactions.push({
sender,
recipient,
amount
});
return this.getLastBlock().index + 1;
}
/**
* Returns last Block in the Chain
* @returns {*} Last Block
*/
getLastBlock() {
return this.chain[this.chain.length - 1];
}
proofOfWork(lastProof) {
let proof = 0;
while (this.validProof(lastProof, proof) === false) {
proof += 1;
}
return proof;
}
validProof(lastProof, proof) {
const guess = `${lastProof}${proof}`;
const hash = crypto.createHash('sha256');
hash.update(guess)
const guesHash = hash.digest('hex');
return /^0{4}.*/.test(guesHash);
}
registerNode(address) {
//TODO: Make sure that nodes are unique
const parsedUrl = url.parse(address);
this.nodes.add(parsedUrl);
}
/**
* Determines if a given blockchain is valid
* @param {list} chain A blockchain
* @returns {boolean} True if valid, False if not
*/
validChain(blockchain) {
let lastBlock = blockchain.chain[0];
let currenIndex = 1;
while (currenIndex < blockchain.length) {
const block = blockchain.chain[currenIndex];
console.log(lastBlock);
console.log(block);
console.log('----------------');
//Check that the hash of the block is correct
if (block.previousHash !== Blockchain.bHash(lastBlock)) {
return false;
}
lastBlock = block;
currenIndex += 1;
}
return true;
}
/**
* This is our Consensus Algorithm, it resolves conflicts
* by replacing our chain with the longest one in the network.
* @returns {boolean} True if our chain was replaced, False if not
*/
resolveConflicts() {
const neighbours = this.nodes;
let newChain = null;
//We are looking only for longer chains than ours
let maxLength = this.chain.length;
//Grab and verify the chains from all nodes in our network
for (let node of neighbours) {
request(`http://${node.host}/chain`, (error, response, body) => {
const chain = JSON.parse(body);
const length = chain.length;
//Check if length is longer and chain is valid
if (length > maxLength && this.validChain(chain)) {
maxLength = length;
newChain = chain;
}
if (newChain) {
this.chain = newChain;
return true;
}
});
}
return false;
}
//Static methods
/**
* Creates a SHA-256 hash of a Block
* @param {Object} block
* @returns {string}
*/
static bHash(block) {
const hash = crypto.createHash('sha256');
const blockAsString = JSON.stringify(block, Object.keys(block).sort());
hash.update(blockAsString);
return hash.digest('hex');
}
}
module.exports = Blockchain;