diff --git a/gateway/aqua/logger.aqua b/gateway/aqua/logger.aqua index 35a75a62..ccfee141 100644 --- a/gateway/aqua/logger.aqua +++ b/gateway/aqua/logger.aqua @@ -5,14 +5,10 @@ export LoggerSrv import Worker from "@fluencelabs/aqua-lib/subnet.aqua" ability Logger: - log(s: []string) - logNum(n: u32) logCall(s: string) logWorker(w: Worker) service LoggerSrv("logger"): - log(s: []string) - logNum(n: u32) logCall(s: string) logWorker(w: Worker) @@ -21,13 +17,6 @@ service LoggerSrv("logger"): -- through LoggerSrv func initPeerLogger() -> Logger: -- closures do not capture topology here - - log = func (s: []string): - on INIT_PEER_ID via HOST_PEER_ID: - LoggerSrv.log(s) - logNum = func (n: u32): - on INIT_PEER_ID via HOST_PEER_ID: - LoggerSrv.logNum(n) logCall = func (s: string): on INIT_PEER_ID via HOST_PEER_ID: LoggerSrv.logCall(s) @@ -35,4 +24,4 @@ func initPeerLogger() -> Logger: on INIT_PEER_ID via HOST_PEER_ID: LoggerSrv.logWorker(w) - <- Logger(log=log, logNum=logNum, logCall=logCall, logWorker=logWorker) \ No newline at end of file + <- Logger(logCall=logCall, logWorker=logWorker) \ No newline at end of file diff --git a/gateway/aqua/quorum.aqua b/gateway/aqua/quorum.aqua index ee73ffa9..242511f8 100644 --- a/gateway/aqua/quorum.aqua +++ b/gateway/aqua/quorum.aqua @@ -1,26 +1,21 @@ -aqua Quorum declares QuorumChecker, QuorumResult, onPeerQuorumChecker +aqua Quorum declares QuorumChecker, onPeerQuorumChecker import JsonString from "services.aqua" export QuorumCheckerSrv -data QuorumResult: - value: string - results: []JsonString - error: string - -- Ability to check if a quorum on results is reached ability QuorumChecker: - check(results: []JsonString, minResults: u32) -> QuorumResult + check(results: []JsonString, minResults: u32) -> JsonString service QuorumCheckerSrv("quorum"): - check(results: []JsonString, minResults: u32) -> QuorumResult + check(results: []JsonString, minResults: u32) -> JsonString -- Create a QuorumChecker ability -- that checks quorum on peer through QuorumCheckerSrv(id) func onPeerQuorumChecker(peer: string, id: string) -> QuorumChecker: -- closure does not capture topology here - check = func (results: []JsonString, minResults: u32) -> QuorumResult: + check = func (results: []JsonString, minResults: u32) -> JsonString: on peer: QuorumCheckerSrv id res <- QuorumCheckerSrv.check(results, minResults) diff --git a/gateway/aqua/rpc.aqua b/gateway/aqua/rpc.aqua index d54040aa..d58a34d0 100644 --- a/gateway/aqua/rpc.aqua +++ b/gateway/aqua/rpc.aqua @@ -9,7 +9,7 @@ use "deals.aqua" import Logger, initPeerLogger from "logger.aqua" import Balancer, ProviderBalancer, randomBalancer, cycleBalancer from "balancer.aqua" import onPeerCounter from "counter.aqua" -import QuorumChecker, QuorumResult, onPeerQuorumChecker from "quorum.aqua" +import QuorumChecker, onPeerQuorumChecker from "quorum.aqua" import timeRandom from "random.aqua" import RPCEth, fromWorkerProvider from "eth_rpc.aqua" @@ -17,9 +17,6 @@ import NumOp from "utils.aqua" export randomLoadBalancingEth, roundRobinEth, quorumEth -func errorQuorumResult(msg: string) -> QuorumResult: - <- QuorumResult(value = "", results = [], error = msg) - func errorJsonString(msg: string) -> JsonString: <- JsonString(value = "", success = false, error = msg) @@ -77,7 +74,7 @@ func roundRobinEth(uris: []string, method: string, jsonArgs: []string, counterSe <- result! -- Call RPC method with workers quorum and provider load balancing -func quorum{ProviderBalancer, QuorumChecker}(workers: []Worker, quorumNumber: u32, timeout: u32, method: string, jsonArgs: []string) -> QuorumResult: +func quorum{ProviderBalancer, QuorumChecker}(workers: []Worker, quorumNumber: u32, timeout: u32, method: string, jsonArgs: []string) -> JsonString: results: *JsonString on HOST_PEER_ID: for worker <- workers par: @@ -92,12 +89,12 @@ func quorum{ProviderBalancer, QuorumChecker}(workers: []Worker, quorumNumber: u3 <- QuorumChecker.check(results, quorumNumber) -- Call RPC method with workers quorum and provider load balancing -func quorumEth(uris: []string, quorumNumber: u32, timeout: u32, method: string, jsonArgs: []string, quorumServiceId: string, quorumPeerId: string) -> QuorumResult: - result: *QuorumResult +func quorumEth(uris: []string, quorumNumber: u32, timeout: u32, method: string, jsonArgs: []string, quorumServiceId: string, quorumPeerId: string) -> JsonString: + result: *JsonString workers, error <- getWorkers() if error != nil: - result <- errorQuorumResult(error!) + result <- errorJsonString(error!) else: random <- timeRandom() balancer <- randomBalancer{random}(workers, uris) diff --git a/gateway/package-lock.json b/gateway/package-lock.json index d7e93728..efb01eac 100644 --- a/gateway/package-lock.json +++ b/gateway/package-lock.json @@ -1,29 +1,19 @@ { - "name": "@fluencelabs/aqua-eth-gateway", + "name": "gateway", "version": "0.0.18", "lockfileVersion": 3, "requires": true, "packages": { "": { - "name": "@fluencelabs/aqua-eth-gateway", - "version": "0.0.18", + "name": "gateway", "license": "Apache-2.0", "dependencies": { "@fluencelabs/js-client": "0.8.4", "body-parser": "1.20.2", "express": "4.18.2", - "json-rpc-2.0": "1.7.0", - "web3": "4.3.0" - }, - "bin": { - "aqua-eth-gateway": "src/index.js" + "json-rpc-2.0": "1.7.0" } }, - "node_modules/@adraffy/ens-normalize": { - "version": "1.10.0", - "resolved": "https://registry.npmjs.org/@adraffy/ens-normalize/-/ens-normalize-1.10.0.tgz", - "integrity": "sha512-nA9XHtlAkYfJxY7bce8DcN7eKxWWCWkU+1GR9d+U6MbNpfwQp8TI7vqOsBsMcHoT4mBu2kypKoSKnghEzOOq5Q==" - }, "node_modules/@chainsafe/as-chacha20poly1305": { "version": "0.1.0", "resolved": "https://registry.npmjs.org/@chainsafe/as-chacha20poly1305/-/as-chacha20poly1305-0.1.0.tgz", @@ -135,17 +125,6 @@ "@chainsafe/is-ip": "^2.0.1" } }, - "node_modules/@ethereumjs/rlp": { - "version": "4.0.1", - "resolved": "https://registry.npmjs.org/@ethereumjs/rlp/-/rlp-4.0.1.tgz", - "integrity": "sha512-tqsQiBQDQdmPWE1xkkBq4rlSW5QZpLOUJ5RJh2/9fug+q9tnUhuZoVLk7s0scUIKTOzEtR72DFBXI4WiZcMpvw==", - "bin": { - "rlp": "bin/rlp" - }, - "engines": { - "node": ">=14" - } - }, "node_modules/@fluencelabs/avm": { "version": "0.59.0", "resolved": "https://registry.npmjs.org/@fluencelabs/avm/-/avm-0.59.0.tgz", @@ -993,61 +972,6 @@ "url": "https://paulmillr.com/funding/" } }, - "node_modules/@scure/base": { - "version": "1.1.3", - "resolved": "https://registry.npmjs.org/@scure/base/-/base-1.1.3.tgz", - "integrity": "sha512-/+SgoRjLq7Xlf0CWuLHq2LUZeL/w65kfzAPG5NH9pcmBhs+nunQTn4gvdwgMTIXnt9b2C/1SeL2XiysZEyIC9Q==", - "funding": { - "url": "https://paulmillr.com/funding/" - } - }, - "node_modules/@scure/bip32": { - "version": "1.3.1", - "resolved": "https://registry.npmjs.org/@scure/bip32/-/bip32-1.3.1.tgz", - "integrity": "sha512-osvveYtyzdEVbt3OfwwXFr4P2iVBL5u1Q3q4ONBfDY/UpOuXmOlbgwc1xECEboY8wIays8Yt6onaWMUdUbfl0A==", - "dependencies": { - "@noble/curves": "~1.1.0", - "@noble/hashes": "~1.3.1", - "@scure/base": "~1.1.0" - }, - "funding": { - "url": "https://paulmillr.com/funding/" - } - }, - "node_modules/@scure/bip32/node_modules/@noble/curves": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/@noble/curves/-/curves-1.1.0.tgz", - "integrity": "sha512-091oBExgENk/kGj3AZmtBDMpxQPDtxQABR2B9lb1JbVTs6ytdzZNwvhxQ4MWasRNEzlbEH8jCWFCwhF/Obj5AA==", - "dependencies": { - "@noble/hashes": "1.3.1" - }, - "funding": { - "url": "https://paulmillr.com/funding/" - } - }, - "node_modules/@scure/bip32/node_modules/@noble/hashes": { - "version": "1.3.1", - "resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-1.3.1.tgz", - "integrity": "sha512-EbqwksQwz9xDRGfDST86whPBgM65E0OH/pCgqW0GBVzO22bNE+NuIbeTb714+IfSjU3aRk47EUvXIb5bTsenKA==", - "engines": { - "node": ">= 16" - }, - "funding": { - "url": "https://paulmillr.com/funding/" - } - }, - "node_modules/@scure/bip39": { - "version": "1.2.1", - "resolved": "https://registry.npmjs.org/@scure/bip39/-/bip39-1.2.1.tgz", - "integrity": "sha512-Z3/Fsz1yr904dduJD0NpiyRHhRYHdcnyh73FZWiV+/qhWi83wNJ3NWolYqCEN+ZWsUz2TWwajJggcRE9r1zUYg==", - "dependencies": { - "@noble/hashes": "~1.3.0", - "@scure/base": "~1.1.0" - }, - "funding": { - "url": "https://paulmillr.com/funding/" - } - }, "node_modules/@types/node": { "version": "20.6.5", "resolved": "https://registry.npmjs.org/@types/node/-/node-20.6.5.tgz", @@ -1082,20 +1006,6 @@ "tar-stream": "^2.1.0" } }, - "node_modules/abitype": { - "version": "0.7.1", - "resolved": "https://registry.npmjs.org/abitype/-/abitype-0.7.1.tgz", - "integrity": "sha512-VBkRHTDZf9Myaek/dO3yMmOzB/y2s3Zo6nVU7yaw1G+TvCHAjwaJzNGN9yo4K5D8bU/VZXKP1EJpRhFr862PlQ==", - "peerDependencies": { - "typescript": ">=4.9.4", - "zod": "^3 >=3.19.1" - }, - "peerDependenciesMeta": { - "zod": { - "optional": true - } - } - }, "node_modules/accepts": { "version": "1.3.8", "resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.8.tgz", @@ -1135,17 +1045,6 @@ "node": ">=12.0.0" } }, - "node_modules/available-typed-arrays": { - "version": "1.0.5", - "resolved": "https://registry.npmjs.org/available-typed-arrays/-/available-typed-arrays-1.0.5.tgz", - "integrity": "sha512-DMD0KiN46eipeziST1LPP/STfDU0sufISXmjSgvVsoU2tqxctQeASejWcfNtxYKqETM1UxQ8sp2OrSBWpHY6sw==", - "engines": { - "node": ">= 0.4" - }, - "funding": { - "url": "https://github.com/sponsors/ljharb" - } - }, "node_modules/base-x": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/base-x/-/base-x-4.0.0.tgz", @@ -1317,25 +1216,6 @@ "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz", "integrity": "sha512-QADzlaHc8icV8I7vbaJXJwod9HWYp8uCqf1xa4OfNu1T7JVxQIrUgOWtHdNDtPiywmFbiS12VjotIXLrKM3orQ==" }, - "node_modules/crc-32": { - "version": "1.2.2", - "resolved": "https://registry.npmjs.org/crc-32/-/crc-32-1.2.2.tgz", - "integrity": "sha512-ROmzCKrTnOwybPcJApAA6WBWij23HVfGVNKqqrZpuyZOHqK2CwHSvpGuyt/UNNvaIjEd8X5IFGp4Mh+Ie1IHJQ==", - "bin": { - "crc32": "bin/crc32.njs" - }, - "engines": { - "node": ">=0.8" - } - }, - "node_modules/cross-fetch": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/cross-fetch/-/cross-fetch-4.0.0.tgz", - "integrity": "sha512-e4a5N8lVvuLgAWgnCrLr2PP0YyDOTHa9H/Rj54dirp61qXnNq46m82bRhNqIA5VccJtWBvPTFRV3TtvHUKPB1g==", - "dependencies": { - "node-fetch": "^2.6.12" - } - }, "node_modules/datastore-core": { "version": "9.2.9", "resolved": "https://registry.npmjs.org/datastore-core/-/datastore-core-9.2.9.tgz", @@ -1464,39 +1344,6 @@ "node": ">= 0.6" } }, - "node_modules/ethereum-cryptography": { - "version": "2.1.2", - "resolved": "https://registry.npmjs.org/ethereum-cryptography/-/ethereum-cryptography-2.1.2.tgz", - "integrity": "sha512-Z5Ba0T0ImZ8fqXrJbpHcbpAvIswRte2wGNR/KePnu8GbbvgJ47lMxT/ZZPG6i9Jaht4azPDop4HaM00J0J59ug==", - "dependencies": { - "@noble/curves": "1.1.0", - "@noble/hashes": "1.3.1", - "@scure/bip32": "1.3.1", - "@scure/bip39": "1.2.1" - } - }, - "node_modules/ethereum-cryptography/node_modules/@noble/curves": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/@noble/curves/-/curves-1.1.0.tgz", - "integrity": "sha512-091oBExgENk/kGj3AZmtBDMpxQPDtxQABR2B9lb1JbVTs6ytdzZNwvhxQ4MWasRNEzlbEH8jCWFCwhF/Obj5AA==", - "dependencies": { - "@noble/hashes": "1.3.1" - }, - "funding": { - "url": "https://paulmillr.com/funding/" - } - }, - "node_modules/ethereum-cryptography/node_modules/@noble/hashes": { - "version": "1.3.1", - "resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-1.3.1.tgz", - "integrity": "sha512-EbqwksQwz9xDRGfDST86whPBgM65E0OH/pCgqW0GBVzO22bNE+NuIbeTb714+IfSjU3aRk47EUvXIb5bTsenKA==", - "engines": { - "node": ">= 16" - }, - "funding": { - "url": "https://paulmillr.com/funding/" - } - }, "node_modules/event-iterator": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/event-iterator/-/event-iterator-2.0.0.tgz", @@ -1638,14 +1485,6 @@ "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", "integrity": "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A==" }, - "node_modules/for-each": { - "version": "0.3.3", - "resolved": "https://registry.npmjs.org/for-each/-/for-each-0.3.3.tgz", - "integrity": "sha512-jqYfLp7mo9vIyQf8ykW2v7A+2N4QjeCeI5+Dz9XraiO1ign81wjiH7Fb9vSOWvQfNtmSa4H2RoQTrrXivdUZmw==", - "dependencies": { - "is-callable": "^1.1.3" - } - }, "node_modules/forwarded": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz", @@ -1699,17 +1538,6 @@ "resolved": "https://registry.npmjs.org/get-iterator/-/get-iterator-2.0.1.tgz", "integrity": "sha512-7HuY/hebu4gryTDT7O/XY/fvY9wRByEGdK6QOa4of8npTcv0+NS6frFKABcf6S9EBAsveTuKTsZQQBFMMNILIg==" }, - "node_modules/gopd": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/gopd/-/gopd-1.0.1.tgz", - "integrity": "sha512-d65bNlIadxvpb/A2abVdlqKqV563juRnZ1Wtk6s1sIR8uNsXR70xqIzVqxVf1eTqDunwT2MkczEeaezCKTZhwA==", - "dependencies": { - "get-intrinsic": "^1.1.3" - }, - "funding": { - "url": "https://github.com/sponsors/ljharb" - } - }, "node_modules/has-proto": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/has-proto/-/has-proto-1.0.1.tgz", @@ -1732,20 +1560,6 @@ "url": "https://github.com/sponsors/ljharb" } }, - "node_modules/has-tostringtag": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/has-tostringtag/-/has-tostringtag-1.0.0.tgz", - "integrity": "sha512-kFjcSNhnlGV1kyoGk7OXKSawH5JOb/LzUc5w9B02hOTO0dfFRjbHQKvg1d6cf3HbeUmtU9VbbV3qzZ2Teh97WQ==", - "dependencies": { - "has-symbols": "^1.0.2" - }, - "engines": { - "node": ">= 0.4" - }, - "funding": { - "url": "https://github.com/sponsors/ljharb" - } - }, "node_modules/hasown": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/hasown/-/hasown-2.0.0.tgz", @@ -1861,51 +1675,11 @@ "node": ">= 10" } }, - "node_modules/is-arguments": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/is-arguments/-/is-arguments-1.1.1.tgz", - "integrity": "sha512-8Q7EARjzEnKpt/PCD7e1cgUS0a6X8u5tdSiMqXhojOdoV9TsMsiO+9VLC5vAmO8N7/GmXn7yjR8qnA6bVAEzfA==", - "dependencies": { - "call-bind": "^1.0.2", - "has-tostringtag": "^1.0.0" - }, - "engines": { - "node": ">= 0.4" - }, - "funding": { - "url": "https://github.com/sponsors/ljharb" - } - }, - "node_modules/is-callable": { - "version": "1.2.7", - "resolved": "https://registry.npmjs.org/is-callable/-/is-callable-1.2.7.tgz", - "integrity": "sha512-1BC0BVFhS/p0qtw6enp8e+8OD0UrK0oFLztSjNzhcKA3WDuJxxAPXzPuPtKkjEY9UUoEWlX/8fgKeu2S8i9JTA==", - "engines": { - "node": ">= 0.4" - }, - "funding": { - "url": "https://github.com/sponsors/ljharb" - } - }, "node_modules/is-electron": { "version": "2.2.2", "resolved": "https://registry.npmjs.org/is-electron/-/is-electron-2.2.2.tgz", "integrity": "sha512-FO/Rhvz5tuw4MCWkpMzHFKWD2LsfHzIb7i6MdPYZ/KW7AlxawyLkqdy+jPZP1WubqEADE3O4FUENlJHDfQASRg==" }, - "node_modules/is-generator-function": { - "version": "1.0.10", - "resolved": "https://registry.npmjs.org/is-generator-function/-/is-generator-function-1.0.10.tgz", - "integrity": "sha512-jsEjy9l3yiXEQ+PsXdmBwEPcOxaXWLspKdplFUVI9vq1iZgIekeC0L167qeu86czQaxed3q/Uzuw0swL0irL8A==", - "dependencies": { - "has-tostringtag": "^1.0.0" - }, - "engines": { - "node": ">= 0.4" - }, - "funding": { - "url": "https://github.com/sponsors/ljharb" - } - }, "node_modules/is-loopback-addr": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/is-loopback-addr/-/is-loopback-addr-2.0.2.tgz", @@ -1930,33 +1704,11 @@ "node": ">=8" } }, - "node_modules/is-typed-array": { - "version": "1.1.12", - "resolved": "https://registry.npmjs.org/is-typed-array/-/is-typed-array-1.1.12.tgz", - "integrity": "sha512-Z14TF2JNG8Lss5/HMqt0//T9JeHXttXy5pH/DBU4vi98ozO2btxzq9MwYDZYnKwU8nRsz/+GVFVRDq3DkVuSPg==", - "dependencies": { - "which-typed-array": "^1.1.11" - }, - "engines": { - "node": ">= 0.4" - }, - "funding": { - "url": "https://github.com/sponsors/ljharb" - } - }, "node_modules/isarray": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", "integrity": "sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ==" }, - "node_modules/isomorphic-ws": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/isomorphic-ws/-/isomorphic-ws-5.0.0.tgz", - "integrity": "sha512-muId7Zzn9ywDsyXgTIafTry2sV3nySZeUDe6YedVd1Hvuuep5AsIlqK+XefWpYTyJG5e503F2xIuT2lcU6rCSw==", - "peerDependencies": { - "ws": "*" - } - }, "node_modules/it-all": { "version": "3.0.4", "resolved": "https://registry.npmjs.org/it-all/-/it-all-3.0.4.tgz", @@ -2369,25 +2121,6 @@ "node": ">= 0.4.0" } }, - "node_modules/node-fetch": { - "version": "2.7.0", - "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.7.0.tgz", - "integrity": "sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==", - "dependencies": { - "whatwg-url": "^5.0.0" - }, - "engines": { - "node": "4.x || >=6.0.0" - }, - "peerDependencies": { - "encoding": "^0.1.0" - }, - "peerDependenciesMeta": { - "encoding": { - "optional": true - } - } - }, "node_modules/node-forge": { "version": "1.3.1", "resolved": "https://registry.npmjs.org/node-forge/-/node-forge-1.3.1.tgz", @@ -2750,11 +2483,6 @@ "node": ">= 0.8.0" } }, - "node_modules/setimmediate": { - "version": "1.0.5", - "resolved": "https://registry.npmjs.org/setimmediate/-/setimmediate-1.0.5.tgz", - "integrity": "sha512-MATJdZp8sLqDl/68LfQmbP8zKPLQNV6BIZoIgrscFDQ+RsvK/BxeDQOgyxKKoh0y/8h3BqVFnCqQ/gd+reiIXA==" - }, "node_modules/setprototypeof": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.2.0.tgz", @@ -2821,11 +2549,6 @@ "node": ">=0.6" } }, - "node_modules/tr46": { - "version": "0.0.3", - "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", - "integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==" - }, "node_modules/tslib": { "version": "2.6.2", "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.6.2.tgz", @@ -2843,19 +2566,6 @@ "node": ">= 0.6" } }, - "node_modules/typescript": { - "version": "5.3.2", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.3.2.tgz", - "integrity": "sha512-6l+RyNy7oAHDfxC4FzSJcz9vnjTKxrLpDG5M2Vu4SHRVNg6xzqZp6LYSR9zjqQTu8DU/f5xwxUdADOkbrIX2gQ==", - "peer": true, - "bin": { - "tsc": "bin/tsc", - "tsserver": "bin/tsserver" - }, - "engines": { - "node": ">=14.17" - } - }, "node_modules/uint8-varint": { "version": "2.0.4", "resolved": "https://registry.npmjs.org/uint8-varint/-/uint8-varint-2.0.4.tgz", @@ -2919,18 +2629,6 @@ "node": ">= 0.8" } }, - "node_modules/util": { - "version": "0.12.5", - "resolved": "https://registry.npmjs.org/util/-/util-0.12.5.tgz", - "integrity": "sha512-kZf/K6hEIrWHI6XqOFUiiMa+79wE/D8Q+NCNAWclkyg3b4d2k7s0QGepNjiABc+aR3N1PAyHL7p6UcLY6LmrnA==", - "dependencies": { - "inherits": "^2.0.3", - "is-arguments": "^1.0.4", - "is-generator-function": "^1.0.7", - "is-typed-array": "^1.1.3", - "which-typed-array": "^1.1.2" - } - }, "node_modules/util-deprecate": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", @@ -2965,331 +2663,6 @@ "node": ">= 0.8" } }, - "node_modules/web3": { - "version": "4.3.0", - "resolved": "https://registry.npmjs.org/web3/-/web3-4.3.0.tgz", - "integrity": "sha512-YiLCsb5wmgJlSxRLzt7Z7H+CmlVVIKD8VaUQaZ+xKVG3Q7CpsO5Z6jmeKnlr6M9c6fDDsDnRM6G8g+nchZehbA==", - "dependencies": { - "web3-core": "^4.3.2", - "web3-errors": "^1.1.4", - "web3-eth": "^4.3.1", - "web3-eth-abi": "^4.1.4", - "web3-eth-accounts": "^4.1.0", - "web3-eth-contract": "^4.1.4", - "web3-eth-ens": "^4.0.8", - "web3-eth-iban": "^4.0.7", - "web3-eth-personal": "^4.0.8", - "web3-net": "^4.0.7", - "web3-providers-http": "^4.1.0", - "web3-providers-ws": "^4.0.7", - "web3-rpc-methods": "^1.1.4", - "web3-types": "^1.3.1", - "web3-utils": "^4.1.0", - "web3-validator": "^2.0.3" - }, - "engines": { - "node": ">=14.0.0", - "npm": ">=6.12.0" - } - }, - "node_modules/web3-core": { - "version": "4.3.2", - "resolved": "https://registry.npmjs.org/web3-core/-/web3-core-4.3.2.tgz", - "integrity": "sha512-uIMVd/j4BgOnwfpY8ZT+QKubOyM4xohEhFZXz9xB8wimXWMMlYVlIK/TbfHqFolS9uOerdSGhsMbcK9lETae8g==", - "dependencies": { - "web3-errors": "^1.1.4", - "web3-eth-accounts": "^4.1.0", - "web3-eth-iban": "^4.0.7", - "web3-providers-http": "^4.1.0", - "web3-providers-ws": "^4.0.7", - "web3-types": "^1.3.1", - "web3-utils": "^4.1.0", - "web3-validator": "^2.0.3" - }, - "engines": { - "node": ">=14", - "npm": ">=6.12.0" - }, - "optionalDependencies": { - "web3-providers-ipc": "^4.0.7" - } - }, - "node_modules/web3-errors": { - "version": "1.1.4", - "resolved": "https://registry.npmjs.org/web3-errors/-/web3-errors-1.1.4.tgz", - "integrity": "sha512-WahtszSqILez+83AxGecVroyZsMuuRT+KmQp4Si5P4Rnqbczno1k748PCrZTS1J4UCPmXMG2/Vt+0Bz2zwXkwQ==", - "dependencies": { - "web3-types": "^1.3.1" - }, - "engines": { - "node": ">=14", - "npm": ">=6.12.0" - } - }, - "node_modules/web3-eth": { - "version": "4.3.1", - "resolved": "https://registry.npmjs.org/web3-eth/-/web3-eth-4.3.1.tgz", - "integrity": "sha512-zJir3GOXooHQT85JB8SrufE+Voo5TtXdjhf1D8IGXmxM8MrhI8AT+Pgt4siBTupJcu5hF17iGmTP/Nj2XnaibQ==", - "dependencies": { - "setimmediate": "^1.0.5", - "web3-core": "^4.3.0", - "web3-errors": "^1.1.3", - "web3-eth-abi": "^4.1.4", - "web3-eth-accounts": "^4.1.0", - "web3-net": "^4.0.7", - "web3-providers-ws": "^4.0.7", - "web3-rpc-methods": "^1.1.3", - "web3-types": "^1.3.0", - "web3-utils": "^4.0.7", - "web3-validator": "^2.0.3" - }, - "engines": { - "node": ">=14", - "npm": ">=6.12.0" - } - }, - "node_modules/web3-eth-abi": { - "version": "4.1.4", - "resolved": "https://registry.npmjs.org/web3-eth-abi/-/web3-eth-abi-4.1.4.tgz", - "integrity": "sha512-YLOBVVxxxLYKXjaiwZjEWYEnkMmmrm0nswZsvzSsINy/UgbWbzfoiZU+zn4YNWIEhORhx1p37iS3u/dP6VyC2w==", - "dependencies": { - "abitype": "0.7.1", - "web3-errors": "^1.1.3", - "web3-types": "^1.3.0", - "web3-utils": "^4.0.7", - "web3-validator": "^2.0.3" - }, - "engines": { - "node": ">=14", - "npm": ">=6.12.0" - } - }, - "node_modules/web3-eth-accounts": { - "version": "4.1.0", - "resolved": "https://registry.npmjs.org/web3-eth-accounts/-/web3-eth-accounts-4.1.0.tgz", - "integrity": "sha512-UFtAsOANsvihTQ6SSvOKguupmQkResyR9M9JNuOxYpKh7+3W+sTnbLXw2UbOSYIsKlc1mpqqW9bVr1SjqHDpUQ==", - "dependencies": { - "@ethereumjs/rlp": "^4.0.1", - "crc-32": "^1.2.2", - "ethereum-cryptography": "^2.0.0", - "web3-errors": "^1.1.3", - "web3-types": "^1.3.0", - "web3-utils": "^4.0.7", - "web3-validator": "^2.0.3" - }, - "engines": { - "node": ">=14", - "npm": ">=6.12.0" - } - }, - "node_modules/web3-eth-contract": { - "version": "4.1.4", - "resolved": "https://registry.npmjs.org/web3-eth-contract/-/web3-eth-contract-4.1.4.tgz", - "integrity": "sha512-tJ4z6QLgtu8EQu2sXnLA7g427oxmngnbAUh+9kJKbP6Yep/oe+z79PqJv7H3MwqwUNW9T+/FeB2PnSQSyxz6ig==", - "dependencies": { - "web3-core": "^4.3.2", - "web3-errors": "^1.1.4", - "web3-eth": "^4.3.1", - "web3-eth-abi": "^4.1.4", - "web3-types": "^1.3.1", - "web3-utils": "^4.1.0", - "web3-validator": "^2.0.3" - }, - "engines": { - "node": ">=14", - "npm": ">=6.12.0" - } - }, - "node_modules/web3-eth-ens": { - "version": "4.0.8", - "resolved": "https://registry.npmjs.org/web3-eth-ens/-/web3-eth-ens-4.0.8.tgz", - "integrity": "sha512-nj0JfeD45BbzVJcVYpUJnSo8iwDcY9CQ7CZhhIVVOFjvpMAPw0zEwjTvZEIQyCW61OoDG9xcBzwxe2tZoYhMRw==", - "dependencies": { - "@adraffy/ens-normalize": "^1.8.8", - "web3-core": "^4.3.0", - "web3-errors": "^1.1.3", - "web3-eth": "^4.3.1", - "web3-eth-contract": "^4.1.2", - "web3-net": "^4.0.7", - "web3-types": "^1.3.0", - "web3-utils": "^4.0.7", - "web3-validator": "^2.0.3" - }, - "engines": { - "node": ">=14", - "npm": ">=6.12.0" - } - }, - "node_modules/web3-eth-iban": { - "version": "4.0.7", - "resolved": "https://registry.npmjs.org/web3-eth-iban/-/web3-eth-iban-4.0.7.tgz", - "integrity": "sha512-8weKLa9KuKRzibC87vNLdkinpUE30gn0IGY027F8doeJdcPUfsa4IlBgNC4k4HLBembBB2CTU0Kr/HAOqMeYVQ==", - "dependencies": { - "web3-errors": "^1.1.3", - "web3-types": "^1.3.0", - "web3-utils": "^4.0.7", - "web3-validator": "^2.0.3" - }, - "engines": { - "node": ">=14", - "npm": ">=6.12.0" - } - }, - "node_modules/web3-eth-personal": { - "version": "4.0.8", - "resolved": "https://registry.npmjs.org/web3-eth-personal/-/web3-eth-personal-4.0.8.tgz", - "integrity": "sha512-sXeyLKJ7ddQdMxz1BZkAwImjqh7OmKxhXoBNF3isDmD4QDpMIwv/t237S3q4Z0sZQamPa/pHebJRWVuvP8jZdw==", - "dependencies": { - "web3-core": "^4.3.0", - "web3-eth": "^4.3.1", - "web3-rpc-methods": "^1.1.3", - "web3-types": "^1.3.0", - "web3-utils": "^4.0.7", - "web3-validator": "^2.0.3" - }, - "engines": { - "node": ">=14", - "npm": ">=6.12.0" - } - }, - "node_modules/web3-net": { - "version": "4.0.7", - "resolved": "https://registry.npmjs.org/web3-net/-/web3-net-4.0.7.tgz", - "integrity": "sha512-SzEaXFrBjY25iQGk5myaOfO9ZyfTwQEa4l4Ps4HDNVMibgZji3WPzpjq8zomVHMwi8bRp6VV7YS71eEsX7zLow==", - "dependencies": { - "web3-core": "^4.3.0", - "web3-rpc-methods": "^1.1.3", - "web3-types": "^1.3.0", - "web3-utils": "^4.0.7" - }, - "engines": { - "node": ">=14", - "npm": ">=6.12.0" - } - }, - "node_modules/web3-providers-http": { - "version": "4.1.0", - "resolved": "https://registry.npmjs.org/web3-providers-http/-/web3-providers-http-4.1.0.tgz", - "integrity": "sha512-6qRUGAhJfVQM41E5t+re5IHYmb5hSaLc02BE2MaRQsz2xKA6RjmHpOA5h/+ojJxEpI9NI2CrfDKOAgtJfoUJQg==", - "dependencies": { - "cross-fetch": "^4.0.0", - "web3-errors": "^1.1.3", - "web3-types": "^1.3.0", - "web3-utils": "^4.0.7" - }, - "engines": { - "node": ">=14", - "npm": ">=6.12.0" - } - }, - "node_modules/web3-providers-ipc": { - "version": "4.0.7", - "resolved": "https://registry.npmjs.org/web3-providers-ipc/-/web3-providers-ipc-4.0.7.tgz", - "integrity": "sha512-YbNqY4zUvIaK2MHr1lQFE53/8t/ejHtJchrWn9zVbFMGXlTsOAbNoIoZWROrg1v+hCBvT2c9z8xt7e/+uz5p1g==", - "optional": true, - "dependencies": { - "web3-errors": "^1.1.3", - "web3-types": "^1.3.0", - "web3-utils": "^4.0.7" - }, - "engines": { - "node": ">=14", - "npm": ">=6.12.0" - } - }, - "node_modules/web3-providers-ws": { - "version": "4.0.7", - "resolved": "https://registry.npmjs.org/web3-providers-ws/-/web3-providers-ws-4.0.7.tgz", - "integrity": "sha512-n4Dal9/rQWjS7d6LjyEPM2R458V8blRm0eLJupDEJOOIBhGYlxw5/4FthZZ/cqB7y/sLVi7K09DdYx2MeRtU5w==", - "dependencies": { - "@types/ws": "8.5.3", - "isomorphic-ws": "^5.0.0", - "web3-errors": "^1.1.3", - "web3-types": "^1.3.0", - "web3-utils": "^4.0.7", - "ws": "^8.8.1" - }, - "engines": { - "node": ">=14", - "npm": ">=6.12.0" - } - }, - "node_modules/web3-providers-ws/node_modules/@types/ws": { - "version": "8.5.3", - "resolved": "https://registry.npmjs.org/@types/ws/-/ws-8.5.3.tgz", - "integrity": "sha512-6YOoWjruKj1uLf3INHH7D3qTXwFfEsg1kf3c0uDdSBJwfa/llkwIjrAGV7j7mVgGNbzTQ3HiHKKDXl6bJPD97w==", - "dependencies": { - "@types/node": "*" - } - }, - "node_modules/web3-rpc-methods": { - "version": "1.1.4", - "resolved": "https://registry.npmjs.org/web3-rpc-methods/-/web3-rpc-methods-1.1.4.tgz", - "integrity": "sha512-LTFNg4LFaeU8K9ecuT8fHDp/LOXyxCneeZjCrRYIW1u82Ly52SrY55FIzMIISGoG/iT5Wh7UiHOB3CQsWLBmbQ==", - "dependencies": { - "web3-core": "^4.3.2", - "web3-types": "^1.3.1", - "web3-validator": "^2.0.3" - }, - "engines": { - "node": ">=14", - "npm": ">=6.12.0" - } - }, - "node_modules/web3-types": { - "version": "1.3.1", - "resolved": "https://registry.npmjs.org/web3-types/-/web3-types-1.3.1.tgz", - "integrity": "sha512-8fXi7h/t95VKRtgU4sxprLPZpsTh3jYDfSghshIDBgUD/OoGe5S+syP24SUzBZYllZ/L+hMr2gdp/0bGJa8pYQ==", - "engines": { - "node": ">=14", - "npm": ">=6.12.0" - } - }, - "node_modules/web3-utils": { - "version": "4.1.0", - "resolved": "https://registry.npmjs.org/web3-utils/-/web3-utils-4.1.0.tgz", - "integrity": "sha512-+VJWR6FtCsgwuJr5tvSvQlSEG06586df8h2CxGc9tcNtIDyJKNkSDDWJkdNPvyDhhXFzQYFh8QOGymD1CIP6fw==", - "dependencies": { - "ethereum-cryptography": "^2.0.0", - "web3-errors": "^1.1.4", - "web3-types": "^1.3.1", - "web3-validator": "^2.0.3" - }, - "engines": { - "node": ">=14", - "npm": ">=6.12.0" - } - }, - "node_modules/web3-validator": { - "version": "2.0.3", - "resolved": "https://registry.npmjs.org/web3-validator/-/web3-validator-2.0.3.tgz", - "integrity": "sha512-fJbAQh+9LSNWy+l5Ze6HABreml8fra98o5+vS073T35jUcLbRZ0IOjF/ZPJhJNbJDt+jP1vseZsc3z3uX9mxxQ==", - "dependencies": { - "ethereum-cryptography": "^2.0.0", - "util": "^0.12.5", - "web3-errors": "^1.1.3", - "web3-types": "^1.3.0", - "zod": "^3.21.4" - }, - "engines": { - "node": ">=14", - "npm": ">=6.12.0" - } - }, - "node_modules/webidl-conversions": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", - "integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==" - }, - "node_modules/whatwg-url": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz", - "integrity": "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==", - "dependencies": { - "tr46": "~0.0.3", - "webidl-conversions": "^3.0.0" - } - }, "node_modules/wherearewe": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/wherearewe/-/wherearewe-2.0.1.tgz", @@ -3302,24 +2675,6 @@ "npm": ">=7.0.0" } }, - "node_modules/which-typed-array": { - "version": "1.1.11", - "resolved": "https://registry.npmjs.org/which-typed-array/-/which-typed-array-1.1.11.tgz", - "integrity": "sha512-qe9UWWpkeG5yzZ0tNYxDmd7vo58HDBc39mZ0xWWpolAGADdFOzkfamWLDxkOWcvHQKVmdTyQdLD4NOfjLWTKew==", - "dependencies": { - "available-typed-arrays": "^1.0.5", - "call-bind": "^1.0.2", - "for-each": "^0.3.3", - "gopd": "^1.0.1", - "has-tostringtag": "^1.0.0" - }, - "engines": { - "node": ">= 0.4" - }, - "funding": { - "url": "https://github.com/sponsors/ljharb" - } - }, "node_modules/wrappy": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", diff --git a/gateway/package.json b/gateway/package.json index d2488bf9..557e2028 100644 --- a/gateway/package.json +++ b/gateway/package.json @@ -1,29 +1,19 @@ { - "name": "@fluencelabs/aqua-eth-gateway", - "version": "0.0.18", - "description": "", + "private": true, + "author": "Fluence Labs", + "license": "Apache-2.0", + "name": "gateway", + "description": "HTTP gateway for fRPC", "main": "src/index.js", "type": "module", "scripts": { "run": "npm run compile && node src/index.js", - "compile": "fluence aqua -i aqua/ -o aqua-compiled/ --js --no-input", - "req": "node web3run.js" - }, - "bin": { - "aqua-eth-gateway": "src/index.js" + "compile": "fluence aqua -i aqua/ -o aqua-compiled/ --js --no-input" }, - "files": [ - "src/*", - "aqua/*", - "aqua-compiled/*" - ], - "author": "Fluence Labs", - "license": "Apache-2.0", "dependencies": { "@fluencelabs/js-client": "0.8.4", "body-parser": "1.20.2", "express": "4.18.2", - "json-rpc-2.0": "1.7.0", - "web3": "4.3.0" + "json-rpc-2.0": "1.7.0" } } \ No newline at end of file diff --git a/gateway/src/index.js b/gateway/src/index.js index e25bae77..3431afc5 100644 --- a/gateway/src/index.js +++ b/gateway/src/index.js @@ -5,109 +5,74 @@ import express from "express"; import bodyParser from "body-parser"; import { JSONRPCServer } from "json-rpc-2.0"; + import { Fluence } from "@fluencelabs/js-client"; + +import { readArguments } from "./arguments.js"; +import { readConfig } from "./config.js"; +import { methods } from "./methods.js"; +import { resultsQuorum } from "./utils.js"; + import { - quorumEth, - randomLoadBalancingEth, - roundRobinEth, + quorumEth, + randomLoadBalancingEth, + roundRobinEth, } from "../aqua-compiled/rpc.js"; import { registerLoggerSrv } from "../aqua-compiled/logger.js"; import { registerCounterSrv } from "../aqua-compiled/counter.js"; import { registerQuorumCheckerSrv } from "../aqua-compiled/quorum.js"; -import { readArguments } from "./arguments.js"; -import { readConfig } from "./config.js"; -import { methods } from "./methods.js"; +// read arguments 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); } +// read config const { config, errors, help } = readConfig(args.configPath); if (errors.length > 0) { - errors.forEach((err) => console.log(err)); - console.log(help); - process.exit(1); + errors.forEach((err) => console.log(err)); + console.log(help); + process.exit(1); } console.log("Running server..."); -const route = "/"; - -const server = new JSONRPCServer(); - // initialize fluence client await Fluence.connect(config.relay, {}); -const peerId = (await Fluence.getClient()).getPeerId(); +const client = Fluence.getClient(); +const peerId = client.getPeerId(); -// handler for logger +// Register logger service registerLoggerSrv({ - log: (s) => { - console.log("log: " + s); - }, - logCall: (s) => { - console.log("Call will be to : " + s); - }, - logWorker: (s) => { - console.log("Worker used: " + JSON.stringify(s)); - }, - logNum: (s) => { - console.log("Number: " + s); - }, + logCall: (s) => { + console.log("Call will be to : " + s); + }, + logWorker: (s) => { + console.log("Worker used: " + JSON.stringify(s)); + } }); +// Register counter service let counter = 0; registerCounterSrv("counter", { - incrementAndReturn: () => { - counter++; - console.log("Counter: " + counter); - return counter; - }, + incrementAndReturn: () => { + counter++; + console.log("Increment counter to:", counter); + return counter; + }, }); -function findSameResults(results, minNum) { - const resultCounts = results - .filter((obj) => obj.success) - .map((obj) => obj.value) - .reduce(function (i, v) { - 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: "", - }; - } -} - +// Register quorum checker service registerQuorumCheckerSrv("quorum", { - check: (ethResults, minQuorum) => { - console.log("Check quorum for:"); - console.log(ethResults); - return findSameResults(ethResults, minQuorum); - }, + check: (ethResults, minQuorum) => { + console.log("Check quorum for:", ethResults); + return resultsQuorum(ethResults, minQuorum); + }, }); const counterServiceId = config.counterServiceId || "counter"; @@ -120,77 +85,66 @@ const mode = config.mode || "random"; console.log(`Using mode '${mode}'`); async function methodHandler(reqRaw, method) { - const req = reqRaw.map((s) => JSON.stringify(s)); - console.log(`Receiving request '${method}'`); - let result; - if (mode === "random") { - result = await randomLoadBalancingEth(config.providers, method, req); - } else if (mode === "round-robin") { - result = await roundRobinEth( - config.providers, - method, - req, - counterServiceId, - counterPeerId, - config.serviceId, - ); - } else if (mode === "quorum") { - const quorumResult = await quorumEth( - config.providers, - quorumNumber, - 10000, - method, - req, - quorumServiceId, - quorumPeerId, - { ttl: 20000 }, - ); - - if (quorumResult.error) { - console.error( - `quorum failed: ${quorumResult.error}\n${JSON.stringify( - quorumResult.results, - )}`, - ); - result = { success: false, error: quorumResult.error }; - } else { - result = { - success: true, - error: quorumResult.error, - value: quorumResult.value, - }; + const req = reqRaw.map((s) => JSON.stringify(s)); + console.log(`Handling request '${method}'`); + let result; + if (mode === "random") { + result = await randomLoadBalancingEth( + config.providers, + method, + req, + { ttl: 20000 } + ); + } else if (mode === "round-robin") { + result = await roundRobinEth( + config.providers, + method, + req, + counterServiceId, + counterPeerId, + config.serviceId, + { ttl: 20000 }, + ); + } else if (mode === "quorum") { + result = await quorumEth( + config.providers, + quorumNumber, + 10000, + method, + req, + quorumServiceId, + quorumPeerId, + { ttl: 20000 }, + ); } - } - if (!result.success) { - throw new Error(result.error); - } + if (!result.success) { + throw new Error(result.error); + } - return JSON.parse(result.value || "{}"); + return JSON.parse(result.value || "{}"); } -function addMethod(op) { - server.addMethod(op, async (req) => methodHandler(req, op)); -} +const server = new JSONRPCServer(); -// register all eth methods +// Register all eth methods methods.forEach((m) => { - addMethod(m); + server.addMethod(m, async (req) => methodHandler(req, 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); - } - }); +// Register JSON-RPC handler +app.post("/", (req, res) => { + const jsonRPCRequest = req.body; + server.receive(jsonRPCRequest).then((jsonRPCResponse) => { + if (jsonRPCResponse) { + res.json(jsonRPCResponse); + } else { + res.sendStatus(204); + } + }); }); app.listen(config.port); diff --git a/gateway/src/methods.js b/gateway/src/methods.js index 9a531c69..52a55aa0 100644 --- a/gateway/src/methods.js +++ b/gateway/src/methods.js @@ -1,4 +1,5 @@ -export const methods = ['eth_accounts', +export const methods = [ + 'eth_accounts', 'eth_blockNumber', 'eth_call', 'eth_chainId', @@ -55,4 +56,5 @@ export const methods = ['eth_accounts', 'shh_newFilter', 'shh_uninstallFilter', 'shh_getFilterChanges', - 'shh_getMessages'] \ No newline at end of file + 'shh_getMessages' +] \ No newline at end of file diff --git a/gateway/src/utils.js b/gateway/src/utils.js new file mode 100644 index 00000000..712c8268 --- /dev/null +++ b/gateway/src/utils.js @@ -0,0 +1,33 @@ +/** + * Find the most repeated value in the results array + */ +export function resultsQuorum(results, minNum) { + const resultCounts = results + .filter((obj) => obj.success) + .map((obj) => obj.value) + .reduce(function (i, v) { + if (i[v] === undefined) { + i[v] = 1; + } else { + i[v] = i[v] + 1; + } + return i; + }, {}); + + const maxRepeated = Math.max(...Object.values(resultCounts)); + if (maxRepeated >= minNum) { + const [val, _] = Object.entries(resultCounts).find( + ([_, count]) => count === maxRepeated, + ); + + return { + value: val, + success: true, + error: "" + }; + } else return { + value: "", + success: false, + error: "No consensus in results" + }; +} \ No newline at end of file diff --git a/gateway/web3run.js b/gateway/web3run.js deleted file mode 100644 index f7c9d51e..00000000 --- a/gateway/web3run.js +++ /dev/null @@ -1,23 +0,0 @@ -import Web3 from 'web3'; - -const web3 = new Web3("http://localhost:3000"); - -async function main() { - try { - console.log("Trying to fetch the block number..."); - const bn = await web3.eth.getBlockNumber(); - console.log("Block number is: ", bn); - } catch (e) { - console.error("Error requesting block number\n", e); - } - - try { - console.log("Trying to fetch the transaction information..."); - const resp = await web3.eth.getTransaction("0x7bfa7c9812c67af61872c66f3af13bb65ad0f81b7a44bcf4a11c11900be16409"); - console.log("Transaction is", resp); - } catch (e) { - console.log("Error requesting transaction info!\n", e) - } -} - -main(); diff --git a/package.json b/package.json index 0bc9b6ce..64cce799 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,9 @@ { + "private": true, "author": "Fluence Labs", "license": "Apache-2.0", "type": "module", + "description": "Fluence app for quering the blockchain", "devDependencies": { "@tsconfig/node18-strictest-esm": "^1.0.1", "@types/jest": "^29.5.5",