| 986 | } |
| 987 | |
| 988 | void channel_set_state(struct channel *channel, |
| 989 | enum channel_state old_state, |
| 990 | enum channel_state state, |
| 991 | enum state_change reason, |
| 992 | const char *why) |
| 993 | { |
| 994 | bool was_important; |
| 995 | |
| 996 | /* Does peer currently have an important channel? */ |
| 997 | was_important = peer_any_channel(channel->peer, |
| 998 | channel_important_filter, NULL, NULL); |
| 999 | |
| 1000 | /* set closer, if known */ |
| 1001 | if (channel_state_closing(state) && channel->closer == NUM_SIDES) { |
| 1002 | if (reason == REASON_LOCAL) channel->closer = LOCAL; |
| 1003 | if (reason == REASON_USER) channel->closer = LOCAL; |
| 1004 | if (reason == REASON_REMOTE) channel->closer = REMOTE; |
| 1005 | if (reason == REASON_ONCHAIN) channel->closer = REMOTE; |
| 1006 | } |
| 1007 | |
| 1008 | /* use or update state_change_cause, if known */ |
| 1009 | if (reason != REASON_UNKNOWN) |
| 1010 | channel->state_change_cause = reason; |
| 1011 | else |
| 1012 | reason = channel->state_change_cause; |
| 1013 | |
| 1014 | log_info(channel->log, "State changed from %s to %s", |
| 1015 | channel_state_name(channel), channel_state_str(state)); |
| 1016 | if (channel->state != old_state) |
| 1017 | fatal("channel state %s should be %s", |
| 1018 | channel_state_name(channel), channel_state_str(old_state)); |
| 1019 | |
| 1020 | channel->state = state; |
| 1021 | |
| 1022 | /* TODO(cdecker) Selectively save updated fields to DB */ |
| 1023 | wallet_channel_save(channel->peer->ld->wallet, channel); |
| 1024 | |
| 1025 | /* plugin notification channel_state_changed and DB entry */ |
| 1026 | if (state != old_state) { /* see issue #4029 */ |
| 1027 | struct channel_state_change *change; |
| 1028 | |
| 1029 | change = new_channel_state_change(channel->state_changes, |
| 1030 | clock_time(), |
| 1031 | old_state, |
| 1032 | state, |
| 1033 | reason, |
| 1034 | why); |
| 1035 | tal_arr_expand(&channel->state_changes, change); |
| 1036 | |
| 1037 | wallet_state_change_add(channel->peer->ld->wallet, |
| 1038 | channel->dbid, |
| 1039 | change->timestamp, |
| 1040 | old_state, |
| 1041 | state, |
| 1042 | reason, |
| 1043 | why); |
| 1044 | notify_channel_state_changed(channel->peer->ld, |
| 1045 | &channel->peer->id, |
no test coverage detected