-
Notifications
You must be signed in to change notification settings - Fork 9
/
witness_action.js
166 lines (154 loc) · 5.17 KB
/
witness_action.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
require('dotenv').config();
const axios = require('axios');
const dhive = require('@hiveio/dhive');
const program = require('commander');
const { exec } = require('child_process');
const packagejson = require('./package.json');
const config = require('./config.json');
const ip = process.env.NODE_IP;
const domain = process.env.NODE_DOMAIN;
const witnessAccount = process.env.ACCOUNT;
const privateSigningKey = dhive.PrivateKey.fromString(process.env.ACTIVE_SIGNING_KEY);
const publicSigningKey = privateSigningKey.createPublic().toString();
const {
rpcNodePort, p2pPort, streamNodes, chainId,
} = config;
// For external NATed-ports customization
const extRPCNodePort = Number(String(process.env.RPCNODEPORT)) || rpcNodePort;
const extP2PPort = Number(String(process.env.P2PPORT)) || p2pPort;
program
.option('-v, --verify', 'verify transaction on hive-engine', false)
.option('-e, --engineNode [url]', 'verify with given hive-engine node', 'https://engine.rishipanthee.com')
.option('-d, --diverganceCheck', 'check for divergance against the given hive-engine node', false)
.parse(process.argv);
let { engineNode } = program;
const { verify, diverganceCheck } = program;
engineNode = engineNode.replace(/\/$/, '');
function awaitValidation(trxID, tries = 1) {
axios.post(`${engineNode}/blockchain`, {
jsonrpc: '2.0', id: 0, method: 'getTransactionInfo', params: { txid: trxID },
}).then((res) => {
if (res.data.result) {
const parsedLogs = JSON.parse(res.data.result.logs);
if (parsedLogs.errors) {
// eslint-disable-next-line no-console
console.log(`Failed action with reason "${parsedLogs.errors[0]}", your trx id: ${trxID}`);
} else {
// eslint-disable-next-line no-console
console.log(`Successfull, transaction id is ${trxID}`);
}
} else {
if (tries >= 4) {
// eslint-disable-next-line no-console
console.log(`Successful broadcast but could not verify, trx id: ${trxID}`);
return;
}
setTimeout(() => {
awaitValidation(trxID, tries + 1);
}, 4000);
}
}).catch((err) => {
// eslint-disable-next-line no-console
console.log(err);
if (tries >= 4) {
// eslint-disable-next-line no-console
console.log(`Successful broadcast but could not verify, trx id: ${trxID}`);
return;
}
setTimeout(() => {
awaitValidation(trxID, tries + 1);
}, 4000);
});
}
function broadcastWitnessAction(contractAction, contractPayload) {
const client = new dhive.Client(streamNodes[0]);
const transaction = {
required_auths: [witnessAccount],
required_posting_auths: [],
id: `ssc-${chainId}`,
json: JSON.stringify({
contractName: 'witnesses',
contractAction,
contractPayload,
}),
};
client.broadcast.json(transaction, privateSigningKey).then((res) => {
if (verify) {
awaitValidation(res.id);
} else {
// eslint-disable-next-line no-console
console.log(`Successful, trx id: ${res.id}`);
}
}).catch((err) => {
// eslint-disable-next-line no-console
console.error('Error', err);
});
}
program.version(packagejson.version);
program
.command('approve <witness>')
.action(witness => broadcastWitnessAction('approve', { witness }));
program
.command('disapprove <witness>')
.action(witness => broadcastWitnessAction('disapprove', { witness }));
program
.command('register')
.action(() => {
const registerJSON = {
RPCPort: extRPCNodePort,
P2PPort: extP2PPort,
signingKey: publicSigningKey,
enabled: true,
};
if (ip && domain) {
// eslint-disable-next-line no-console
console.log('Both domain and IP specified in your .env file, please only use one');
return;
} else if (ip) {
registerJSON.IP = ip;
} else if (domain) {
registerJSON.domain = domain;
} else {
// eslint-disable-next-line no-console
console.log('Missing domain or IP, please add it to your .env file');
return;
}
if (diverganceCheck) {
exec(`node find_divergent_block.js -h -n ${engineNode}`).on('exit', (code) => {
if (code !== 0) {
// eslint-disable-next-line no-console
console.log(`A divergent block was found, not registering. Run node find_divergent_block.js -n ${engineNode} to learn where.`);
return;
} else {
broadcastWitnessAction('register', registerJSON);
}
});
} else {
broadcastWitnessAction('register', registerJSON);
}
});
program
.command('unregister')
.action(() => {
const registerJSON = {
RPCPort: extRPCNodePort,
P2PPort: extP2PPort,
signingKey: publicSigningKey,
enabled: false,
};
if (ip && domain) {
// eslint-disable-next-line no-console
console.log('Both domain and IP specified in your .env file, please only use one');
return;
} else if (ip) {
registerJSON.IP = ip;
} else if (domain) {
registerJSON.domain = domain;
} else {
// eslint-disable-next-line no-console
console.log('Missing domain or IP, please add it to your .env file');
return;
}
broadcastWitnessAction('register', registerJSON);
});
program.parse(process.argv);