-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathclient.js
More file actions
177 lines (154 loc) · 4.44 KB
/
client.js
File metadata and controls
177 lines (154 loc) · 4.44 KB
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
var Web3 = require('web3');
var net = require('net');
var fs = require('fs');
var request = require('request');
var sleep = require('sleep');
let ipcPath = "/home/geth/.geth/geth.ipc";
function web3Client() {
this.failCount = 0;
}
web3Client.prototype.Refresh = function () {
if (this.failCount > 15) {
throw new Error("Too many exceptions. . . Exiting")
}
if (!this._web3) {
var client = net.Socket();
var web3 = new Web3(new Web3.providers.IpcProvider(ipcPath, client));
web3._extend({
property: 'geth',
properties:
[
new web3._extend.Property({
name: 'nodeInfo',
getter: 'admin_nodeInfo'
}),
]
});
web3._extend({
property: 'parity',
properties:
[
new web3._extend.Property({
name: 'nodeInfo',
getter: 'parity_enode',
outputFormatter: function (result) {
var pattern = /enode\:\/\/([^@]+)@[^:]+:(.+)/g;
var match = pattern.exec(result);
if (match) {
result = {
id: match[1],
ports: {
listener: match[2]
}
}
}
return result;
}
}),
]
});
this._web3 = web3
this.default = web3.geth;
this.geth = web3.geth;
this.parity = web3.parity;
}
this._web3.reset();
}
function updateEnode(url, data, callback) {
console.log("update enode - " + url);
request.post(
url,
{
json: data,
timeout: 1000
},
function (error, response, body) {
var e = error || response.statusCode != 200;
if (e) {
console.log(error);
}
callback(e, response);
}
)
};
function enodeUpdater(web3Client) {
this.web3 = web3Client;
}
function runLoop(obj, timeout) {
setTimeout(function () {
obj.Run(obj);
}, timeout);
}
function readNode(web3, fn) {
web3.Refresh();
web3.default.getNodeInfo(function (error, result) {
if (error) {
web3.geth.getNodeInfo(function (error, result) {
if (error) {
web3.parity.getNodeInfo(function (error, result) {
if (!error)
{
web3.default = web3.parity;
}
fn(error, result);
});
}
else
{
web3.default = web3.geth;
fn(error, result);
}
})
}
else
{
fn(error, result);
}
});
}
enodeUpdater.prototype.Run = function (obj) {
var web3 = obj.web3;
readNode(obj.web3, function (error, result) {
var timeout = 1000 * 10;
if (error) {
web3.failCount++;
console.log("Fail count: " + web3.failCount + " " + error);
runLoop(obj, 500);
}
else {
var listenPort = result.ports.listener;
if (!listenPort)
{
listenPort = 30303;
}
var data = {
enode: result.id,
port: listenPort,
ip: process.env.HOST_IP,
publicIp: process.env.BOOTNODE_PUBLIC_IP,
network: process.env.BOOTNODE_NETWORK,
miner: false || process.env.ENABLE_MINER
}
updateEnode(process.env.BOOTNODE_URL, data, function (err, result) {
if (err) {
runLoop(obj, 1000 * 3);
}
else {
console.log(data);
runLoop(obj, 1000 * 15);
}
});
}
});
};
if (process.env.ETH_IPC_PATH) {
ipcPath = process.env.ETH_IPC_PATH;
}
if (process.env.BOOTNODE_URL) {
var client = new web3Client();
var enode = new enodeUpdater(client);
enode.Run(enode);
}
else {
console.log("No BOOTNODE_URL,BOOTNODE_NETWORK or BOOTNODE_PUBLIC_IP provided");
}