*Consolidate code to flatHash helper

This commit is contained in:
Derrick Hammer 2022-08-28 23:16:33 -04:00
parent 2785bdfa50
commit bb0841cf4a
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 9 additions and 9 deletions

View File

@ -5,6 +5,13 @@ import type { RPCResponse } from "@lumeweb/relay-types";
import { blake2b } from "libskynet";
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 {
private _maxTries = 3;
private _tries = 0;
@ -16,20 +23,13 @@ export default class WisdomRpcQuery extends RpcQueryBase {
type ResponseGroup = { [response: string]: number };
const responseObjects = responseStoreData.reduce((output: any, item) => {
const itemFlattened = flatten(item?.data).sort();
const hash = Buffer.from(
blake2b(Buffer.from(JSON.stringify(itemFlattened)))
).toString("hex");
const hash = flatHash(item?.data);
output[hash] = item?.data;
return output;
}, {});
const responses: ResponseGroup = responseStoreData.reduce(
(output: ResponseGroup, item) => {
const itemFlattened = flatten(item?.data).sort();
const hash = Buffer.from(
blake2b(Buffer.from(JSON.stringify(itemFlattened)))
).toString("hex");
const hash = flatHash(item?.data);
output[hash] = output[hash] ?? 0;
output[hash]++;
return output;