Skip to content

Commit

Permalink
delpay: refactoring logic inside the command and update command doc
Browse files Browse the repository at this point in the history
Changelog-Added: JSON-RPC: delpay a new method
to delete the payment completed or failed.

Signed-off-by: Vincenzo Palazzo <[email protected]>
  • Loading branch information
vincenzopalazzo committed Aug 7, 2020
1 parent 01a82d3 commit 8fef912
Show file tree
Hide file tree
Showing 9 changed files with 393 additions and 1 deletion.
1 change: 1 addition & 0 deletions common/jsonrpc_errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ static const errcode_t PAY_INVOICE_EXPIRED = 207;
static const errcode_t PAY_NO_SUCH_PAYMENT = 208;
static const errcode_t PAY_UNSPECIFIED_ERROR = 209;
static const errcode_t PAY_STOPPED_RETRYING = 210;
static const errcode_t PAY_STATUS_UNEXPECTED = 211;

/* `fundchannel` or `withdraw` errors */
static const errcode_t FUND_MAX_EXCEEDED = 300;
Expand Down
1 change: 1 addition & 0 deletions doc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ MANPAGES := doc/lightning-cli.1 \
doc/lightning-decodepay.7 \
doc/lightning-delexpiredinvoice.7 \
doc/lightning-delinvoice.7 \
doc/lightning-delpay.7 \
doc/lightning-dev-sendcustommsg.7 \
doc/lightning-disconnect.7 \
doc/lightning-feerates.7 \
Expand Down
1 change: 1 addition & 0 deletions doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ c-lightning Documentation
lightning-decodepay <lightning-decodepay.7.md>
lightning-delexpiredinvoice <lightning-delexpiredinvoice.7.md>
lightning-delinvoice <lightning-delinvoice.7.md>
lightning-delpay <lightning-delpay.7.md>
lightning-dev-sendcustommsg <lightning-dev-sendcustommsg.7.md>
lightning-disconnect <lightning-disconnect.7.md>
lightning-feerates <lightning-feerates.7.md>
Expand Down
89 changes: 89 additions & 0 deletions doc/lightning-delpay.7

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

83 changes: 83 additions & 0 deletions doc/lightning-delpay.7.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
lightning-delpay -- Command for removing a completed or failed payment
============================================================

SYNOPSIS
--------

**delpay** *payment_hash* \[status\]

DESCRIPTION
-----------

The **delpay** RPC command removes a payment as given in **listsendpays** or **listpays** with a complete or failed
status. However, the command doesn't permit to remove a pending payment.

- *payment_hash*: Rapresents the unique identifier of a payment. To find it, the caller can run **listpays** or **listsendpays**;
- *status*: Represents a payment status. Otherwise, if it is not specified it is equal to *complete*.

EXAMPLE JSON REQUEST
------------
```json
{
"id": 82,
"method": "delpay",
"params": {
"payment_hash": "4fa2f1b001067ec06d7f95b8695b8acd9ef04c1b4d1110e3b94e1fa0687bb1e0",
"status": "complete"
}
}
```

RETURN VALUE
------------

On success, the command will return a payment object, such as the **listsendpays**. In addition, if the payment is a MPP (Multi part payment) the command return a list of
payments; a payment object for each partid.

On failure, an error is returned and any payment is deleted. If the lightning process fails before responding, the
caller should use lightning-listsentpays(7) or lightning-listpays(7) to query whether this payment was deleted or not.

The following error codes may occur:

- -32602: Some parameter missed or some parameter is malformed;
- 211: Payment with payment_hash have a wrong status. To check the correct status run the command **paystatus**;
- 208: Payment with payment_hash not found.

EXAMPLE JSON RESPONSE
-----
```json
{
"payments": [
{
"id": 2,
"payment_hash": "8dfd6538eeb33811c9114a75f792a143728d7f05643f38c3d574d3097e8910c0",
"destination": "0219f8900ee78a89f050c24d8b69492954f9fdbabed753710845eb75d3a75a5880",
"msatoshi": 1000,
"amount_msat": "1000msat",
"msatoshi_sent": 1000,
"amount_sent_msat": "1000msat",
"created_at": 1596224858,
"status": "complete",
"payment_preimage": "35bd4e2b481a1a84a22215b5372672cf81460a671816960ddb206464359e1822",
"bolt11": "lntb10n1p0jga20pp53h7k2w8wkvuprjg3ff6l0y4pgdeg6lc9vsln3s74wnfsjl5fzrqqdqdw3jhxazldahx2xqyjw5qcqp2sp5wut5jnhr6n7jd5747ky2g5flmw7hgx9yjnqzu60ps2jf6f7tc0us9qy9qsqu2a0k37nckl62005p69xavlkydkvhnypk4dphffy4x09zltwh9437ad7xkl83tefdarzhu5t30ju5s56wlrg97qkx404pq3srfc425cq3ke9af"
}
]
}

```


AUTHOR
------

Vincenzo Palazzo <<[email protected]>> is mainly responsible.

SEE ALSO
--------

lightning-listpays(7), lightning-listsendpays(7), lightning-paystatus(7).

RESOURCES
---------

Main web site: <https://github.com/ElementsProject/lightning>
96 changes: 96 additions & 0 deletions lightningd/pay.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,31 @@ struct sendpay_command {
struct command *cmd;
};

static enum wallet_payment_status string_to_payment_status(const char *status_str)
{
if (streq(status_str, "complete")) {
return PAYMENT_COMPLETE;
} else if (streq(status_str, "failed")) {
return PAYMENT_FAILED;
} else if (streq(status_str, "pending")) {
return PAYMENT_PENDING;
}
return -1;
}

static const char *payment_status_to_string(const enum wallet_payment_status status)
{
switch (status) {
case PAYMENT_COMPLETE:
return "complete";
case PAYMENT_FAILED:
return "failed";
default:
return "pending";
}
}


static void destroy_sendpay_command(struct sendpay_command *pc)
{
list_del(&pc->list);
Expand Down Expand Up @@ -1496,6 +1521,77 @@ static const struct json_command listsendpays_command = {
};
AUTODATA(json_command, &listsendpays_command);


static struct command_result *json_delpay(struct command *cmd,
const char *buffer,
const jsmntok_t *obj UNNEEDED,
const jsmntok_t *params)
{
struct json_stream *response;
const struct wallet_payment **payments;
const char *status_str;
enum wallet_payment_status status;
struct sha256 *payment_hash;

if (!param(cmd, buffer, params,
p_req("payment_hash", param_sha256, &payment_hash),
p_opt("status", param_string, &status_str),
NULL))
return command_param_failed();

if (!payment_hash)
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"missing required parameter: payment_hash");


status = status_str == NULL ? PAYMENT_COMPLETE : string_to_payment_status(status_str);

if (status == -1)
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"status insert %s wrong", status_str);

if (status == PAYMENT_PENDING)
return command_fail(cmd, PAY_STATUS_UNEXPECTED, "invalid status: %s",
payment_status_to_string(status));

payments = wallet_payment_list(cmd, cmd->ld->wallet, payment_hash);

if (tal_count(payments) == 0)
return command_fail(cmd, PAY_NO_SUCH_PAYMENT,
"unknown payment with payment_hash: %s",
type_to_string(tmpctx, struct sha256, payment_hash));

for (int i = 0; i < tal_count(payments); i++) {
if (payments[i]->status != status) {
return command_fail(cmd, PAY_STATUS_UNEXPECTED,
"payment with hash %s has %s status but it should be %s",
type_to_string(tmpctx, struct sha256, payment_hash),
payment_status_to_string(payments[i]->status),
payment_status_to_string(status));
}
}

wallet_payment_delete_by_hash(cmd->ld->wallet, payment_hash);

response = json_stream_success(cmd);
json_array_start(response, "payments");
for (int i = 0; i < tal_count(payments); i++) {
json_object_start(response, NULL);
json_add_payment_fields(response, payments[i]);
json_object_end(response);
}
json_array_end(response);
return command_success(cmd, response);
}

static const struct json_command delpay_command = {
"delpay",
"payment",
json_delpay,
"Delete payment with {payment_hash} and {status}",
};
AUTODATA(json_command, &delpay_command);

static struct command_result *json_createonion(struct command *cmd,
const char *buffer,
const jsmntok_t *obj UNNEEDED,
Expand Down
Loading

0 comments on commit 8fef912

Please sign in to comment.