Browse Source

lightningd: move pay internals back into pay.c

Now we don't have a second caller for these routines, we can move
them back into pay.c and make the functions static.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
sanitizers
Rusty Russell 6 years ago
committed by Christian Decker
parent
commit
a45a62aff6
  1. 31
      lightningd/json.c
  2. 5
      lightningd/json.h
  3. 77
      lightningd/pay.c
  4. 84
      lightningd/pay.h

31
lightningd/json.c

@ -49,37 +49,6 @@ json_add_route(struct json_stream *r, char const *n,
json_array_end(r);
}
/* Outputs fields, not a separate object*/
void
json_add_payment_fields(struct json_stream *response,
const struct wallet_payment *t)
{
json_add_u64(response, "id", t->id);
json_add_hex(response, "payment_hash", &t->payment_hash, sizeof(t->payment_hash));
json_add_pubkey(response, "destination", &t->destination);
json_add_u64(response, "msatoshi", t->msatoshi);
json_add_u64(response, "msatoshi_sent", t->msatoshi_sent);
json_add_u64(response, "created_at", t->timestamp);
switch (t->status) {
case PAYMENT_PENDING:
json_add_string(response, "status", "pending");
break;
case PAYMENT_COMPLETE:
json_add_string(response, "status", "complete");
break;
case PAYMENT_FAILED:
json_add_string(response, "status", "failed");
break;
}
if (t->payment_preimage)
json_add_hex(response, "payment_preimage",
t->payment_preimage,
sizeof(*t->payment_preimage));
if (t->description)
json_add_string(response, "description", t->description);
}
void json_add_pubkey(struct json_stream *response,
const char *fieldname,
const struct pubkey *key)

5
lightningd/json.h

@ -33,11 +33,6 @@ struct wireaddr_internal;
void json_add_route(struct json_stream *r, char const *n,
const struct route_hop *hops, size_t hops_len);
/* Output the fields of a wallet payment.
* Should be used within an object context. */
void json_add_payment_fields(struct json_stream *response,
const struct wallet_payment *t);
/* '"fieldname" : "0289abcdef..."' or "0289abcdef..." if fieldname is NULL */
void json_add_pubkey(struct json_stream *response,
const char *fieldname,

77
lightningd/pay.c

@ -18,9 +18,37 @@
#include <lightningd/subd.h>
#include <sodium/randombytes.h>
/*-----------------------------------------------------------------------------
Internal sendpay interface
-----------------------------------------------------------------------------*/
/* Routing failure object */
struct routing_failure {
unsigned int erring_index;
enum onion_type failcode;
struct pubkey erring_node;
struct short_channel_id erring_channel;
int channel_dir;
};
/* Result of send_payment */
struct sendpay_result {
/* Did the payment succeed? */
bool succeeded;
/* Preimage. Only loaded if payment succeeded. */
struct preimage preimage;
/* Error code, one of the PAY_* macro in jsonrpc_errors.h.
* Only loaded if payment failed. */
int errorcode;
/* Pointer to the payment. Only loaded if payment
* succeeded or if error is PAY_IN_PROGRESS */
const struct wallet_payment *payment;
/* Unparseable onion reply. Only loaded if payment failed,
* and errorcode == PAY_UNPARSEABLE_ONION. */
const u8* onionreply;
/* Routing failure object. Only loaded if payment failed,
* and errorcode == PAY_DESTINATION_PERM_FAIL or
* errorcode == PAY_TRY_OTHER_ROUTE */
struct routing_failure* routing_failure;
/* Error message. Only loaded if payment failed. */
const char *details;
};
/* sendpay command */
struct sendpay_command {
@ -463,11 +491,11 @@ void payment_failed(struct lightningd *ld, const struct htlc_out *hout,
* no longer be called.
* Return false if we called callback already, true if
* callback is scheduled for later. */
bool wait_payment(const tal_t *cxt,
struct lightningd *ld,
const struct sha256 *payment_hash,
void (*cb)(const struct sendpay_result *, void*),
void *cbarg)
static bool wait_payment(const tal_t *cxt,
struct lightningd *ld,
const struct sha256 *payment_hash,
void (*cb)(const struct sendpay_result *, void*),
void *cbarg)
{
struct wallet_payment *payment;
struct sendpay_result *result;
@ -558,7 +586,7 @@ end:
}
/* Returns false if cb was called, true if cb not yet called. */
bool
static bool
send_payment(const tal_t *ctx,
struct lightningd* ld,
const struct sha256 *rhash,
@ -742,6 +770,37 @@ send_payment(const tal_t *ctx,
JSON-RPC sendpay interface
-----------------------------------------------------------------------------*/
/* Outputs fields, not a separate object*/
static void
json_add_payment_fields(struct json_stream *response,
const struct wallet_payment *t)
{
json_add_u64(response, "id", t->id);
json_add_hex(response, "payment_hash", &t->payment_hash, sizeof(t->payment_hash));
json_add_pubkey(response, "destination", &t->destination);
json_add_u64(response, "msatoshi", t->msatoshi);
json_add_u64(response, "msatoshi_sent", t->msatoshi_sent);
json_add_u64(response, "created_at", t->timestamp);
switch (t->status) {
case PAYMENT_PENDING:
json_add_string(response, "status", "pending");
break;
case PAYMENT_COMPLETE:
json_add_string(response, "status", "complete");
break;
case PAYMENT_FAILED:
json_add_string(response, "status", "failed");
break;
}
if (t->payment_preimage)
json_add_hex(response, "payment_preimage",
t->payment_preimage,
sizeof(*t->payment_preimage));
if (t->description)
json_add_string(response, "description", t->description);
}
static void
json_sendpay_success(struct command *cmd,
const struct sendpay_result *r)

84
lightningd/pay.h

@ -1,87 +1,11 @@
#ifndef LIGHTNING_LIGHTNINGD_PAY_H
#define LIGHTNING_LIGHTNINGD_PAY_H
#include "config.h"
#include <bitcoin/preimage.h>
#include <bitcoin/pubkey.h>
#include <bitcoin/short_channel_id.h>
#include <wire/gen_onion_wire.h>
struct htlc_out;
struct json_stream;
struct lightningd;
struct route_hop;
struct preimage;
struct sha256;
struct wallet_payment;
/* Routing failure object */
struct routing_failure {
unsigned int erring_index;
enum onion_type failcode;
struct pubkey erring_node;
struct short_channel_id erring_channel;
int channel_dir;
};
/* Result of send_payment */
struct sendpay_result {
/* Did the payment succeed? */
bool succeeded;
/* Preimage. Only loaded if payment succeeded. */
struct preimage preimage;
/* Error code, one of the PAY_* macro in jsonrpc_errors.h.
* Only loaded if payment failed. */
int errorcode;
/* Pointer to the payment. Only loaded if payment
* succeeded or if error is PAY_IN_PROGRESS */
const struct wallet_payment *payment;
/* Unparseable onion reply. Only loaded if payment failed,
* and errorcode == PAY_UNPARSEABLE_ONION. */
const u8* onionreply;
/* Routing failure object. Only loaded if payment failed,
* and errorcode == PAY_DESTINATION_PERM_FAIL or
* errorcode == PAY_TRY_OTHER_ROUTE */
struct routing_failure* routing_failure;
/* Error message. Only loaded if payment failed. */
const char *details;
};
/* Initiate a payment. Return true if the callback will be
* scheduled for later, or false if the callback has already
* been called. If the given context is freed before the
* callback is called, then the callback will no longer be
* called.
*
* This will call the callback "soon" in 10ms or less.
*
* Typically the callback will be called with a failed
* sendpay_result indicating an error code of PAY_IN_PROGRESS.
* It will only call the callback with successful sendpay_result
* if the payment has already completed with the same amount
* and destination before.
*
* The msatoshi given is what is recorded in the payment. */
bool send_payment(const tal_t *ctx,
struct lightningd* ld,
const struct sha256 *rhash,
const struct route_hop *route,
u64 msatoshi,
const char *description TAKES,
void (*cb)(const struct sendpay_result *, void*),
void *cbarg);
/* Wait for a previous send_payment to complete in definite
* success or failure. If the given context is freed before
* the callback is called, then the callback will no longer
* be called.
*
* Return true if the payment is still pending on return, or
* false if the callback was already invoked before this
* function returned. */
bool wait_payment(const tal_t *ctx,
struct lightningd* ld,
const struct sha256 *payment_hash,
void (*cb)(const struct sendpay_result *, void *cbarg),
void *cbarg);
void payment_succeeded(struct lightningd *ld, struct htlc_out *hout,
const struct preimage *rval);
@ -92,10 +16,4 @@ void payment_failed(struct lightningd *ld, const struct htlc_out *hout,
/* Inform payment system to save the payment. */
void payment_store(struct lightningd *ld, const struct sha256 *payment_hash);
/* Output the fields of a payment. Caller should have put the
* response within a JSON object and is responsible for
* closing the object. */
void json_add_payment_fields(struct json_stream *response,
const struct wallet_payment *t);
#endif /* LIGHTNING_LIGHTNINGD_PAY_H */

Loading…
Cancel
Save