2022-08-28 06:33:49 +00:00
|
|
|
import RpcQueryBase from "./base.js";
|
|
|
|
import { flatten } from "../util.js";
|
|
|
|
import { Buffer } from "buffer";
|
|
|
|
import { blake2b } from "libskynet";
|
|
|
|
import { ERR_MAX_TRIES_HIT } from "../error.js";
|
2022-08-29 03:19:45 +00:00
|
|
|
function flatHash(data) {
|
|
|
|
const flattenedData = flatten(data).sort();
|
|
|
|
return Buffer.from(blake2b(Buffer.from(JSON.stringify(flattenedData)))).toString("hex");
|
|
|
|
}
|
2022-08-28 06:33:49 +00:00
|
|
|
export default class WisdomRpcQuery extends RpcQueryBase {
|
|
|
|
_maxTries = 3;
|
|
|
|
_tries = 0;
|
|
|
|
checkResponses() {
|
|
|
|
const responseStore = this._responses;
|
|
|
|
const responseStoreData = Object.values(responseStore);
|
|
|
|
const responseObjects = responseStoreData.reduce((output, item) => {
|
2022-08-29 03:19:45 +00:00
|
|
|
const hash = flatHash(item?.data);
|
2022-08-28 06:33:49 +00:00
|
|
|
output[hash] = item?.data;
|
|
|
|
return output;
|
|
|
|
}, {});
|
|
|
|
const responses = responseStoreData.reduce((output, item) => {
|
2022-08-29 03:19:45 +00:00
|
|
|
const hash = flatHash(item?.data);
|
2022-08-28 06:33:49 +00:00
|
|
|
output[hash] = output[hash] ?? 0;
|
|
|
|
output[hash]++;
|
|
|
|
return output;
|
|
|
|
}, {});
|
2022-09-09 10:05:13 +00:00
|
|
|
if (!Object.keys(responses).length) {
|
|
|
|
if (Object.keys(this._errors).length) {
|
|
|
|
this.resolve({ error: Object.values(this._errors).pop() });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this._tries <= this._maxTries) {
|
|
|
|
this._tries++;
|
|
|
|
this.retry();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.resolve({ data: { error: ERR_MAX_TRIES_HIT } });
|
|
|
|
return;
|
|
|
|
}
|
2022-08-28 06:33:49 +00:00
|
|
|
for (const responseHash in responses) {
|
|
|
|
if (responses[responseHash] / responseStoreData.length >=
|
|
|
|
this._network.majorityThreshold) {
|
|
|
|
let response = responseObjects[responseHash];
|
|
|
|
// @ts-ignore
|
|
|
|
if (null === response) {
|
|
|
|
if (this._tries <= this._maxTries) {
|
|
|
|
this._tries++;
|
|
|
|
this.retry();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
response = { error: ERR_MAX_TRIES_HIT };
|
|
|
|
}
|
2022-08-29 03:19:45 +00:00
|
|
|
this.resolve({ data: response });
|
2022-08-28 06:33:49 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
retry() {
|
|
|
|
this._responses = {};
|
|
|
|
this._errors = {};
|
|
|
|
if (this._completed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.run();
|
|
|
|
}
|
|
|
|
getRelays() {
|
|
|
|
return this._network.relays;
|
|
|
|
}
|
|
|
|
}
|