2023-02-09 14:07:18 +04:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
import express from "express";
|
|
|
|
import bodyParser from "body-parser";
|
|
|
|
import { JSONRPCServer } from "json-rpc-2.0";
|
|
|
|
import { FluencePeer } from "@fluencelabs/fluence";
|
2023-02-14 08:38:15 +04:00
|
|
|
import {call, randomLoadBalancingEth, registerLogger} from "../aqua-compiled/rpc.js";
|
|
|
|
import {readArguments} from "./arguments.js";
|
|
|
|
import {readConfig} from "./config.js";
|
2023-02-09 14:07:18 +04:00
|
|
|
|
2023-02-14 08:38:15 +04:00
|
|
|
const args = readArguments(process.argv.slice(2))
|
2023-02-09 14:07:18 +04:00
|
|
|
|
2023-02-14 08:38:15 +04:00
|
|
|
if (args.errors.length > 0) {
|
|
|
|
console.log(args.help)
|
|
|
|
args.errors.forEach((err) => console.log(err))
|
2023-02-09 14:07:18 +04:00
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
|
2023-02-14 08:38:15 +04:00
|
|
|
const config = readConfig(args.configPath)
|
|
|
|
|
2023-02-09 14:07:18 +04:00
|
|
|
console.log("Running server...")
|
|
|
|
|
|
|
|
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']
|
|
|
|
|
|
|
|
|
|
|
|
const server = new JSONRPCServer();
|
|
|
|
|
|
|
|
// initialize fluence client
|
|
|
|
const fluence = new FluencePeer();
|
2023-02-14 08:38:15 +04:00
|
|
|
await fluence.start({connectTo: args.relay})
|
2023-02-09 14:07:18 +04:00
|
|
|
|
|
|
|
// handler for logger
|
|
|
|
registerLogger(fluence, {
|
|
|
|
log: s => {
|
|
|
|
console.log("log: " + s)
|
2023-02-14 08:38:15 +04:00
|
|
|
},
|
|
|
|
logCall: s => {
|
|
|
|
console.log("Call will be to : " + s)
|
|
|
|
},
|
2023-02-09 14:07:18 +04:00
|
|
|
})
|
|
|
|
|
2023-02-14 08:38:15 +04:00
|
|
|
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)
|
2023-02-09 14:07:18 +04:00
|
|
|
|
|
|
|
return JSON.parse(result.value)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function addMethod(op) {
|
|
|
|
server.addMethod(op, async (req) => methodHandler(req, op));
|
|
|
|
}
|
|
|
|
|
|
|
|
// register all eth methods
|
|
|
|
methods.forEach( (m) =>{
|
|
|
|
addMethod(m);
|
|
|
|
})
|
|
|
|
|
|
|
|
const app = express();
|
|
|
|
app.use(bodyParser.json());
|
|
|
|
|
|
|
|
// register JSON-RPC handler
|
|
|
|
app.post(route, (req, res) => {
|
|
|
|
const jsonRPCRequest = req.body;
|
|
|
|
server.receive(jsonRPCRequest).then((jsonRPCResponse) => {
|
|
|
|
if (jsonRPCResponse) {
|
|
|
|
res.json(jsonRPCResponse);
|
|
|
|
} else {
|
|
|
|
res.sendStatus(204);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-02-14 08:38:15 +04:00
|
|
|
app.listen(args.port);
|
2023-02-09 14:07:18 +04:00
|
|
|
|
2023-02-14 08:38:15 +04:00
|
|
|
console.log("Server was started on port " + args.port)
|