forked from nguyenphuminh/JeChain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jecoin.js
230 lines (171 loc) · 8.43 KB
/
jecoin.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
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
"use strict";
const crypto = require("crypto"), SHA256 = message => crypto.createHash("sha256").update(message).digest("hex");
const WS = require("ws");
const { Block, Transaction, Blockchain, JeChain } = require("./jechain");
const EC = require("elliptic").ec, ec = new EC("secp256k1");
const privateKey = process.env.PRIVATE_KEY || ec.genKeyPair().getPrivate("hex");
const keyPair = ec.keyFromPrivate(privateKey, "hex");
const publicKey = keyPair.getPublic("hex");
const MINT_PRIVATE_ADDRESS = "0700a1ad28a20e5b2a517c00242d3e25a88d84bf54dce9e1733e6096e6d6495e";
const MINT_KEY_PAIR = ec.keyFromPrivate(MINT_PRIVATE_ADDRESS, "hex");
const MINT_PUBLIC_ADDRESS = MINT_KEY_PAIR.getPublic("hex");
const PORT = process.env.PORT || 3000;
const PEERS = process.env.PEERS ? process.env.PEERS.split(",") : [];
const MY_ADDRESS = process.env.MY_ADDRESS || "ws://localhost:3000";
const server = new WS.Server({ port: PORT });
console.log("Listening on PORT", PORT);
let opened = [];
let connected = [];
let check = [];
let checked = [];
let checking = false;
let tempChain = new Blockchain();
server.on("connection", async (socket, req) => {
socket.on("message", message => {
const _message = JSON.parse(message);
switch(_message.type) {
case "TYPE_REPLACE_CHAIN":
const [ newBlock, newDiff ] = _message.data;
const ourTx = [...JeChain.transactions.map(tx => JSON.stringify(tx))];
const theirTx = [...newBlock.data.filter(tx => tx.from !== MINT_PUBLIC_ADDRESS).map(tx => JSON.stringify(tx))];
const n = theirTx.length;
if (newBlock.prevHash !== JeChain.getLastBlock().prevHash) {
for (let i = 0; i < n; i++) {
const index = ourTx.indexOf(theirTx[0]);
if (index === -1) break;
ourTx.splice(index, 1);
theirTx.splice(0, 1);
}
if (
theirTx.length === 0 &&
SHA256(JeChain.getLastBlock().hash + newBlock.timestamp + JSON.stringify(newBlock.data) + newBlock.nonce) === newBlock.hash &&
newBlock.hash.startsWith(Array(JeChain.difficulty + 1).join("0")) &&
Block.hasValidTransactions(newBlock, JeChain) &&
(parseInt(newBlock.timestamp) > parseInt(JeChain.getLastBlock().timestamp) || JeChain.getLastBlock().timestamp === "") &&
parseInt(newBlock.timestamp) < Date.now() &&
JeChain.getLastBlock().hash === newBlock.prevHash &&
(newDiff + 1 === JeChain.difficulty || newDiff - 1 === JeChain.difficulty)
) {
JeChain.chain.push(newBlock);
JeChain.difficulty = newDiff;
JeChain.transactions = [...ourTx.map(tx => JSON.parse(tx))];
}
} else if (!checked.includes(JSON.stringify([newBlock.prevHash, JeChain.chain[JeChain.chain.length-2].timestamp || ""]))) {
checked.push(JSON.stringify([JeChain.getLastBlock().prevHash, JeChain.chain[JeChain.chain.length-2].timestamp || ""]));
const position = JeChain.chain.length - 1;
checking = true;
sendMessage(produceMessage("TYPE_REQUEST_CHECK", MY_ADDRESS));
setTimeout(() => {
checking = false;
let mostAppeared = check[0];
check.forEach(group => {
if (check.filter(_group => _group === group).length > check.filter(_group => _group === mostAppeared).length) {
mostAppeared = group;
}
})
const group = JSON.parse(mostAppeared)
JeChain.chain[position] = group[0];
JeChain.transactions = [...group[1]];
JeChain.difficulty = group[2];
check.splice(0, check.length);
}, 5000);
}
break;
case "TYPE_REQUEST_CHECK":
opened.filter(node => node.address === _message.data)[0].socket.send(
JSON.stringify(produceMessage(
"TYPE_SEND_CHECK",
JSON.stringify([JeChain.getLastBlock(), JeChain.transactions, JeChain.difficulty])
))
);
break;
case "TYPE_SEND_CHECK":
if (checking) check.push(_message.data);
break;
case "TYPE_CREATE_TRANSACTION":
const transaction = _message.data;
JeChain.addTransaction(transaction);
break;
case "TYPE_SEND_CHAIN":
const { block, finished } = _message.data;
if (!finished) {
tempChain.chain.push(block);
} else {
if (Blockchain.isValid(tempChain)) {
JeChain.chain = tempChain.chain;
}
tempChain = new Blockchain();
}
break;
case "TYPE_REQUEST_CHAIN":
const socket = opened.filter(node => node.address === _message.data)[0].socket;
for (let i = 0; i < JeChain.chain.length; i++) {
socket.send(JSON.stringify(produceMessage(
"TYPE_SEND_CHAIN",
{
block: JeChain.chain[i],
finished: i === JeChain.chain.length
}
)));
}
break;
case "TYPE_REQUEST_INFO":
opened.filter(node => node.address === _message.data)[0].socket.send(
"TYPE_SEND_INFO",
[JeChain.difficulty, JeChain.transactions]
);
break;
case "TYPE_SEND_INFO":
[ JeChain.difficulty, JeChain.transactions ] = _message.data;
break;
case "TYPE_HANDSHAKE":
const nodes = _message.data;
nodes.forEach(node => connect(node))
}
});
})
async function connect(address) {
if (!connected.find(peerAddress => peerAddress === address) && address !== MY_ADDRESS) {
const socket = new WS(address);
socket.on("open", () => {
socket.send(JSON.stringify(produceMessage("TYPE_HANDSHAKE", [MY_ADDRESS, ...connected])));
opened.forEach(node => node.socket.send(JSON.stringify(produceMessage("TYPE_HANDSHAKE", [address]))));
if (!opened.find(peer => peer.address === address) && address !== MY_ADDRESS) {
opened.push({ socket, address });
}
if (!connected.find(peerAddress => peerAddress === address) && address !== MY_ADDRESS) {
connected.push(address);
}
});
socket.on("close", () => {
opened.splice(connected.indexOf(address), 1);
connected.splice(connected.indexOf(address), 1);
});
}
}
function produceMessage(type, data) {
return { type, data }
}
function sendMessage(message) {
opened.forEach(node => {
node.socket.send(JSON.stringify(message));
});
}
function mine() {
JeChain.mineTransactions(publicKey);
sendMessage(produceMessage("TYPE_REPLACE_CHAIN", [
JeChain.getLastBlock(),
JeChain.difficulty
]));
}
function sendTransaction(transaction) {
sendMessage(produceMessage("TYPE_CREATE_TRANSACTION", transaction));
JeChain.addTransaction(transaction);
}
function requestChain(address) {
const socket = opened.filter(node => node.address === address)[0].socket;
socket.send(JSON.stringify(produceMessage("TYPE_REQUEST_CHAIN", MY_ADDRESS)));
socket.send(JSON.stringify(produceMessage("TYPE_REQUEST_INFO", MY_ADDRESS)));
}
PEERS.forEach(peer => connect(peer));
process.on("uncaughtException", err => console.log(err));