| 1021 | } |
| 1022 | |
| 1023 | void channel_set_state(struct channel *channel, |
| 1024 | enum channel_state old_state, |
| 1025 | enum channel_state state, |
| 1026 | enum state_change reason, |
| 1027 | const char *why) |
| 1028 | { |
| 1029 | bool was_important; |
| 1030 | |
| 1031 | /* Does peer currently have an important channel? */ |
| 1032 | was_important = peer_any_channel(channel->peer, |
| 1033 | channel_important_filter, NULL, NULL); |
| 1034 | |
| 1035 | /* set closer, if known */ |
| 1036 | if (channel_state_closing(state) && channel->closer == NUM_SIDES) { |
| 1037 | if (reason == REASON_LOCAL) channel->closer = LOCAL; |
| 1038 | if (reason == REASON_USER) channel->closer = LOCAL; |
| 1039 | if (reason == REASON_REMOTE) channel->closer = REMOTE; |
| 1040 | if (reason == REASON_ONCHAIN) channel->closer = REMOTE; |
| 1041 | } |
| 1042 | |
| 1043 | /* use or update state_change_cause, if known */ |
| 1044 | if (reason != REASON_UNKNOWN) |
| 1045 | channel->state_change_cause = reason; |
| 1046 | else |
| 1047 | reason = channel->state_change_cause; |
| 1048 | |
| 1049 | log_info(channel->log, "State changed from %s to %s", |
| 1050 | channel_state_name(channel), channel_state_str(state)); |
| 1051 | if (channel->state != old_state) |
| 1052 | fatal("channel state %s should be %s", |
| 1053 | channel_state_name(channel), channel_state_str(old_state)); |
| 1054 | |
| 1055 | channel->state = state; |
| 1056 | |
| 1057 | /* TODO(cdecker) Selectively save updated fields to DB */ |
| 1058 | wallet_channel_save(channel->peer->ld->wallet, channel); |
| 1059 | |
| 1060 | /* plugin notification channel_state_changed and DB entry */ |
| 1061 | if (state != old_state) { /* see issue #4029 */ |
| 1062 | struct channel_state_change *change; |
| 1063 | |
| 1064 | change = new_channel_state_change(channel->state_changes, |
| 1065 | clock_time(), |
| 1066 | old_state, |
| 1067 | state, |
| 1068 | reason, |
| 1069 | why); |
| 1070 | tal_arr_expand(&channel->state_changes, change); |
| 1071 | |
| 1072 | wallet_state_change_add(channel->peer->ld->wallet, |
| 1073 | channel->dbid, |
| 1074 | change->timestamp, |
| 1075 | old_state, |
| 1076 | state, |
| 1077 | reason, |
| 1078 | why); |
| 1079 | notify_channel_state_changed(channel->peer->ld, |
| 1080 | &channel->peer->id, |
no test coverage detected