This repository has been archived by the owner on Apr 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathserver.js
278 lines (228 loc) · 8.49 KB
/
server.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
'use strict';
const config = require('config');
const request = require('request');
const express = require('express');
const bodyParser = require('body-parser');
const morganBody = require('morgan-body');
const { celebrate, isCelebrate } = require('celebrate');
const keccak256 = require('js-sha3').keccak_256;
const Parity = require('@parity/api');
const toml = require('toml');
const validate = require('./validation');
const boom = require('./error');
const transport = new Parity.Provider.Http(`http://localhost:${config.get('rpc.port')}`);
const api = new Parity(transport);
const app = express();
// Support health checking by sending HEAD
app.head('/', (req, res) => res.status(200).end());
app.get('/health', handleAsync(async (req, res) => {
const network = getNetwork();
const health = api.parity.nodeHealth();
res.setHeader('Content-Type', 'application/json');
return {
network: await network,
health: await health
};
}));
// Middlewares
app.use(bodyParser.urlencoded({extended: true}));
morganBody(app);
// validate secret for every request
app.use((req, res, next) => {
if (keccak256(req.body.secret || '') !== secretHash) {
next(boom.unauthorized('Invalid secret'));
} else {
next();
}
});
module.exports = app;
const reduceObject = (obj, prop) => ({ ...obj, [prop]: true });
const enabledTracks = config.get('enabledTracks').reduce(reduceObject, {});
const account = {
address: config.get('account.address'),
password: config.get('account.password'),
gasPrice: config.get('account.gasPrice')
};
const httpPort = config.get('http.port');
const baseUrl = config.get('assetsBaseUrl');
const secretHash = config.get('secretHash');
const githubRepo = config.get('repository');
const operationsContract = api.util.sha3('parityoperations');
const githubHint = api.util.sha3('githubhint');
const RegistrarABI = require('./res/registrar.json');
const GitHubHintABI = require('./res/githubhint.json');
const OperationsABI = require('./res/operations.json');
const tracks = {
stable: 1,
beta: 2,
nightly: 3,
master: 3,
testing: 4
};
const validateRelease = celebrate({
params: {
tag: validate.tag,
commit: validate.commit
},
body: {
secret: validate.secret
}
});
app.post('/push-release/:tag/:commit', validateRelease, handleAsync(async function (req, res) {
const { commit, tag } = req.params;
console.log(`curl --data "secret=${req.body.secret}" http://localhost:${httpPort}/push-release/${tag}/${commit}`);
console.log(`Pushing commit: ${commit} (tag: ${tag})`);
const meta = await readParityMetadata(commit);
const track = tracks[meta.track] ? meta.track : 'testing';
console.log(`Track: ${meta.track} => ${track} (${tracks[track]}) [enabled: ${enabledTracks[track]}]`);
if (!enabledTracks[track]) {
throw boom.accepted(`Track not enabled: ${track}`);
}
const network = (await getNetwork()).toLowerCase();
const networkSettings = meta.networks[network];
let forkSupported = parseInt(networkSettings.forkBlock, 10);
if (isNaN(forkSupported)) {
console.warn(`Invalid fork data for ${network}: '${networkSettings.forkBlock}', assuming 0`);
forkSupported = 0;
}
console.log(`Fork supported: ${forkSupported}`);
let versionMatch = meta.version.match(/([0-9]+)\.([0-9]+)\.([0-9]+)/);
if (!versionMatch) {
throw new Error(`Unable to detect version in ${meta.version}`);
}
versionMatch = versionMatch.slice(1);
const [major, minor, patch] = versionMatch.map(x => parseInt(x, 10));
const semver = major * 65536 + minor * 256 + patch;
console.log(`Version: ${versionMatch.join('.')} = ${semver}`);
const registryAddress = await api.parity.registryAddress();
const registry = api.newContract(RegistrarABI, registryAddress);
console.log(`Registering release: 0x000000000000000000000000${commit}, ${forkSupported}, ${tracks[track]}, ${semver}, ${networkSettings.critical}`);
const operationsAddress = await registry.instance.getAddress.call({}, [operationsContract, 'A']);
const hash = await sendTransaction(OperationsABI, operationsAddress, 'addRelease', [`0x000000000000000000000000${commit}`, forkSupported, tracks[track], semver, networkSettings.critical]);
// Return the response
return `RELEASE: ${commit}/${track}/${meta.track}/${forkSupported};\ntxhash: ${hash}`;
}));
const validateBuild = celebrate({
params: {
tag: validate.tag,
platform: validate.platform
},
body: {
secret: validate.secret,
sha3: validate.sha3,
filename: validate.filename,
commit: validate.commit
}
});
app.post('/push-build/:tag/:platform', validateBuild, handleAsync(async function (req, res) {
const { tag, platform } = req.params;
const { commit, filename, sha3 } = req.body;
console.log(`curl --data "secret=${req.body.secret}&commit=${commit}&filename=${filename}&sha3=${sha3}" http://localhost:${httpPort}/push-build/${tag}/${platform}`);
const url = `${baseUrl}/${tag}/${platform}/${filename}`;
const out = `BUILD: ${platform}/${commit} -> ${sha3}/${tag}/${filename} [${url}]`;
console.log(out);
const meta = await readParityMetadata(commit);
const track = tracks[meta.track] ? meta.track : 'testing';
console.log(`Track: ${meta.track} => ${track} (${tracks[track]}) [enabled: ${!!enabledTracks[track]}]`);
if (!enabledTracks[track]) {
throw boom.accepted(`Track not enabled: ${track}`);
}
const registryAddress = await api.parity.registryAddress();
const reg = api.newContract(RegistrarABI, registryAddress);
console.log(`Registering on GithubHint: ${sha3}, ${url}`);
const githubHintAddress = await reg.instance.getAddress.call({}, [githubHint, 'A']);
const h1 = await sendTransaction(GitHubHintABI, githubHintAddress, 'hintURL', [`0x${sha3}`, url]);
console.log(`Registering platform binary: ${commit}, ${platform}, ${sha3}`);
const operationsAddress = await reg.instance.getAddress.call({}, [operationsContract, 'A']);
const h2 = await sendTransaction(OperationsABI, operationsAddress, 'addChecksum', [`0x000000000000000000000000${commit}`, platform, `0x${sha3}`]);
return `${out}\ntxhash1:${h1} [githubhint]\ntxhash2:${h2} [operations]`;
}));
// make sure that the errors are added at the end
app.use((err, req, res, next) => {
if (isCelebrate(err)) {
const fields = err.details.map(x => x.path && x.path.join ? x.path.join('.') : x.path);
if (fields.indexOf('platform') !== -1 || fields.indexOf('tag') !== -1) {
res.status(202).send(err.message);
} else {
res.status(400).send(err.message);
}
return;
}
if (err.isBoom) {
res.status(err.statusCode).send(err.message);
return;
}
console.error(err);
return res.status(500).send(err.message);
});
function handleAsync (asyncFn) {
return (req, res, next) => asyncFn(req, res)
.then(result => {
return res.send(result);
})
.catch(err => {
console.error(err);
next(err);
});
}
async function readParityMetadata (commit) {
try {
const metaFile = await fetchFile(commit, '/util/version/Cargo.toml');
const parsed = toml.parse(metaFile);
const metadata = parsed.package.metadata;
if (metadata.networks === undefined) {
// backwards compatibility with legacy format
metadata.networks = metadata.forks;
const critical = parsed.package.critical || false;
for (let network in metadata.forks) {
metadata.networks[network] = { forkBlock: metadata.forks[network], critical: critical };
}
}
return {
version: parsed.package.version,
...parsed.package.metadata
};
} catch (err) {
throw new Error(`Unable to parse Parity metadata: ${err.message}`);
}
}
function fetchFile (commit, path) {
return new Promise((resolve, reject) => {
request.get({
headers: {
'User-Agent': githubRepo
},
url: `https://raw.githubusercontent.com/${githubRepo}/${commit}${path}`
}, function (error, response, body) {
if (error) {
reject(error);
} else {
resolve(body);
}
});
});
}
async function getNetwork () {
const mainnets = ['homestead', 'mainnet', 'foundation', 'ethereum'];
const n = await api.parity.netChain();
const network = mainnets.indexOf(n) !== -1 ? 'foundation' : n.indexOf('kovan') !== -1 ? 'kovan' : n;
console.log(`On network ${network}`);
return network;
}
async function sendTransaction (abi, address, method, args) {
let o = api.newContract(abi, address);
let tx = {
from: account.address,
to: address,
data: o.getCallData(o.instance[method], {}, args)
};
if (account.gasPrice) {
tx.gasPrice = account.gasPrice;
}
console.log('Sending transaction: ', tx);
const hash = account.password === null
? await api.eth.sendTransaction(tx)
: await api.personal.signAndSendTransaction(tx, account.password);
console.log(`Transaction sent with hash: ${hash}`);
return hash;
}