*Consolidate code to flatHash helper
This commit is contained in:
parent
2785bdfa50
commit
bb0841cf4a
|
@ -5,6 +5,13 @@ import type { RPCResponse } from "@lumeweb/relay-types";
|
||||||
import { blake2b } from "libskynet";
|
import { blake2b } from "libskynet";
|
||||||
import { ERR_MAX_TRIES_HIT } from "../error.js";
|
import { ERR_MAX_TRIES_HIT } from "../error.js";
|
||||||
|
|
||||||
|
function flatHash(data: any) {
|
||||||
|
const flattenedData = flatten(data).sort();
|
||||||
|
return Buffer.from(
|
||||||
|
blake2b(Buffer.from(JSON.stringify(flattenedData)))
|
||||||
|
).toString("hex");
|
||||||
|
}
|
||||||
|
|
||||||
export default class WisdomRpcQuery extends RpcQueryBase {
|
export default class WisdomRpcQuery extends RpcQueryBase {
|
||||||
private _maxTries = 3;
|
private _maxTries = 3;
|
||||||
private _tries = 0;
|
private _tries = 0;
|
||||||
|
@ -16,20 +23,13 @@ export default class WisdomRpcQuery extends RpcQueryBase {
|
||||||
type ResponseGroup = { [response: string]: number };
|
type ResponseGroup = { [response: string]: number };
|
||||||
|
|
||||||
const responseObjects = responseStoreData.reduce((output: any, item) => {
|
const responseObjects = responseStoreData.reduce((output: any, item) => {
|
||||||
const itemFlattened = flatten(item?.data).sort();
|
const hash = flatHash(item?.data);
|
||||||
|
|
||||||
const hash = Buffer.from(
|
|
||||||
blake2b(Buffer.from(JSON.stringify(itemFlattened)))
|
|
||||||
).toString("hex");
|
|
||||||
output[hash] = item?.data;
|
output[hash] = item?.data;
|
||||||
return output;
|
return output;
|
||||||
}, {});
|
}, {});
|
||||||
const responses: ResponseGroup = responseStoreData.reduce(
|
const responses: ResponseGroup = responseStoreData.reduce(
|
||||||
(output: ResponseGroup, item) => {
|
(output: ResponseGroup, item) => {
|
||||||
const itemFlattened = flatten(item?.data).sort();
|
const hash = flatHash(item?.data);
|
||||||
const hash = Buffer.from(
|
|
||||||
blake2b(Buffer.from(JSON.stringify(itemFlattened)))
|
|
||||||
).toString("hex");
|
|
||||||
output[hash] = output[hash] ?? 0;
|
output[hash] = output[hash] ?? 0;
|
||||||
output[hash]++;
|
output[hash]++;
|
||||||
return output;
|
return output;
|
||||||
|
|
Loading…
Reference in New Issue