-
Notifications
You must be signed in to change notification settings - Fork 996
offers: add used_count to listoffers #9162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -91,6 +91,12 @@ | |
| "True if an associated invoice has been paid." | ||
| ] | ||
| }, | ||
| "used_count": { | ||
| "type": "u64", | ||
| "description": [ | ||
| "The number of times this offer has been paid." | ||
| ] | ||
| }, | ||
| "label": { | ||
| "type": "string", | ||
| "description": [ | ||
|
|
@@ -129,23 +135,26 @@ | |
| "single_use": false, | ||
| "bolt12": "lno1qgsq000bolt210002100021000210002100021000210002100021000210002100021000210002100021000210002100021000210002100021000210002100021000", | ||
| "description": "Fish sale!", | ||
| "used": false | ||
| "used": false, | ||
| "used_count": 1 | ||
|
Comment on lines
+138
to
+139
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All four examples show |
||
| }, | ||
| { | ||
| "offer_id": "offeridl22000002200000220000022000002200000220000022000002200000", | ||
| "active": true, | ||
| "single_use": false, | ||
| "bolt12": "lno1qgsq000bolt220002200022000220002200022000220002200022000220002200022000220002200022000220002200022000220002200022000220002200022000", | ||
| "description": "Coffee", | ||
| "used": false | ||
| "used": false, | ||
| "used_count": 1 | ||
| }, | ||
| { | ||
| "offer_id": "offeridl23000002300000230000023000002300000230000023000002300000", | ||
| "active": true, | ||
| "single_use": false, | ||
| "bolt12": "lno1qgsq000bolt230002300023000230002300023000230002300023000230002300023000230002300023000230002300023000230002300023000230002300023000", | ||
| "description": "Movie ticket", | ||
| "used": false | ||
| "used": false, | ||
| "used_count": 1 | ||
| } | ||
| ] | ||
| } | ||
|
|
@@ -165,7 +174,8 @@ | |
| "active": true, | ||
| "single_use": false, | ||
| "bolt12": "lno1qgsq000bolt230002300023000230002300023000230002300023000230002300023000230002300023000230002300023000230002300023000230002300023000", | ||
| "used": false | ||
| "used": false, | ||
| "used_count": 1 | ||
| } | ||
| ] | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -179,6 +179,8 @@ static struct command_result *json_listoffers(struct command *cmd, | |
| json_populate_offer(response, | ||
| offer_id, b12, | ||
| label, status, force_paths); | ||
| u64 used_count = wallet_offer_count_payments(wallet, offer_id); | ||
| json_add_u64(response, "used_count", used_count); | ||
| description = offer_description_from_b12(tmpctx, cmd->ld, b12); | ||
| if (description) | ||
| json_add_stringn(response, "description", description, tal_bytelen(description)); | ||
|
|
@@ -198,6 +200,8 @@ static struct command_result *json_listoffers(struct command *cmd, | |
| json_populate_offer(response, | ||
| &id, b12, | ||
| label, status, force_paths); | ||
| u64 used_count = wallet_offer_count_payments(wallet, offer_id); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're passing |
||
| json_add_u64(response, "used_count", used_count); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The indentation here is inconsistent with a mix of spaces and tabs. Please align it with the surrounding code (tabs only).
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're adding this field between |
||
| description = offer_description_from_b12(tmpctx, cmd->ld, b12); | ||
| if (description) | ||
| json_add_stringn(response, "description", description, tal_bytelen(description)); | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -7329,3 +7329,39 @@ def test_createproof_include(node_factory, bitcoind): | |||||||
| # Unknown name is an error. | ||||||||
| with pytest.raises(RpcError, match=r'Unknown field name'): | ||||||||
| l1.rpc.call('createproof', {'invstring': inv, 'include': ['no_such_field']}) | ||||||||
|
|
||||||||
| def test_offer_used_count(node_factory, bitcoind): | ||||||||
| """Verify that the used_count counter increments correctly for BOLT12 offers""" | ||||||||
| l1, l2, l3 = node_factory.line_graph(3, wait_for_announce=True, | ||||||||
| opts=[{}, | ||||||||
| {'dev-allow-localhost': None}, | ||||||||
| {'dev-allow-localhost': None}]) | ||||||||
|
|
||||||||
| # Create a BOLT12 offer on the recipient node (l3) | ||||||||
| offer = l3.rpc.offer('5000msat', 'used_count test') | ||||||||
| offer_id = offer['offer_id'] | ||||||||
|
|
||||||||
| # Verify that the initial usage counter on l3 is equal to 0 | ||||||||
| offers = l3.rpc.listoffers(offer_id=offer_id)['offers'] | ||||||||
| assert len(offers) == 1 | ||||||||
| assert 'used_count' in offers[0], "Error: used_count field is missing from listoffers response!" | ||||||||
| assert offers[0]['used_count'] == 0 | ||||||||
|
Comment on lines
+7347
to
+7348
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's drop the custom message string. The
Suggested change
|
||||||||
|
|
||||||||
| # The sender node (l1) requests an invoice based on l3's offer | ||||||||
| inv = l1.rpc.fetchinvoice(offer['bolt12'])['invoice'] | ||||||||
|
|
||||||||
| # Pay the invoice from l1 using xpay | ||||||||
| l1.rpc.xpay(inv) | ||||||||
|
|
||||||||
| # Verify that l3's database counter increments to 1 after successful settlement | ||||||||
| offers = l3.rpc.listoffers(offer_id=offer_id)['offers'] | ||||||||
| assert offers[0]['used_count'] == 1 | ||||||||
|
|
||||||||
| # Perform a second payment to ensure the aggregate COUNT(*) functions properly | ||||||||
| inv2 = l1.rpc.fetchinvoice(offer['bolt12'])['invoice'] | ||||||||
| l1.rpc.xpay(inv2) | ||||||||
|
|
||||||||
| # Verify that the counter scales up to 2 | ||||||||
| offers = l3.rpc.listoffers(offer_id=offer_id)['offers'] | ||||||||
| assert offers[0]['used_count'] == 2 | ||||||||
|
|
||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -6057,6 +6057,28 @@ char *wallet_offer_find(const tal_t *ctx, | |||||||||||||||
| return bolt12; | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| u64 wallet_offer_count_payments(struct wallet *w, | ||||||||||||||||
| const struct sha256 *offer_id) | ||||||||||||||||
| { | ||||||||||||||||
| struct db_stmt *stmt; | ||||||||||||||||
| u64 count = 0; | ||||||||||||||||
|
|
||||||||||||||||
| stmt = db_prepare_v2(w->db, SQL("SELECT COUNT(*)" | ||||||||||||||||
| " FROM invoices" | ||||||||||||||||
| " WHERE local_offer_id = ?" | ||||||||||||||||
| " AND state = 1;")); | ||||||||||||||||
|
Comment on lines
+6066
to
+6069
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This executes one
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Trailing whitespace: |
||||||||||||||||
|
|
||||||||||||||||
| db_bind_sha256(stmt, offer_id); | ||||||||||||||||
|
Comment on lines
+6069
to
+6071
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The hardcoded
Suggested change
|
||||||||||||||||
| db_query_prepared(stmt); | ||||||||||||||||
|
|
||||||||||||||||
| if (db_step(stmt)) { | ||||||||||||||||
| count = db_col_u64(stmt, "COUNT(*)"); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| tal_free(stmt); | ||||||||||||||||
| return count; | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| struct db_stmt *wallet_offer_id_first(struct wallet *w, struct sha256 *offer_id) | ||||||||||||||||
| { | ||||||||||||||||
| struct db_stmt *stmt; | ||||||||||||||||
|
|
||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1504,6 +1504,20 @@ char *wallet_offer_find(const tal_t *ctx, | |||||||||||||
| bool *force_paths) | ||||||||||||||
| NON_NULL_ARGS(1,2,3); | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * Count the number of successful payments for a given offer. | ||||||||||||||
| * @w: the wallet | ||||||||||||||
| * @offer_id: the merkle root of the offer to check (must be unique) | ||||||||||||||
| * | ||||||||||||||
| * Scans the database invoices table to find all settled (paid) invoices | ||||||||||||||
| * that are associated with the specified @offer_id. | ||||||||||||||
| * | ||||||||||||||
| * Returns the total count of completed payments as a 64-bit unsigned integer. | ||||||||||||||
| */ | ||||||||||||||
| u64 wallet_offer_count_payments(struct wallet *w, | ||||||||||||||
| const struct sha256 *offer_id); | ||||||||||||||
| NON_NULL_ARGS(1,2); | ||||||||||||||
|
Comment on lines
+1517
to
+1519
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should remove the
Suggested change
|
||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * Iterate through all the offers. | ||||||||||||||
| * @w: the wallet | ||||||||||||||
|
|
||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pre-v0.10.1is reserved for fields that existed before versioning was introduced. This is a new field, so it needs the correct release tag. There's no time to include it inv26.06, so let's target the next one:v26.09.