-
Notifications
You must be signed in to change notification settings - Fork 8
/
relayer.js
232 lines (209 loc) · 7.99 KB
/
relayer.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
#!/usr/bin/env node
const Web3 = require('web3');
const request = require('request');
/*
* Usage: Subscribe to Geth node and push header to syscoin via RPC
*
*/
let argv = require('yargs')
.usage('Usage: $0 -sysrpcuser [username] -sysrpcpw [password] -sysrpcport [port] -ethwsport [port]')
.default("sysrpcport", 8370)
.default("ethwsport", 8546)
.default("sysrpcuser", "u")
.default("sysrpcpw", "p")
.argv
;
if (argv.sysrpcport < 0 || argv.sysrpcport > 65535) {
console.log('Invalid Syscoin RPC port');
exit();
}
if (argv.ethwsport < 0 || argv.ethwsport > 65535) {
console.log('Invalid Geth RPC port');
exit();
}
const sysrpcport = argv.sysrpcport;
const ethwsport = argv.ethwsport;
const sysrpcuser = argv.sysrpcuser;
const sysrpcpw = argv.sysrpcpw;
/* Global Variables */
var highestBlock = 0;
var currentBlock = 0;
var timediff = 0;
/* Initialize Geth Web3 */
var web3 = new Web3("ws://127.0.0.1:" + argv.ethwsport);
var provider = web3.currentProvider;
var collection = [];
var missingBlocks = [];
var requestedBlocks = [];
var downloadingBlocks = false;
SetupListener();
function SetupListener() {
provider.on("error", err => {
console.log("web3 socket error\n")
});
provider.on("end", err => {
console.log("web3 socket ended. Retrying...\n");
setTimeout(function () {
provider = new Web3.providers.WebsocketProvider("ws://127.0.0.1:" + ethwsport);
web3.setProvider(provider);
SetupListener();
}, 3000);
});
provider.on("connect", function () {SetupSubscriber()});
}
function SetupSubscriber() {
console.log("Subscribed to newBlockHeaders");
/* Geth subscriber for new block headers */
const subscriptionHeader = web3.eth.subscribe('newBlockHeaders', (error, blockHeader) => {
if (error) return console.error(error);
if (blockHeader['number'] > currentBlock) {
currentBlock = blockHeader['number'];
}
if (currentBlock > highestBlock) {
highestBlock = currentBlock;
}
let obj = [blockHeader['number'],blockHeader['transactionsRoot'],blockHeader['receiptsRoot']];
collection.push(obj);
// Check blockheight and timestamp to notify synced status
timediff = new Date() / 1000 - blockHeader['timestamp'];
});
/* Timer for submitting header lists to Syscoin via RPC */
const timer = setInterval(RPCsyscoinsetethheaders, 5000);
function RPCsyscoinsetethheaders() {
// Check if there's anything in the collection
if (collection.length == 0) {
// console.log("collection is empty");
return;
}
if (highestBlock != 0 && currentBlock >= highestBlock && timediff < 600) {
console.log("Geth is synced based on block height and timestamp");
RPCsyscoinsetethstatus(["synced", currentBlock]);
timediff = 0;
}
// Request options
let options = {
url: "http://localhost:" + sysrpcport,
method: "post",
headers:
{
"content-type": "text/plain"
},
auth: {
user: sysrpcuser,
pass: sysrpcpw
},
body: JSON.stringify( {"jsonrpc": "1.0", "id": "ethheader_update", "method": "syscoinsetethheaders", "params": [collection]})
};
request(options, (error, response, body) => {
if (error) {
console.error('An error has occurred: ', error);
}
});
console.log("syscoinsetethheaders: ", JSON.stringify(collection));
collection = [];
};
/* Subscription for Geth syncing status */
const subscriptionSync = web3.eth.subscribe('syncing', function(error, sync){
if (error) return console.error(error);
var params = [];
if (typeof(sync) == "boolean") {
if (sync) {
params = ["syncing", 0];
} else {
// Syncing === false doesn't meant that it's done syncing.
// It simply means it's not syncing
if (currentBlock < highestBlock || highestBlock == 0) {
// highestBlock == 0 should really mean it's waiting to connect to peer
params = ["syncing", highestBlock];
} else {
console.log("Geth is synced based on syncing subscription");
params = ["synced", highestBlock];
}
}
} else {
if (highestBlock < sync.status.HighestBlock) {
highestBlock = sync.status.HighestBlock;
}
params = ["syncing", highestBlock];
}
RPCsyscoinsetethstatus(params);
});
function RPCsyscoinsetethstatus(params) {
let options = {
url: "http://localhost:" + sysrpcport,
method: "post",
headers:
{
"content-type": "text/plain"
},
auth: {
user: sysrpcuser,
pass: sysrpcpw
},
body: JSON.stringify( {
"jsonrpc": "1.0",
"id": "eth_sync_update",
"method": "syscoinsetethstatus",
"params": params})
};
// console.log(options.body);
request(options, (error, response, body) => {
if (error) {
console.error('An error has occurred: ', error);
} else {
console.log('Post successful: ', body);
var parsedBody = JSON.parse(body);
var missingBlockRanges = parsedBody.result.missing_blocks;
var counter = 0;
if (missingBlockRanges.length == 0) {
console.log(" there is no missing_blocks ");
} else {
for(var i = 0; i < missingBlockRanges.length; i++) {
for(var i2 = missingBlockRanges[i].from; i2 <= missingBlockRanges[i].to; i2++) {
missingBlocks[i2] = true;
counter++;
}
}
}
console.log("missingBlocks count: ", counter);
}
});
console.log("syscoinsetethstatus: ", params);
};
const timer2 = setInterval(retrieveBlock, 15000);
function retrieveBlock() {
missingBlocks.forEach(function(value, key) {
if (requestedBlocks.hasOwnProperty(key) && requestedBlocks[key] == true) {
return;
}
requestedBlocks[key] = true;
if (value) {
try {
web3.eth.getBlock(key, function(error, result) {
if (error) {
web3Infura.eth.getBlock(key, function(error, result){
if (error) {
requestedBlocks[key] = false;
console.log("infura error: ", error);
}
else if (result != "undefined") {
missingBlocks[key] = false;
let obj = [result['number'],result['transactionsRoot'],result['receiptsRoot']];
collection.push(obj);
}
});
}
else if (result != "undefined") {
missingBlocks[key] = false;
let obj = [result['number'],result['transactionsRoot'],result['receiptsRoot']];
collection.push(obj);
}
});
} catch(e) {
requestedBlocks[key] = false;
console.log("getBlock: error2", e, key, missingBlocks[key], value, requestedBlocks[key]);
}
}
});
}
};