Skip to content

Commit 8b58c5b

Browse files
cdeckerrustyrussell
authored andcommitted
pay: Simplify the channel_hint update logic
We were ignoring more reliable information because of the stricter-is-better logic. This meant that we were also ignoring local channel information, despite knowing better. By simplifying the logic to pick the one with the newer timestamp we ensure that later observations always trump earlier ones. There is a bit of interplay between the relaxation of the constraints updating the timestamp, and comparing to real observation timestamps, but that should not impact us for local channels.
1 parent 4b870a8 commit 8b58c5b

File tree

1 file changed

+27
-38
lines changed

1 file changed

+27
-38
lines changed

plugins/channel_hint.c

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -111,53 +111,42 @@ channel_hint_set_add(struct channel_hint_set *self, u32 timestamp,
111111
const struct amount_msat *estimated_capacity,
112112
const struct amount_sat capacity, u16 *htlc_budget)
113113
{
114-
bool modified = false;
115-
struct channel_hint *old, *newhint = tal(tmpctx, struct channel_hint);
116-
struct timeabs now = time_now();
114+
struct channel_hint *copy, *old, *newhint;
117115

118116
/* If the channel is marked as enabled it must have an estimate. */
119117
assert(!enabled || estimated_capacity != NULL);
120-
newhint->enabled = enabled;
121-
newhint->scid = *scidd;
122-
newhint->capacity = capacity;
123-
if (estimated_capacity != NULL)
124-
newhint->estimated_capacity = *estimated_capacity;
125-
newhint->local = NULL;
126-
newhint->timestamp = timestamp;
127-
128-
/* Project the channel_hints into the same domain, so we can merge them.
129-
*/
130-
channel_hint_update(now, newhint);
131-
channel_hint_set_update(self, now);
132118

133-
/* And now we can merge the new hint into the existing ones if there
134-
are any. */
119+
/* If there was no hint, add the new one, if there was one,
120+
* pick the one with the newer timestamp. */
135121
old = channel_hint_set_find(self, scidd);
122+
copy = tal_dup(tmpctx, struct channel_hint, old);
136123
if (old == NULL) {
124+
newhint = tal(tmpctx, struct channel_hint);
125+
newhint->enabled = enabled;
126+
newhint->scid = *scidd;
127+
newhint->capacity = capacity;
128+
if (estimated_capacity != NULL)
129+
newhint->estimated_capacity = *estimated_capacity;
130+
newhint->local = NULL;
131+
newhint->timestamp = timestamp;
137132
tal_arr_expand(&self->hints, *newhint);
138-
// TODO extend the array
139133
return &self->hints[tal_count(self->hints) - 1];
140-
} else {
141-
/* Prefer to disable a channel. */
142-
if (!enabled && old->enabled) {
143-
old->enabled = false;
144-
modified = true;
145-
}
146-
/* Prefer the more conservative estimate. */
147-
if (estimated_capacity != NULL &&
148-
amount_msat_greater(old->estimated_capacity,
149-
newhint->estimated_capacity)) {
150-
old->estimated_capacity = newhint->estimated_capacity;
151-
modified = true;
152-
}
153-
if (newhint->local) {
154-
tal_free(old->local);
155-
old->local = tal_steal(old, newhint->local);
156-
}
157-
}
158-
if (modified) {
134+
} else if (old->timestamp <= timestamp) {
135+
/* `local` is kept, since we do not pass in those
136+
* annotations here. */
137+
old->enabled = enabled;
159138
old->timestamp = timestamp;
160-
return old;
139+
if (estimated_capacity != NULL)
140+
old->estimated_capacity = *estimated_capacity;
141+
142+
/* We always pick the larger of the capacities we are
143+
* being told. This is because in some cases, such as
144+
* routehints, we're not actually being told the total
145+
* capacity, just lower values. */
146+
if (amount_sat_greater(capacity, old->capacity))
147+
old->capacity = capacity;
148+
149+
return copy;
161150
} else {
162151
return NULL;
163152
}

0 commit comments

Comments
 (0)