From 50471bf4fe3d02fbe44806c27e9a0c491002e978 Mon Sep 17 00:00:00 2001 From: ZmnSCPxj Date: Tue, 9 Jan 2018 14:14:42 +0000 Subject: [PATCH] invoice: Make msatoshi field nullable. Note that the database itself has the field nullable (sqlite3 does not even have non-nullable columns) but our in-memory structures did not. --- lightningd/invoice.c | 18 +++++++++++------- lightningd/invoice.h | 2 +- lightningd/peer_htlcs.c | 4 ++-- wallet/wallet.c | 15 +++++++++++++-- 4 files changed, 27 insertions(+), 12 deletions(-) diff --git a/lightningd/invoice.c b/lightningd/invoice.c index 1a9d87f0a..cc0630696 100644 --- a/lightningd/invoice.c +++ b/lightningd/invoice.c @@ -81,7 +81,8 @@ static void tell_waiter(struct command *cmd, const struct invoice *paid) json_object_start(response, NULL); json_add_string(response, "label", paid->label); json_add_hex(response, "rhash", &paid->rhash, sizeof(paid->rhash)); - json_add_u64(response, "msatoshi", paid->msatoshi); + if (paid->msatoshi) + json_add_u64(response, "msatoshi", *paid->msatoshi); json_add_bool(response, "complete", paid->state == PAID); if (paid->state == PAID) json_add_u64(response, "pay_index", paid->pay_index); @@ -196,8 +197,9 @@ static void json_invoice(struct command *cmd, sha256(&invoice->rhash, invoice->r.r, sizeof(invoice->r.r)); - if (!json_tok_u64(buffer, msatoshi, &invoice->msatoshi) - || invoice->msatoshi == 0) { + invoice->msatoshi = tal(invoice, u64); + if (!json_tok_u64(buffer, msatoshi, invoice->msatoshi) + || *invoice->msatoshi == 0) { command_fail(cmd, "'%.*s' is not a valid positive number", msatoshi->end - msatoshi->start, buffer + msatoshi->start); @@ -229,7 +231,7 @@ static void json_invoice(struct command *cmd, wallet_invoice_save(cmd->ld->wallet, invoice); /* Construct bolt11 string. */ - b11 = new_bolt11(cmd, &invoice->msatoshi); + b11 = new_bolt11(cmd, invoice->msatoshi); b11->chain = get_chainparams(cmd->ld); b11->timestamp = time_now().ts.tv_sec; b11->payment_hash = invoice->rhash; @@ -257,7 +259,7 @@ static void json_invoice(struct command *cmd, payment.payment_hash = invoice->rhash; payment.destination = NULL; payment.status = PAYMENT_PENDING; - payment.msatoshi = invoice->msatoshi; + payment.msatoshi = *invoice->msatoshi; payment.timestamp = b11->timestamp; if (!wallet_payment_add(cmd->ld->wallet, &payment)) { @@ -300,7 +302,8 @@ static void json_add_invoices(struct json_result *response, json_object_start(response, NULL); json_add_string(response, "label", i->label); json_add_hex(response, "rhash", &i->rhash, sizeof(i->rhash)); - json_add_u64(response, "msatoshi", i->msatoshi); + if (i->msatoshi) + json_add_u64(response, "msatoshi", *i->msatoshi); json_add_bool(response, "complete", i->state == PAID); json_add_u64(response, "expiry_time", i->expiry_time); json_object_end(response); @@ -366,7 +369,8 @@ static void json_delinvoice(struct command *cmd, json_object_start(response, NULL); json_add_string(response, "label", i->label); json_add_hex(response, "rhash", &i->rhash, sizeof(i->rhash)); - json_add_u64(response, "msatoshi", i->msatoshi); + if (i->msatoshi) + json_add_u64(response, "msatoshi", *i->msatoshi); json_object_end(response); error = delete_invoice(cmd, cmd->ld->wallet, invs, i); diff --git a/lightningd/invoice.h b/lightningd/invoice.h index 9bf95cb5e..2695f3608 100644 --- a/lightningd/invoice.h +++ b/lightningd/invoice.h @@ -21,7 +21,7 @@ struct invoice { u64 id; enum invoice_status state; const char *label; - u64 msatoshi; + u64 *msatoshi; struct preimage r; u64 expiry_time; struct sha256 rhash; diff --git a/lightningd/peer_htlcs.c b/lightningd/peer_htlcs.c index 538cbe60d..a68709e75 100644 --- a/lightningd/peer_htlcs.c +++ b/lightningd/peer_htlcs.c @@ -269,10 +269,10 @@ static void handle_localpay(struct htlc_in *hin, * * 1. type: PERM|16 (`incorrect_payment_amount`) */ - if (hin->msatoshi < invoice->msatoshi) { + if (invoice->msatoshi != NULL && hin->msatoshi < *invoice->msatoshi) { failcode = WIRE_INCORRECT_PAYMENT_AMOUNT; goto fail; - } else if (hin->msatoshi > invoice->msatoshi * 2) { + } else if (invoice->msatoshi != NULL && hin->msatoshi > *invoice->msatoshi * 2) { failcode = WIRE_INCORRECT_PAYMENT_AMOUNT; goto fail; } diff --git a/wallet/wallet.c b/wallet/wallet.c index 93626b0e6..049470bf6 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -1236,7 +1236,11 @@ void wallet_invoice_save(struct wallet *wallet, struct invoice *inv) sqlite3_bind_blob(stmt, 1, &inv->rhash, sizeof(inv->rhash), SQLITE_TRANSIENT); sqlite3_bind_blob(stmt, 2, &inv->r, sizeof(inv->r), SQLITE_TRANSIENT); sqlite3_bind_int(stmt, 3, inv->state); - sqlite3_bind_int64(stmt, 4, inv->msatoshi); + if (inv->msatoshi) { + sqlite3_bind_int64(stmt, 4, *inv->msatoshi); + } else { + sqlite3_bind_null(stmt, 4); + } sqlite3_bind_text(stmt, 5, inv->label, strlen(inv->label), SQLITE_TRANSIENT); sqlite3_bind_int64(stmt, 6, inv->expiry_time); if (inv->state == PAID) { @@ -1287,7 +1291,14 @@ static bool wallet_stmt2invoice(sqlite3_stmt *stmt, struct invoice *inv) memcpy(&inv->rhash, sqlite3_column_blob(stmt, 3), sqlite3_column_bytes(stmt, 3)); inv->label = tal_strndup(inv, sqlite3_column_blob(stmt, 4), sqlite3_column_bytes(stmt, 4)); - inv->msatoshi = sqlite3_column_int64(stmt, 5); + + if (sqlite3_column_type(stmt, 5) != SQLITE_NULL) { + inv->msatoshi = tal(inv, u64); + *inv->msatoshi = sqlite3_column_int64(stmt, 5); + } else { + inv->msatoshi = NULL; + } + inv->expiry_time = sqlite3_column_int64(stmt, 6); /* Correctly 0 if pay_index is NULL. */ inv->pay_index = sqlite3_column_int64(stmt, 7);