mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-19 23:24:27 +01:00
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.
This commit is contained in:
@@ -81,7 +81,8 @@ static void tell_waiter(struct command *cmd, const struct invoice *paid)
|
|||||||
json_object_start(response, NULL);
|
json_object_start(response, NULL);
|
||||||
json_add_string(response, "label", paid->label);
|
json_add_string(response, "label", paid->label);
|
||||||
json_add_hex(response, "rhash", &paid->rhash, sizeof(paid->rhash));
|
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);
|
json_add_bool(response, "complete", paid->state == PAID);
|
||||||
if (paid->state == PAID)
|
if (paid->state == PAID)
|
||||||
json_add_u64(response, "pay_index", paid->pay_index);
|
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));
|
sha256(&invoice->rhash, invoice->r.r, sizeof(invoice->r.r));
|
||||||
|
|
||||||
if (!json_tok_u64(buffer, msatoshi, &invoice->msatoshi)
|
invoice->msatoshi = tal(invoice, u64);
|
||||||
|| invoice->msatoshi == 0) {
|
if (!json_tok_u64(buffer, msatoshi, invoice->msatoshi)
|
||||||
|
|| *invoice->msatoshi == 0) {
|
||||||
command_fail(cmd, "'%.*s' is not a valid positive number",
|
command_fail(cmd, "'%.*s' is not a valid positive number",
|
||||||
msatoshi->end - msatoshi->start,
|
msatoshi->end - msatoshi->start,
|
||||||
buffer + msatoshi->start);
|
buffer + msatoshi->start);
|
||||||
@@ -229,7 +231,7 @@ static void json_invoice(struct command *cmd,
|
|||||||
wallet_invoice_save(cmd->ld->wallet, invoice);
|
wallet_invoice_save(cmd->ld->wallet, invoice);
|
||||||
|
|
||||||
/* Construct bolt11 string. */
|
/* Construct bolt11 string. */
|
||||||
b11 = new_bolt11(cmd, &invoice->msatoshi);
|
b11 = new_bolt11(cmd, invoice->msatoshi);
|
||||||
b11->chain = get_chainparams(cmd->ld);
|
b11->chain = get_chainparams(cmd->ld);
|
||||||
b11->timestamp = time_now().ts.tv_sec;
|
b11->timestamp = time_now().ts.tv_sec;
|
||||||
b11->payment_hash = invoice->rhash;
|
b11->payment_hash = invoice->rhash;
|
||||||
@@ -257,7 +259,7 @@ static void json_invoice(struct command *cmd,
|
|||||||
payment.payment_hash = invoice->rhash;
|
payment.payment_hash = invoice->rhash;
|
||||||
payment.destination = NULL;
|
payment.destination = NULL;
|
||||||
payment.status = PAYMENT_PENDING;
|
payment.status = PAYMENT_PENDING;
|
||||||
payment.msatoshi = invoice->msatoshi;
|
payment.msatoshi = *invoice->msatoshi;
|
||||||
payment.timestamp = b11->timestamp;
|
payment.timestamp = b11->timestamp;
|
||||||
|
|
||||||
if (!wallet_payment_add(cmd->ld->wallet, &payment)) {
|
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_object_start(response, NULL);
|
||||||
json_add_string(response, "label", i->label);
|
json_add_string(response, "label", i->label);
|
||||||
json_add_hex(response, "rhash", &i->rhash, sizeof(i->rhash));
|
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_bool(response, "complete", i->state == PAID);
|
||||||
json_add_u64(response, "expiry_time", i->expiry_time);
|
json_add_u64(response, "expiry_time", i->expiry_time);
|
||||||
json_object_end(response);
|
json_object_end(response);
|
||||||
@@ -366,7 +369,8 @@ static void json_delinvoice(struct command *cmd,
|
|||||||
json_object_start(response, NULL);
|
json_object_start(response, NULL);
|
||||||
json_add_string(response, "label", i->label);
|
json_add_string(response, "label", i->label);
|
||||||
json_add_hex(response, "rhash", &i->rhash, sizeof(i->rhash));
|
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);
|
json_object_end(response);
|
||||||
|
|
||||||
error = delete_invoice(cmd, cmd->ld->wallet, invs, i);
|
error = delete_invoice(cmd, cmd->ld->wallet, invs, i);
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ struct invoice {
|
|||||||
u64 id;
|
u64 id;
|
||||||
enum invoice_status state;
|
enum invoice_status state;
|
||||||
const char *label;
|
const char *label;
|
||||||
u64 msatoshi;
|
u64 *msatoshi;
|
||||||
struct preimage r;
|
struct preimage r;
|
||||||
u64 expiry_time;
|
u64 expiry_time;
|
||||||
struct sha256 rhash;
|
struct sha256 rhash;
|
||||||
|
|||||||
@@ -269,10 +269,10 @@ static void handle_localpay(struct htlc_in *hin,
|
|||||||
*
|
*
|
||||||
* 1. type: PERM|16 (`incorrect_payment_amount`)
|
* 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;
|
failcode = WIRE_INCORRECT_PAYMENT_AMOUNT;
|
||||||
goto fail;
|
goto fail;
|
||||||
} else if (hin->msatoshi > invoice->msatoshi * 2) {
|
} else if (invoice->msatoshi != NULL && hin->msatoshi > *invoice->msatoshi * 2) {
|
||||||
failcode = WIRE_INCORRECT_PAYMENT_AMOUNT;
|
failcode = WIRE_INCORRECT_PAYMENT_AMOUNT;
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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, 1, &inv->rhash, sizeof(inv->rhash), SQLITE_TRANSIENT);
|
||||||
sqlite3_bind_blob(stmt, 2, &inv->r, sizeof(inv->r), SQLITE_TRANSIENT);
|
sqlite3_bind_blob(stmt, 2, &inv->r, sizeof(inv->r), SQLITE_TRANSIENT);
|
||||||
sqlite3_bind_int(stmt, 3, inv->state);
|
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_text(stmt, 5, inv->label, strlen(inv->label), SQLITE_TRANSIENT);
|
||||||
sqlite3_bind_int64(stmt, 6, inv->expiry_time);
|
sqlite3_bind_int64(stmt, 6, inv->expiry_time);
|
||||||
if (inv->state == PAID) {
|
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));
|
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->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);
|
inv->expiry_time = sqlite3_column_int64(stmt, 6);
|
||||||
/* Correctly 0 if pay_index is NULL. */
|
/* Correctly 0 if pay_index is NULL. */
|
||||||
inv->pay_index = sqlite3_column_int64(stmt, 7);
|
inv->pay_index = sqlite3_column_int64(stmt, 7);
|
||||||
|
|||||||
Reference in New Issue
Block a user