Skip to content

Commit ed15494

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 d16810a commit ed15494

2 files changed

Lines changed: 23 additions & 14 deletions

File tree

lnwallet/channel.go

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2108,7 +2108,12 @@ func NewBreachRetribution(chanState *channeldb.OpenChannel, stateNum uint64,
21082108
return l.LocalAuxLeaf
21092109
},
21102110
)(auxResult.AuxLeaves)
2111-
theirDelay := uint32(chanState.ChanCfgs.Remote.CsvDelay)
2111+
theirDelay := uint32(
2112+
chanState.CommitChainEpochHistory.NormalizedParamsAt(
2113+
lntypes.Remote, stateNum,
2114+
).CsvDelay,
2115+
)
2116+
21122117
theirScript, err := CommitScriptToSelf(
21132118
chanState.ChanType, isRemoteInitiator, keyRing.ToLocalKey,
21142119
keyRing.RevocationKey, theirDelay, leaseExpiry, localAuxLeaf,
@@ -2128,7 +2133,7 @@ func NewBreachRetribution(chanState *channeldb.OpenChannel, stateNum uint64,
21282133
// we need.
21292134
if revokedLog != nil {
21302135
br, ourAmt, theirAmt, err = createBreachRetribution(
2131-
revokedLog, spendTx, chanState, keyRing,
2136+
revokedLog, spendTx, chanState, stateNum, keyRing,
21322137
commitmentSecret, leaseExpiry, auxResult.AuxLeaves,
21332138
)
21342139
if err != nil {
@@ -2143,8 +2148,8 @@ func NewBreachRetribution(chanState *channeldb.OpenChannel, stateNum uint64,
21432148
// data can still function. This branch can be deleted once we
21442149
// are confident that no legacy format is in use.
21452150
br, ourAmt, theirAmt, err = createBreachRetributionLegacy(
2146-
revokedLogLegacy, chanState, keyRing, commitmentSecret,
2147-
ourScript, theirScript, leaseExpiry,
2151+
revokedLogLegacy, chanState, stateNum, keyRing,
2152+
commitmentSecret, ourScript, theirScript, leaseExpiry,
21482153
)
21492154
if err != nil {
21502155
return nil, err
@@ -2325,15 +2330,19 @@ func NewBreachRetribution(chanState *channeldb.OpenChannel, stateNum uint64,
23252330

23262331
// createHtlcRetribution is a helper function to construct an HtlcRetribution
23272332
// based on the passed params.
2328-
func createHtlcRetribution(chanState *channeldb.OpenChannel,
2333+
func createHtlcRetribution(chanState *channeldb.OpenChannel, stateNum uint64,
23292334
keyRing *CommitmentKeyRing, commitHash chainhash.Hash,
23302335
commitmentSecret *btcec.PrivateKey, leaseExpiry uint32,
23312336
htlc *channeldb.HTLCEntry,
23322337
auxLeaves fn.Option[CommitAuxLeaves]) (HtlcRetribution, error) {
23332338

23342339
var emptyRetribution HtlcRetribution
23352340

2336-
theirDelay := uint32(chanState.ChanCfgs.Remote.CsvDelay)
2341+
theirDelay := uint32(
2342+
chanState.CommitChainEpochHistory.NormalizedParamsAt(
2343+
lntypes.Remote, stateNum,
2344+
).CsvDelay,
2345+
)
23372346
isRemoteInitiator := !chanState.IsInitiator
23382347

23392348
// We'll generate the original second level witness script now, as
@@ -2446,7 +2455,7 @@ func createHtlcRetribution(chanState *channeldb.OpenChannel,
24462455
// see if these fields are present there. If they are not, then
24472456
// ErrRevLogDataMissing is returned.
24482457
func createBreachRetribution(revokedLog *channeldb.RevocationLog,
2449-
spendTx *wire.MsgTx, chanState *channeldb.OpenChannel,
2458+
spendTx *wire.MsgTx, chanState *channeldb.OpenChannel, stateNum uint64,
24502459
keyRing *CommitmentKeyRing, commitmentSecret *btcec.PrivateKey,
24512460
leaseExpiry uint32,
24522461
auxLeaves fn.Option[CommitAuxLeaves]) (*BreachRetribution, int64, int64,
@@ -2458,7 +2467,7 @@ func createBreachRetribution(revokedLog *channeldb.RevocationLog,
24582467
htlcRetributions := make([]HtlcRetribution, len(revokedLog.HTLCEntries))
24592468
for i, htlc := range revokedLog.HTLCEntries {
24602469
hr, err := createHtlcRetribution(
2461-
chanState, keyRing, commitHash.Val,
2470+
chanState, stateNum, keyRing, commitHash.Val,
24622471
commitmentSecret, leaseExpiry, htlc, auxLeaves,
24632472
)
24642473
if err != nil {
@@ -2562,8 +2571,8 @@ func createBreachRetribution(revokedLog *channeldb.RevocationLog,
25622571
// BreachRetribution using a ChannelCommitment. Returns the constructed
25632572
// retribution, our amount, their amount, and a possible non-nil error.
25642573
func createBreachRetributionLegacy(revokedLog *channeldb.ChannelCommitment,
2565-
chanState *channeldb.OpenChannel, keyRing *CommitmentKeyRing,
2566-
commitmentSecret *btcec.PrivateKey,
2574+
chanState *channeldb.OpenChannel, stateNum uint64,
2575+
keyRing *CommitmentKeyRing, commitmentSecret *btcec.PrivateKey,
25672576
ourScript, theirScript input.ScriptDescriptor,
25682577
leaseExpiry uint32) (*BreachRetribution, int64, int64, error) {
25692578

@@ -2609,7 +2618,7 @@ func createBreachRetributionLegacy(revokedLog *channeldb.ChannelCommitment,
26092618
}
26102619

26112620
hr, err := createHtlcRetribution(
2612-
chanState, keyRing, commitHash,
2621+
chanState, stateNum, keyRing, commitHash,
26132622
commitmentSecret, leaseExpiry, entry,
26142623
fn.None[CommitAuxLeaves](),
26152624
)

lnwallet/channel_test.go

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

97539753
// Create the htlc retribution.
97549754
hr, err := createHtlcRetribution(
9755-
aliceChannel.channelState, keyRing, commitHash,
9755+
aliceChannel.channelState, 0,keyRing, commitHash,
97569756
dummyPrivate, leaseExpiry, htlc, fn.None[CommitAuxLeaves](),
97579757
)
97589758
// Expect no error.
@@ -9957,7 +9957,7 @@ func TestCreateBreachRetribution(t *testing.T) {
99579957

99589958
br, our, their, err := createBreachRetribution(
99599959
tc.revocationLog, tx,
9960-
aliceChannel.channelState, keyRing,
9960+
aliceChannel.channelState, 0, keyRing,
99619961
dummyPrivate, leaseExpiry,
99629962
fn.None[CommitAuxLeaves](),
99639963
)
@@ -10016,7 +10016,7 @@ func TestCreateBreachRetributionLegacy(t *testing.T) {
1001610016

1001710017
// Create the breach retribution using the legacy format.
1001810018
br, ourAmt, theirAmt, err := createBreachRetributionLegacy(
10019-
&revokedLog, aliceChannel.channelState, keyRing,
10019+
&revokedLog, aliceChannel.channelState, 0, keyRing,
1002010020
dummyPrivate, ourScript, theirScript, leaseExpiry,
1002110021
)
1002210022
require.NoError(t, err)

0 commit comments

Comments
 (0)