Skip to content

Commit 282f19d

Browse files
committed
paymod: Simplify routehint data initialization
It was spread over the step callback, but we only need to initialize the routehints array there, child-payments can just inherit most of the information.
1 parent b78aa3f commit 282f19d

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

plugins/libplugin-pay.c

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1771,9 +1771,13 @@ static struct route_info *next_routehint(struct routehints_data *d,
17711771
struct payment *p)
17721772
{
17731773
size_t numhints = tal_count(d->routehints);
1774-
size_t offset = pseudorand(numhints);
1774+
size_t offset;
17751775
struct route_info *curr;
17761776

1777+
if (d->routehints == NULL || numhints == 0)
1778+
return NULL;
1779+
1780+
offset = pseudorand(numhints);
17771781
for (size_t i=0; i<tal_count(d->routehints); i++) {
17781782
curr = d->routehints[(offset + i) % numhints];
17791783
if (curr == NULL || !routehint_excluded(p, curr))
@@ -1870,7 +1874,6 @@ static void routehint_check_reachable(struct payment *p)
18701874

18711875
static void routehint_step_cb(struct routehints_data *d, struct payment *p)
18721876
{
1873-
struct routehints_data *pd;
18741877
struct route_hop hop;
18751878
const struct payment *root = payment_root(p);
18761879

@@ -1892,17 +1895,9 @@ static void routehint_step_cb(struct routehints_data *d, struct payment *p)
18921895
/* Do not continue normally, instead go and check if
18931896
* we can reach the destination directly. */
18941897
return routehint_check_reachable(p);
1895-
} else {
1896-
pd = payment_mod_get_data(p->parent,
1897-
&routehints_pay_mod);
1898-
/* Since we don't modify the list of routehints after
1899-
* the root has filtered them we can just shared a
1900-
* pointer here. */
1901-
d->routehints = pd->routehints;
1902-
d->destination_reachable = pd->destination_reachable;
19031898
}
1904-
d->current_routehint = next_routehint(d, p);
19051899

1900+
d->current_routehint = next_routehint(d, p);
19061901
if (d->current_routehint != NULL) {
19071902
/* Change the destination and compute the final msatoshi
19081903
* amount to send to the routehint entry point. */
@@ -1921,7 +1916,7 @@ static void routehint_step_cb(struct routehints_data *d, struct payment *p)
19211916
d->current_routehint->cltv_expiry_delta
19221917
);
19231918
}
1924-
} else if (p->step == PAYMENT_STEP_GOT_ROUTE) {
1919+
} else if (p->step == PAYMENT_STEP_GOT_ROUTE && d->current_routehint != NULL) {
19251920
/* Now it's time to stitch the two partial routes together. */
19261921
struct amount_msat dest_amount;
19271922
struct route_info *routehint = d->current_routehint;
@@ -1959,9 +1954,20 @@ static void routehint_step_cb(struct routehints_data *d, struct payment *p)
19591954

19601955
static struct routehints_data *routehint_data_init(struct payment *p)
19611956
{
1962-
/* We defer the actual initialization to the step callback when
1963-
* we have the invoice attached. */
1964-
return talz(p, struct routehints_data);
1957+
struct routehints_data *pd, *d = tal(p, struct routehints_data);
1958+
/* If for some reason we skipped the getroute call (directpay) we'll
1959+
* need this to be initialized. */
1960+
d->current_routehint = NULL;
1961+
if (p->parent != NULL) {
1962+
pd = payment_mod_routehints_get_data(payment_root(p));
1963+
d->destination_reachable = pd->destination_reachable;
1964+
d->routehints = pd->routehints;
1965+
} else {
1966+
/* We defer the actual initialization of the routehints array to
1967+
* the step callback when we have the invoice attached. */
1968+
d->routehints = NULL;
1969+
}
1970+
return d;
19651971
}
19661972

19671973
REGISTER_PAYMENT_MODIFIER(routehints, struct routehints_data *,

0 commit comments

Comments
 (0)