*Rwrite checkResponses
This commit is contained in:
parent
da06f787dc
commit
42d53b6137
|
@ -2,6 +2,8 @@ import { clearTimeout, setTimeout } from "timers";
|
||||||
import RpcNetwork from "./rpcNetwork.js";
|
import RpcNetwork from "./rpcNetwork.js";
|
||||||
import { pack, unpack } from "msgpackr";
|
import { pack, unpack } from "msgpackr";
|
||||||
import { RPCRequest, RPCResponse } from "./types";
|
import { RPCRequest, RPCResponse } from "./types";
|
||||||
|
import { Buffer } from "buffer";
|
||||||
|
import {blake2b} from "libskynet"
|
||||||
|
|
||||||
export default class RpcQuery {
|
export default class RpcQuery {
|
||||||
private _network: RpcNetwork;
|
private _network: RpcNetwork;
|
||||||
|
@ -89,33 +91,43 @@ export default class RpcQuery {
|
||||||
}
|
}
|
||||||
|
|
||||||
private checkResponses() {
|
private checkResponses() {
|
||||||
const responses: { [response: string]: number } = {};
|
|
||||||
const responseStore = this._responses;
|
const responseStore = this._responses;
|
||||||
|
|
||||||
const responseStoreKeys = Object.keys(responseStore);
|
const responseStoreData = Object.values(responseStore);
|
||||||
|
|
||||||
// tslint:disable-next-line:forin
|
type ResponseGroup = { [response: string]: number };
|
||||||
for (const peer in responseStore) {
|
|
||||||
const responseIndex = responseStoreKeys.indexOf(peer);
|
|
||||||
|
|
||||||
responses[responseIndex] = responses[responseIndex] ?? 0;
|
const responseObjects = responseStoreData.reduce((output: any, item) => {
|
||||||
responses[responseIndex]++;
|
const hash = Buffer.from(blake2b(Buffer.from(JSON.stringify(item?.data)))).toString("hex");
|
||||||
}
|
output[hash] = item?.data;
|
||||||
for (const responseIndex in responses) {
|
return output;
|
||||||
|
}, {});
|
||||||
|
const responses: ResponseGroup = responseStoreData.reduce(
|
||||||
|
(output: ResponseGroup, 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 (
|
if (
|
||||||
responses[responseIndex] / responseStoreKeys.length >=
|
responses[responseHash] / responseStoreData.length >=
|
||||||
this._network.majorityThreshold
|
this._network.majorityThreshold
|
||||||
) {
|
) {
|
||||||
const response: RPCResponse | null =
|
// @ts-ignore
|
||||||
responseStore[responseStoreKeys[parseInt(responseIndex, 10)]];
|
const response: RPCResponse = responseObjects[responseHash];
|
||||||
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
if (null === response || null === response?.data) {
|
if (null === response) {
|
||||||
this.retry();
|
this.retry();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.resolve(response?.data);
|
this.resolve(response);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -130,3 +142,11 @@ export default class RpcQuery {
|
||||||
this.init();
|
this.init();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isPromise(obj: Promise<any>) {
|
||||||
|
return (
|
||||||
|
!!obj &&
|
||||||
|
(typeof obj === "object" || typeof obj === "function") &&
|
||||||
|
typeof obj.then === "function"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue