Skip to content

Commit 5cb4811

Browse files
authored
Merge pull request #8425 from ProofOfKeags/refactor/lnwallet/chan-point-leaks
[EZ Review]: avoid leaking pointers to authoritative ChannelPoint
2 parents 780ffe9 + 659ad45 commit 5cb4811

31 files changed

+160
-149
lines changed

channeldb/channel.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,7 @@ func (c *OpenChannel) fullSync(tx kvdb.RwTx) error {
11691169
return ErrChanAlreadyExists
11701170
}
11711171

1172-
cid := lnwire.NewChanIDFromOutPoint(&c.FundingOutpoint)
1172+
cid := lnwire.NewChanIDFromOutPoint(c.FundingOutpoint)
11731173
if cidBucket.Get(cid[:]) != nil {
11741174
return ErrChanAlreadyExists
11751175
}
@@ -1574,7 +1574,7 @@ func (c *OpenChannel) ChanSyncMsg() (*lnwire.ChannelReestablish, error) {
15741574

15751575
return &lnwire.ChannelReestablish{
15761576
ChanID: lnwire.NewChanIDFromOutPoint(
1577-
&c.FundingOutpoint,
1577+
c.FundingOutpoint,
15781578
),
15791579
NextLocalCommitHeight: nextLocalCommitHeight,
15801580
RemoteCommitTailHeight: remoteChainTipHeight,

channeldb/db.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ func (c *ChannelStateDB) FetchChannelByID(tx kvdb.RTx, id lnwire.ChannelID) (
704704
return err
705705
}
706706

707-
chanID := lnwire.NewChanIDFromOutPoint(&outPoint)
707+
chanID := lnwire.NewChanIDFromOutPoint(outPoint)
708708
if chanID != id {
709709
return nil
710710
}

channeldb/db_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func TestFetchClosedChannelForID(t *testing.T) {
140140
state.FundingOutpoint.Index = i
141141

142142
// We calculate the ChannelID and use it to fetch the summary.
143-
cid := lnwire.NewChanIDFromOutPoint(&state.FundingOutpoint)
143+
cid := lnwire.NewChanIDFromOutPoint(state.FundingOutpoint)
144144
fetchedSummary, err := cdb.FetchClosedChannelForID(cid)
145145
if err != nil {
146146
t.Fatalf("unable to fetch close summary: %v", err)
@@ -158,7 +158,7 @@ func TestFetchClosedChannelForID(t *testing.T) {
158158
// As a final test we make sure that we get ErrClosedChannelNotFound
159159
// for a ChannelID we didn't add to the DB.
160160
state.FundingOutpoint.Index++
161-
cid := lnwire.NewChanIDFromOutPoint(&state.FundingOutpoint)
161+
cid := lnwire.NewChanIDFromOutPoint(state.FundingOutpoint)
162162
_, err = cdb.FetchClosedChannelForID(cid)
163163
if err != ErrClosedChannelNotFound {
164164
t.Fatalf("expected ErrClosedChannelNotFound, instead got: %v", err)
@@ -240,7 +240,7 @@ func TestFetchChannel(t *testing.T) {
240240
require.Equal(t, channelState, dbChannel)
241241

242242
// Next, attempt to fetch the channel by its channel ID.
243-
chanID := lnwire.NewChanIDFromOutPoint(&channelState.FundingOutpoint)
243+
chanID := lnwire.NewChanIDFromOutPoint(channelState.FundingOutpoint)
244244
dbChannel, err = cdb.FetchChannelByID(nil, chanID)
245245
require.NoError(t, err, "unable to fetch channel")
246246

@@ -259,7 +259,7 @@ func TestFetchChannel(t *testing.T) {
259259
_, err = cdb.FetchChannel(nil, channelState2.FundingOutpoint)
260260
require.ErrorIs(t, err, ErrChannelNotFound)
261261

262-
chanID2 := lnwire.NewChanIDFromOutPoint(&channelState2.FundingOutpoint)
262+
chanID2 := lnwire.NewChanIDFromOutPoint(channelState2.FundingOutpoint)
263263
_, err = cdb.FetchChannelByID(nil, chanID2)
264264
require.ErrorIs(t, err, ErrChannelNotFound)
265265
}

channeldb/forwarding_package_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func checkPkgFilterEncodeDecode(t *testing.T, i uint16, f *channeldb.PkgFilter)
141141
}
142142

143143
var (
144-
chanID = lnwire.NewChanIDFromOutPoint(&wire.OutPoint{})
144+
chanID = lnwire.NewChanIDFromOutPoint(wire.OutPoint{})
145145

146146
adds = []channeldb.LogUpdate{
147147
{

contractcourt/breach_arbitrator_test.go

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,13 +1010,13 @@ func initBreachedState(t *testing.T) (*BreachArbitrator,
10101010
func TestBreachHandoffSuccess(t *testing.T) {
10111011
brar, alice, _, bobClose, contractBreaches := initBreachedState(t)
10121012

1013-
chanPoint := alice.ChanPoint
1013+
chanPoint := alice.ChannelPoint()
10141014

10151015
// Signal a spend of the funding transaction and wait for the close
10161016
// observer to exit.
10171017
processACK := make(chan error)
10181018
breach := &ContractBreachEvent{
1019-
ChanPoint: *chanPoint,
1019+
ChanPoint: chanPoint,
10201020
ProcessACK: func(brarErr error) {
10211021
processACK <- brarErr
10221022
},
@@ -1044,13 +1044,13 @@ func TestBreachHandoffSuccess(t *testing.T) {
10441044
// After exiting, the breach arbiter should have persisted the
10451045
// retribution information and the channel should be shown as pending
10461046
// force closed.
1047-
assertArbiterBreach(t, brar, chanPoint)
1047+
assertArbiterBreach(t, brar, &chanPoint)
10481048

10491049
// Send another breach event. Since the handoff for this channel was
10501050
// already ACKed, the breach arbiter should immediately ACK and ignore
10511051
// this event.
10521052
breach = &ContractBreachEvent{
1053-
ChanPoint: *chanPoint,
1053+
ChanPoint: chanPoint,
10541054
ProcessACK: func(brarErr error) {
10551055
processACK <- brarErr
10561056
},
@@ -1077,7 +1077,7 @@ func TestBreachHandoffSuccess(t *testing.T) {
10771077
}
10781078

10791079
// State should not have changed.
1080-
assertArbiterBreach(t, brar, chanPoint)
1080+
assertArbiterBreach(t, brar, &chanPoint)
10811081
}
10821082

10831083
// TestBreachHandoffFail tests that a channel's close observer properly
@@ -1096,10 +1096,10 @@ func TestBreachHandoffFail(t *testing.T) {
10961096

10971097
// Signal the notifier to dispatch spend notifications of the funding
10981098
// transaction using the transaction from bob's closing summary.
1099-
chanPoint := alice.ChanPoint
1099+
chanPoint := alice.ChannelPoint()
11001100
processACK := make(chan error)
11011101
breach := &ContractBreachEvent{
1102-
ChanPoint: *chanPoint,
1102+
ChanPoint: chanPoint,
11031103
ProcessACK: func(brarErr error) {
11041104
processACK <- brarErr
11051105
},
@@ -1127,7 +1127,7 @@ func TestBreachHandoffFail(t *testing.T) {
11271127
// Since the handoff failed, the breach arbiter should not show the
11281128
// channel as breached, and the channel should also not have been marked
11291129
// pending closed.
1130-
assertNoArbiterBreach(t, brar, chanPoint)
1130+
assertNoArbiterBreach(t, brar, &chanPoint)
11311131
assertNotPendingClosed(t, alice)
11321132

11331133
brar, err := createTestArbiter(
@@ -1138,7 +1138,7 @@ func TestBreachHandoffFail(t *testing.T) {
11381138
// Signal a spend of the funding transaction and wait for the close
11391139
// observer to exit. This time we are allowing the handoff to succeed.
11401140
breach = &ContractBreachEvent{
1141-
ChanPoint: *chanPoint,
1141+
ChanPoint: chanPoint,
11421142
ProcessACK: func(brarErr error) {
11431143
processACK <- brarErr
11441144
},
@@ -1166,7 +1166,7 @@ func TestBreachHandoffFail(t *testing.T) {
11661166
// Check that the breach was properly recorded in the breach arbiter,
11671167
// and that the close observer marked the channel as pending closed
11681168
// before exiting.
1169-
assertArbiterBreach(t, brar, chanPoint)
1169+
assertArbiterBreach(t, brar, &chanPoint)
11701170
}
11711171

11721172
// TestBreachCreateJusticeTx tests that we create three different variants of
@@ -1560,7 +1560,7 @@ func testBreachSpends(t *testing.T, test breachTest) {
15601560
var (
15611561
height = bobClose.ChanSnapshot.CommitHeight
15621562
forceCloseTx = bobClose.CloseTx
1563-
chanPoint = alice.ChanPoint
1563+
chanPoint = alice.ChannelPoint()
15641564
publTx = make(chan *wire.MsgTx)
15651565
publErr error
15661566
publMtx sync.Mutex
@@ -1590,7 +1590,7 @@ func testBreachSpends(t *testing.T, test breachTest) {
15901590

15911591
processACK := make(chan error)
15921592
breach := &ContractBreachEvent{
1593-
ChanPoint: *chanPoint,
1593+
ChanPoint: chanPoint,
15941594
ProcessACK: func(brarErr error) {
15951595
processACK <- brarErr
15961596
},
@@ -1630,7 +1630,7 @@ func testBreachSpends(t *testing.T, test breachTest) {
16301630
// After exiting, the breach arbiter should have persisted the
16311631
// retribution information and the channel should be shown as pending
16321632
// force closed.
1633-
assertArbiterBreach(t, brar, chanPoint)
1633+
assertArbiterBreach(t, brar, &chanPoint)
16341634

16351635
// Assert that the database sees the channel as pending close, otherwise
16361636
// the breach arbiter won't be able to fully close it.
@@ -1669,7 +1669,7 @@ func testBreachSpends(t *testing.T, test breachTest) {
16691669
htlcOutpoint := retribution.HtlcRetributions[0].OutPoint
16701670

16711671
spendTxs, err := getSpendTransactions(
1672-
brar.cfg.Signer, chanPoint, retribution,
1672+
brar.cfg.Signer, &chanPoint, retribution,
16731673
)
16741674
require.NoError(t, err)
16751675

@@ -1764,7 +1764,7 @@ func testBreachSpends(t *testing.T, test breachTest) {
17641764
}
17651765

17661766
// Assert that the channel is fully resolved.
1767-
assertBrarCleanup(t, brar, alice.ChanPoint, alice.State().Db)
1767+
assertBrarCleanup(t, brar, &chanPoint, alice.State().Db)
17681768
}
17691769

17701770
// TestBreachDelayedJusticeConfirmation tests that the breach arbiter will
@@ -1777,7 +1777,7 @@ func TestBreachDelayedJusticeConfirmation(t *testing.T) {
17771777
height = bobClose.ChanSnapshot.CommitHeight
17781778
blockHeight = int32(10)
17791779
forceCloseTx = bobClose.CloseTx
1780-
chanPoint = alice.ChanPoint
1780+
chanPoint = alice.ChannelPoint()
17811781
publTx = make(chan *wire.MsgTx)
17821782
)
17831783

@@ -1799,7 +1799,7 @@ func TestBreachDelayedJusticeConfirmation(t *testing.T) {
17991799

18001800
processACK := make(chan error, 1)
18011801
breach := &ContractBreachEvent{
1802-
ChanPoint: *chanPoint,
1802+
ChanPoint: chanPoint,
18031803
ProcessACK: func(brarErr error) {
18041804
processACK <- brarErr
18051805
},
@@ -1840,7 +1840,7 @@ func TestBreachDelayedJusticeConfirmation(t *testing.T) {
18401840
// After exiting, the breach arbiter should have persisted the
18411841
// retribution information and the channel should be shown as pending
18421842
// force closed.
1843-
assertArbiterBreach(t, brar, chanPoint)
1843+
assertArbiterBreach(t, brar, &chanPoint)
18441844

18451845
// Assert that the database sees the channel as pending close, otherwise
18461846
// the breach arbiter won't be able to fully close it.
@@ -1965,7 +1965,7 @@ func TestBreachDelayedJusticeConfirmation(t *testing.T) {
19651965
}
19661966

19671967
// Assert that the channel is fully resolved.
1968-
assertBrarCleanup(t, brar, alice.ChanPoint, alice.State().Db)
1968+
assertBrarCleanup(t, brar, &chanPoint, alice.State().Db)
19691969
}
19701970

19711971
// findInputIndex returns the index of the input that spends from the given
@@ -2081,12 +2081,12 @@ func assertPendingClosed(t *testing.T, c *lnwallet.LightningChannel) {
20812081
require.NoError(t, err, "unable to load pending closed channels")
20822082

20832083
for _, chanSummary := range closedChans {
2084-
if chanSummary.ChanPoint == *c.ChanPoint {
2084+
if chanSummary.ChanPoint == c.ChannelPoint() {
20852085
return
20862086
}
20872087
}
20882088

2089-
t.Fatalf("channel %v was not marked pending closed", c.ChanPoint)
2089+
t.Fatalf("channel %v was not marked pending closed", c.ChannelPoint())
20902090
}
20912091

20922092
// assertNotPendingClosed checks that the channel has not been marked pending
@@ -2098,9 +2098,9 @@ func assertNotPendingClosed(t *testing.T, c *lnwallet.LightningChannel) {
20982098
require.NoError(t, err, "unable to load pending closed channels")
20992099

21002100
for _, chanSummary := range closedChans {
2101-
if chanSummary.ChanPoint == *c.ChanPoint {
2101+
if chanSummary.ChanPoint == c.ChannelPoint() {
21022102
t.Fatalf("channel %v was marked pending closed",
2103-
c.ChanPoint)
2103+
c.ChannelPoint())
21042104
}
21052105
}
21062106
}

discovery/gossiper.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1748,8 +1748,9 @@ func (d *AuthenticatedGossiper) processChanPolicyUpdate(
17481748
// update with the peer's alias. We do this after
17491749
// updateChannel so that the alias isn't persisted to
17501750
// the database.
1751-
op := &edgeInfo.Info.ChannelPoint
1752-
chanID := lnwire.NewChanIDFromOutPoint(op)
1751+
chanID := lnwire.NewChanIDFromOutPoint(
1752+
edgeInfo.Info.ChannelPoint,
1753+
)
17531754

17541755
var defaultAlias lnwire.ShortChannelID
17551756
foundAlias, _ := d.cfg.GetAlias(chanID)

0 commit comments

Comments
 (0)