-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP2P.js
More file actions
161 lines (148 loc) · 3.34 KB
/
Copy pathP2P.js
File metadata and controls
161 lines (148 loc) · 3.34 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
const wrtc = require("wrtc");
const net = require("net");
const Exchange = require("peer-exchange");
const ex = new Exchange("Sample Blockchain",{wrtc: wrtc});
class P2P {
constructor(blockchain) {
this.peers = [];
this.blockchain = blockchain;
}
startServer(port) {
const server = net.createServer(socket =>
ex.accept(socket, (error,connection) => {
if (error) {
throw error;
} else {
this.setupConnection.call(this,connection);
}
})
).listen(port);
}
findPeers() {
ex.getNewPeer((error,connection) => {
if (error) {
throw error;
} else {
this.setupConnection.call(this,connection);
}
});
}
connectToPeer(host,port) {
const socket = net.connect(port,host, () =>
ex.connect(socket,(error,connection) => {
if (error) {
throw error;
} else {
this.setupConnection.call(this,connection);
}
})
);
}
setupConnection(peer) {
// Add new connection to list of peers
this.peers.push(peer);
// Set up data and error handling
peer.on("data",data=> {
const message = JSON.parse(data.toString("utf-8"));
this.parseMessage(peer,message);
});
peer.on("error",error=> {throw error;});
// Send newest peer latest block
this.sendLatestBlock(peer);
}
send(peer,message) {
peer.write(JSON.stringify(message));
}
parseMessage(peer,message) {
let data = null;
switch(message.code) {
case 0: // Request latest block
data = {
code:1,
data:this.blockchain.latestBlock
}
this.send(peer,data);
break;
case 1: // Receive latest block
this.handleReceivedBlock(peer,message.data);
break;
case 2: // Request blockchain
data = {
code:3,
data:this.blockchain.get()
}
this.send(peer,data);
break;
case 3: // Receive blockchain
this.handleReceivedBlockchain(peer,message.data);
break;
case 4: // Receive transaction
this.handleReceivedTransaction(peer,message.data);
break;
default:
throw "Invalid message.";
}
}
handleReceivedBlock(peer,received) {
console.log("Received block");
const latestBlock = this.blockchain.latestBlock;
if (latestBlock.hash === received.previousHash) {
this.blockchain.addExternalBlock(received);
} else if (received.index > latestBlock.index) {
console.log("Requesting full chain");
let data = {code: 2};
this.send(peer,data);
} else {
// Current blockchain is of equal length or longer
}
}
handleReceivedBlockchain(peer,received) {
try {
console.log("Testing chain");
let validation = this.blockchain.replaceChain(received);
if (validation[0]) {
let unsent = validation[1];
for (let i = 0; i < unsent.length; i++) {
this.sendTransaction(unsent[i]);
}
}
} catch (error) {
throw error;
}
}
handleReceivedTransaction(peer,received) {
try {
if (this.blockchain.isValidTransaction(received)) {
this.blockchain.insertTransaction(received);
}
} catch (error) {
throw error;
}
}
sendBlock(block) {
let message = {
code: 1,
data: block
};
this.peers.forEach(
peer => this.send(peer,message)
);
}
sendTransaction(trans) {
let message = {
code:4,
data:trans
};
this.peers.forEach(
peer => this.send(peer,message)
);
}
sendLatestBlock(peer) {
let message = {
code: 1,
data: this.blockchain.latestBlock
};
this.send(peer,message);
}
}
module.exports=P2P;