From 41221b6ecbdfaff5a305a98d07b0bb4cac9d9481 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Thu, 7 Nov 2019 23:13:29 +0100 Subject: [PATCH] pay: Make wallet_payment->destination optional If we use `sendonion` we don't actually know the destination, so we make the destination a pointer which is NULL if we don't know. --- lightningd/pay.c | 12 ++++++++---- wallet/test/run-wallet.c | 7 ++++--- wallet/wallet.c | 15 +++++++++++++-- wallet/wallet.h | 4 +++- 4 files changed, 28 insertions(+), 10 deletions(-) diff --git a/lightningd/pay.c b/lightningd/pay.c index 5f1828581..ab46d79a5 100644 --- a/lightningd/pay.c +++ b/lightningd/pay.c @@ -80,7 +80,9 @@ void json_add_payment_fields(struct json_stream *response, { json_add_u64(response, "id", t->id); json_add_sha256(response, "payment_hash", &t->payment_hash); - json_add_node_id(response, "destination", &t->destination); + if (t->destination != NULL) + json_add_node_id(response, "destination", t->destination); + json_add_amount_msat_compat(response, t->msatoshi, "msatoshi", "amount_msat"); json_add_amount_msat_compat(response, t->msatoshi_sent, @@ -738,12 +740,14 @@ send_payment(struct lightningd *ld, struct amount_msat, &payment->msatoshi)); } - if (!node_id_eq(&payment->destination, &ids[n_hops-1])) { + if (payment->destination && + !node_id_eq(payment->destination, + &ids[n_hops - 1])) { return command_fail(cmd, PAY_RHASH_ALREADY_USED, "Already succeeded to %s", type_to_string(tmpctx, struct node_id, - &payment->destination)); + payment->destination)); } return sendpay_success(cmd, payment); } @@ -800,7 +804,7 @@ send_payment(struct lightningd *ld, payment = tal(hout, struct wallet_payment); payment->id = 0; payment->payment_hash = *rhash; - payment->destination = ids[n_hops - 1]; + payment->destination = tal_dup(payment, struct node_id, &ids[n_hops - 1]); payment->status = PAYMENT_PENDING; payment->msatoshi = msat; payment->msatoshi_sent = route[0].amount; diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index 76cbc1878..4a162a8c5 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -1248,7 +1248,8 @@ static bool test_payment_crud(struct lightningd *ld, const tal_t *ctx) struct wallet *w = create_test_wallet(ld, ctx); mempat(t, sizeof(*t)); - memset(&t->destination, 2, sizeof(t->destination)); + t->destination = tal(t, struct node_id); + memset(t->destination, 2, sizeof(struct node_id)); t->id = 0; t->msatoshi = AMOUNT_MSAT(100); @@ -1263,7 +1264,7 @@ static bool test_payment_crud(struct lightningd *ld, const tal_t *ctx) t2 = wallet_payment_by_hash(ctx, w, &t->payment_hash); CHECK(t2 != NULL); CHECK(t2->status == t->status); - CHECK(node_id_cmp(&t2->destination, &t->destination) == 0); + CHECK(node_id_cmp(t2->destination, t->destination) == 0); CHECK(amount_msat_eq(t2->msatoshi, t->msatoshi)); CHECK(amount_msat_eq(t2->msatoshi_sent, t->msatoshi_sent)); CHECK(!t2->payment_preimage); @@ -1276,7 +1277,7 @@ static bool test_payment_crud(struct lightningd *ld, const tal_t *ctx) t2 = wallet_payment_by_hash(ctx, w, &t->payment_hash); CHECK(t2 != NULL); CHECK(t2->status == t->status); - CHECK(node_id_eq(&t2->destination, &t->destination)); + CHECK(node_id_eq(t2->destination, t->destination)); CHECK(amount_msat_eq(t2->msatoshi, t->msatoshi)); CHECK(amount_msat_eq(t2->msatoshi_sent, t->msatoshi_sent)); CHECK(preimage_eq(t->payment_preimage, t2->payment_preimage)); diff --git a/wallet/wallet.c b/wallet/wallet.c index 7edd2ddc2..70de1a782 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -2129,7 +2129,12 @@ void wallet_payment_store(struct wallet *wallet, db_bind_int(stmt, 0, payment->status); db_bind_sha256(stmt, 1, &payment->payment_hash); - db_bind_node_id(stmt, 2, &payment->destination); + + if (payment->destination != NULL) + db_bind_node_id(stmt, 2, payment->destination); + else + db_bind_null(stmt, 2); + db_bind_amount_msat(stmt, 3, &payment->msatoshi); db_bind_int(stmt, 4, payment->timestamp); @@ -2189,7 +2194,13 @@ static struct wallet_payment *wallet_stmt2payment(const tal_t *ctx, payment->id = db_column_u64(stmt, 0); payment->status = db_column_int(stmt, 1); - db_column_node_id(stmt, 2, &payment->destination); + if (!db_column_is_null(stmt, 2)) { + payment->destination = tal(payment, struct node_id); + db_column_node_id(stmt, 2, payment->destination); + } else { + payment->destination = NULL; + } + db_column_amount_msat(stmt, 3, &payment->msatoshi); db_column_sha256(stmt, 4, &payment->payment_hash); diff --git a/wallet/wallet.h b/wallet/wallet.h index dafb1085b..c8a51e937 100644 --- a/wallet/wallet.h +++ b/wallet/wallet.h @@ -251,7 +251,9 @@ struct wallet_payment { u32 timestamp; struct sha256 payment_hash; enum wallet_payment_status status; - struct node_id destination; + + /* The destination may not be known if we used `sendonion` */ + struct node_id *destination; struct amount_msat msatoshi; struct amount_msat msatoshi_sent; /* If and only if PAYMENT_COMPLETE */