-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlight_wallet.js
176 lines (165 loc) · 5.64 KB
/
light_wallet.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
/*jslint node: true */
"use strict";
var db = require('./db.js');
var conf = require('./conf.js');
var myWitnesses = require('./my_witnesses.js');
var network = require('./network.js');
var walletGeneral = require('./wallet_general.js');
var light = require('./light.js');
var eventBus = require('./event_bus.js');
var RECONNECT_TO_LIGHT_VENDOR_PERIOD = 60 * 1000;
var changeLightVender = false;
var timerFlag;
function setLightVendorHost(light_vendor_host) {
network.light_vendor_url = conf.WS_PROTOCOL + light_vendor_host; // for now, light vendor is also a hub
if (conf.bLight) {
timerFlag && clearInterval(timerFlag);
timerFlag = setInterval(reconnectToLightVendor, RECONNECT_TO_LIGHT_VENDOR_PERIOD);
connecteToLightVendor();
}
}
function connecteToLightVendor() {
if (!conf.bLight)
return;
if (!network.light_vendor_url)
return console.log('refreshLightClientHistory called too early: light_vendor_url not set yet');
network.findOutboundPeerOrConnect(network.light_vendor_url, function (err, ws) {
var finish = function (err) {
if (err) {
if (!changeLightVender) {
eventBus.emit('refresh_light_timeout', function (msg) {
console.log("changeLightVender: " + msg);
if (!msg) {
setTimeout(connecteToLightVendor, 5000);
return;
};
changeLightVender = true;
setLightVendorHost(msg);
});
}
}
if (ws)
ws.bRefreshingHistory = false;
};
if (err)
return finish("refreshLightClientHistory: " + err);
console.log('refreshLightClientHistory connected');
})
}
function reconnectToLightVendor() {
network.findOutboundPeerOrConnect(network.light_vendor_url, function (err, ws) {
if (err)
return console.log("reconnectToLightVendor: " + err);
if (ws.bLightVendor)
return console.log("already connected to light vendor");
if (ws.bRefreshingHistory)
return console.log("already refreshing history");
refreshLightClientHistory();
});
}
function readListOfUnstableUnits(handleUnits) {
db.query("SELECT unit FROM units WHERE is_stable=0", function (rows) {
var arrUnits = rows.map(function (row) { return row.unit; });
handleUnits(arrUnits);
});
}
function prepareRequestForHistory(handleResult) {
myWitnesses.readMyWitnesses(function (arrWitnesses) {
if (arrWitnesses.length === 0) // first start, witnesses not set yet
return handleResult(null);
var objHistoryRequest = { witnesses: arrWitnesses };
walletGeneral.readMyAddresses(function (arrAddresses) {
if (arrAddresses.length > 0)
objHistoryRequest.addresses = arrAddresses;
readListOfUnstableUnits(function (arrUnits) {
if (arrUnits.length > 0)
objHistoryRequest.requested_joints = arrUnits;
if (!objHistoryRequest.addresses && !objHistoryRequest.requested_joints)
return handleResult(null);
if (!objHistoryRequest.addresses)
return handleResult(objHistoryRequest);
objHistoryRequest.last_stable_mci = 0;
var strAddressList = arrAddresses.map(db.escape).join(', ');
db.query(
"SELECT unit FROM unit_authors JOIN units USING(unit) WHERE is_stable=1 AND address IN(" + strAddressList + ") \n\
UNION \n\
SELECT unit FROM outputs JOIN units USING(unit) WHERE is_stable=1 AND address IN("+ strAddressList + ")",
function (rows) {
if (rows.length)
objHistoryRequest.known_stable_units = rows.map(function (row) { return row.unit; });
handleResult(objHistoryRequest);
}
);
/*db.query(
"SELECT MAX(main_chain_index) AS last_stable_mci FROM units JOIN unit_authors USING(unit) WHERE is_stable=1 AND address IN(?)",
[arrAddresses],
function(rows){
objHistoryRequest.last_stable_mci = rows[0].last_stable_mci || 0;
handleResult(objHistoryRequest);
}
);*/
});
});
}, 'wait');
}
function refreshLightClientHistory() {
if (!conf.bLight)
return;
if (!network.light_vendor_url)
return console.log('refreshLightClientHistory called too early: light_vendor_url not set yet');
eventBus.emit('refresh_light_started');
network.findOutboundPeerOrConnect(network.light_vendor_url, function onLocatedLightVendor(err, ws) {
var finish = function (err) {
if (err) {
if (!changeLightVender) {
eventBus.emit('refresh_light_timeout', function (msg) {
console.log("changeLightVender: " + msg);
if (!msg) {
setTimeout(connecteToLightVendor, 5000);
return;
};
changeLightVender = true;
setLightVendorHost(msg);
});
}
return;
}
if (ws)
ws.bRefreshingHistory = false;
eventBus.emit('refresh_light_done');
};
if (err)
return finish("refreshLightClientHistory: " + err);
console.log('refreshLightClientHistory connected');
// handling the response may take some time, don't send new requests
if (ws.bRefreshingHistory)
return console.log("previous refresh not finished yet");
ws.bRefreshingHistory = true;
prepareRequestForHistory(function (objRequest) {
if (!objRequest)
return finish();
network.sendRequest(ws, 'light/get_history', objRequest, false, function (ws, request, response) {
if (response.error)
return finish(response.error);
ws.bLightVendor = true;
var interval = setInterval(function () { // refresh UI periodically while we are processing history
eventBus.emit('maybe_new_transactions');
}, 500);
light.processHistory(response, {
ifError: function (err) {
clearInterval(interval);
network.sendError(ws, err);
finish();
},
ifOk: function () {
clearInterval(interval);
finish();
eventBus.emit('maybe_new_transactions');
}
});
});
});
});
}
exports.setLightVendorHost = setLightVendorHost;
exports.refreshLightClientHistory = refreshLightClientHistory;