-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbootnode.js
110 lines (87 loc) · 2.92 KB
/
bootnode.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
const net = require('net');
const PEERLIST = [
{"host": "localhost", "port": 3001}
];
//let blockchain = [];
(async function() {
try {
await peerDiscovery();
} catch (err) {
console.log("Cannot connect to peers: ", err.message);
}
})();
// PEER DISCOVERY FUNCTIONS
async function peerDiscovery() {
const randomPeer = await getRandomPeer();
console.log(randomPeer);
if (randomPeer) {
const { host, port } = randomPeer;
const client = net.connect(port, host, () => {
client.write(JSON.stringify({
type: 'getPeers',
requester: {
host: "wallet", // Your own host
port: 3030, // Your own port
}
}));
});
client.on('data', (data) => {
const receivedData = JSON.parse(data);
if (receivedData.type === 'sendPeers') {
// Update your list of known peers with the information received
const newPeers = receivedData.peers;
newPeers.forEach((peer) => {
if (!PEERLIST.some((knownPeer) => knownPeer.host === peer.host && knownPeer.port === peer.port)) {
if (peer.host != config.serverIP && peer.port != config.serverPort && peer.host != "wallet"){
PEERLIST.push(peer);
}
}
});
console.log('Updated the list of known peers!');
// Attempt to connect to the newly discovered peers
}
client.end();
});
client.on('error', (error) => {
console.error('Error during peer discovery: Could not connect to seed peer');
});
}
}
function removePeer(peerSocket) {
const peerIndex = connectedPeers.indexOf(peerSocket);
if (peerIndex !== -1) {
connectedPeers.splice(peerIndex, 1);
}
}
async function getRandomPeer() {
const randomIndex = Math.floor(Math.random() * PEERLIST.length);
return PEERLIST[randomIndex];
}
const server = net.createServer(socket => {
console.log('Node connected:', socket.remoteAddress + ':' + socket.remotePort);
//Listen for data
socket.on('data', async data => {
const receivedData = await JSON.parse(data);
if(receivedData.type === 'getPeers'){
console.log("\x1b[92m PEER \x1b[0m","Requested peers");
const responseData = { type: 'sendPeers', peers:PEERLIST };
const requester = receivedData.requester;
if (!PEERLIST.some((request) => request.host === requester.host && request.port === requester.port)) {
if (requester.host != config.serverIP && requester.port != config.serverPort && requester.host != "wallet"){
PEERLIST.push(requester);
}
}
socket.write(JSON.stringify(responseData));
}
});
socket.on('error', error => {
console.error("\x1b[91m ERROR \x1b[0m",'Socket error:', error);
});
socket.on('end', () => {
console.log("\x1b[91m DISCONNECT \x1b[0m",'Node disconnected:', socket.remoteAddress + ':' + socket.remotePort);
});
});
const PORT = 3030;
server.listen(PORT, () => {
console.log(" OPERATION ",`Node is running on port ${PORT}`);
});