Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support forked deployments #1283

Draft
wants to merge 6 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions packages/reputation-miner/bin/forked.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
const hre = require("hardhat");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason you don't use globals here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't being run from inside hardhat, so I don't think it exists?

const path = require("path");
const express = require("express");
const axios = require("axios")

const { argv } = require("yargs")
.option('privateKey', {string:true})
.option('colonyNetworkAddress', {string:true})
.option('minerAddress', {string:true})
.option('providerAddress', {type: "array", default: []});
// const ethers = require("ethers");

const {ethers} = hre;

const ReputationMinerClient = require("../ReputationMinerClient");
const { ConsoleAdapter, TruffleLoader } = require("../../package-utils");

const {
minerAddress,
privateKey,
colonyNetworkAddress,
dbPath,
syncFrom,
auto,
oracle,
exitOnError,
oraclePort,
processingDelay,
} = argv;

const loader = new TruffleLoader({
contractRoot: path.resolve(__dirname, "..", "..", "..", "artifacts", "contracts")
});

const provider = new ethers.providers.StaticJsonRpcProvider("http://localhost:8545");
const adapterObject = new ConsoleAdapter();

const client = new ReputationMinerClient({
loader,
minerAddress,
privateKey,
provider,
useJsTree: true,
dbPath,
auto,
oracle,
exitOnError,
adapter: adapterObject,
oraclePort: 3001,
processingDelay
});

async function main() {
await client.initialise(colonyNetworkAddress, syncFrom);
client._miner.realWallet = await ethers.getImpersonatedSigner(minerAddress);

if (oracle) {
// Start a forked oracle. This will query our local node, and if that fails, query upstream.

this._app = express();

this._app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
next();
});

this._app.get("/favicon.ico", (req, res) => {
res.status(204).end();
});

this._app.get("/", (req, res) => {
res.status(204).end();
});


this._app.get("*", async (req, res) => {

try {
const { data } = await axios.get(`http://localhost:${3001}/${req.originalUrl}`);
res.send(data);
} catch (e) {
console.log('Local reputation request failed, trying upstream URL:');
// If the local oracle fails, query the upstream oracle.
console.log(`${process.env.REPUTATION_URL}/${req.originalUrl}`)
try {

const { data } = await axios({
url: `${process.env.REPUTATION_URL}/${req.originalUrl}`,
});

res.send(data);
} catch (e2) {
console.log('Upstream reputation request failed, forwarding result');
res.status(e2.response.status).send(await e2.response.data);
}
}
});


this._app.listen(oraclePort || 3000, () => {
console.log(`Forked (pass-through) oracle listening on port ${oraclePort || 3000}`);
});
}
}

main();
2 changes: 1 addition & 1 deletion packages/reputation-miner/bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const { argv } = require("yargs")
const ethers = require("ethers");

const ReputationMinerClient = require("../ReputationMinerClient");
const {RetryProvider} = require("../../package-utils");
const { RetryProvider } = require("../../package-utils");

const { ConsoleAdapter, SlackAdapter, DiscordAdapter, TruffleLoader } = require("../../package-utils");

Expand Down
2 changes: 2 additions & 0 deletions packages/reputation-miner/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
"license": "ISC",
"dependencies": {
"apicache": "^1.6.3",
"axios": "^1.7.3",
"better-sqlite3": "^9.5.0",
"bn.js": "^5.2.1",
"ethers": "^5.6.9",
"exponential-backoff": "^3.1.0",
"express": "^4.19.2",
"ganache": "^7.9.2",
"hardhat": "^2.22.7",
"web3-utils": "^1.7.5",
"yargs": "^17.5.1"
},
Expand Down
Loading