Construct a route from a flow. * * @ctx: allocator * @groupid, @partid, @payment_hash: unique identification keys for this route * @final_cltv: final delay required by the payment * @gossmap: global gossmap * @flow: the flow to convert to route */
| 32 | * @gossmap: global gossmap |
| 33 | * @flow: the flow to convert to route */ |
| 34 | struct route *flow_to_route(const tal_t *ctx, |
| 35 | u64 groupid, u64 partid, struct sha256 payment_hash, |
| 36 | u32 final_cltv, struct gossmap *gossmap, |
| 37 | struct flow *flow, |
| 38 | bool blinded_destination) |
| 39 | { |
| 40 | struct route *route = |
| 41 | new_route(ctx, groupid, partid, payment_hash, |
| 42 | AMOUNT_MSAT(0), AMOUNT_MSAT(0)); |
| 43 | |
| 44 | size_t pathlen = tal_count(flow->path); |
| 45 | route->hops = tal_arr(route, struct route_hop, pathlen); |
| 46 | |
| 47 | for (size_t i = 0; i < pathlen; i++) { |
| 48 | struct route_hop *hop = &route->hops[i]; |
| 49 | struct gossmap_node *n; |
| 50 | n = gossmap_nth_node(gossmap, flow->path[i], !flow->dirs[i]); |
| 51 | gossmap_node_get_id(gossmap, n, &hop->node_id); |
| 52 | |
| 53 | hop->scid = gossmap_chan_scid(gossmap, flow->path[i]); |
| 54 | hop->direction = flow->dirs[i]; |
| 55 | } |
| 56 | |
| 57 | /* Calculate cumulative delays (backwards) */ |
| 58 | route->hops[pathlen - 1].delay = final_cltv; |
| 59 | route->hops[pathlen - 1].amount = flow->amount; |
| 60 | |
| 61 | for (int i = (int)pathlen - 2; i >= 0; i--) { |
| 62 | const struct half_chan *h = flow_edge(flow, i + 1); |
| 63 | |
| 64 | route->hops[i].delay = route->hops[i + 1].delay + h->delay; |
| 65 | route->hops[i].amount = route->hops[i + 1].amount; |
| 66 | if (!amount_msat_add_fee(&route->hops[i].amount, h->base_fee, |
| 67 | h->proportional_fee)) |
| 68 | goto function_fail; |
| 69 | } |
| 70 | route->success_prob = flow->success_prob; |
| 71 | route->amount_deliver = route->hops[pathlen - 1].amount; |
| 72 | route->amount_sent = route->hops[0].amount; |
| 73 | |
| 74 | if (blinded_destination) { |
| 75 | route->path_num = route->hops[pathlen - 1].scid.u64; |
| 76 | tal_arr_remove(&route->hops, pathlen - 1); |
| 77 | } |
| 78 | |
| 79 | return route; |
| 80 | |
| 81 | function_fail: |
| 82 | return tal_free(route); |
| 83 | } |
| 84 | |
| 85 | struct route **flows_to_routes(const tal_t *ctx, |
| 86 | u64 groupid, u64 partid, |