-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.js
200 lines (180 loc) · 4.61 KB
/
utils.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
/*
* Copyright © 2019 Lisk Foundation
*
* See the LICENSE file at the top-level directory of this distribution
* for licensing information.
*
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation,
* no part of this software, including this file, may be copied, modified,
* propagated, or distributed except according to the terms contained in the
* LICENSE file.
*
* Removal or modification of this copyright notice is prohibited.
*/
const net = require('net');
const dns = require('dns');
const shuffle = require('lodash.shuffle');
const PEER_STATE_CONNECTED = 2;
const PEER_STATE_DISCONNECTED = 1;
const lookupDNS = (hostname, options) => {
return new Promise((resolve, reject) => {
dns.lookup(hostname, options, (err, address) => {
if (err) {
return reject(err);
}
return resolve(address);
});
});
};
const lookupPeersIPs = async (peersList, enabled) => {
// If peers layer is not enabled there is no need to create the peer's list
if (!enabled) {
return [];
}
// In case domain names are used, resolve those to IP addresses.
return Promise.all(
peersList.map(async peer => {
if (net.isIPv4(peer.ip)) {
return peer;
}
try {
const address = await lookupDNS(peer.ip, { family: 4 });
return {
...peer,
ip: address,
};
} catch (err) {
console.error(
`Failed to resolve peer domain name ${peer.ip} to an IP address`
);
return peer;
}
})
);
};
const sortPeers = (field, asc) => (a, b) => {
// Match the default JavaScript sort order.
if (a[field] === b[field]) {
return 0;
}
// Ascending
if (asc) {
// Undefined last
if (a[field] === undefined) {
return 1;
}
if (b[field] === undefined) {
return -1;
}
// Null second last
if (a[field] === null) {
return 1;
}
if (b[field] === null) {
return -1;
}
if (a[field] < b[field]) {
return -1;
}
return 1;
}
// Descending
// Undefined first
if (a[field] === undefined) {
return -1;
}
if (b[field] === undefined) {
return 1;
}
// Null second
if (a[field] === null) {
return -1;
}
if (b[field] === null) {
return 1;
}
if (a[field] < b[field]) {
return 1;
}
return -1;
};
const filterByParams = (peers, filters) => {
const allowedFields = [
'ip',
'wsPort',
'httpPort',
'state',
'os',
'version',
'protocolVersion',
'broadhash',
'height',
'nonce'
];
const {
limit: filterLimit,
offset: filterOffset,
sort,
...otherFilters
} = filters;
const limit = filterLimit ? Math.abs(filterLimit) : null;
const offset = filterOffset ? Math.abs(filterOffset) : 0;
let filteredPeers = peers.reduce((prev, peer) => {
const matchFilters =
typeof otherFilters === 'object' && otherFilters !== null ? otherFilters : {};
const applicableFilters = Object.keys(matchFilters).filter(key =>
allowedFields.includes(key)
);
if (
applicableFilters.every(
key => peer[key] !== undefined && peer[key] === matchFilters[key],
)
) {
prev.push(peer);
}
return prev;
}, []);
// Sorting
if (filters.sort) {
const sortArray = String(filters.sort).split(':');
const auxSortField = allowedFields.includes(sortArray[0]) ? sortArray[0] : null;
const sortField = sortArray[0] ? auxSortField : null;
const sortMethod = sortArray.length === 2 ? sortArray[1] !== 'desc' : true;
if (sortField) {
filteredPeers.sort(sortPeers(sortField, sortMethod));
}
} else {
// Sort randomly by default
filteredPeers = shuffle(filteredPeers);
}
// Apply limit if supplied
if (limit) {
return filteredPeers.slice(offset, offset + limit);
}
// Apply offset if supplied
if (offset) {
return filteredPeers.slice(offset);
}
return filteredPeers;
};
const consolidatePeers = ({connectedPeers = [], disconnectedPeers = []}) => {
// Assign state 2 to the connected peers
let connectedList = connectedPeers.map(peer => {
let { ipAddress, options, minVersion, nethash, ...peerWithoutIp } = peer;
return { ip: ipAddress, ...peerWithoutIp, state: PEER_STATE_CONNECTED };
});
let disconnectedList = disconnectedPeers.map(peer => {
let { ipAddress, options, minVersion, nethash, ...peerWithoutIp } = peer;
return {
ip: ipAddress,
...peerWithoutIp,
state: PEER_STATE_DISCONNECTED,
};
});
return [...connectedList, ...disconnectedList];
};
module.exports = {
lookupPeersIPs,
filterByParams,
consolidatePeers
};