quorum implementation

This commit is contained in:
DieMyst
2023-02-21 16:31:21 +04:00
parent e9e57b09f4
commit d302f269ad
7 changed files with 821 additions and 54 deletions

View File

@ -1,8 +1,9 @@
import fs from 'fs';
export const configHelp = "Config structure: { port, relay, serviceId, providers, mode, counterServiceId?, counterPeerId?}\n" +
"Where 'mode' can be: 'random' (default) or 'round-robin',\n" +
"'counterServiceId' and 'counterPeerId' will use local service if undefined"
"Where 'mode' can be: 'random' (default), 'round-robin' or 'quorum',\n" +
"'counterServiceId' and 'counterPeerId' will use local service if undefined.\n"
"'quorumServiceId' and 'quorumPeerId' will use local service if undefined.\n"
export function readConfig(path) {
const rawdata = fs.readFileSync(path);

View File

@ -7,7 +7,14 @@ import bodyParser from "body-parser";
import {JSONRPCServer} from "json-rpc-2.0";
import {Fluence} from '@fluencelabs/js-client.api';
import "@fluencelabs/js-client.node"
import {randomLoadBalancingEth, registerCounter, registerLogger, roundRobinEth} from "../aqua-compiled/rpc.js";
import {
quorumEth,
randomLoadBalancingEth,
registerCounter,
registerLogger,
registerQuorumChecker,
roundRobinEth
} from "../aqua-compiled/rpc.js";
import {readArguments} from "./arguments.js";
import {readConfig} from "./config.js";
import {methods} from "./methods.js";
@ -57,8 +64,48 @@ registerCounter("counter", {
}
})
function findSameResults(results, minNum) {
const resultCounts = results.filter((obj) => obj.success).map((obj) => obj.value).reduce(function(i, v, idx) {
if (i[v] === undefined) {
i[v] = 1
} else {
i[v] = i[v] + 1;
}
return i;
}, {});
const getMaxRepeated = Math.max(...Object.values(resultCounts));
if (getMaxRepeated >= minNum) {
console.log(resultCounts)
const max = Object.entries(resultCounts).find((kv) => kv[1] === getMaxRepeated)
return {
value: max[0],
results: [],
error: ""
}
} else {
return {
error: "No consensus in results",
results: results,
value: ""
}
}
}
registerQuorumChecker("quorum",
{
check: (ethResults, minQuorum) => {
console.log(ethResults)
return findSameResults(ethResults, minQuorum)
}
}
)
const counterServiceId = config.counterServiceId || 'counter'
const counterPeerId = config.counterPeerId || peerId
const quorumServiceId = config.quorumServiceId || 'quorum'
const quorumPeerId = config.quorumPeerId || peerId
const quorumNumber = config.quorumNumber || 2
async function methodHandler(req, method) {
console.log(`Receiving request '${method}'`);
@ -69,6 +116,15 @@ async function methodHandler(req, method) {
console.log("peerId: " + peerId)
result = await roundRobinEth(config.providers, method, req.map((s) => JSON.stringify(s)), config.serviceId, counterServiceId, counterPeerId,
config.serviceId);
} else if (config.mode === "quorum") {
result = await quorumEth(config.providers, config.quorumNumber, 5000, method, req.map((s) => JSON.stringify(s)), config.serviceId, quorumServiceId, quorumPeerId,
config.serviceId);
if (result.error) {
return {error: result.error, results: result.results}
}
console.log(result)
}