Skip to content

Commit 98eb147

Browse files
committed
cache xmr node connection
1 parent 5198838 commit 98eb147

6 files changed

+144
-97
lines changed

assets_js_bitrequest_core.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@ function finishfunctions() {
388388
//initaddressform
389389
submit_erc20();
390390
//validateaddress_vk
391+
//set_xmr_node_access
391392
//validateaddress
392393
//check_address
393394
//check_vk
@@ -1870,7 +1871,7 @@ function after_scan_init(api_data) {
18701871
const rq_init = request.rq_init,
18711872
request_ts = rq_init + glob_const.timezone,
18721873
set_confirmations = request.set_confirmations || 0,
1873-
rdo = {
1874+
rdo = { // request data object
18741875
"request_timestamp": request_ts,
18751876
"setconfirmations": set_confirmations,
18761877
"pending": "scanning",
@@ -2456,6 +2457,7 @@ function validateaddress_vk(ad) {
24562457
}
24572458
const start_height = data.start_height;
24582459
if (start_height > -1) { // success!
2460+
set_xmr_node_access(vkinputval);
24592461
validateaddress(ad, vkinputval);
24602462
}
24612463
}).fail(function(xhr, stat, err) {
@@ -2469,6 +2471,17 @@ function validateaddress_vk(ad) {
24692471
popnotify("error", translate("pickacurrency"));
24702472
}
24712473

2474+
// Save mymonero node status in session
2475+
function set_xmr_node_access(vk) {
2476+
const stored_vk_list = br_get_session("xmrvks", true);
2477+
if (stored_vk_list) {
2478+
stored_vk_list.push(vk);
2479+
br_set_session("xmrvks", stored_vk_list, true);
2480+
return
2481+
}
2482+
br_set_session("xmrvks", [vk], true);
2483+
}
2484+
24722485
// Validates the address for the selected currency and handles the addition or editing of the address
24732486
function validateaddress(ad, vk) {
24742487
const currency = ad.currency,

assets_js_bitrequest_fetchblocks.js

Lines changed: 70 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ $(document).ready(function() {
33
// ** API **
44

55
//lightning_fetch
6+
//monero_fetch_init
67
//monero_fetch
78
//match_xmr_pid
89
//blockchair_xmr_poll
@@ -299,28 +300,27 @@ function lightning_fetch(rd, api_data, rdo) {
299300

300301
// This function handles fetching and processing Monero transaction data.
301302
// It uses different APIs based on the pending status and performs various checks and data manipulations.
302-
function monero_fetch(rd, api_data, rdo) {
303+
function monero_fetch_init(rd, api_data, rdo) {
303304
if (rdo.pending === "polling") {
304305
blockchair_xmr_poll(rd, api_data, rdo); // use blockchair api for tx lookup
305306
return
306307
}
307-
const vk = rd.viewkey;
308+
const vk = q_obj(rd, "viewkey.vk");
308309
if (!vk) return;
309-
const api_name = api_data.name,
310-
transactionlist = rdo.transactionlist,
311-
xmr_ia = rd.xmr_ia,
312-
payment_id = rd.payment_id,
313-
account = vk.account || rd.address,
314-
viewkey = vk.vk,
315-
source = rdo.source,
310+
const viewkey = rd.viewkey;
311+
if (xmr_node_access(vk)) {
312+
monero_fetch(rd, api_data, rdo, viewkey);
313+
return
314+
}
315+
const account = vk.account || rd.address,
316316
payload = JSON.stringify({
317317
"address": account,
318-
"view_key": viewkey,
318+
"view_key": vk,
319319
"create_account": true,
320320
"generated_locally": false
321321
});
322322
api_proxy({
323-
"api": api_name,
323+
"api": "mymonero api",
324324
"search": "login",
325325
"cachetime": 25,
326326
"cachefolder": "1h",
@@ -336,59 +336,8 @@ function monero_fetch(rd, api_data, rdo) {
336336
const data = br_result(e).result;
337337
if (data) {
338338
if (data.start_height > -1) { // success!
339-
const pl = {
340-
"address": account,
341-
"view_key": viewkey
342-
};
343-
api_proxy({
344-
"api": api_name,
345-
"search": "get_address_txs",
346-
"cachetime": 25,
347-
"cachefolder": "1h",
348-
"proxy": true,
349-
"params": {
350-
"method": "POST",
351-
"data": JSON.stringify(pl),
352-
"headers": {
353-
"Content-Type": "application/json"
354-
}
355-
}
356-
}).done(function(e) {
357-
const data = br_result(e).result,
358-
transactions = data.transactions;
359-
if (transactions) {
360-
const sortlist = sort_by_date(xmr_scan_data, transactions);
361-
let counter = 0,
362-
txdat = false;
363-
$.each(sortlist, function(dat, value) {
364-
const txd = xmr_scan_data(value, rdo.setconfirmations, "xmr", data.blockchain_height);
365-
if (txd) {
366-
const xid_match = match_xmr_pid(xmr_ia, payment_id, txd.payment_id); // match xmr payment_id if set
367-
if (xid_match) {
368-
if (txd.ccval && txd.transactiontime > rdo.request_timestamp) {
369-
txdat = txd;
370-
if (source === "requests") {
371-
const tx_listitem = append_tx_li(txd, rd.requesttype);
372-
if (tx_listitem) {
373-
transactionlist.append(tx_listitem.data(txd));
374-
counter++;
375-
}
376-
}
377-
return false;
378-
}
379-
}
380-
}
381-
});
382-
scan_match(rd, api_data, rdo, counter, txdat);
383-
return
384-
}
385-
api_callback(rdo);
386-
}).fail(function(xhr, stat, err) {
387-
const error_object = xhr || stat || err;
388-
tx_api_scan_fail({
389-
"error": error_object
390-
}, rd, api_data, rdo);
391-
});
339+
set_xmr_node_access(vk);
340+
monero_fetch(rd, api_data, rdo, viewkey);
392341
return
393342
}
394343
}
@@ -407,6 +356,63 @@ function monero_fetch(rd, api_data, rdo) {
407356
});
408357
}
409358

359+
function monero_fetch(rd, api_data, rdo, viewkey) {
360+
const account = viewkey.account || rd.address,
361+
pl = {
362+
"address": account,
363+
"view_key": viewkey.vk
364+
};
365+
api_proxy({
366+
"api": "mymonero api",
367+
"search": "get_address_txs",
368+
"cachetime": 25,
369+
"cachefolder": "1h",
370+
"proxy": true,
371+
"params": {
372+
"method": "POST",
373+
"data": JSON.stringify(pl),
374+
"headers": {
375+
"Content-Type": "application/json"
376+
}
377+
}
378+
}).done(function(e) {
379+
const data = br_result(e).result,
380+
transactions = data.transactions;
381+
if (transactions) {
382+
const sortlist = sort_by_date(xmr_scan_data, transactions);
383+
let counter = 0,
384+
txdat = false;
385+
$.each(sortlist, function(dat, value) {
386+
const txd = xmr_scan_data(value, rdo.setconfirmations, "xmr", data.blockchain_height);
387+
if (txd) {
388+
const xid_match = match_xmr_pid(rd.xmr_ia, rd.payment_id, txd.payment_id); // match xmr payment_id if set
389+
if (xid_match) {
390+
if (txd.ccval && txd.transactiontime > rdo.request_timestamp) {
391+
txdat = txd;
392+
if (rdo.source === "requests") {
393+
const tx_listitem = append_tx_li(txd, rd.requesttype);
394+
if (tx_listitem) {
395+
rdo.transactionlist.append(tx_listitem.data(txd));
396+
counter++;
397+
}
398+
}
399+
return false;
400+
}
401+
}
402+
}
403+
});
404+
scan_match(rd, api_data, rdo, counter, txdat);
405+
return
406+
}
407+
api_callback(rdo);
408+
}).fail(function(xhr, stat, err) {
409+
const error_object = xhr || stat || err;
410+
tx_api_scan_fail({
411+
"error": error_object
412+
}, rd, api_data, rdo);
413+
});
414+
}
415+
410416
// This function matches Monero payment IDs.
411417
// It checks if the provided payment IDs match based on certain conditions.
412418
function match_xmr_pid(xmria, xmrpid, xmr_pid) {

assets_js_bitrequest_lang_en.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ function lang_en(id, data) {
137137
"fallbackaddress": "Fallback address",
138138
"paymentid": "Payment ID",
139139
"integratedaddress": "Integrated Address",
140-
"network": "Netwerk",
140+
"network": "Network",
141141
"incoming": "Incoming",
142142
"outgoing": "Outgoing",
143143
"point of sale": "Point of sale",

assets_js_bitrequest_monitors.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,11 @@ function continue_select(rd, api_data, rdo) {
222222
function continue_select_api(rd, api_data, rdo) {
223223
const api_name = api_data.name;
224224
if (api_name === "mymonero api") {
225-
monero_fetch(rd, api_data, rdo);
225+
monero_fetch_init(rd, api_data, rdo);
226226
return
227227
}
228228
if (api_name === "blockchair_xmr") {
229-
monero_fetch(rd, api_data, rdo);
229+
monero_fetch_init(rd, api_data, rdo);
230230
return
231231
}
232232
if (api_name === "mempool.space") {

assets_js_bitrequest_polling.js

Lines changed: 56 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22

33
//tx_polling_init
44
//tx_polling
5+
//tx_polling_l1
6+
//tx_polling_l2
57
//clear_tpto
68
//address_polling_init
79
//address_polling
810
//init_xmr_node
11+
//xmr_node_access
12+
//init_xmr_ping
913
//ping_xmr_node
1014
//confirmations
1115
//clearpinging
@@ -51,33 +55,20 @@ function tx_polling(tx_data, api_dat, retry) {
5155
glob_const.paymentdialogbox.removeClass("transacting");
5256
}
5357

54-
function tx_polling_l2(eth_layer2, api_dat, retry) {
55-
clear_tpto();
56-
const to_time = retry ? 10 : api_dat ? 30000 : 10,
57-
l2_options = fertch_l2s(request.payment),
58-
api_data = api_dat || q_obj(l2_options, eth_layer2 + ".apis.selected"),
59-
ctracts = contracts(request.currencysymbol),
60-
contract = ctracts ? ctracts[eth_layer2] : false;
61-
glob_let.tpto = setTimeout(function() {
62-
omni_poll(api_data, contract);
63-
}, to_time, function() {
64-
clear_tpto();
65-
});
66-
}
67-
58+
// Layer 1 transaction polling
6859
function tx_polling_l1(tx_data, api_dat, retry) {
6960
clear_tpto();
7061
const to_time = retry ? 10 : api_dat ? 30000 : 10,
7162
api_data = api_dat || q_obj(helper, "api_info.data"),
72-
rdo = {
63+
rdo = { // request data object
7364
"requestid": request.requestid,
7465
"pending": "polling",
7566
"txdat": tx_data,
7667
"source": "tx_polling",
7768
"setconfirmations": tx_data.setconfirmations,
7869
"cachetime": 25
7970
},
80-
rd = {
71+
rd = { // custom request data
8172
"requestid": request.requestid,
8273
"payment": request.payment,
8374
"erc20": request.erc20,
@@ -94,6 +85,21 @@ function tx_polling_l1(tx_data, api_dat, retry) {
9485
});
9586
}
9687

88+
// Layer 2 transaction polling
89+
function tx_polling_l2(eth_layer2, api_dat, retry) {
90+
clear_tpto();
91+
const to_time = retry ? 10 : api_dat ? 30000 : 10,
92+
l2_options = fertch_l2s(request.payment),
93+
api_data = api_dat || q_obj(l2_options, eth_layer2 + ".apis.selected"),
94+
ctracts = contracts(request.currencysymbol),
95+
contract = ctracts ? ctracts[eth_layer2] : false;
96+
glob_let.tpto = setTimeout(function() {
97+
omni_poll(api_data, contract);
98+
}, to_time, function() {
99+
clear_tpto();
100+
});
101+
}
102+
97103
// clear tx_polling timer
98104
function clear_tpto() {
99105
clearTimeout(glob_let.tpto);
@@ -127,7 +133,7 @@ function address_polling(timeout, api_data) {
127133
request_ts = request_ts_utc - 15000, // 15 second margin
128134
set_confirmations = request.set_confirmations || 0,
129135
cachetime = (timeout - 1000) / 1000,
130-
rdo = {
136+
rdo = { // request data object
131137
"requestid": request.requestid,
132138
"request_timestamp": request_ts,
133139
"setconfirmations": set_confirmations,
@@ -148,6 +154,10 @@ function address_polling(timeout, api_data) {
148154

149155
// Initializes Monero node connection
150156
function init_xmr_node(cachetime, address, vk) {
157+
if (xmr_node_access(vk)) {
158+
init_xmr_ping(cachetime, address, vk);
159+
return
160+
}
151161
const payload = {
152162
"address": address,
153163
"view_key": vk,
@@ -178,16 +188,8 @@ function init_xmr_node(cachetime, address, vk) {
178188
}
179189
const start_height = data.start_height;
180190
if (start_height > -1) { // success!
181-
const rq_init = request.rq_init,
182-
request_ts_utc = rq_init + glob_const.timezone,
183-
request_ts = request_ts_utc - 10000; // 10 second compensation
184-
socket_info({
185-
"url": "mymonero api"
186-
}, true, true);
187-
glob_let.pinging[address] = setInterval(function() {
188-
poll_animate();
189-
ping_xmr_node(cachetime, address, vk, request_ts);
190-
}, 12000);
191+
set_xmr_node_access(vk);
192+
init_xmr_ping(cachetime, address, vk);
191193
return
192194
}
193195
}
@@ -201,6 +203,32 @@ function init_xmr_node(cachetime, address, vk) {
201203
});
202204
}
203205

206+
function xmr_node_access(vk) {
207+
if (vk) {
208+
const stored_vk_list = br_get_session("xmrvks", true);
209+
if (stored_vk_list) {
210+
if (stored_vk_list.includes(vk)) {
211+
return true;
212+
}
213+
}
214+
}
215+
return false;
216+
}
217+
218+
// Initializes Monero node connection
219+
function init_xmr_ping(cachetime, address, vk) {
220+
const rq_init = request.rq_init,
221+
request_ts_utc = rq_init + glob_const.timezone,
222+
request_ts = request_ts_utc - 10000; // 10 second compensation
223+
socket_info({
224+
"url": "mymonero api"
225+
}, true, true);
226+
glob_let.pinging[address] = setInterval(function() {
227+
poll_animate();
228+
ping_xmr_node(cachetime, address, vk, request_ts);
229+
}, 12000);
230+
}
231+
204232
// Pings Monero node for transaction updates
205233
function ping_xmr_node(cachetime, address, vk, request_ts) {
206234
if (!isopenrequest()) { // only when request is visible
@@ -245,7 +273,7 @@ function ping_xmr_node(cachetime, address, vk, request_ts) {
245273
$.each(txflip_strip, function(dat, value) {
246274
const txd = xmr_scan_data(value, set_confirmations, "xmr", data.blockchain_height);
247275
if (txd.ccval && txd.transactiontime > request_ts) {
248-
const txid_match = get_requestli("txhash", txd.txhash); // check if txhash already exists
276+
const txid_match = get_requestli("txhash", txd.txhash); // scan pending xmr tx's to prevent duplicates, mempool tx's don't have correct timestamps
249277
if (txid_match.length) {
250278
return false;
251279
}

0 commit comments

Comments
 (0)