Skip to content

Commit 6678f08

Browse files
committed
lnwallet: use CommitChainEpochHistory to determine CsvDelay during Breach
This commit changes the way we create breach retributions to use the CsvDelay we compute from the CommitChainEpochHistory so as to account for the possibility that the channel parameters have changed since opening.
1 parent dc3aef4 commit 6678f08

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

lnwallet/channel.go

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2102,7 +2102,12 @@ func NewBreachRetribution(chanState *channeldb.OpenChannel, stateNum uint64,
21022102
return l.LocalAuxLeaf
21032103
},
21042104
)(auxResult.AuxLeaves)
2105-
theirDelay := uint32(chanState.ChanCfgs.Remote.CsvDelay)
2105+
theirDelay := uint32(
2106+
chanState.CommitChainEpochHistory.NormalizedParamsAt(
2107+
lntypes.Remote, stateNum,
2108+
).CsvDelay,
2109+
)
2110+
21062111
theirScript, err := CommitScriptToSelf(
21072112
chanState.ChanType, isRemoteInitiator, keyRing.ToLocalKey,
21082113
keyRing.RevocationKey, theirDelay, leaseExpiry, localAuxLeaf,
@@ -2122,7 +2127,7 @@ func NewBreachRetribution(chanState *channeldb.OpenChannel, stateNum uint64,
21222127
// we need.
21232128
if revokedLog != nil {
21242129
br, ourAmt, theirAmt, err = createBreachRetribution(
2125-
revokedLog, spendTx, chanState, keyRing,
2130+
revokedLog, spendTx, chanState, stateNum, keyRing,
21262131
commitmentSecret, leaseExpiry, auxResult.AuxLeaves,
21272132
)
21282133
if err != nil {
@@ -2137,8 +2142,8 @@ func NewBreachRetribution(chanState *channeldb.OpenChannel, stateNum uint64,
21372142
// data can still function. This branch can be deleted once we
21382143
// are confident that no legacy format is in use.
21392144
br, ourAmt, theirAmt, err = createBreachRetributionLegacy(
2140-
revokedLogLegacy, chanState, keyRing, commitmentSecret,
2141-
ourScript, theirScript, leaseExpiry,
2145+
revokedLogLegacy, chanState, stateNum, keyRing,
2146+
commitmentSecret, ourScript, theirScript, leaseExpiry,
21422147
)
21432148
if err != nil {
21442149
return nil, err
@@ -2317,15 +2322,19 @@ func NewBreachRetribution(chanState *channeldb.OpenChannel, stateNum uint64,
23172322

23182323
// createHtlcRetribution is a helper function to construct an HtlcRetribution
23192324
// based on the passed params.
2320-
func createHtlcRetribution(chanState *channeldb.OpenChannel,
2325+
func createHtlcRetribution(chanState *channeldb.OpenChannel, stateNum uint64,
23212326
keyRing *CommitmentKeyRing, commitHash chainhash.Hash,
23222327
commitmentSecret *btcec.PrivateKey, leaseExpiry uint32,
23232328
htlc *channeldb.HTLCEntry,
23242329
auxLeaves fn.Option[CommitAuxLeaves]) (HtlcRetribution, error) {
23252330

23262331
var emptyRetribution HtlcRetribution
23272332

2328-
theirDelay := uint32(chanState.ChanCfgs.Remote.CsvDelay)
2333+
theirDelay := uint32(
2334+
chanState.CommitChainEpochHistory.NormalizedParamsAt(
2335+
lntypes.Remote, stateNum,
2336+
).CsvDelay,
2337+
)
23292338
isRemoteInitiator := !chanState.IsInitiator
23302339

23312340
// We'll generate the original second level witness script now, as
@@ -2438,7 +2447,7 @@ func createHtlcRetribution(chanState *channeldb.OpenChannel,
24382447
// see if these fields are present there. If they are not, then
24392448
// ErrRevLogDataMissing is returned.
24402449
func createBreachRetribution(revokedLog *channeldb.RevocationLog,
2441-
spendTx *wire.MsgTx, chanState *channeldb.OpenChannel,
2450+
spendTx *wire.MsgTx, chanState *channeldb.OpenChannel, stateNum uint64,
24422451
keyRing *CommitmentKeyRing, commitmentSecret *btcec.PrivateKey,
24432452
leaseExpiry uint32,
24442453
auxLeaves fn.Option[CommitAuxLeaves]) (*BreachRetribution, int64, int64,
@@ -2450,7 +2459,7 @@ func createBreachRetribution(revokedLog *channeldb.RevocationLog,
24502459
htlcRetributions := make([]HtlcRetribution, len(revokedLog.HTLCEntries))
24512460
for i, htlc := range revokedLog.HTLCEntries {
24522461
hr, err := createHtlcRetribution(
2453-
chanState, keyRing, commitHash.Val,
2462+
chanState, stateNum, keyRing, commitHash.Val,
24542463
commitmentSecret, leaseExpiry, htlc, auxLeaves,
24552464
)
24562465
if err != nil {
@@ -2554,8 +2563,8 @@ func createBreachRetribution(revokedLog *channeldb.RevocationLog,
25542563
// BreachRetribution using a ChannelCommitment. Returns the constructed
25552564
// retribution, our amount, their amount, and a possible non-nil error.
25562565
func createBreachRetributionLegacy(revokedLog *channeldb.ChannelCommitment,
2557-
chanState *channeldb.OpenChannel, keyRing *CommitmentKeyRing,
2558-
commitmentSecret *btcec.PrivateKey,
2566+
chanState *channeldb.OpenChannel, stateNum uint64,
2567+
keyRing *CommitmentKeyRing, commitmentSecret *btcec.PrivateKey,
25592568
ourScript, theirScript input.ScriptDescriptor,
25602569
leaseExpiry uint32) (*BreachRetribution, int64, int64, error) {
25612570

@@ -2601,7 +2610,7 @@ func createBreachRetributionLegacy(revokedLog *channeldb.ChannelCommitment,
26012610
}
26022611

26032612
hr, err := createHtlcRetribution(
2604-
chanState, keyRing, commitHash,
2613+
chanState, stateNum, keyRing, commitHash,
26052614
commitmentSecret, leaseExpiry, entry,
26062615
fn.None[CommitAuxLeaves](),
26072616
)

lnwallet/channel_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9807,7 +9807,7 @@ func TestCreateHtlcRetribution(t *testing.T) {
98079807

98089808
// Create the htlc retribution.
98099809
hr, err := createHtlcRetribution(
9810-
aliceChannel.channelState, keyRing, commitHash,
9810+
aliceChannel.channelState, 0,keyRing, commitHash,
98119811
dummyPrivate, leaseExpiry, htlc, fn.None[CommitAuxLeaves](),
98129812
)
98139813
// Expect no error.
@@ -10012,7 +10012,7 @@ func TestCreateBreachRetribution(t *testing.T) {
1001210012

1001310013
br, our, their, err := createBreachRetribution(
1001410014
tc.revocationLog, tx,
10015-
aliceChannel.channelState, keyRing,
10015+
aliceChannel.channelState, 0, keyRing,
1001610016
dummyPrivate, leaseExpiry,
1001710017
fn.None[CommitAuxLeaves](),
1001810018
)
@@ -10071,7 +10071,7 @@ func TestCreateBreachRetributionLegacy(t *testing.T) {
1007110071

1007210072
// Create the breach retribution using the legacy format.
1007310073
br, ourAmt, theirAmt, err := createBreachRetributionLegacy(
10074-
&revokedLog, aliceChannel.channelState, keyRing,
10074+
&revokedLog, aliceChannel.channelState, 0, keyRing,
1007510075
dummyPrivate, ourScript, theirScript, leaseExpiry,
1007610076
)
1007710077
require.NoError(t, err)

0 commit comments

Comments
 (0)