-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
134 lines (116 loc) · 4.06 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
const ethers = require('ethers'); // eslint-disable-line @typescript-eslint/no-var-requires
const colonyNetworkAddress = process.argv[2];
const provider = new ethers.providers.StaticJsonRpcProvider('http://network-contracts:8545');
// eslint-disable-next-line max-len
const networkAbi = require('../colonyNetwork/artifacts/contracts/colonyNetwork/IColonyNetwork.sol/IColonyNetwork.json')
.abi;
// eslint-disable-next-line max-len
const cycleAbi = require('../colonyNetwork/artifacts/contracts/reputationMiningCycle/IReputationMiningCycle.sol/IReputationMiningCycle.json')
.abi;
const colonyNetwork = new ethers.Contract(
colonyNetworkAddress,
networkAbi,
provider,
);
let lastBlockThisServiceMined = null;
let reputationMonitorActive = false;
let autominerId;
async function mine(retryTimes = 3) {
try {
await provider.send('evm_mine');
} catch (err) {
console.error('Mine failed', err);
if (retryTimes) {
console.info('Retrying...');
await mine(retryTimes - 1);
}
}
}
async function forwardTime(seconds) {
await provider.send('evm_increaseTime', [seconds]);
if (!autominerId) {
await mine();
}
}
async function automine(seconds = 0) {
if (autominerId){
autominerId = clearInterval(autominerId);
}
if (seconds === 0) {
return;
} else {
autominerId = setInterval(mine, seconds * 1000);
}
}
async function doBlockChecks(blockNumber) {
// Don't mine two blocks in a row
if (lastBlockThisServiceMined >= blockNumber) { return; }
if (!reputationMonitorActive) { return; }
// Inactive log length greater than one, mine a block
const inactiveCycleAddress = await colonyNetwork.getReputationMiningCycle(
false,
);
const inactiveMiningCycle = new ethers.Contract(
inactiveCycleAddress,
cycleAbi,
provider,
);
let logLength = await inactiveMiningCycle.getReputationUpdateLogLength();
if (logLength.gt(1)) {
await forwardTime(86401);
lastBlockThisServiceMined = blockNumber + 1;
return;
}
// If the active log length is anything other than 0, mine a block
const activeCycleAddress = await colonyNetwork.getReputationMiningCycle(true);
const activeMiningCycle = new ethers.Contract(
activeCycleAddress,
cycleAbi,
provider,
);
logLength = await activeMiningCycle.getReputationUpdateLogLength();
if (!logLength.eq(1)) {
await forwardTime(86401);
lastBlockThisServiceMined = blockNumber + 1;
return;
}
// Has the miner submitted? If so, mine a block
const nSubmitted = await activeMiningCycle.getNUniqueSubmittedHashes();
if (nSubmitted.eq(1)) {
await forwardTime(86401);
lastBlockThisServiceMined = blockNumber + 1;
}
}
provider.on('block', doBlockChecks);
// Also proxy oracle reqeusts from 127.0.0.1:3001/reputation/local to the oracle
// to accommodate differences between dev and production
const express = require('express');
const { createProxyMiddleware } = require('http-proxy-middleware');
const app = express();
app.use(function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
next();
});
app.use('/reputation/local', createProxyMiddleware({ target: "http://network-contracts:3002", changeOrigin: true, pathRewrite: {'^/reputation/local' : ''}}));
app.get('/reputation/monitor/toggle', function (req, res) {
reputationMonitorActive = !reputationMonitorActive;
res.send(`Reputation monitor auto mining is now ${reputationMonitorActive ? "on" : "off" }`)
});
app.get('/reputation/monitor/status', function (req, res) {
res.send(`{state: ${reputationMonitorActive}}`);
});
app.get('/automine/status', async function (req, res) {
res.status(200).send('Autominer ' + (autominerId ? 'is started' : `is stopped`));
});
app.get('/automine/:seconds', async function (req, res){
let seconds = req.param.seconds || 0;
try {
seconds = parseInt(req.params.seconds, 10);
} catch (err) {
return res.status(400).send("Seconds must be a parseable integer");
}
await automine(seconds);
res.status(200).send('Autominer ' + seconds === 0 ? 'stopped' : `started with ${seconds} seconds period`);
});
app.listen(3001);
automine(5);