6 namespace org { namespace ldk { namespace structs {
10 * [`Score`] implementation using channel success probability distributions.
12 * Channels are tracked with upper and lower liquidity bounds - when an HTLC fails at a channel,
13 * we learn that the upper-bound on the available liquidity is lower than the amount of the HTLC.
14 * When a payment is forwarded through a channel (but fails later in the route), we learn the
15 * lower-bound on the channel's available liquidity must be at least the value of the HTLC.
17 * These bounds are then used to determine a success probability using the formula from
18 * Optimally Reliable & Cheap Payment Flows on the Lightning Network* by Rene Pickhardt
19 * and Stefan Richter [[1]] (i.e. `(upper_bound - payment_amount) / (upper_bound - lower_bound)`).
21 * This probability is combined with the [`liquidity_penalty_multiplier_msat`] and
22 * [`liquidity_penalty_amount_multiplier_msat`] parameters to calculate a concrete penalty in
23 * milli-satoshis. The penalties, when added across all hops, have the property of being linear in
24 * terms of the entire path's success probability. This allows the router to directly compare
25 * penalties for different paths. See the documentation of those parameters for the exact formulas.
27 * The liquidity bounds are decayed by halving them every [`liquidity_offset_half_life`].
29 * Further, we track the history of our upper and lower liquidity bounds for each channel,
30 * allowing us to assign a second penalty (using [`historical_liquidity_penalty_multiplier_msat`]
31 * and [`historical_liquidity_penalty_amount_multiplier_msat`]) based on the same probability
32 * formula, but using the history of a channel rather than our latest estimates for the liquidity
37 * Mixing the `no-std` feature between serialization and deserialization results in undefined
40 * [1]: https://arxiv.org/abs/2107.05322
41 * [`liquidity_penalty_multiplier_msat`]: ProbabilisticScoringParameters::liquidity_penalty_multiplier_msat
42 * [`liquidity_penalty_amount_multiplier_msat`]: ProbabilisticScoringParameters::liquidity_penalty_amount_multiplier_msat
43 * [`liquidity_offset_half_life`]: ProbabilisticScoringParameters::liquidity_offset_half_life
44 * [`historical_liquidity_penalty_multiplier_msat`]: ProbabilisticScoringParameters::historical_liquidity_penalty_multiplier_msat
45 * [`historical_liquidity_penalty_amount_multiplier_msat`]: ProbabilisticScoringParameters::historical_liquidity_penalty_amount_multiplier_msat
47 public class ProbabilisticScorer : CommonBase {
48 internal ProbabilisticScorer(object _dummy, long ptr) : base(ptr) { }
49 ~ProbabilisticScorer() {
50 if (ptr != 0) { bindings.ProbabilisticScorer_free(ptr); }
54 * Creates a new scorer using the given scoring parameters for sending payments from a node
55 * through a network graph.
57 public static ProbabilisticScorer of(org.ldk.structs.ProbabilisticScoringParameters _params, org.ldk.structs.NetworkGraph network_graph, org.ldk.structs.Logger logger) {
58 long ret = bindings.ProbabilisticScorer_new(_params == null ? 0 : _params.ptr, network_graph == null ? 0 : network_graph.ptr, logger == null ? 0 : logger.ptr);
59 GC.KeepAlive(_params);
60 GC.KeepAlive(network_graph);
62 if (ret >= 0 && ret <= 4096) { return null; }
63 org.ldk.structs.ProbabilisticScorer ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.ProbabilisticScorer(null, ret); }
64 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(ret_hu_conv); };
65 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(_params); };
66 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(network_graph); };
67 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(logger); };
72 * Dump the contents of this scorer into the configured logger.
74 * Note that this writes roughly one line per channel for which we have a liquidity estimate,
75 * which may be a substantial amount of log output.
77 public void debug_log_liquidity_stats() {
78 bindings.ProbabilisticScorer_debug_log_liquidity_stats(this.ptr);
83 * Query the estimated minimum and maximum liquidity available for sending a payment over the
84 * channel with `scid` towards the given `target` node.
86 public Option_C2Tuple_u64u64ZZ estimated_channel_liquidity_range(long scid, org.ldk.structs.NodeId target) {
87 long ret = bindings.ProbabilisticScorer_estimated_channel_liquidity_range(this.ptr, scid, target == null ? 0 : target.ptr);
91 if (ret >= 0 && ret <= 4096) { return null; }
92 org.ldk.structs.Option_C2Tuple_u64u64ZZ ret_hu_conv = org.ldk.structs.Option_C2Tuple_u64u64ZZ.constr_from_ptr(ret);
93 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
94 if (this != null) { this.ptrs_to.AddLast(target); };
99 * Marks the node with the given `node_id` as banned, i.e.,
100 * it will be avoided during path finding.
102 public void add_banned(org.ldk.structs.NodeId node_id) {
103 bindings.ProbabilisticScorer_add_banned(this.ptr, node_id == null ? 0 : node_id.ptr);
105 GC.KeepAlive(node_id);
106 if (this != null) { this.ptrs_to.AddLast(node_id); };
110 * Removes the node with the given `node_id` from the list of nodes to avoid.
112 public void remove_banned(org.ldk.structs.NodeId node_id) {
113 bindings.ProbabilisticScorer_remove_banned(this.ptr, node_id == null ? 0 : node_id.ptr);
115 GC.KeepAlive(node_id);
116 if (this != null) { this.ptrs_to.AddLast(node_id); };
120 * Sets a manual penalty for the given node.
122 public void set_manual_penalty(org.ldk.structs.NodeId node_id, long penalty) {
123 bindings.ProbabilisticScorer_set_manual_penalty(this.ptr, node_id == null ? 0 : node_id.ptr, penalty);
125 GC.KeepAlive(node_id);
126 GC.KeepAlive(penalty);
127 if (this != null) { this.ptrs_to.AddLast(node_id); };
131 * Removes the node with the given `node_id` from the list of manual penalties.
133 public void remove_manual_penalty(org.ldk.structs.NodeId node_id) {
134 bindings.ProbabilisticScorer_remove_manual_penalty(this.ptr, node_id == null ? 0 : node_id.ptr);
136 GC.KeepAlive(node_id);
137 if (this != null) { this.ptrs_to.AddLast(node_id); };
141 * Clears the list of manual penalties that are applied during path finding.
143 public void clear_manual_penalties() {
144 bindings.ProbabilisticScorer_clear_manual_penalties(this.ptr);
149 * Constructs a new Score which calls the relevant methods on this_arg.
150 * This copies the `inner` pointer in this_arg and thus the returned Score must be freed before this_arg is
152 public Score as_Score() {
153 long ret = bindings.ProbabilisticScorer_as_Score(this.ptr);
155 if (ret >= 0 && ret <= 4096) { return null; }
156 Score ret_hu_conv = new Score(null, ret);
157 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
162 * Serialize the ProbabilisticScorer object into a byte array which can be read by ProbabilisticScorer_read
164 public byte[] write() {
165 byte[] ret = bindings.ProbabilisticScorer_write(this.ptr);
171 * Read a ProbabilisticScorer from a byte array, created by ProbabilisticScorer_write
173 public static Result_ProbabilisticScorerDecodeErrorZ read(byte[] ser, org.ldk.structs.ProbabilisticScoringParameters arg_a, org.ldk.structs.NetworkGraph arg_b, org.ldk.structs.Logger arg_c) {
174 long ret = bindings.ProbabilisticScorer_read(ser, arg_a == null ? 0 : arg_a.ptr, arg_b == null ? 0 : arg_b.ptr, arg_c == null ? 0 : arg_c.ptr);
179 if (ret >= 0 && ret <= 4096) { return null; }
180 Result_ProbabilisticScorerDecodeErrorZ ret_hu_conv = Result_ProbabilisticScorerDecodeErrorZ.constr_from_ptr(ret);
181 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(arg_a); };
182 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(arg_b); };
183 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(arg_c); };