Skip to content

Commit 606f8e7

Browse files
committed
lnwallet: rewrite channelState to bool for clarity
Over the last few commits we have systematically eliminated all but two states. This allows us to replace it with a boolean to encode the two remaining states. We would like to be able to eliminate this field entirely, but doing so requires being able to prove that the concurrent request block is necessary. This is more difficult and will be left to future commits.
1 parent 17e6734 commit 606f8e7

File tree

2 files changed

+11
-28
lines changed

2 files changed

+11
-28
lines changed

lnwallet/channel.go

+9-26
Original file line numberDiff line numberDiff line change
@@ -153,22 +153,6 @@ func (e *ErrCommitSyncLocalDataLoss) Error() string {
153153
e.CommitPoint.SerializeCompressed())
154154
}
155155

156-
// channelState is an enum like type which represents the current state of a
157-
// particular channel.
158-
// TODO(roasbeef): actually update state
159-
type channelState uint8
160-
161-
const (
162-
// channelOpen represents an open, active channel capable of
163-
// sending/receiving HTLCs.
164-
channelOpen channelState = iota
165-
166-
// channelClosed represents a channel which has been fully closed. Note
167-
// that before a channel can be closed, ALL pending HTLCs must be
168-
// settled/removed.
169-
channelClosed
170-
)
171-
172156
// PaymentHash represents the sha256 of a random value. This hash is used to
173157
// uniquely track incoming/outgoing payments within this channel, as well as
174158
// payments requested by the wallet/daemon.
@@ -1267,7 +1251,7 @@ type LightningChannel struct {
12671251
// the commitment transaction that spends the multi-sig output.
12681252
signDesc *input.SignDescriptor
12691253

1270-
status channelState
1254+
isClosed bool
12711255

12721256
// ChanPoint is the funding outpoint of this channel.
12731257
ChanPoint *wire.OutPoint
@@ -1522,7 +1506,7 @@ func (lc *LightningChannel) createSignDesc() error {
15221506
// events do so properly.
15231507
func (lc *LightningChannel) ResetState() {
15241508
lc.Lock()
1525-
lc.status = channelOpen
1509+
lc.isClosed = false
15261510
lc.Unlock()
15271511
}
15281512

@@ -7577,9 +7561,8 @@ func (lc *LightningChannel) ForceClose() (*LocalForceCloseSummary, error) {
75777561
"summary: %w", err)
75787562
}
75797563

7580-
// Set the channel state to indicate that the channel is now in a
7581-
// contested state.
7582-
lc.status = channelClosed
7564+
// Mark the channel as closed to block future closure requests.
7565+
lc.isClosed = true
75837566

75847567
return summary, nil
75857568
}
@@ -7773,8 +7756,8 @@ func (lc *LightningChannel) CreateCloseProposal(proposedFee btcutil.Amount,
77737756
lc.Lock()
77747757
defer lc.Unlock()
77757758

7776-
// If we've already closed the channel, then ignore this request.
7777-
if lc.status == channelClosed {
7759+
// If we're already closing the channel, then ignore this request.
7760+
if lc.isClosed {
77787761
// TODO(roasbeef): check to ensure no pending payments
77797762
return nil, nil, 0, ErrChanClosing
77807763
}
@@ -7857,8 +7840,8 @@ func (lc *LightningChannel) CompleteCooperativeClose(
78577840
lc.Lock()
78587841
defer lc.Unlock()
78597842

7860-
// If the channel is already closed, then ignore this request.
7861-
if lc.status == channelClosed {
7843+
// If the channel is already closing, then ignore this request.
7844+
if lc.isClosed {
78627845
// TODO(roasbeef): check to ensure no pending payments
78637846
return nil, 0, ErrChanClosing
78647847
}
@@ -7962,7 +7945,7 @@ func (lc *LightningChannel) CompleteCooperativeClose(
79627945
// As the transaction is sane, and the scripts are valid we'll mark the
79637946
// channel now as closed as the closure transaction should get into the
79647947
// chain in a timely manner and possibly be re-broadcast by the wallet.
7965-
lc.status = channelClosed
7948+
lc.isClosed = true
79667949

79677950
return closeTx, ourBalance, nil
79687951
}

lnwallet/channel_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -2147,8 +2147,8 @@ func TestCooperativeCloseDustAdherence(t *testing.T) {
21472147
}
21482148

21492149
resetChannelState := func() {
2150-
aliceChannel.status = channelOpen
2151-
bobChannel.status = channelOpen
2150+
aliceChannel.isClosed = false
2151+
bobChannel.isClosed = false
21522152
}
21532153

21542154
setBalances := func(aliceBalance, bobBalance lnwire.MilliSatoshi) {

0 commit comments

Comments
 (0)