diff --git a/aqua-examples/decentralized-blockchain-gateway/gateway/config.json b/aqua-examples/decentralized-blockchain-gateway/gateway/config.json index 9878224..fea4593 100644 --- a/aqua-examples/decentralized-blockchain-gateway/gateway/config.json +++ b/aqua-examples/decentralized-blockchain-gateway/gateway/config.json @@ -2,5 +2,9 @@ "providers": [ "https://goerli.infura.io/v3/77214656b25f4cad9cd2540c6d40c301", "https://goerli.infura.io/v3/c48f3b538f154204ad53d04aa8990544" - ] + ], + "mode": "round-robin", + "relay": "/dns4/kras-02.fluence.dev/tcp/19001/wss/p2p/12D3KooWHLxVhUQyAuZe6AHMB29P7wkvTNMn7eDMcsqimJYLKREf", + "serviceId": "25bf2293-7503-4a01-af00-d1b7d089ca37", + "port": 3000 } \ No newline at end of file diff --git a/aqua-examples/decentralized-blockchain-gateway/gateway/package.json b/aqua-examples/decentralized-blockchain-gateway/gateway/package.json index 31a49a1..2ca51c6 100644 --- a/aqua-examples/decentralized-blockchain-gateway/gateway/package.json +++ b/aqua-examples/decentralized-blockchain-gateway/gateway/package.json @@ -5,7 +5,7 @@ "main": "src/index.js", "type": "module", "scripts": { - "run:example": "node src/index.js 3000 \"/dns4/kras-02.fluence.dev/tcp/19001/wss/p2p/12D3KooWHLxVhUQyAuZe6AHMB29P7wkvTNMn7eDMcsqimJYLKREf\" \"config.json\" \"25bf2293-7503-4a01-af00-d1b7d089ca37\"", + "run:example": "node src/index.js \"config.json\"", "run": "node src/index.js", "req": "node web3run.js" }, diff --git a/aqua-examples/decentralized-blockchain-gateway/gateway/src/arguments.js b/aqua-examples/decentralized-blockchain-gateway/gateway/src/arguments.js index 7ebc49c..ab7fb55 100644 --- a/aqua-examples/decentralized-blockchain-gateway/gateway/src/arguments.js +++ b/aqua-examples/decentralized-blockchain-gateway/gateway/src/arguments.js @@ -1,25 +1,16 @@ +import {configHelp} from "./config.js"; + export function readArguments(args) { - const port = args[0] - const relay = args[1] - const configPath = args[2] - const serviceId = args[3] + const configPath = args[0] let errors = [] - if (!port) { - errors.push("Specify port") - } - if (!relay) { - errors.push("Specify Fluence peer address") - } + if (!configPath) { errors.push("Specify config with uri to ethereum RPC providers") } - if (!serviceId) { - errors.push("Specify id to ethereum Aqua service") - } return { - port, relay, configPath, serviceId, errors, - help: "Example: aqua-eth-gateway " + configPath, errors, + help: "Example: aqua-eth-gateway \n" + configHelp } } \ No newline at end of file diff --git a/aqua-examples/decentralized-blockchain-gateway/gateway/src/config.js b/aqua-examples/decentralized-blockchain-gateway/gateway/src/config.js index 2689819..88df10b 100644 --- a/aqua-examples/decentralized-blockchain-gateway/gateway/src/config.js +++ b/aqua-examples/decentralized-blockchain-gateway/gateway/src/config.js @@ -1,6 +1,26 @@ import fs from 'fs'; +export const configHelp = "Config structure: { port, relay, serviceId, providers, mode}\n" + + "Where mode can be: 'random' (default) or 'round-robin'" + export function readConfig(path) { const rawdata = fs.readFileSync(path); - return JSON.parse(rawdata); + const config = JSON.parse(rawdata); + + let errors = [] + if (!config.port) { + errors.push("Specify port ('port') in config") + } + if (!config.relay) { + errors.push("Specify Fluence peer address ('relay') in config") + } + + if (!config.serviceId) { + errors.push("Specify id to ethereum Aqua service ('serviceId') in config") + } + + return { + config, errors, + help: configHelp + } } diff --git a/aqua-examples/decentralized-blockchain-gateway/gateway/src/index.js b/aqua-examples/decentralized-blockchain-gateway/gateway/src/index.js index 41c8a04..a116e2e 100644 --- a/aqua-examples/decentralized-blockchain-gateway/gateway/src/index.js +++ b/aqua-examples/decentralized-blockchain-gateway/gateway/src/index.js @@ -4,107 +4,71 @@ import express from "express"; import bodyParser from "body-parser"; -import { JSONRPCServer } from "json-rpc-2.0"; -import { FluencePeer } from "@fluencelabs/fluence"; -import {call, randomLoadBalancingEth, registerLogger} from "../aqua-compiled/rpc.js"; +import {JSONRPCServer} from "json-rpc-2.0"; +import {FluencePeer} from "@fluencelabs/fluence"; +import {randomLoadBalancingEth, registerCounter, registerLogger, roundRobinEth} from "../aqua-compiled/rpc.js"; import {readArguments} from "./arguments.js"; import {readConfig} from "./config.js"; +import {methods} from "./methods.js"; -const args = readArguments(process.argv.slice(2)) +const args = readArguments(process.argv.slice(2)); if (args.errors.length > 0) { - console.log(args.help) - args.errors.forEach((err) => console.log(err)) - process.exit(1) + console.log(args.help); + args.errors.forEach((err) => console.log(err)); + process.exit(1); } -const config = readConfig(args.configPath) +const {config, errors, help} = readConfig(args.configPath); -console.log("Running server...") +if (errors.length > 0) { + errors.forEach((err) => console.log(err)); + console.log(help); + process.exit(1); +} -const route = "/" - -const methods = ['eth_accounts', - 'eth_blockNumber', - 'eth_call', - 'eth_chainId', - 'eth_estimateGas', - 'eth_getBalance', - 'eth_getBlockByHash', - 'eth_getBlockByNumber', - 'eth_getBlockTransactionCountByHash', - 'eth_getBlockTransactionCountByNumber', - 'eth_getCode', - 'eth_getLogs', - 'eth_getStorageAt', - 'eth_getTransactionByBlockHashAndIndex', - 'eth_getTransactionByBlockNumberAndIndex', - 'eth_getTransactionByHash', - 'eth_getTransactionCount', - 'eth_getTransactionReceipt', - 'eth_sendTransaction', - 'net_version', - 'web3_sha3', - 'eth_sendRawTransaction', - 'eth_subscribe', - 'eth_maxPriorityFeePerGas', - 'eth_getUncleCountByBlockHash', - 'eth_getUncleCountByBlockNumber', - 'net_listening', - 'net_peerCount', - 'eth_protocolVersion', - 'eth_syncing', - 'eth_coinbase', - 'eth_mining', - 'eth_hashrate', - 'eth_gasPrice', - 'eth_getStorageAt', - 'eth_sign', - 'eth_getCompilers', - 'eth_newBlockFilter', - 'eth_newPendingTransactionFilter', - 'eth_uninstallFilter', - 'eth_getFilterChanges', - 'eth_getWork', - 'eth_submitWork', - 'eth_submitHashrate', - 'db_putString', - 'db_getString', - 'db_putHex', - 'db_getHex', - 'shh_post', - 'shh_version', - 'shh_newIdentity', - 'shh_hasIdentity', - 'shh_newGroup', - 'shh_addToGroup', - 'shh_newFilter', - 'shh_uninstallFilter', - 'shh_getFilterChanges', - 'shh_getMessages'] +console.log("Running server..."); +const route = "/"; const server = new JSONRPCServer(); // initialize fluence client const fluence = new FluencePeer(); -await fluence.start({connectTo: args.relay}) +await fluence.start({connectTo: config.relay}); // handler for logger registerLogger(fluence, { log: s => { - console.log("log: " + s) + console.log("log: " + s); }, logCall: s => { - console.log("Call will be to : " + s) + console.log("Call will be to : " + s); }, }) -async function methodHandler(req, method) { - console.log(`Receiving request '${method}'`) - const result = await randomLoadBalancingEth(fluence, config.providers, method, req.map((s) => JSON.stringify(s)), args.serviceId) +let counter = 0; +registerCounter(fluence, "counter", { + incrementAndReturn: () => { + counter++; + console.log("Counter: " + counter) + return counter; + } +}) - return JSON.parse(result.value) +async function methodHandler(req, method) { + console.log(`Receiving request '${method}'`); + let result; + if (!config.mode || config.mode === "random") { + result = await randomLoadBalancingEth(fluence, config.providers, method, req.map((s) => JSON.stringify(s)), config.serviceId); + } else if (config.mode === "round-robin") { + console.log("peerId: " + fluence.getStatus().peerId) + result = await roundRobinEth(fluence, config.providers, method, req.map((s) => JSON.stringify(s)), config.serviceId, "counter", fluence.getStatus().peerId, + config.serviceId); + } + + + return JSON.parse(result.value); } @@ -113,7 +77,7 @@ function addMethod(op) { } // register all eth methods -methods.forEach( (m) =>{ +methods.forEach((m) => { addMethod(m); }) @@ -132,6 +96,6 @@ app.post(route, (req, res) => { }); }); -app.listen(args.port); +app.listen(config.port); -console.log("Server was started on port " + args.port) \ No newline at end of file +console.log("Server was started on port " + config.port); \ No newline at end of file diff --git a/aqua-examples/decentralized-blockchain-gateway/gateway/src/methods.js b/aqua-examples/decentralized-blockchain-gateway/gateway/src/methods.js new file mode 100644 index 0000000..9a531c6 --- /dev/null +++ b/aqua-examples/decentralized-blockchain-gateway/gateway/src/methods.js @@ -0,0 +1,58 @@ +export const methods = ['eth_accounts', + 'eth_blockNumber', + 'eth_call', + 'eth_chainId', + 'eth_estimateGas', + 'eth_getBalance', + 'eth_getBlockByHash', + 'eth_getBlockByNumber', + 'eth_getBlockTransactionCountByHash', + 'eth_getBlockTransactionCountByNumber', + 'eth_getCode', + 'eth_getLogs', + 'eth_getStorageAt', + 'eth_getTransactionByBlockHashAndIndex', + 'eth_getTransactionByBlockNumberAndIndex', + 'eth_getTransactionByHash', + 'eth_getTransactionCount', + 'eth_getTransactionReceipt', + 'eth_sendTransaction', + 'net_version', + 'web3_sha3', + 'eth_sendRawTransaction', + 'eth_subscribe', + 'eth_maxPriorityFeePerGas', + 'eth_getUncleCountByBlockHash', + 'eth_getUncleCountByBlockNumber', + 'net_listening', + 'net_peerCount', + 'eth_protocolVersion', + 'eth_syncing', + 'eth_coinbase', + 'eth_mining', + 'eth_hashrate', + 'eth_gasPrice', + 'eth_getStorageAt', + 'eth_sign', + 'eth_getCompilers', + 'eth_newBlockFilter', + 'eth_newPendingTransactionFilter', + 'eth_uninstallFilter', + 'eth_getFilterChanges', + 'eth_getWork', + 'eth_submitWork', + 'eth_submitHashrate', + 'db_putString', + 'db_getString', + 'db_putHex', + 'db_getHex', + 'shh_post', + 'shh_version', + 'shh_newIdentity', + 'shh_hasIdentity', + 'shh_newGroup', + 'shh_addToGroup', + 'shh_newFilter', + 'shh_uninstallFilter', + 'shh_getFilterChanges', + 'shh_getMessages'] \ No newline at end of file diff --git a/aqua-examples/decentralized-blockchain-gateway/gateway/web3run.js b/aqua-examples/decentralized-blockchain-gateway/gateway/web3run.js index bc4c167..216bf51 100644 --- a/aqua-examples/decentralized-blockchain-gateway/gateway/web3run.js +++ b/aqua-examples/decentralized-blockchain-gateway/gateway/web3run.js @@ -4,6 +4,12 @@ const web3 = new Web3("http://localhost:3000"); async function main() { const bn = await web3.eth.getBlockNumber() + await web3.eth.getBlockNumber() + await web3.eth.getBlockNumber() + await web3.eth.getBlockNumber() + await web3.eth.getBlockNumber() + await web3.eth.getBlockNumber() + await web3.eth.getBlockNumber() console.log(bn) const resp = await web3.eth.getTransaction("0x8bad403edde37642e4dab6c91eeca77b979fce1c979c14ca8755f5c3573eaeb4")