-
Notifications
You must be signed in to change notification settings - Fork 0
/
latechain.js
159 lines (139 loc) · 4.04 KB
/
latechain.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
const crypto = require("crypto");
const DEFAULT_GENESIS_DATA = "genesis block";
const DEFAULT_DIFFICULTY = 16;
const BYTE_LENGTH = 8;
function Module(
difficultyScore = DEFAULT_DIFFICULTY,
genesisData = DEFAULT_GENESIS_DATA
) {
this.genesisData = genesisData;
this.chain = [];
this.setDifficultyScore(difficultyScore);
}
function Block(
index,
previousHash,
timestamp,
data,
hash,
difficultyScore,
nonce
) {
this.index = index;
this.previousHash = previousHash;
this.timestamp = timestamp;
this.data = data;
this.hash = hash;
this.difficultyScore = difficultyScore;
this.nonce = nonce;
}
function calculateHashForBlock(block) {
return crypto
.createHash("sha256")
.update(block.index.toString())
.update(block.previousHash.toString())
.update(block.timestamp.toString())
.update(block.data.toString())
.update(block.difficultyScore.toString())
.update(block.nonce.toString())
.digest("hex");
}
function getLatestBlock(chain) {
if (chain && chain.length > 0) {
return chain[chain.length - 1];
}
return null;
}
function difficultyOfChain(chain) {
return chain.reduce(
(block, currentValue) => currentValue + block.difficultyScore,
0
);
}
function checkProofOfWork(proofOfWork, difficultyScore) {
const neededMaskBytes = Math.ceil(difficultyScore / BYTE_LENGTH);
const usePartialMask = difficultyScore % BYTE_LENGTH !== 0;
const partialBitMask = parseInt(
"1".repeat(difficultyScore % BYTE_LENGTH).padEnd(BYTE_LENGTH, "0"),
2
);
return new Buffer(proofOfWork, "hex") // Convert hex digest into buffer
.slice(0, neededMaskBytes) // Take only the needed bytes
.every((byte, byteIdx, array) => {
if (usePartialMask && byteIdx === array.length - 1) {
return (byte & partialBitMask) === 0;
}
return byte === 0x00;
});
}
function isValidNewBlock(newBlock, previousBlock) {
if (previousBlock.index + 1 !== newBlock.index) {
console.info("invalid index");
return false;
} else if (previousBlock.hash !== newBlock.previousHash) {
console.info("invalid previoushash");
return false;
} else if (calculateHashForBlock(newBlock) !== newBlock.hash) {
console.info("invalid hash");
return false;
} else if (!checkProofOfWork(newBlock.hash, newBlock.difficultyScore)) {
console.info("invalid proof of work");
return false;
}
return true;
}
Module.prototype.setDifficultyScore = function(newDifficultyScore) {
this.difficultyScore = newDifficultyScore;
};
Module.prototype.generateNextBlock = async function(data) {
const previousBlock = getLatestBlock(this.chain);
const nextIndex = (previousBlock && previousBlock.index + 1) || 0;
const previousHash = (previousBlock && previousBlock.hash) || 0;
const nextTimestamp = Math.floor(new Date().getTime() / 1000);
const newBlock = new Block(
nextIndex,
previousHash,
nextTimestamp,
data,
null,
this.difficultyScore,
0
);
newBlock.hash = calculateHashForBlock(newBlock);
while (!checkProofOfWork(newBlock.hash, newBlock.difficultyScore)) {
newBlock.nonce++;
newBlock.hash = calculateHashForBlock(newBlock);
}
return newBlock;
};
Module.prototype.isValidChain = function(chain) {
return chain.every((block, index, array) => {
if (index > 0) {
return isValidNewBlock(block, array[index - 1]);
} else {
// Dealing with the genesis block
return (
block.index === 0 &&
block.previousHash === 0 &&
block.data === this.genesisData
);
}
});
};
Module.prototype.generateGenesisBlock = function() {
return this.generateNextBlock(this.genesisData);
};
Module.prototype.addBlock = function(block) {
this.chain.push(block);
};
Module.prototype.replaceChain = function(newChain) {
if (isValidChain(newChain) && newChain.length > this.chain.length) {
console.info(
"Received blockchain is valid. Replacing current blockchain with received blockchain"
);
this.chain = newChain;
} else {
console.info("Received blockchain invalid");
}
};
module.exports = Module;