-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.js
329 lines (287 loc) · 12.1 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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
* This file is part of the 0chain js-client distribution (https://github.com/0chain/client-sdk).
* Copyright (c) 2018 0chain LLC.
*
* 0chain js-client program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
var sha3 = require('js-sha3');
var JSONbig = require('json-bigint');
const axios = require('axios');
const PromiseAll = require('promises-all');
var BlueBirdPromise = require("bluebird");
const consensusPercentage = 20;
// This will return null if not enough consensus , otherwise will return max voted response
const getConsensusMessageFromResponse = function (hashedResponses, consensusNo, responseArray) {
let uniqueCounts = {};
hashedResponses.forEach(function (x) { uniqueCounts[x] = (uniqueCounts[x] || 0) + 1; });
var maxResponses = { key: hashedResponses[0], val: uniqueCounts[hashedResponses[0]] };
for (var key in uniqueCounts) {
if (uniqueCounts.hasOwnProperty(key)) {
if (maxResponses.val < uniqueCounts[key]) {
maxResponses = { key: key, val: uniqueCounts[key] }
}
}
}
if (maxResponses.val >= consensusNo) {
let responseIndex = hashedResponses.indexOf(maxResponses.key);
return responseArray[responseIndex];
// console.log("responseIndex => ", responseIndex);
// let finalResponse = responseArray[responseIndex].data;
// console.log("response => ", responseArray[responseIndex]);
// console.log("Final response =>", finalResponse);
// if (finalResponse) {
// console.log("parser => ", parser);
// const data = typeof parser !== "undefined" ? parser(finalResponse) : finalResponse;
// return data;
// }
}
else {
return null;
}
}
const parseConsensusMessage = function (finalResponse, parser) {
const data = typeof parser !== "undefined" ? parser(finalResponse) : finalResponse;
return data;
}
module.exports = {
byteToHexString: function byteToHexString(uint8arr) {
if (!uint8arr) {
return '';
}
var hexStr = '';
for (var i = 0; i < uint8arr.length; i++) {
var hex = (uint8arr[i] & 0xff).toString(16);
hex = (hex.length === 1) ? '0' + hex : hex;
hexStr += hex;
}
//console.log("byteToHexString returning non-empty value")
return hexStr;
},
hexStringToByte: function hexStringToByte(str) {
if (!str) {
return new Uint8Array();
}
var a = [];
for (var i = 0, len = str.length; i < len; i += 2) {
a.push(parseInt(str.substr(i, 2), 16));
}
//console.log("HexStringToByte returning non-empty")
return new Uint8Array(a);
},
shuffleArray: function shuffleArray(array) {
var m = array.length, t, i;
// While there remain elements to shuffle…
while (m) {
// Pick a remaining element…
i = Math.floor(Math.random() * m--);
// And swap it with the current element.
t = array[m];
array[m] = array[i];
array[i] = t;
}
return array;
},
sleep: function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
},
toHex: (str) => {
var result = '';
for (var i = 0; i < str.length; i++) {
result += str.charCodeAt(i).toString(16);
}
return result;
},
computeStoragePartDataId: function (allocationId, path, fileName, partNum) {
return sha3.sha3_256(allocationId + ":" + path + ":" + fileName + ":" + partNum);
},
/*
A utility function to make a post request.
url: Complete URL along with path to where the post request is to be sent
jsonPostString: A stringfy of JSON object the payload for the request
Return: Returns a Promise.
*/
postReq: function postReq(url, data) {
const self = this;
return axios({
method: 'post',
url: url,
data: data,
transformResponse: function (responseData) {
return self.parseJson(responseData)
}
});
},
postReqToBlobber: function postReqToBlobber(url, data, params, clientId) {
return axios({
method: 'post',
url: `https://cors-anywhere.herokuapp.com/${url}`,
params: params,
body: data,
headers: {
'X-App-Client-ID': clientId
}
}).then((response)=> {
return response
}).catch((error)=> {
return error
})
},
deleteReqToBlobber: function deleteReqToBlobber(url, data, params, clientId) {
return axios({
method: 'delete',
url: `https://cors-anywhere.herokuapp.com/${url}`,
params: params,
body: data,
headers: {
'X-App-Client-ID': clientId,
'Content-Type': 'multipart/form-data'
}
}).then((response)=> {
return response
}).catch((error)=> {
return error
})
},
getReq: function getReq(url, params) {
const self = this;
return axios.get(url, {
params: params,
// validateStatus: function (status) {
// console.log("Status", status)
// console.log("Status url", url)
// return (status >= 200 && status < 300) || status == 400;
// },
transformResponse: function (data, headers) {
return self.parseJson(data)
}
});
},
parseJson: function (jsonString) {
return JSONbig.parse(jsonString)
},
getConsensusMessageFromResponse: getConsensusMessageFromResponse,
getConsensusedInformationFromSharders: function (sharders, url, params, parser) {
const self = this;
return new Promise(function (resolve, reject) {
const urls = sharders.map(sharder => sharder + url);
const promises = urls.map(url => self.getReq(url, params));
let percentage = Math.ceil((promises.length * consensusPercentage) / 100);
BlueBirdPromise.some(promises, percentage)
.then(function (result) {
//console.log("Result", result);
const hashedResponses = result.map(r => {
return sha3.sha3_256(JSON.stringify(r.data))
});
const consensusResponse = getConsensusMessageFromResponse(hashedResponses, percentage, result);
//console.log("consensusResponse", consensusResponse)
if (consensusResponse === null) {
reject({ error: "Not enough consensus" });
}
else {
resolve(parseConsensusMessage(consensusResponse.data, parser));
}
})
.catch(BlueBirdPromise.AggregateError, function (err) {
//console.log("Error in blue bird", err)
const errors = err.map(e => {
if (e.response !== undefined && e.response.status !== undefined && e.response.status === 400 && e.response.data !== undefined) {
return sha3.sha3_256(JSON.stringify(e.response.data))
}
else {
return e.code;
}
});
const consensusErrorResponse = getConsensusMessageFromResponse(errors, percentage, err, undefined);
if (consensusErrorResponse === null) {
reject({ error: "Not enough consensus" });
}
else {
reject(parseConsensusMessage(consensusErrorResponse.response.data));
}
});
// PromiseAll.all(promises).then(function (result) {
// // console.log("result.reject", result.reject);
// // This is needed otherwise error will print big trace from axios
// let consensusNo = ((sharders.length * 20) / 100);
// if (result.resolve.length >= consensusNo) {
// const hashedResponses = result.resolve.map(r => {
// return sha3.sha3_256(JSON.stringify(r.data))
// });
// const consensusResponse = getConsensusMessageFromResponse(hashedResponses, consensusNo, result.resolve);
// if (consensusResponse === null) {
// reject({ error: "Not enough consensus" });
// }
// else {
// resolve(parseConsensusMessage(consensusResponse.data, parser));
// }
// }
// else {
// const errors = result.reject.map(e => {
// if (e.response.status !== undefined && e.response.status === 400 && e.response.data !== undefined) {
// return sha3.sha3_256(JSON.stringify(e.response.data))
// }
// else {
// return e.message
// }
// });
// const consensusErrorResponse = getConsensusMessageFromResponse(errors, consensusNo, result.reject, undefined);
// if (consensusErrorResponse === null) {
// reject({ error: "Not enough consensus" });
// }
// else {
// reject(parseConsensusMessage(consensusErrorResponse.response.data));
// }
// // return error here
// // reject({ error: errors });
// }
// }, function (error) {
// console.error("This should never happen", error);
// reject({ error: error });
// });
});
},
doParallelPostReqToAllMiners: function (miners, url, postData) {
const self = this;
return new Promise(function (resolve, reject) {
const urls = miners.map(miner => miner + url);
const promises = urls.map(url => self.postReq(url, postData));
let percentage = Math.ceil(promises.length * consensusPercentage / 100);
BlueBirdPromise.some(promises, percentage)
.then(function (result) {
resolve(result[0].data);
})
.catch(BlueBirdPromise.AggregateError, function (err) {
reject({ error: err[0].code });
// err.forEach(function (e) {
// console.error(e.stack);
// });
});
// PromiseAll.all(promises).then(function (result) {
// // This is needed otherwise error will print big trace from axios
// const errors = result.reject.map(e => e.message);
// if (result.resolve.length === 0) {
// // return error here
// reject({ error: errors });
// }
// else {
// //console.log("Response", result.resolve[0].data);
// //resolve({ data: result.resolve[0].data, error: errors })
// resolve(result.resolve[0].data);
// }
// }, function (error) {
// reject({ error: error });
// });
});
}
}