mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-09-19 03:57:40 +02:00
contractcourt/timeout_resolver: extract logic into sweepSecondLevelTransaction
This commit moves the logic for sweeping the confirmed second-level timeout transaction into its own method. We do a small change to the logic: When setting the spending tx in the report, we use the detected commitspend instead of the presigned tiemout tx. This is to prepare for the coming change where the spending transaction might actually be a re-signed timeout tx, and will therefore have a different txid.
This commit is contained in:
@@ -3,6 +3,7 @@ package contractcourt
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -17,6 +18,7 @@ import (
|
||||
"github.com/lightningnetwork/lnd/lntest/mock"
|
||||
"github.com/lightningnetwork/lnd/lntypes"
|
||||
"github.com/lightningnetwork/lnd/lnwallet"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
type mockWitnessBeacon struct {
|
||||
@@ -127,6 +129,16 @@ func TestHtlcTimeoutResolver(t *testing.T) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// To avoid triggering the race detector by
|
||||
// setting the witness the second time this
|
||||
// method is called during tests, we return
|
||||
// immediately if the witness is already set
|
||||
// correctly.
|
||||
if reflect.DeepEqual(
|
||||
templateTx.TxIn[0].Witness, witness,
|
||||
) {
|
||||
return templateTx, nil
|
||||
}
|
||||
templateTx.TxIn[0].Witness = witness
|
||||
return templateTx, nil
|
||||
},
|
||||
@@ -148,6 +160,17 @@ func TestHtlcTimeoutResolver(t *testing.T) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// To avoid triggering the race detector by
|
||||
// setting the witness the second time this
|
||||
// method is called during tests, we return
|
||||
// immediately if the witness is already set
|
||||
// correctly.
|
||||
if reflect.DeepEqual(
|
||||
templateTx.TxIn[0].Witness, witness,
|
||||
) {
|
||||
return templateTx, nil
|
||||
}
|
||||
|
||||
templateTx.TxIn[0].Witness = witness
|
||||
|
||||
// Set the outpoint to be on our commitment, since
|
||||
@@ -174,6 +197,17 @@ func TestHtlcTimeoutResolver(t *testing.T) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// To avoid triggering the race detector by
|
||||
// setting the witness the second time this
|
||||
// method is called during tests, we return
|
||||
// immediately if the witness is already set
|
||||
// correctly.
|
||||
if reflect.DeepEqual(
|
||||
templateTx.TxIn[0].Witness, witness,
|
||||
) {
|
||||
return templateTx, nil
|
||||
}
|
||||
|
||||
templateTx.TxIn[0].Witness = witness
|
||||
return templateTx, nil
|
||||
},
|
||||
@@ -196,6 +230,17 @@ func TestHtlcTimeoutResolver(t *testing.T) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// To avoid triggering the race detector by
|
||||
// setting the witness the second time this
|
||||
// method is called during tests, we return
|
||||
// immediately if the witness is already set
|
||||
// correctly.
|
||||
if reflect.DeepEqual(
|
||||
templateTx.TxIn[0].Witness, witness,
|
||||
) {
|
||||
return templateTx, nil
|
||||
}
|
||||
|
||||
templateTx.TxIn[0].Witness = witness
|
||||
return templateTx, nil
|
||||
},
|
||||
@@ -282,16 +327,19 @@ func TestHtlcTimeoutResolver(t *testing.T) {
|
||||
// broadcast, then we'll set the timeout commit to a fake
|
||||
// transaction to force the code path.
|
||||
if !testCase.remoteCommit {
|
||||
resolver.htlcResolution.SignedTimeoutTx = sweepTx
|
||||
timeoutTx, err := testCase.txToBroadcast()
|
||||
require.NoError(t, err)
|
||||
|
||||
resolver.htlcResolution.SignedTimeoutTx = timeoutTx
|
||||
|
||||
if testCase.timeout {
|
||||
success := sweepTx.TxHash()
|
||||
timeoutTxID := timeoutTx.TxHash()
|
||||
reports = append(reports, &channeldb.ResolverReport{
|
||||
OutPoint: sweepTx.TxIn[0].PreviousOutPoint,
|
||||
OutPoint: timeoutTx.TxIn[0].PreviousOutPoint,
|
||||
Amount: testHtlcAmt.ToSatoshis(),
|
||||
ResolverType: channeldb.ResolverTypeOutgoingHtlc,
|
||||
ResolverOutcome: channeldb.ResolverOutcomeFirstStage,
|
||||
SpendTxID: &success,
|
||||
SpendTxID: &timeoutTxID,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user