lnwallet+channeldb: add anchor resolutions

Co-authored-by: Joost Jager <joost.jager@gmail.com>
This commit is contained in:
Johan T. Halseth
2019-12-13 11:14:22 +01:00
committed by Joost Jager
parent 30fc03d84d
commit dc6c4637b6
2 changed files with 199 additions and 3 deletions

View File

@@ -526,13 +526,36 @@ func TestCooperativeChannelClosure(t *testing.T) {
// force close generates HTLC resolutions that are capable of sweeping both
// incoming and outgoing HTLC's.
func TestForceClose(t *testing.T) {
t.Run("tweakless", func(t *testing.T) {
testForceClose(t, &forceCloseTestCase{
chanType: channeldb.SingleFunderTweaklessBit,
expectedCommitWeight: input.CommitWeight,
})
})
t.Run("anchors", func(t *testing.T) {
testForceClose(t, &forceCloseTestCase{
chanType: channeldb.SingleFunderTweaklessBit |
channeldb.AnchorOutputsBit,
expectedCommitWeight: input.AnchorCommitWeight,
anchorAmt: anchorSize * 2,
})
})
}
type forceCloseTestCase struct {
chanType channeldb.ChannelType
expectedCommitWeight int64
anchorAmt btcutil.Amount
}
func testForceClose(t *testing.T, testCase *forceCloseTestCase) {
t.Parallel()
// Create a test channel which will be used for the duration of this
// unittest. The channel will be funded evenly with Alice having 5 BTC,
// and Bob having 5 BTC.
aliceChannel, bobChannel, cleanUp, err := CreateTestChannels(
channeldb.SingleFunderTweaklessBit,
testCase.chanType,
)
if err != nil {
t.Fatalf("unable to create test channels: %v", err)
@@ -594,6 +617,36 @@ func TestForceClose(t *testing.T) {
1, len(closeSummary.HtlcResolutions.IncomingHTLCs))
}
// Verify the anchor resolutions for the anchor commitment format.
if testCase.chanType.HasAnchors() {
// Check the close summary resolution.
anchorRes := closeSummary.AnchorResolution
if anchorRes == nil {
t.Fatal("expected anchor resolution")
}
if anchorRes.CommitAnchor.Hash != closeSummary.CloseTx.TxHash() {
t.Fatal("commit tx not referenced by anchor res")
}
if anchorRes.AnchorSignDescriptor.Output.Value !=
int64(anchorSize) {
t.Fatal("unexpected anchor size")
}
if anchorRes.AnchorSignDescriptor.WitnessScript == nil {
t.Fatal("expected anchor witness script")
}
// Check the pre-confirmation resolutions.
resList, err := aliceChannel.NewAnchorResolutions()
if err != nil {
t.Fatalf("pre-confirmation resolution error: %v", err)
}
if len(resList) != 2 {
t.Fatal("expected two resolutions")
}
}
// The SelfOutputSignDesc should be non-nil since the output to-self is
// non-dust.
aliceCommitResolution := closeSummary.CommitResolution
@@ -612,12 +665,16 @@ func TestForceClose(t *testing.T) {
// Factoring in the fee rate, Alice's amount should properly reflect
// that we've added two additional HTLC to the commitment transaction.
totalCommitWeight := int64(input.CommitWeight + (input.HTLCWeight * 2))
totalCommitWeight := testCase.expectedCommitWeight +
(input.HTLCWeight * 2)
feePerKw := chainfee.SatPerKWeight(
aliceChannel.channelState.LocalCommitment.FeePerKw,
)
commitFee := feePerKw.FeeForWeight(totalCommitWeight)
expectedAmount := (aliceChannel.Capacity / 2) - htlcAmount.ToSatoshis() - commitFee
expectedAmount := (aliceChannel.Capacity / 2) -
htlcAmount.ToSatoshis() - commitFee - testCase.anchorAmt
if aliceCommitResolution.SelfOutputSignDesc.Output.Value != int64(expectedAmount) {
t.Fatalf("alice incorrect output value in SelfOutputSignDesc, "+
"expected %v, got %v", int64(expectedAmount),