forked from flashbots/mev-relay-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
208 lines (182 loc) · 5.53 KB
/
main.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// A simple server that proxies only specific methods to an Ethereum JSON-RPC
const express = require('express')
const bodyParser = require('body-parser')
const morgan = require('morgan')
const rateLimit = require('express-rate-limit')
const _ = require('lodash')
const Sentry = require('@sentry/node')
const promBundle = require('express-prom-bundle')
const { Handler } = require('./handlers')
const { writeError } = require('./utils')
const { verifyMessage, id } = require('ethers/lib/utils')
const { constants } = require('ethers')
if (process.env.SENTRY_DSN) {
console.log('initializing sentry')
Sentry.init({
dsn: process.env.SENTRY_DSN
})
}
const ALLOWED_METHODS = ['eth_sendBundle', 'eth_callBundle', 'flashbots_getUserStats']
function help() {
console.log('node ./server/main.js minerUrls simulationRpc sqsUrl [PORT]')
}
function validPort(port) {
if (isNaN(port) || port < 0 || port > 65535) {
return false
}
return true
}
if (_.includes(process.argv, '-h') || _.includes(process.argv, '--help')) {
help()
process.exit(0)
}
let MINERS = []
if (process.argv[2]) {
MINERS = _.split(process.argv[2], ',')
}
const SIMULATION_RPC = process.argv[3]
if (!SIMULATION_RPC) {
console.error('invalid simulation rpc provided')
help()
process.exit(1)
}
const SQS_URL = process.argv[4]
if (!SIMULATION_RPC) {
console.error('invalid simulation rpc provided')
help()
process.exit(1)
}
const PORT = parseInt(_.get(process.argv, '[5]', '18545'))
if (!validPort(PORT)) {
console.error(`invalid port specified for PORT: ${PORT}`)
process.exit(1)
}
const app = express()
app.set('trust proxy', true)
app.use(
rateLimit({
windowMs: 60 * 1000, // 1 minute
max: 60, // limit each IP to this many requests
onLimitReached: (req) => {
console.log(`rate limit reached for IP: ${req.ip}`)
}
})
)
const metricsRequestMiddleware = promBundle({
includePath: true,
includeMethod: true,
autoregister: false, // Do not register /metrics
promClient: {
collectDefaultMetrics: {}
}
})
const { promClient, metricsMiddleware } = metricsRequestMiddleware
// Metrics app to expose /metrics endpoint
const metricsApp = express()
metricsApp.use(metricsMiddleware)
const UNIQUE_USER_COUNT = {}
const bundleCounterPerUser = new promClient.Counter({
name: 'relay_bundles_per_user',
help: '# of bundles received per user',
labelNames: ['username']
})
app.use(metricsRequestMiddleware)
app.use(morgan('combined'))
function rawBodySaver(req, _, buf, encoding) {
if (buf && buf.length) {
req.rawBody = buf.toString(encoding || 'utf8')
}
}
app.use(bodyParser.json({ verify: rawBodySaver }))
app.use(async (req, res, next) => {
const auth = req.header('Authorization')
let signature = req.header('X-Flashbots-Signature')
if (!signature) {
writeError(res, 403, 'missing X-Flashbots-Signature header')
return
}
if (!req.body) {
writeError(res, 400, 'invalid json body')
return
}
if (!req.body.method) {
writeError(res, 400, 'missing method')
return
}
if (!_.includes(ALLOWED_METHODS, req.body.method)) {
writeError(res, 400, `invalid method, only ${ALLOWED_METHODS} supported, you provided: ${req.body.method}`)
return
}
req.user = {}
if (signature) {
signature = _.split(signature, ':')
if (signature.length !== 2) {
writeError(res, 403, 'invalid X-Flashbots-Signature header')
return
}
const msg = id(req.rawBody)
try {
const address = verifyMessage(msg, signature[1])
if (address === constants.AddressZero) {
writeError(res, 403, `invalid signature for ${signature[0]}`)
return
}
if (address.toLowerCase() !== signature[0].toLowerCase()) {
writeError(res, 403, `signer address does not equal expected, got ${address}, expected ${signature[0]}`)
return
}
req.user.address = address
} catch (err) {
console.error(`error verifyMessage: ${err}`)
writeError(res, 403, `error in signature check ${signature[0]}`)
return
}
}
if (auth) {
writeError(res, 403, 'invalid authorization method, API keys have been deprecated, use X-Flashbots-Signature')
return
}
const username = req.user.address || req.user.keyID.slice(0, 8)
let count = UNIQUE_USER_COUNT[username]
if (!count) {
count = 0
}
UNIQUE_USER_COUNT[username] = count + 1
bundleCounterPerUser.inc({ username })
next()
})
const handler = new Handler(MINERS, SIMULATION_RPC, SQS_URL, process.env.POSTGRES_DSN, promClient)
app.use(async (req, res) => {
try {
console.log(`request body: ${JSON.stringify(req.body)}`)
if (req.body.method === 'eth_sendBundle') {
await handler.handleSendBundle(req, res)
} else if (req.body.method === 'eth_callBundle') {
await handler.handleCallBundle(req, res)
} else if (req.body.method === 'flashbots_getUserStats') {
await handler.handleUserStats(req, res)
} else {
const err = `unknown method: ${req.body.method}`
Sentry.captureException(err)
console.error(err)
writeError(res, 400, err)
}
} catch (error) {
Sentry.captureException(error)
console.error(`error in handler: ${error}`)
try {
writeError(res, 500, 'internal server error')
} catch (error2) {
Sentry.captureException(error2)
console.error(`error in error response: ${error2}`)
}
}
})
process.on('unhandledRejection', (err) => {
Sentry.captureException(err)
console.error(`unhandled rejection: ${err} ${err.stack}`)
})
app.listen(PORT, () => {
metricsApp.listen(9090)
console.log(`relay listening at ${PORT}`)
})