forked from renproject/command-center
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapContainer.ts
288 lines (255 loc) · 8.72 KB
/
mapContainer.ts
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import { RenNetworkDetails } from "@renproject/contracts";
import Axios from "axios";
import { Map } from "immutable";
import localforage from "localforage";
import { useRef, useState } from "react";
import { Point } from "react-simple-maps";
import { createContainer } from "unstated-next";
import { DEFAULT_REQUEST_TIMEOUT } from "../lib/react/environmentVariables";
import { peerResponse } from "./vDot2Peers";
import { Web3Container } from "./web3Container";
export const SESSION_MAP_LIMIT = 30;
export interface DarknodeLocation {
darknodeID: string;
point: Point;
}
// const sampleDarknodes: DarknodeLocation[] = [];
export interface QueryResponse {
jsonrpc: "2.0";
id: number;
result: {
peers: string[];
};
}
const parallelLimit = <T>(
promiseFactories: Array<() => Promise<T>>,
limit: number,
): unknown => {
const result: T[] = [];
let cnt = 0;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const chain = (innerPromiseFactories: Array<() => Promise<T>>): any => {
if (!innerPromiseFactories.length) {
return;
}
const i = cnt++; // preserve order in result
const shifted = innerPromiseFactories.shift();
if (!shifted) {
return null;
}
return shifted().then((res) => {
result[i] = res; // save result
return chain(innerPromiseFactories); // append next promise
});
};
const arrChains = [];
while (limit-- > 0 && promiseFactories.length > 0) {
// create `limit` chains which run in parallel
arrChains.push(chain(promiseFactories));
}
// return when all arrChains are finished
return Promise.all(arrChains).then(() => result);
};
export const getLightnode = (
network: RenNetworkDetails,
isNew = false,
): string => {
if (isNew) {
switch (network.name) {
case "mainnet":
return "https://lightnode-mainnet.herokuapp.com";
case "testnet":
return "https://lightnode-testnet.herokuapp.com";
// TODO: fees - not sure about following ones;
// case "chaosnet":
// return "https://lightnode-chaosnet-new.herokuapp.com";
case "devnet":
return "https://lightnode-devnet.herokuapp.com";
case "localnet":
return "http://0.0.0.0:8888";
}
}
switch (network.name) {
case "mainnet":
return "https://lightnode-mainnet.herokuapp.com";
case "chaosnet":
return "https://lightnode-chaosnet-new.herokuapp.com";
case "testnet":
return "https://lightnode-testnet.herokuapp.com";
case "devnet":
return "https://lightnode-devnet.herokuapp.com";
case "localnet":
return "http://0.0.0.0:8888";
}
return "";
};
const fetchLocationFromAPI = async (ip: string): Promise<Location> => {
try {
const apiResponse = (
await Axios.get<{
latitude: number;
longitude: number;
}>(`https://ipapi.co/${ip}/json`, {
timeout: DEFAULT_REQUEST_TIMEOUT,
})
).data;
return {
longitude: apiResponse.longitude,
latitude: apiResponse.latitude,
};
} catch (error) {
try {
const apiResponse = (
await Axios.get<{
lat: number;
lon: number;
}>(`http://ip-api.com/json/${ip}`, {
timeout: DEFAULT_REQUEST_TIMEOUT,
})
).data;
return {
longitude: apiResponse.lon || 0,
latitude: apiResponse.lat || 0,
};
// Seems to share a rate-limiter with https://ipapi.co.
// const apiResponse = (await Axios.get<{
// loc: string,
// }>(`https://ipinfo.io/${ip}/json`)).data;
// const [latitude, longitude] = apiResponse.loc.split(",").map((x) => parseInt(x));
// return { longitude, latitude };
} catch (_error) {
// Ignore error.
throw new Error(`Unable to fetch location for ${ip}.`);
}
}
};
const parseMultiAddress = (
multiAddress: string,
): { ip: string; darknodeID: string } => {
const [, , ip, , , , darknodeID] = multiAddress.split("/");
return { ip, darknodeID };
};
const getAllDarknodes = async (network: RenNetworkDetails) => {
const lightnode = getLightnode(network);
if (!lightnode) {
throw new Error(`No lightnode to fetch darknode locations.`);
}
// const request = {
// jsonrpc: "2.0",
// method: "ren_queryPeers",
// params: {},
// id: 67,
// };
// const response = (
// await retryNTimes(
// async () =>
// await Axios.post<QueryResponse>(lightnode, request, {
// timeout: DEFAULT_REQUEST_TIMEOUT,
// }),
// 2,
// )
// ).data;
const response = peerResponse;
return Promise.resolve(response.result.peers);
// return darknodeIDs.map(parseMultiAddress);
};
interface Location {
longitude: number;
latitude: number;
}
const configureCache = (): LocalForage => {
return localforage.createInstance({
name: "command-center",
version: 1.0,
storeName: "geoip",
description: "Cache API requests to resolve IP address's coordinates",
});
};
const writeCache = async (ip: string, location: Location) => {
await configureCache().setItem(ip, location);
};
const readCache = async (ip: string) => {
return await configureCache().getItem<Location>(ip);
};
const useMapContainer = () => {
const { renNetwork: network } = Web3Container.useContainer();
// eslint-disable-next-line prefer-const
let [darknodes, setDarknodes] = useState<Map<string, DarknodeLocation>>(
Map(),
);
const sessionMapCount = useRef(0);
const [darknodeCount, setDarknodeCount] = useState<number | null>(null);
const getLocation = async (ip: string): Promise<Location | null> => {
// Check if we've already fetched for this IP
const previousLocation = await readCache(ip);
if (previousLocation) {
return previousLocation;
}
if (sessionMapCount.current >= SESSION_MAP_LIMIT) {
return null;
}
sessionMapCount.current += 1;
try {
const location = await fetchLocationFromAPI(ip);
// Store in map for next time
await writeCache(ip, location);
// Return location
return location;
} catch (error) {
// If requests are failing, increase session request limit faster.
sessionMapCount.current += 4;
throw error;
}
};
const addDarknodeID = async (multiAddress: string) => {
const { ip, darknodeID } = parseMultiAddress(multiAddress);
try {
const location = await getLocation(ip);
if (!location) {
return;
}
const { longitude, latitude } = location;
if (typeof longitude !== "number" || typeof latitude !== "number") {
return;
}
// Shift by random amount to avoid markers covering one another.
const random = (seedS: string) => {
const seed = Array.from(seedS).reduce(
(r, i) => r + i.charCodeAt(0),
0,
);
const x = Math.sin(seed) * 20000;
const xInt = x - Math.floor(x);
return xInt * 2 - 1;
};
setDarknodes((latestDarknodes) =>
latestDarknodes.set(darknodeID, {
darknodeID,
point: [
longitude + random(darknodeID),
latitude + random(darknodeID),
],
}),
);
} catch (error) {
// Ignore errors
}
};
const fetchDarknodes = async () => {
if (network) {
try {
const darknodeIDs = await getAllDarknodes(network);
setDarknodeCount(darknodeIDs.length);
const updateDarknodes = darknodeIDs.map(
(darknodeID: string) => async () =>
addDarknodeID(darknodeID),
);
await parallelLimit(updateDarknodes, 4);
} catch (error) {
console.error(error);
}
}
};
return { fetchDarknodes, darknodes, darknodeCount };
};
export const MapContainer = createContainer(useMapContainer);