@@ -90,6 +90,32 @@ static void peer_set_owner(struct peer *peer, struct subd *owner)
90
90
91
91
static void destroy_peer (struct peer * peer )
92
92
{
93
+ /* Must not have any HTLCs! */
94
+ struct htlc_out_map_iter outi ;
95
+ struct htlc_out * hout ;
96
+ struct htlc_in_map_iter ini ;
97
+ struct htlc_in * hin ;
98
+
99
+ for (hout = htlc_out_map_first (& peer -> ld -> htlcs_out , & outi );
100
+ hout ;
101
+ hout = htlc_out_map_next (& peer -> ld -> htlcs_out , & outi )) {
102
+ if (hout -> key .peer != peer )
103
+ continue ;
104
+ fatal ("Freeing peer %s has hout %s" ,
105
+ peer_state_name (peer -> state ),
106
+ htlc_state_name (hout -> hstate ));
107
+ }
108
+
109
+ for (hin = htlc_in_map_first (& peer -> ld -> htlcs_in , & ini );
110
+ hin ;
111
+ hin = htlc_in_map_next (& peer -> ld -> htlcs_in , & ini )) {
112
+ if (hin -> key .peer != peer )
113
+ continue ;
114
+ fatal ("Freeing peer %s has hin %s" ,
115
+ peer_state_name (peer -> state ),
116
+ htlc_state_name (hin -> hstate ));
117
+ }
118
+
93
119
/* Free any old owner still hanging around. */
94
120
peer_set_owner (peer , NULL );
95
121
list_del_from (& peer -> ld -> peers , & peer -> list );
@@ -1163,7 +1189,8 @@ static void handle_onchain_htlc_timeout(struct peer *peer, const u8 *msg)
1163
1189
onchain_failed_our_htlc (peer , & htlc , "timed out" );
1164
1190
}
1165
1191
1166
- static void handle_irrevocably_resolved (struct peer * peer , const u8 * msg )
1192
+ /* If peer is NULL, free them all (for shutdown) */
1193
+ void free_htlcs (struct lightningd * ld , const struct peer * peer )
1167
1194
{
1168
1195
struct htlc_out_map_iter outi ;
1169
1196
struct htlc_out * hout ;
@@ -1175,25 +1202,31 @@ static void handle_irrevocably_resolved(struct peer *peer, const u8 *msg)
1175
1202
1176
1203
do {
1177
1204
deleted = false;
1178
- for (hout = htlc_out_map_first (& peer -> ld -> htlcs_out , & outi );
1205
+ for (hout = htlc_out_map_first (& ld -> htlcs_out , & outi );
1179
1206
hout ;
1180
- hout = htlc_out_map_next (& peer -> ld -> htlcs_out , & outi )) {
1181
- if (hout -> key .peer != peer )
1207
+ hout = htlc_out_map_next (& ld -> htlcs_out , & outi )) {
1208
+ if (peer && hout -> key .peer != peer )
1182
1209
continue ;
1183
1210
tal_free (hout );
1184
1211
deleted = true;
1185
1212
}
1186
1213
1187
- for (hin = htlc_in_map_first (& peer -> ld -> htlcs_in , & ini );
1214
+ for (hin = htlc_in_map_first (& ld -> htlcs_in , & ini );
1188
1215
hin ;
1189
- hin = htlc_in_map_next (& peer -> ld -> htlcs_in , & ini )) {
1190
- if (hin -> key .peer != peer )
1216
+ hin = htlc_in_map_next (& ld -> htlcs_in , & ini )) {
1217
+ if (peer && hin -> key .peer != peer )
1191
1218
continue ;
1192
1219
tal_free (hin );
1193
1220
deleted = true;
1194
1221
}
1195
1222
/* Can skip over elements due to iterating while deleting. */
1196
1223
} while (deleted );
1224
+ }
1225
+
1226
+ static void handle_irrevocably_resolved (struct peer * peer , const u8 * msg )
1227
+ {
1228
+ /* FIXME: Implement check_htlcs to ensure no dangling hout->in ptrs! */
1229
+ free_htlcs (peer -> ld , peer );
1197
1230
1198
1231
/* FIXME: Remove peer from db. */
1199
1232
log_info (peer -> log , "onchaind complete, forgetting peer" );
0 commit comments