Skip to content

Commit 3393444

Browse files
committed
multi: refactor select methods within channeldb to use ChannelParty
Also in this commit is a small adjustment to the call-sites to get the boundaries stitched back together.
1 parent 3a15085 commit 3393444

File tree

7 files changed

+58
-21
lines changed

7 files changed

+58
-21
lines changed

channeldb/channel.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/lightningnetwork/lnd/input"
2626
"github.com/lightningnetwork/lnd/keychain"
2727
"github.com/lightningnetwork/lnd/kvdb"
28+
"github.com/lightningnetwork/lnd/lntypes"
2829
"github.com/lightningnetwork/lnd/lnwire"
2930
"github.com/lightningnetwork/lnd/shachain"
3031
"github.com/lightningnetwork/lnd/tlv"
@@ -1690,11 +1691,11 @@ func (c *OpenChannel) isBorked(chanBucket kvdb.RBucket) (bool, error) {
16901691
// republish this tx at startup to ensure propagation, and we should still
16911692
// handle the case where a different tx actually hits the chain.
16921693
func (c *OpenChannel) MarkCommitmentBroadcasted(closeTx *wire.MsgTx,
1693-
locallyInitiated bool) error {
1694+
closer lntypes.ChannelParty) error {
16941695

16951696
return c.markBroadcasted(
16961697
ChanStatusCommitBroadcasted, forceCloseTxKey, closeTx,
1697-
locallyInitiated,
1698+
closer,
16981699
)
16991700
}
17001701

@@ -1706,11 +1707,11 @@ func (c *OpenChannel) MarkCommitmentBroadcasted(closeTx *wire.MsgTx,
17061707
// ensure propagation, and we should still handle the case where a different tx
17071708
// actually hits the chain.
17081709
func (c *OpenChannel) MarkCoopBroadcasted(closeTx *wire.MsgTx,
1709-
locallyInitiated bool) error {
1710+
closer lntypes.ChannelParty) error {
17101711

17111712
return c.markBroadcasted(
17121713
ChanStatusCoopBroadcasted, coopCloseTxKey, closeTx,
1713-
locallyInitiated,
1714+
closer,
17141715
)
17151716
}
17161717

@@ -1719,7 +1720,7 @@ func (c *OpenChannel) MarkCoopBroadcasted(closeTx *wire.MsgTx,
17191720
// which should specify either a coop or force close. It adds a status which
17201721
// indicates the party that initiated the channel close.
17211722
func (c *OpenChannel) markBroadcasted(status ChannelStatus, key []byte,
1722-
closeTx *wire.MsgTx, locallyInitiated bool) error {
1723+
closeTx *wire.MsgTx, closer lntypes.ChannelParty) error {
17231724

17241725
c.Lock()
17251726
defer c.Unlock()
@@ -1741,7 +1742,7 @@ func (c *OpenChannel) markBroadcasted(status ChannelStatus, key []byte,
17411742
// Add the initiator status to the status provided. These statuses are
17421743
// set in addition to the broadcast status so that we do not need to
17431744
// migrate the original logic which does not store initiator.
1744-
if locallyInitiated {
1745+
if closer.IsLocal() {
17451746
status |= ChanStatusLocalCloseInitiator
17461747
} else {
17471748
status |= ChanStatusRemoteCloseInitiator
@@ -4486,6 +4487,15 @@ func NewShutdownInfo(deliveryScript lnwire.DeliveryAddress,
44864487
}
44874488
}
44884489

4490+
// Closer identifies the ChannelParty that initiated the coop-closure process.
4491+
func (s ShutdownInfo) Closer() lntypes.ChannelParty {
4492+
if s.LocalInitiator.Val {
4493+
return lntypes.Local
4494+
}
4495+
4496+
return lntypes.Remote
4497+
}
4498+
44894499
// encode serialises the ShutdownInfo to the given io.Writer.
44904500
func (s *ShutdownInfo) encode(w io.Writer) error {
44914501
records := []tlv.Record{

channeldb/channel_test.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/lightningnetwork/lnd/kvdb"
2222
"github.com/lightningnetwork/lnd/lnmock"
2323
"github.com/lightningnetwork/lnd/lntest/channels"
24+
"github.com/lightningnetwork/lnd/lntypes"
2425
"github.com/lightningnetwork/lnd/lnwire"
2526
"github.com/lightningnetwork/lnd/shachain"
2627
"github.com/lightningnetwork/lnd/tlv"
@@ -1084,13 +1085,17 @@ func TestFetchWaitingCloseChannels(t *testing.T) {
10841085
},
10851086
)
10861087

1087-
if err := channel.MarkCommitmentBroadcasted(closeTx, true); err != nil {
1088+
if err := channel.MarkCommitmentBroadcasted(
1089+
closeTx, lntypes.Local,
1090+
); err != nil {
10881091
t.Fatalf("unable to mark commitment broadcast: %v", err)
10891092
}
10901093

10911094
// Now try to marking a coop close with a nil tx. This should
10921095
// succeed, but it shouldn't exit when queried.
1093-
if err = channel.MarkCoopBroadcasted(nil, true); err != nil {
1096+
if err = channel.MarkCoopBroadcasted(
1097+
nil, lntypes.Local,
1098+
); err != nil {
10941099
t.Fatalf("unable to mark nil coop broadcast: %v", err)
10951100
}
10961101
_, err := channel.BroadcastedCooperative()
@@ -1102,7 +1107,9 @@ func TestFetchWaitingCloseChannels(t *testing.T) {
11021107
// it as coop closed. Later we will test that distinct
11031108
// transactions are returned for both coop and force closes.
11041109
closeTx.TxIn[0].PreviousOutPoint.Index ^= 1
1105-
if err := channel.MarkCoopBroadcasted(closeTx, true); err != nil {
1110+
if err := channel.MarkCoopBroadcasted(
1111+
closeTx, lntypes.Local,
1112+
); err != nil {
11061113
t.Fatalf("unable to mark coop broadcast: %v", err)
11071114
}
11081115
}
@@ -1324,7 +1331,7 @@ func TestCloseInitiator(t *testing.T) {
13241331
// by the local party.
13251332
updateChannel: func(c *OpenChannel) error {
13261333
return c.MarkCoopBroadcasted(
1327-
&wire.MsgTx{}, true,
1334+
&wire.MsgTx{}, lntypes.Local,
13281335
)
13291336
},
13301337
expectedStatuses: []ChannelStatus{
@@ -1338,7 +1345,7 @@ func TestCloseInitiator(t *testing.T) {
13381345
// by the remote party.
13391346
updateChannel: func(c *OpenChannel) error {
13401347
return c.MarkCoopBroadcasted(
1341-
&wire.MsgTx{}, false,
1348+
&wire.MsgTx{}, lntypes.Remote,
13421349
)
13431350
},
13441351
expectedStatuses: []ChannelStatus{
@@ -1352,7 +1359,7 @@ func TestCloseInitiator(t *testing.T) {
13521359
// local initiator.
13531360
updateChannel: func(c *OpenChannel) error {
13541361
return c.MarkCommitmentBroadcasted(
1355-
&wire.MsgTx{}, true,
1362+
&wire.MsgTx{}, lntypes.Local,
13561363
)
13571364
},
13581365
expectedStatuses: []ChannelStatus{

channeldb/db_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/btcsuite/btcd/wire"
1515
"github.com/lightningnetwork/lnd/keychain"
1616
"github.com/lightningnetwork/lnd/kvdb"
17+
"github.com/lightningnetwork/lnd/lntypes"
1718
"github.com/lightningnetwork/lnd/lnwire"
1819
"github.com/lightningnetwork/lnd/shachain"
1920
"github.com/stretchr/testify/require"
@@ -606,7 +607,9 @@ func TestFetchChannels(t *testing.T) {
606607
channelIDOption(pendingWaitingChan),
607608
)
608609

609-
err = pendingClosing.MarkCoopBroadcasted(nil, true)
610+
err = pendingClosing.MarkCoopBroadcasted(
611+
nil, lntypes.Local,
612+
)
610613
if err != nil {
611614
t.Fatalf("unexpected error: %v", err)
612615
}
@@ -626,7 +629,9 @@ func TestFetchChannels(t *testing.T) {
626629
channelIDOption(openWaitingChan),
627630
openChannelOption(),
628631
)
629-
err = openClosing.MarkCoopBroadcasted(nil, true)
632+
err = openClosing.MarkCoopBroadcasted(
633+
nil, lntypes.Local,
634+
)
630635
if err != nil {
631636
t.Fatalf("unexpected error: %v", err)
632637
}

contractcourt/chain_arbitrator_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/lightningnetwork/lnd/channeldb/models"
1212
"github.com/lightningnetwork/lnd/clock"
1313
"github.com/lightningnetwork/lnd/lntest/mock"
14+
"github.com/lightningnetwork/lnd/lntypes"
1415
"github.com/lightningnetwork/lnd/lnwallet"
1516
"github.com/stretchr/testify/require"
1617
)
@@ -61,12 +62,14 @@ func TestChainArbitratorRepublishCloses(t *testing.T) {
6162
for i := 0; i < numChans/2; i++ {
6263
closeTx := channels[i].FundingTxn.Copy()
6364
closeTx.TxIn[0].PreviousOutPoint = channels[i].FundingOutpoint
64-
err := channels[i].MarkCommitmentBroadcasted(closeTx, true)
65+
err := channels[i].MarkCommitmentBroadcasted(
66+
closeTx, lntypes.Local,
67+
)
6568
if err != nil {
6669
t.Fatal(err)
6770
}
6871

69-
err = channels[i].MarkCoopBroadcasted(closeTx, true)
72+
err = channels[i].MarkCoopBroadcasted(closeTx, lntypes.Local)
7073
if err != nil {
7174
t.Fatal(err)
7275
}

contractcourt/channel_arbitrator.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ type ChannelArbitratorConfig struct {
129129

130130
// MarkCommitmentBroadcasted should mark the channel as the commitment
131131
// being broadcast, and we are waiting for the commitment to confirm.
132-
MarkCommitmentBroadcasted func(*wire.MsgTx, bool) error
132+
MarkCommitmentBroadcasted func(*wire.MsgTx, lntypes.ChannelParty) error
133133

134134
// MarkChannelClosed marks the channel closed in the database, with the
135135
// passed close summary. After this method successfully returns we can
@@ -1084,7 +1084,7 @@ func (c *ChannelArbitrator) stateStep(
10841084
// database, such that we can re-publish later in case it
10851085
// didn't propagate. We initiated the force close, so we
10861086
// mark broadcast with local initiator set to true.
1087-
err = c.cfg.MarkCommitmentBroadcasted(closeTx, true)
1087+
err = c.cfg.MarkCommitmentBroadcasted(closeTx, lntypes.Local)
10881088
if err != nil {
10891089
log.Errorf("ChannelArbitrator(%v): unable to "+
10901090
"mark commitment broadcasted: %v",

contractcourt/channel_arbitrator_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,9 @@ func createTestChannelArbitrator(t *testing.T, log ArbitratorLog,
416416
resolvedChan <- struct{}{}
417417
return nil
418418
},
419-
MarkCommitmentBroadcasted: func(_ *wire.MsgTx, _ bool) error {
419+
MarkCommitmentBroadcasted: func(_ *wire.MsgTx,
420+
_ lntypes.ChannelParty) error {
421+
420422
return nil
421423
},
422424
MarkChannelClosed: func(*channeldb.ChannelCloseSummary,

lnwallet/channel.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8460,7 +8460,12 @@ func (lc *LightningChannel) MarkCommitmentBroadcasted(tx *wire.MsgTx,
84608460
lc.Lock()
84618461
defer lc.Unlock()
84628462

8463-
return lc.channelState.MarkCommitmentBroadcasted(tx, locallyInitiated)
8463+
party := lntypes.Remote
8464+
if locallyInitiated {
8465+
party = lntypes.Local
8466+
}
8467+
8468+
return lc.channelState.MarkCommitmentBroadcasted(tx, party)
84648469
}
84658470

84668471
// MarkCoopBroadcasted marks the channel as a cooperative close transaction has
@@ -8473,7 +8478,12 @@ func (lc *LightningChannel) MarkCoopBroadcasted(tx *wire.MsgTx,
84738478
lc.Lock()
84748479
defer lc.Unlock()
84758480

8476-
return lc.channelState.MarkCoopBroadcasted(tx, localInitiated)
8481+
party := lntypes.Remote
8482+
if localInitiated {
8483+
party = lntypes.Local
8484+
}
8485+
8486+
return lc.channelState.MarkCoopBroadcasted(tx, party)
84778487
}
84788488

84798489
// MarkShutdownSent persists the given ShutdownInfo. The existence of the

0 commit comments

Comments
 (0)