-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathelected_validatoraddr.js
84 lines (68 loc) · 2.54 KB
/
elected_validatoraddr.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
var synthetics = require('Synthetics');
const log = require('SyntheticsLogger');
const apiCanaryBlueprint = async function () {
// Handle validation for positive scenario
const validateSuccessful = async function(res) {
return new Promise((resolve, reject) => {
if (res.statusCode < 200 || res.statusCode > 299) {
throw res.statusCode + ' ' + res.statusMessage;
}
let responseBody = '';
res.on('data', (d) => {
responseBody += d;
});
res.on('end', () => {
// Add validation on 'responseBody' here if required.
let body = JSON.parse(responseBody);
if (('error' in body)) {
throw `Found error in response! Error: ${JSON.stringify(body.error)}`;
}
let requiredFields = ['jsonrpc', 'id', 'result'];
requiredFields.map(function(field){
if (!(field in body)) {
console.debug('body', body);
throw `${field} is missing in body`;
}
});
let result = body.result;
if ("object" !== typeof result) {
throw `result type: ${typeof result}, should be "object"`;
}
if (result.length == 0) {
throw `there should be more than 0 validators`;
}
resolve();
});
});
};
let requestBody = {
jsonrpc: '2.0',
id: 1,
method: 'hmyv2_getElectedValidatorAddresses',
params: []
};
let requestOptionsStep1 = {
hostname: 'rpc.s0.t.hmny.io',
method: 'POST',
path: '',
port: '443',
protocol: 'https:',
body: JSON.stringify(requestBody),
headers: {
'Content-Type': 'application/json'
}
};
requestOptionsStep1['headers']['User-Agent'] = [synthetics.getCanaryUserAgentString(), requestOptionsStep1['headers']['User-Agent']].join(' ');
let stepConfig1 = {
includeRequestHeaders: true,
includeResponseHeaders: true,
includeRequestBody: true,
includeResponseBody: true,
restrictedHeaders: [],
continueOnHttpStepFailure: true
};
await synthetics.executeHttpStep('Verify hmyv2_getElectedValidatorAddresses', requestOptionsStep1, validateSuccessful, stepConfig1);
};
exports.handler = async () => {
return await apiCanaryBlueprint();
};