Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

channeld: remove dead HTLCs from htable and free them (eventually) #5882

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion channeld/channeld_htlc.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ static inline struct htlc *htlc_get(struct htlc_map *htlcs, u64 id, enum side ow
return NULL;
}

/* FIXME: Move these out of the hash! */
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe drop a short comment on what dead precisely means for safety-of-deletion?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe whoever named the function originally should write the comment. I almost deleted the function entirely since it's an implementation detail that doesn't belong in the public interface (i.e., the header file), but I left it there to minimize the diff.

static inline bool htlc_is_dead(const struct htlc *htlc)
{
return htlc->state == RCVD_REMOVE_ACK_REVOCATION
Expand Down
21 changes: 15 additions & 6 deletions channeld/full_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -934,10 +934,15 @@ static int change_htlcs(struct channel *channel,
for (i = 0; i < n_hstates; i++) {
if (h->state == htlc_states[i]) {
htlc_incstate(channel, h, sidechanged, owed);
if (htlc_is_dead(h)) {
htlc_map_delval(channel->htlcs, &it);
tal_steal(htlcs ? *htlcs : tmpctx, h);
}
dump_htlc(h, prefix);
htlc_arr_append(htlcs, h);
cflags |= (htlc_state_flags(htlc_states[i])
^ htlc_state_flags(h->state));
break;
}
}
}
Expand Down Expand Up @@ -1144,20 +1149,24 @@ bool channel_sending_revoke_and_ack(struct channel *channel)
return (change & HTLC_REMOTE_F_PENDING);
}

size_t num_channel_htlcs(const struct channel *channel)
static inline bool any_htlc_is_dead(const struct channel *channel)
{
struct htlc_map_iter it;
const struct htlc *htlc;
size_t n = 0;

for (htlc = htlc_map_first(channel->htlcs, &it);
htlc;
htlc = htlc_map_next(channel->htlcs, &it)) {
/* FIXME: Clean these out! */
if (!htlc_is_dead(htlc))
n++;
if (htlc_is_dead(htlc))
return true;
}
return n;
return false;
}

size_t num_channel_htlcs(const struct channel *channel)
{
assert(!any_htlc_is_dead(channel));
vincenzopalazzo marked this conversation as resolved.
Show resolved Hide resolved
return htlc_map_count(channel->htlcs);
}

static bool adjust_balance(struct balance view_owed[NUM_SIDES][NUM_SIDES],
Expand Down
11 changes: 6 additions & 5 deletions channeld/test/run-full_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,14 +266,16 @@ static void send_and_fulfill_htlc(struct channel *channel,
struct sha256 rhash;
u8 *dummy_routing = tal_arr(channel, u8, TOTAL_PACKET_SIZE);
bool ret;
const struct htlc **changed_htlcs;
const struct htlc *htlc, **changed_htlcs;

memset(&r, 0, sizeof(r));
sha256(&rhash, &r, sizeof(r));

assert(channel_add_htlc(channel, sender, 1337, msatoshi, 900, &rhash,
dummy_routing, NULL, NULL, NULL)
== CHANNEL_ERR_ADD_OK);
htlc = channel_get_htlc(channel, sender, 1337);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please make 1337 a variable and use it everywhere

assert(htlc);

changed_htlcs = tal_arr(channel, const struct htlc *, 0);

Expand All @@ -297,8 +299,7 @@ static void send_and_fulfill_htlc(struct channel *channel,
assert(ret);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert(ret);
assert(ret);
assert(channel_get_htlc(channel, sender, 1337));

ret = channel_rcvd_revoke_and_ack(channel, &changed_htlcs);
assert(!ret);
assert(channel_get_htlc(channel, sender, 1337)->state
== RCVD_REMOVE_ACK_REVOCATION);
assert(htlc->state == RCVD_REMOVE_ACK_REVOCATION);
} else {
ret = channel_rcvd_commit(channel, &changed_htlcs);
assert(ret);
Expand All @@ -318,9 +319,9 @@ static void send_and_fulfill_htlc(struct channel *channel,
assert(ret);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert(ret);
assert(ret);
assert(channel_get_htlc(channel, sender, 1337));

ret = channel_sending_revoke_and_ack(channel);
assert(!ret);
assert(channel_get_htlc(channel, sender, 1337)->state
== SENT_REMOVE_ACK_REVOCATION);
assert(htlc->state == SENT_REMOVE_ACK_REVOCATION);
}
assert(!channel_get_htlc(channel, sender, 1337));
}

static void update_feerate(struct channel *channel, u32 feerate)
Expand Down