-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockchain.js
More file actions
171 lines (148 loc) · 4.67 KB
/
Blockchain.js
File metadata and controls
171 lines (148 loc) · 4.67 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
const Block = require('./Block');
const SyncError = require('./Errors/SyncError');
const BlockchainResponse = {
QUERY_LATEST: 0,
QUERY_ALL: 1,
RESPONSE_BLOCKCHAIN: 2
};
module.exports = class Blockchain
{
constructor()
{
this.blocks = [this.getGenesisBlock()];
}
/**
* create a new block from the data and append it to the blockchain
* @param {string} data
*/
createBlock(data)
{
var previousBlock = this.getLastBlock();
var nextIndex = previousBlock.index + 1;
var nextTimestamp = new Date().getTime() / 1000;
return this.addBlocks([new Block(nextIndex, previousBlock.hash, nextTimestamp, data)]);
}
/**
* append those new blocks to the blockchain
* @param {array} blocks
*/
addBlocks(blocks)
{
if (!blocks.length) {
return false;
}
const lastBlock = this.getLastBlock();
const newBlocks = blocks.sort( (b1, b2) => {b1.index - b2.index});
const block = newBlocks[newBlocks.length - 1];
console.log("NEW block.index:" + block.index + " - our latest.index:" + lastBlock.index);
if (block.index > lastBlock.index) {
console.log("This new block is behind our chain.");
if (block.previousHash === lastBlock.hash) {
console.log("Hash match => block appended");
if (!this.isBlockValid(block, lastBlock)) {
console.log("Invalid block");
return false;
}
this.blocks.push(block);
return true;
}
else if (newBlocks.length === 1) {
console.log("This new block is to far away for this blockchain, needs a resync");
throw new SyncError("This new block is to far away for this blockchain");
}
else {
console.log("this blockchain needs to be replaced");
return this.replaceChain(newBlocks);
}
}
else {
console.log("No need to add this block cause our latest is behind");
}
return false;
}
/**
* replace the current blocks with this new one
* @param {array} blocks
*/
replaceChain(blocks)
{
if (!blocks.length || blocks.length < this.blocks) {
console.log("invalid new chain length (shorter than the current one)");
return false;
}
if (!this.isChainValid(blocks)) {
console.log("Invalid chain cannot replace it");
return false;
}
this.blocks = blocks;
console.log("Blockchain replaced and up to date !")
return true;
}
/**
* Check a chain validity
* @param {array} blocks
*/
isChainValid(blocks)
{
if (!blocks.length) {
console.log("invalid chain length");
return false;
}
if (JSON.stringify(blocks[0]) !== JSON.stringify(this.getGenesisBlock())) {
console.log("First block does not match genesis block");
return false;
}
for (var i = 1; i < blocks.length; i++) {
if (!this.isBlockValid(blocks[i], blocks[i-1])) {
console.log("block.index:" + blocks[i].index + " is invalid");
return false;
}
}
return true;
}
/**
* Check the validity of a new block and whether it can be appended
* @param {Block} newBlock
* @param {Block} previousBlock
*/
isBlockValid(newBlock, previousBlock)
{
if (newBlock.index !== (previousBlock.index+1)) {
console.log("invalid index");
return false;
}
if (newBlock.previousHash !== previousBlock.hash) {
console.log("invalid previousHash");
return false;
}
if (newBlock.timestamp <= previousBlock.timestamp) {
console.log("invalid timestamp");
return false;
}
if (newBlock.hash !== Block.computeHash(newBlock)) {
console.log("invalid hash");
return false;
}
return true;
}
/**
* return the last block (at least the genesis block if the blockchain is empty)
*/
getLastBlock()
{
return this.blocks.length ? this.blocks[this.blocks.length-1] : null;
}
/**
* return the first block
*/
getGenesisBlock() {
return new Block(0, "0", 1465154705, "my genesis block!!", "816534932c2b7154836da6afc367695e6337db8a921823784c14378abed4f7d7");
}
/**
* accessor to get all blocks
*/
getAllBlocks()
{
return this.blocks;
}
}