#ifndef LIGHTNING_PLUGINS_LIBPLUGIN_PAY_H
#define LIGHTNING_PLUGINS_LIBPLUGIN_PAY_H
#include "config.h"
#include <ccan/io/io.h>
#include <common/bolt11.h>
#include <common/route.h>
#include <plugins/channel_hint.h>
#include <plugins/libplugin.h>
#include <wire/onion_wire.h>
struct legacy_payload {
struct short_channel_id scid;
struct amount_msat forward_amt;
u32 outgoing_cltv;
};
struct createonion_hop {
struct node_id pubkey;
struct tlv_payload *tlv_payload;
};
struct createonion_request {
struct createonion_hop *hops;
u8 *assocdata;
struct secret *session_key;
};
enum payment_result_state {
PAYMENT_PENDING = 1,
PAYMENT_COMPLETE = 2,
PAYMENT_FAILED = 4,
};
struct payment_result {
u64 id;
u32 partid;
enum payment_result_state state;
struct amount_msat amount_sent;
struct preimage *payment_preimage;
u32 code;
const char* failcodename;
enum onion_wire failcode;
const u8 *raw_message;
const char *message;
u32 *erring_index;
struct node_id *erring_node;
struct short_channel_id *erring_channel;
int *erring_direction;
};
struct local_hint {
u16 htlc_budget;
};
enum payment_step {
PAYMENT_STEP_INITIALIZED = 1,
PAYMENT_STEP_GOT_ROUTE = 2,
PAYMENT_STEP_RETRY_GETROUTE = 3,
PAYMENT_STEP_ONION_PAYLOAD = 4,
PAYMENT_STEP_SPLIT = 8,
PAYMENT_STEP_RETRY = 16,
PAYMENT_STEP_FAILED = 32,
PAYMENT_STEP_SUCCESS = 64,
};
struct payment_tree_result {
enum payment_step leafstates;
enum payment_step treestates;
struct amount_msat sent;
struct preimage *preimage;
u32 attempts;
struct payment_result *failure;
};
struct getroute_request {
struct node_id *destination;
struct amount_msat amount;
u32 cltv;
u32 max_hops;
u64 riskfactorppm;
};
struct payment_constraints {
struct amount_msat fee_budget;
u32 cltv_budget;
};
struct payment {
struct list_node list;
struct command *cmd;
bool finished;
struct plugin *plugin;
struct node_id *local_id;
const char *json_buffer;
const jsmntok_t *json_toks;
enum payment_step step;
struct node_id *route_destination;
struct node_id *pay_destination;
struct sha256 *payment_hash;
struct secret *payment_secret;
u8 *payment_metadata;
struct blinded_path *blindedpath;
struct blinded_payinfo *blindedpay;
struct amount_msat blindedouramount;
struct amount_msat blindedfinalamount;
u64 groupid;
u32 partid;
u32 next_partid;
struct getroute_request *getroute;
struct createonion_request *createonion_request;
struct createonion_response *createonion_response;
struct amount_msat final_amount;
struct amount_msat our_amount;
struct route_hop *route;
struct channel_status *peer_channels;
u32 start_block;
struct timeabs start_time, end_time;
struct timeabs deadline;
struct payment_constraints constraints;
struct payment_constraints *start_constraints;
struct short_channel_id *exclusions;
struct gossmap_localmods *mods;
struct payment *parent, **children;
struct payment_modifier **modifiers;
void **modifier_data;
int current_modifier;
u32 min_final_cltv_expiry;
struct route_info **routes;
const u8 *features;
struct channel_hint_set *hints;
struct node_id *excluded_nodes;
struct node_id *temp_exclusion;
struct payment_result *result;
bool abort;
bool invstring_used;
const char *invstring;
const char *description;
struct sha256 *local_invreq_id;
const char *why;
const char *label;
const char *failreason;
bool failroute_retry;
u64 id;
char *routetxt;
u32 max_htlcs;
char *aborterror;
u32 errorcode;
u32 chainlag;
void (*on_payment_success)(struct payment *p);
void (*on_payment_failure)(struct payment *p);
};
struct payment_modifier {
const char *name;
void *(*data_init)(struct payment *p);
struct command_result *(*post_step_cb)(void *data, struct payment *p);
};
void *payment_mod_get_data(const struct payment *payment,
const struct payment_modifier *mod);
#define REGISTER_PAYMENT_MODIFIER(name, data_type, data_init_cb, step_cb) \
struct payment_modifier name##_pay_mod = { \
stringify(name), \
typesafe_cb_cast(void *(*)(struct payment *), \
data_type (*)(struct payment *), data_init_cb), \
typesafe_cb_cast(struct command_result *(*)(void *, struct payment *), \
struct command_result *(*)(data_type, struct payment *), step_cb), \
};
#define REGISTER_PAYMENT_MODIFIER_HEADER(name, data_type) \
extern struct payment_modifier name##_pay_mod; \
UNUSED static inline data_type *payment_mod_##name##_get_data( \
const struct payment *p) \
{ \
return payment_mod_get_data(p, &name##_pay_mod); \
}
struct retry_mod_data {
int retries;
};
struct routehints_data {
const char *routehint_modifications;
struct route_info **routehints;
struct route_info *current_routehint;
int offset;
int base;
u32 final_cltv;
bool destination_reachable;
};
struct exemptfee_data {
struct amount_msat amount;
};
struct shadow_route_data {
bool use_shadow;
struct payment_constraints constraints;
struct node_id destination;
struct route_hop *route;
bool fuzz_amount;
};
struct direct_pay_data {
struct short_channel_id_dir *chan;
};
struct presplit_mod_data {
bool disable;
};
struct adaptive_split_mod_data {
bool disable;
u32 htlc_budget;
};
struct route_exclusions_data {
struct route_exclusion **exclusions;
};
REGISTER_PAYMENT_MODIFIER_HEADER(retry, struct retry_mod_data);
REGISTER_PAYMENT_MODIFIER_HEADER(routehints, struct routehints_data);
REGISTER_PAYMENT_MODIFIER_HEADER(exemptfee, struct exemptfee_data);
REGISTER_PAYMENT_MODIFIER_HEADER(shadowroute, struct shadow_route_data);
REGISTER_PAYMENT_MODIFIER_HEADER(directpay, struct direct_pay_data);
REGISTER_PAYMENT_MODIFIER_HEADER(presplit, struct presplit_mod_data);
REGISTER_PAYMENT_MODIFIER_HEADER(adaptive_splitter, struct adaptive_split_mod_data);
REGISTER_PAYMENT_MODIFIER_HEADER(local_channel_hints, void);
REGISTER_PAYMENT_MODIFIER_HEADER(payee_incoming_limit, void);
REGISTER_PAYMENT_MODIFIER_HEADER(route_exclusions, struct route_exclusions_data);
struct payment *payment_new(tal_t *ctx, struct command *cmd,
struct payment *parent,
struct channel_hint_set *channel_hints,
struct payment_modifier **mods);
void payment_start(struct payment *p);
struct command_result *payment_continue(struct payment *p);
void payment_set_step(struct payment *p, enum payment_step newstep);
struct command_result *payment_fail(struct payment *p, const char *fmt, ...) PRINTF_FMT(2,3);
struct command_result *payment_abort(struct payment *p, enum jsonrpc_errcode code, const char *fmt, ...) PRINTF_FMT(3,4);
struct payment *payment_root(struct payment *p);
struct payment_tree_result payment_collect_result(struct payment *p);
struct gossmap *get_raw_gossmap(struct payment *payment);
void json_add_payment_success(struct json_stream *js,
struct payment *p,
const struct preimage *preimage,
const struct payment_tree_result *result);
int libplugin_pay_poll(struct pollfd *fds, nfds_t nfds, int timeout);
void
paymod_log(struct payment *p, enum log_level l, const char *fmt, ...);
#endif