Notify all channels of new blocks. */
| 2064 | |
| 2065 | /* Notify all channels of new blocks. */ |
| 2066 | void channel_notify_new_block(struct lightningd *ld) |
| 2067 | { |
| 2068 | struct peer *peer; |
| 2069 | struct channel *channel; |
| 2070 | struct channel **to_forget = tal_arr(tmpctx, struct channel *, 0); |
| 2071 | size_t i; |
| 2072 | struct peer_node_id_map_iter it; |
| 2073 | |
| 2074 | /* BOLT #2: |
| 2075 | * |
| 2076 | * A non-funding node (fundee): |
| 2077 | * - SHOULD forget the channel if it does not see the |
| 2078 | * correct funding transaction after a timeout of 2016 blocks. |
| 2079 | */ |
| 2080 | |
| 2081 | /* But we give some latitude! Boltz reported that after a months-long |
| 2082 | * fee spike, they had some failed opens when the tx finally got mined. |
| 2083 | * They're individually cheap, keep the latest 100. */ |
| 2084 | size_t forgettable_channels_to_keep = 100; |
| 2085 | |
| 2086 | /* For testing */ |
| 2087 | if (ld->dev_max_funding_unconfirmed != 2016) |
| 2088 | forgettable_channels_to_keep = 1; |
| 2089 | |
| 2090 | /* FIXME: keep separate block-aware channel structure instead? */ |
| 2091 | for (peer = peer_node_id_map_first(ld->peers, &it); |
| 2092 | peer; |
| 2093 | peer = peer_node_id_map_next(ld->peers, &it)) { |
| 2094 | list_for_each(&peer->channels, channel, list) { |
| 2095 | if (channel_state_uncommitted(channel->state)) |
| 2096 | continue; |
| 2097 | if (is_fundee_should_forget(ld, channel)) |
| 2098 | tal_arr_expand(&to_forget, channel); |
| 2099 | |
| 2100 | /* Let channels know about new blocks, |
| 2101 | * required for lease updates */ |
| 2102 | try_update_blockheight(ld, channel); |
| 2103 | } |
| 2104 | } |
| 2105 | |
| 2106 | /* Need to forget in a separate loop, else the above |
| 2107 | * nested loops may crash due to the last channel of |
| 2108 | * a peer also deleting the peer, making the inner |
| 2109 | * loop crash. |
| 2110 | * list_for_each_safe does not work because it is not |
| 2111 | * just the freeing of the channel that occurs, but the |
| 2112 | * potential destruction of the peer that invalidates |
| 2113 | * memory the inner loop is accessing. */ |
| 2114 | if (tal_count(to_forget) < forgettable_channels_to_keep) |
| 2115 | return; |
| 2116 | |
| 2117 | asort(to_forget, tal_count(to_forget), cmp_channel_start, NULL); |
| 2118 | for (i = 0; i + forgettable_channels_to_keep < tal_count(to_forget); ++i) { |
| 2119 | channel = to_forget[i]; |
| 2120 | /* Report it first. */ |
| 2121 | log_unusual(channel->log, |
| 2122 | "Forgetting channel: " |
| 2123 | "It has been %"PRIu32" blocks without the " |
no test coverage detected