-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect_node.js
70 lines (64 loc) · 1.68 KB
/
connect_node.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
const request = require('request');
module.exports = function (dependencies) {
let peer_list = [
"http://10.30.32.138:3000"
]
/**
* Send the newly generated transaction to the nodes
* @param {String} data; send serialized txn
*/
const send_txn = function (data) {
for (let i = 0; i < peer_list.length; i++) {
request({
method: 'post',
body: {
txn: data
},
json: true,
url: peer_list[i] + '/txn/new-txn'
},
function (err, res, body) {
if (err) {
console.log("Error sending transaction :: ", err);
}
console.log("Resp :: ", body);
}
);
}
};
const find_txn_at = function (block_num, txn_sig) {
return new Promise(function(resolve, reject) {
let resolved = false;
let current = 0;
let last_sent = Date.now();
// Send a POST request to the peer_list sequentially
// but waits 2 seconds in between. If the response is
// received, stop sending request.
let send_req = function () {
if (resolved) return;
setTimeout(send_req, 2000 - (Date.now() - last_sent));
last_sent = Date.now();
console.log("Asking :: ", peer_list[current]);
request({
url : peer_list[current] + '/chain/find_txn_at',
qs : {
block_num,
txn_sig
}
}, function (err, res, body) {
if (resolved) return;
if (!err) {
resolved = true;
resolve (body.value);
return ;
}
});
current ++;
}
});
}
return {
send_txn,
find_txn_at
}
}