Skip to content

Commit 76861a5

Browse files
committed
Reputation query pass-through
1 parent 1a953e4 commit 76861a5

File tree

4 files changed

+70
-5
lines changed

4 files changed

+70
-5
lines changed

packages/reputation-miner/bin/forked.js

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const hre = require("hardhat");
22
const path = require("path");
3+
const express = require("express");
4+
const axios = require("axios")
35

46
const { argv } = require("yargs")
57
.option('privateKey', {string:true})
@@ -44,13 +46,62 @@ const client = new ReputationMinerClient({
4446
oracle,
4547
exitOnError,
4648
adapter: adapterObject,
47-
oraclePort,
49+
oraclePort: 3001,
4850
processingDelay
4951
});
5052

5153
async function main() {
5254
await client.initialise(colonyNetworkAddress, syncFrom);
5355
client._miner.realWallet = await ethers.getImpersonatedSigner(minerAddress);
56+
57+
if (oracle) {
58+
// Start a forked oracle. This will query our local node, and if that fails, query upstream.
59+
60+
this._app = express();
61+
62+
this._app.use(function(req, res, next) {
63+
res.header("Access-Control-Allow-Origin", "*");
64+
next();
65+
});
66+
67+
this._app.get("/favicon.ico", (req, res) => {
68+
res.status(204).end();
69+
});
70+
71+
this._app.get("/", (req, res) => {
72+
res.status(204).end();
73+
});
74+
75+
76+
this._app.get("*", async (req, res) => {
77+
78+
try {
79+
const { data } = await axios.get(`http://localhost:${3001}/${req.originalUrl}`);
80+
res.send(data);
81+
} catch (e) {
82+
console.log('Local reputation request failed, try upstream URL:');
83+
// If the local oracle fails, query the upstream oracle.
84+
console.log(`${process.env.REPUTATION_URL}/${req.originalUrl}`)
85+
try {
86+
87+
const { data } = await axios({
88+
url: `${process.env.REPUTATION_URL}/${req.originalUrl}`,
89+
responseType: "stream",
90+
});
91+
92+
res.send(data);
93+
} catch (e2) {
94+
console.log('Upstream reputation request failed');
95+
res.status(500).send(e2);
96+
}
97+
}
98+
});
99+
100+
101+
this._app.listen(oraclePort || 3000, () => {
102+
console.log(`Forked (pass-through) oracle listening on port ${oraclePort || 3000}`);
103+
});
104+
}
54105
}
55106

56107
main();

packages/reputation-miner/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"license": "ISC",
1616
"dependencies": {
1717
"apicache": "^1.6.3",
18+
"axios": "^1.7.3",
1819
"better-sqlite3": "^9.5.0",
1920
"bn.js": "^5.2.1",
2021
"ethers": "^5.6.9",

pnpm-lock.yaml

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/deployToForkedChain.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ const axios = require("axios");
99

1010
const provider = new ethers.providers.StaticJsonRpcProvider("http://127.0.0.1:8545");
1111

12-
const { FORKED_NETWORK_ADDRESS, LATEST_STATE_URL } = process.env;
12+
const { FORKED_NETWORK_ADDRESS, REPUTATION_URL } = process.env;
1313

1414
if (!FORKED_NETWORK_ADDRESS) {
1515
throw new Error("FORKED_NETWORK_ADDRESS must be set");
1616
}
1717

18-
if (!LATEST_STATE_URL) {
19-
throw new Error("LATEST_STATE_URL must be set");
18+
if (!REPUTATION_URL) {
19+
throw new Error("REPUTATION_URL must be set");
2020
}
2121

2222
let signer;
@@ -125,7 +125,7 @@ async function main() {
125125

126126
const response = await axios({
127127
method: "GET",
128-
url: LATEST_STATE_URL,
128+
url: `${REPUTATION_URL}/latestState`,
129129
responseType: "stream",
130130
});
131131

0 commit comments

Comments
 (0)