2022-06-27 19:51:51 +00:00
|
|
|
import { clearTimeout, setTimeout } from "timers";
|
|
|
|
import { pack, unpack } from "msgpackr";
|
2022-07-19 18:48:27 +00:00
|
|
|
import { Buffer } from "buffer";
|
|
|
|
import { blake2b } from "libskynet";
|
2022-06-27 19:51:51 +00:00
|
|
|
export default class RpcQuery {
|
2022-07-19 18:48:27 +00:00
|
|
|
_network;
|
|
|
|
_query;
|
|
|
|
_promise;
|
|
|
|
_timeoutTimer;
|
|
|
|
_timeout = false;
|
|
|
|
_completed = false;
|
|
|
|
_responses = {};
|
|
|
|
_promiseResolve;
|
2022-06-27 19:51:51 +00:00
|
|
|
constructor(network, query) {
|
|
|
|
this._network = network;
|
|
|
|
this._query = query;
|
|
|
|
this.init();
|
|
|
|
}
|
2022-07-20 06:04:37 +00:00
|
|
|
get result() {
|
2022-06-27 19:51:51 +00:00
|
|
|
return this._promise;
|
|
|
|
}
|
|
|
|
handeTimeout() {
|
|
|
|
this.resolve(false, true);
|
|
|
|
}
|
|
|
|
resolve(data, timeout = false) {
|
|
|
|
clearTimeout(this._timeoutTimer);
|
|
|
|
this._timeout = timeout;
|
|
|
|
this._completed = true;
|
|
|
|
// @ts-ignore
|
|
|
|
this._promiseResolve(data);
|
|
|
|
}
|
|
|
|
async init() {
|
|
|
|
this._promise =
|
|
|
|
this._promise ??
|
|
|
|
new Promise((resolve) => {
|
|
|
|
this._promiseResolve = resolve;
|
|
|
|
});
|
|
|
|
this._timeoutTimer =
|
|
|
|
this._timeoutTimer ??
|
|
|
|
setTimeout(this.handeTimeout.bind(this), this._network.queryTimeout * 1000);
|
|
|
|
await this._network.ready;
|
|
|
|
const promises = [];
|
|
|
|
// tslint:disable-next-line:forin
|
|
|
|
for (const relay of this._network.relays) {
|
|
|
|
promises.push(this.queryRelay(relay));
|
|
|
|
}
|
|
|
|
await Promise.allSettled(promises);
|
|
|
|
this.checkResponses();
|
|
|
|
}
|
|
|
|
async queryRelay(relay) {
|
2022-07-19 18:48:27 +00:00
|
|
|
let socket;
|
|
|
|
try {
|
|
|
|
socket = this._network.dht.connect(Buffer.from(relay, "hex"));
|
|
|
|
if (isPromise(socket)) {
|
|
|
|
socket = await socket;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
return;
|
|
|
|
}
|
2022-06-27 19:51:51 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2022-07-23 15:43:05 +00:00
|
|
|
let timer;
|
2022-06-27 19:51:51 +00:00
|
|
|
socket.on("data", (res) => {
|
2022-07-23 15:43:05 +00:00
|
|
|
clearTimeout(timer);
|
2022-06-27 19:51:51 +00:00
|
|
|
socket.end();
|
|
|
|
const response = unpack(res);
|
|
|
|
if (response && response.error) {
|
|
|
|
return reject(response);
|
|
|
|
}
|
|
|
|
this._responses[relay] = response;
|
|
|
|
resolve(null);
|
|
|
|
});
|
|
|
|
socket.on("error", (error) => reject({ error }));
|
2022-07-23 15:43:05 +00:00
|
|
|
socket.write("rpc");
|
2022-06-27 19:51:51 +00:00
|
|
|
socket.write(pack(this._query));
|
2022-07-23 15:43:05 +00:00
|
|
|
timer = setTimeout(() => {
|
|
|
|
reject("timeout");
|
|
|
|
}, this._network.relayTimeout * 1000);
|
2022-06-27 19:51:51 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
checkResponses() {
|
|
|
|
const responseStore = this._responses;
|
2022-07-19 18:48:27 +00:00
|
|
|
const responseStoreData = Object.values(responseStore);
|
|
|
|
const responseObjects = responseStoreData.reduce((output, item) => {
|
|
|
|
const hash = Buffer.from(blake2b(Buffer.from(JSON.stringify(item?.data)))).toString("hex");
|
|
|
|
output[hash] = item?.data;
|
|
|
|
return output;
|
|
|
|
}, {});
|
|
|
|
const responses = responseStoreData.reduce((output, item) => {
|
|
|
|
const hash = Buffer.from(blake2b(Buffer.from(JSON.stringify(item?.data)))).toString("hex");
|
|
|
|
output[hash] = output[hash] ?? 0;
|
|
|
|
output[hash]++;
|
|
|
|
return output;
|
|
|
|
}, {});
|
|
|
|
for (const responseHash in responses) {
|
|
|
|
if (responses[responseHash] / responseStoreData.length >=
|
2022-06-27 19:56:56 +00:00
|
|
|
this._network.majorityThreshold) {
|
2022-06-27 19:51:51 +00:00
|
|
|
// @ts-ignore
|
2022-07-19 18:48:27 +00:00
|
|
|
const response = responseObjects[responseHash];
|
|
|
|
// @ts-ignore
|
|
|
|
if (null === response) {
|
2022-06-27 19:51:51 +00:00
|
|
|
this.retry();
|
|
|
|
return;
|
|
|
|
}
|
2022-07-19 18:48:27 +00:00
|
|
|
this.resolve(response);
|
|
|
|
break;
|
2022-06-27 19:51:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
retry() {
|
|
|
|
this._responses = {};
|
|
|
|
if (this._completed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
}
|
2022-07-19 18:48:27 +00:00
|
|
|
function isPromise(obj) {
|
|
|
|
return (!!obj &&
|
|
|
|
(typeof obj === "object" || typeof obj === "function") &&
|
|
|
|
typeof obj.then === "function");
|
|
|
|
}
|