Skip to content

Commit

Permalink
Merge pull request moneysocket#12 from jarret/update
Browse files Browse the repository at this point in the history
wallet handle, track and forward errors
  • Loading branch information
jarret authored Mar 16, 2021
2 parents 992fc93 + e2fff99 commit 6bb6a4e
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 19 deletions.
14 changes: 13 additions & 1 deletion src/js/model/manual-receive-receipt.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,30 @@ class ManualReceiveReceipt {
return entry;
}

static manualReceiveErrorEntry(error_msg, request_reference_uuid)
{
var entry = {'type': 'error',
'time': Timestamp.getNowTimestamp(),
'error_msg': error_msg,
'request_reference_uuid': request_reference_uuid,
};
return entry;
}

static manualReceiveInfo(manual_receive) {
//console.log("manual_receive: " + JSON.stringify(manual_receive));
var entries = manual_receive['entries']
var completed = ((entries.length == 3) &&
entries[2]['type'] == 'preimage_notified');
var got_invoice = ((entries.length >= 2) &&
entries[1]['type'] == 'invoice_notified');
var error = (((entries.length >= 2) && entries[1]['type'] == 'error') ?
entries[1]['error_msg'] : null);

var wad = entries[0]['wad'];
var now = Timestamp.getNowTimestamp();
var expired = got_invoice ? (now > entries[1]['timeout']) : false;
return [got_invoice, completed, wad, expired];
return [error, got_invoice, completed, wad, expired];
}
}

Expand Down
13 changes: 12 additions & 1 deletion src/js/model/manual-send-receipt.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,27 @@ class ManualSendReceipt {
return entry;
}

static manualSendErrorEntry(error_msg, request_reference_uuid) {
var entry = {'type': 'error',
'time': Timestamp.getNowTimestamp(),
'error_msg': error_msg,
'request_reference_uuid': request_reference_uuid,
};
return entry;
}

static manualSendInfo(manual_send) {
//console.log("manual_send: " + JSON.stringify(manual_send));
var entries = manual_send['entries']
var completed = ((entries.length == 2) &&
entries[1]['type'] == 'preimage_notified');
var error = (((entries.length == 2) && entries[1]['type'] == 'error') ?
entries[1]['error_msg'] : null);

var bolt11 = entries[0]['bolt11'];
var wad = entries[0]['wad'];
var description = entries[0]['description'];
return [completed, bolt11, wad, description];
return [error, completed, bolt11, wad, description];
}
}

Expand Down
31 changes: 31 additions & 0 deletions src/js/model/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ class CostanzaModel {
s.onpreimage = (function(preimage, request_reference_uuid) {
this.consumerOnPreimage(preimage, request_reference_uuid);
}).bind(this);
s.onerror = (function(error_msg, request_reference_uuid) {
this.consumerOnError(error_msg, request_reference_uuid);
}).bind(this);
return s;
}

Expand Down Expand Up @@ -258,6 +261,34 @@ class CostanzaModel {
}
}

consumerOnError(error_msg, request_reference_uuid) {
console.log("error on request: " + error_msg);

var [known, socket, send] = this.transact.errorNotified(
request_reference_uuid);

if (! known) {
var err = "error message not matching request: " + error_msg;
console.log(err);
return;
}

if (socket) {
this.provider_stack.sendErrorNotification(error_msg,
request_reference_uuid);
this.receipts.socketSessionErrorNotified(error_msg,
request_reference_uuid);
} else {
if (send) {
this.receipts.manualSendError(error_msg,
request_reference_uuid);
} else {
this.receipts.manualReceiveError(error_msg,
request_reference_uuid);
}
}
}

///////////////////////////////////////////////////////////////////////////
// Provider Stack Callbacks
///////////////////////////////////////////////////////////////////////////
Expand Down
25 changes: 23 additions & 2 deletions src/js/model/receipts.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ class Receipts {
this.model.receiptsUpdated(this.socket_session.receipt_uuid);
}

socketSessionErrorNotified(err) {
var entry = SocketSessionReceipt.errNotifiedEntry(err);
socketSessionErrorNotified(err, request_reference_uuid) {
var entry = SocketSessionReceipt.errNotifiedEntry(
err, request_reference_uuid);
this.socket_session.entries.push(entry);
this.storeReceipts();
this.model.receiptsUpdated(this.socket_session.receipt_uuid);
Expand Down Expand Up @@ -138,6 +139,16 @@ class Receipts {
this.model.receiptsUpdated(receive.receipt_uuid);
}

manualReceiveError(error_msg, request_reference_uuid) {
var entry = ManualReceiveReceipt.manualReceiveErrorEntry(error_msg,
request_reference_uuid);
var receive = this.receive_requests[request_reference_uuid];
delete this.receive_requests[request_reference_uuid];
receive.entries.push(entry);
this.storeReceipts();
this.model.receiptsUpdated(receive.receipt_uuid);
}

manualReceiveTimeout() {
// TODO
}
Expand Down Expand Up @@ -175,6 +186,16 @@ class Receipts {
this.model.receiptsUpdated(send.receipt_uuid);
}

manualSendError(error_msg, request_reference_uuid) {
var entry = ManualSendReceipt.manualSendErrorEntry(error_msg,
request_reference_uuid);
var send = this.send_requests[request_reference_uuid];
delete this.send_requests[request_reference_uuid];
send.entries.push(entry);
this.storeReceipts();
this.model.receiptsUpdated(send.receipt_uuid);
}

manualSendTimeout() {
// TODO
}
Expand Down
9 changes: 5 additions & 4 deletions src/js/model/socket-session-receipt.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,11 @@ class SocketSessionReceipt {
return entry;
}

static errNotifiedEntry(err) {
var entry = {'type': 'error_notified',
'time': Timestamp.getNowTimestamp(),
'error': err,
static errNotifiedEntry(error_msg, request_reference_uuid) {
var entry = {'type': 'error_notified',
'time': Timestamp.getNowTimestamp(),
'error_msg': error_msg,
'request_reference_uuid': request_reference_uuid,
};
return entry;
}
Expand Down
62 changes: 55 additions & 7 deletions src/js/model/transact.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class Transact {

this.pays_requested_socket = {};
this.pays_requested_manual = {};
this.pays_requested_socket_by_request_uuid = {};
this.pays_requested_manual_by_request_uuid = {};
}

//////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -136,6 +138,22 @@ class Transact {
if (this.isManualRequest(request_reference_uuid)) {
delete this.invoices_requested_manual[request_reference_uuid];
}
if (request_reference_uuid in
this.pays_requested_socket_by_request_uuid)
{
var payment_hash = this.pays_requested_socket_by_request_uuid;
delete this.pays_requested_socket[payment_hash];
delete this.pays_requested_socket_by_request_uuid[
request_reference_uuid];
}
if (request_reference_uuid in
this.pays_requested_manual_by_request_uuid)
{
var payment_hash = this.pays_requested_manual_by_request_uuid;
delete this.pays_requested_manual[payment_hash];
delete this.pays_requested_manual_by_request_uuid[
request_reference_uuid];
}
}


Expand Down Expand Up @@ -184,6 +202,30 @@ class Transact {
return [false, "unknown request_reference_uuid"];
}

errorNotified(request_reference_uuid) {
if (request_reference_uuid in this.invoices_requested_socket) {
this.forgetRequest(request_reference_uuid);
return [true, true, false];
}
else if (request_reference_uuid in this.invoices_requested_manual) {
this.forgetRequest(request_reference_uuid);
return [true, false, false];
}
else if (request_reference_uuid in
this.pays_requested_socket_by_request_uuid)
{
this.forgetRequest(request_reference_uuid);
return [true, true, true];
}
else if (request_reference_uuid in
this.pays_requested_manual_by_request_uuid)
{
this.forgetRequest(request_reference_uuid);
return [true, false, true];
}
return [false, null, null]
}

//////////////////////////////////////////////////////////////////////////
// pay requests socket
//////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -216,9 +258,12 @@ class Transact {
var payment_hash = Bolt11.getPaymentHash(bolt11);
var msats = this.getMsats(bolt11);
var expiry = this.getExpiryTimestamp(bolt11);
this.pays_requested_socket[payment_hash] = {'bolt11': bolt11,
'msats': msats,
'expiry': expiry};
this.pays_requested_socket[payment_hash] = {
'bolt11': bolt11,
'msats': msats,
'expiry': expiry,
'request_uuid': request_uuid};
this.pays_requested_socket_by_request_uuid[request_uuid] = payment_hash;
}

//////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -246,13 +291,16 @@ class Transact {
return null;
}

payRequestedManual(bolt11) {
payRequestedManual(bolt11, request_uuid) {
var payment_hash = Bolt11.getPaymentHash(bolt11);
var msats = this.getMsats(bolt11);
var expiry = this.getExpiryTimestamp(bolt11);
this.pays_requested_manual[payment_hash] = {'bolt11': bolt11,
'msats': msats,
'expiry': expiry};
this.pays_requested_manual[payment_hash] = {
'bolt11': bolt11,
'msats': msats,
'expiry': expiry,
'request_uuid': request_uuid};
this.pays_requested_manual_by_request_uuid[request_uuid] = payment_hash;
}

//////////////////////////////////////////////////////////////////////////
Expand Down
8 changes: 8 additions & 0 deletions src/js/view/screen/drill-level-one.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ class DrillLevelOneScreen {
return [entry['wad'].toString(), ""];
} else if (entry['type'] == 'pay_request') {
return [entry['wad'].toString(), ""];
} else if (entry['type'] == 'error_notified') {
return [entry['error_msg'], "font-bold text-red-400"];
} else if (entry['type'] == "preimage_notified") {
if (entry['increment']) {
return ["+" + entry['wad'].toString(),
Expand All @@ -101,6 +103,9 @@ class DrillLevelOneScreen {
case "manual_send":
if (entry['type'] == 'request_pay') {
return [entry['wad'].toString(), ""];
} else if (entry['type'] == 'error') {
return ["Error: " + entry['error_msg'],
"font-bold text-red-400"];
} else {
var w = this.receipt['entries'][0]['wad'];
return [ "-" + w.toString(), "font-bold text-red-400"];
Expand All @@ -110,6 +115,9 @@ class DrillLevelOneScreen {
case "manual_receive":
if (entry['type'] == 'request_invoice') {
return [entry['wad'].toString(), ""];
} else if (entry['type'] == 'error') {
return ["Error: " + entry['error_msg'],
"font-bold text-red-400"];
} else if (entry['type'] == 'preimage_notified') {
var w = this.receipt['entries'][0]['wad'];
return [ "+" + w.toString(), "font-bold text-green-400"];
Expand Down
13 changes: 9 additions & 4 deletions src/js/view/screen/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class MainScreen {
///////////////////////////////////////////////////////////////////////////

drawManualReceiveReceipt(div, manual_receive, click_func) {
var [got_invoice, completed, wad, expired] = (
var [error, got_invoice, completed, wad, expired] = (
ManualReceiveReceipt.manualReceiveInfo(manual_receive));

var d = D.emptyDiv(div, "tx-button-qr");
Expand All @@ -140,7 +140,10 @@ class MainScreen {
var icon_span = D.emptySpan(flex, "px-2 font-bold");
I.qrcode1x(icon_span);

if (! got_invoice) {
if (error != null) {
D.textSpan(flex, "Manual Receive Err: " + error,
"flex-grow font-bold");
} else if (! got_invoice) {
D.textSpan(flex, "Waiting for invoice " + wad.toString(),
"flex-grow text-sm");
} else if (completed) {
Expand All @@ -154,7 +157,7 @@ class MainScreen {
}

drawManualSendReceipt(div, manual_send, click_func) {
var [completed, bolt11, wad, description] = (
var [error, completed, bolt11, wad, description] = (
ManualSendReceipt.manualSendInfo(manual_send));

var d = D.emptyDiv(div, "tx-button-qr");
Expand All @@ -167,7 +170,9 @@ class MainScreen {

description = (description == null) ? "(no description)" : description;

if (! completed) {
if (error != null) {
D.textSpan(flex, "Pay Error: " + error, "flex-grow font-bold");
} else if (! completed) {
D.textSpan(flex, "Paying", "flex-grow font-bold");
D.textSpan(flex, description, "flex-grow text-sm");
} else {
Expand Down

0 comments on commit 6bb6a4e

Please sign in to comment.