| 1024 | } |
| 1025 | |
| 1026 | struct command_result *cancel_channel_before_broadcast(struct command *cmd, |
| 1027 | struct peer *peer) |
| 1028 | { |
| 1029 | struct channel *cancel_channel; |
| 1030 | struct channel_to_cancel *cc = tal(cmd, struct channel_to_cancel); |
| 1031 | struct channel *channel; |
| 1032 | |
| 1033 | cc->peer = peer->id; |
| 1034 | cancel_channel = NULL; |
| 1035 | list_for_each(&peer->channels, channel, list) { |
| 1036 | /* After `fundchannel_complete`, channel is in |
| 1037 | * `CHANNELD_AWAITING_LOCKIN` state. |
| 1038 | * |
| 1039 | * TODO: This assumes only one channel at a time |
| 1040 | * can be in this state, which is true at the |
| 1041 | * time of this writing, but may change *if* we |
| 1042 | * ever implement multiple channels per peer. |
| 1043 | */ |
| 1044 | if (channel->state != CHANNELD_AWAITING_LOCKIN) |
| 1045 | continue; |
| 1046 | cancel_channel = channel; |
| 1047 | break; |
| 1048 | } |
| 1049 | if (!cancel_channel) |
| 1050 | return command_fail(cmd, FUNDING_NOTHING_TO_CANCEL, |
| 1051 | "No channels being opened or " |
| 1052 | "awaiting lock-in for " |
| 1053 | "peer_id %s", |
| 1054 | type_to_string(tmpctx, struct node_id, |
| 1055 | &peer->id)); |
| 1056 | cc->cid = cancel_channel->cid; |
| 1057 | |
| 1058 | if (cancel_channel->opener == REMOTE) |
| 1059 | return command_fail(cmd, FUNDING_CANCEL_NOT_SAFE, |
| 1060 | "Cannot cancel channel that was " |
| 1061 | "initiated by peer"); |
| 1062 | |
| 1063 | /* Check if we broadcast the transaction. (We store the transaction |
| 1064 | * type into DB before broadcast). */ |
| 1065 | enum wallet_tx_type type; |
| 1066 | if (wallet_transaction_type(cmd->ld->wallet, |
| 1067 | &cancel_channel->funding.txid, |
| 1068 | &type)) |
| 1069 | return command_fail(cmd, FUNDING_CANCEL_NOT_SAFE, |
| 1070 | "Has the funding transaction been" |
| 1071 | " broadcast? Please use `close` or" |
| 1072 | " `dev-fail` instead."); |
| 1073 | |
| 1074 | if (channel_has_htlc_out(cancel_channel) || |
| 1075 | channel_has_htlc_in(cancel_channel)) { |
| 1076 | return command_fail(cmd, FUNDING_CANCEL_NOT_SAFE, |
| 1077 | "This channel has HTLCs attached and it" |
| 1078 | " is not safe to cancel. Has the funding" |
| 1079 | " transaction been broadcast? Please use" |
| 1080 | " `close` or `dev-fail` instead."); |
| 1081 | } |
| 1082 | |
| 1083 | tal_arr_expand(&cancel_channel->forgets, cmd); |
no test coverage detected