| 720 | } |
| 721 | |
| 722 | void channel_set_state(struct channel *channel, |
| 723 | enum channel_state old_state, |
| 724 | enum channel_state state, |
| 725 | enum state_change reason, |
| 726 | char *why) |
| 727 | { |
| 728 | struct timeabs timestamp; |
| 729 | |
| 730 | /* set closer, if known */ |
| 731 | if (state > CHANNELD_NORMAL && channel->closer == NUM_SIDES) { |
| 732 | if (reason == REASON_LOCAL) channel->closer = LOCAL; |
| 733 | if (reason == REASON_USER) channel->closer = LOCAL; |
| 734 | if (reason == REASON_REMOTE) channel->closer = REMOTE; |
| 735 | if (reason == REASON_ONCHAIN) channel->closer = REMOTE; |
| 736 | } |
| 737 | |
| 738 | /* use or update state_change_cause, if known */ |
| 739 | if (reason != REASON_UNKNOWN) |
| 740 | channel->state_change_cause = reason; |
| 741 | else |
| 742 | reason = channel->state_change_cause; |
| 743 | |
| 744 | log_info(channel->log, "State changed from %s to %s", |
| 745 | channel_state_name(channel), channel_state_str(state)); |
| 746 | if (channel->state != old_state) |
| 747 | fatal("channel state %s should be %s", |
| 748 | channel_state_name(channel), channel_state_str(old_state)); |
| 749 | |
| 750 | channel->state = state; |
| 751 | |
| 752 | /* TODO(cdecker) Selectively save updated fields to DB */ |
| 753 | wallet_channel_save(channel->peer->ld->wallet, channel); |
| 754 | |
| 755 | /* plugin notification channel_state_changed and DB entry */ |
| 756 | if (state != old_state) { /* see issue #4029 */ |
| 757 | timestamp = time_now(); |
| 758 | wallet_state_change_add(channel->peer->ld->wallet, |
| 759 | channel->dbid, |
| 760 | ×tamp, |
| 761 | old_state, |
| 762 | state, |
| 763 | reason, |
| 764 | why); |
| 765 | notify_channel_state_changed(channel->peer->ld, |
| 766 | &channel->peer->id, |
| 767 | &channel->cid, |
| 768 | channel->scid, |
| 769 | ×tamp, |
| 770 | old_state, |
| 771 | state, |
| 772 | reason, |
| 773 | why); |
| 774 | } |
| 775 | } |
| 776 | |
| 777 | const char *channel_change_state_reason_str(enum state_change reason) |
| 778 | { |
no test coverage detected