mirror of
https://github.com/fluencelabs/examples
synced 2025-06-22 14:21:32 +00:00
quorum implementation
This commit is contained in:
@ -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);
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user