From 1621b8cc99dfbf3fe6f1adb2df81246320d378f3 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Wed, 16 Dec 2015 14:41:15 -0600 Subject: [PATCH] wallet/wallet_test: test for canceling reservations --- wallet/wallet_test.go | 82 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 80 insertions(+), 2 deletions(-) diff --git a/wallet/wallet_test.go b/wallet/wallet_test.go index 64bab7ff3..6d0ecb419 100644 --- a/wallet/wallet_test.go +++ b/wallet/wallet_test.go @@ -435,6 +435,7 @@ func TestFundingTransactionLockedOutputs(t *testing.T) { // Create two channels, both asking for 8 BTC each, totalling 16 // BTC. // TODO(roasbeef): tests for concurrent funding. + // * also func for below fundingAmount := btcutil.Amount(8 * 1e8) chanReservation1, err := lnwallet.InitChannelReservation(fundingAmount, SIGHASH) if err != nil { @@ -482,10 +483,87 @@ func TestFundingTransactionLockedOutputs(t *testing.T) { } } -func TestFundingTransactionCancellationFreeOutputs(t *testing.T) { +func TestFundingCancellationNotEnoughFunds(t *testing.T) { + // TODO(roasbeef): factor out, such copy pasta + // Create our test wallet, will have a total of 20 BTC available for + // funding via 5 outputs with 4BTC each. + testDir, lnwallet, err := createTestWallet() + if err != nil { + t.Fatalf("unable to create test ln wallet: %v", err) + } + defer os.RemoveAll(testDir) + defer lnwallet.Stop() + + // The wallet should now have 20BTC available for spending. + assertProperBalance(t, lnwallet, 1, 20) + + // Create a reservation for 12 BTC. + fundingAmount := btcutil.Amount(12 * 1e8) + chanReservation, err := lnwallet.InitChannelReservation(fundingAmount, SIGHASH) + if err != nil { + t.Fatalf("unable to initialize funding reservation: %v", err) + } + + // There should be three locked outpoints. + lockedOutPoints := lnwallet.wallet.LockedOutpoints() + if len(lockedOutPoints) != 3 { + t.Fatalf("two outpoints should now be locked, instead %v are", + lockedOutPoints) + } + + // Attempt to create another channel with 12 BTC, this should fail. + failedReservation, err := lnwallet.InitChannelReservation(fundingAmount, SIGHASH) + if err != coinset.ErrCoinsNoSelectionAvailable { + t.Fatalf("coin selection succeded should have insufficient funds: %+v", + failedReservation) + } + + // Now cancel that old reservation. + if err := chanReservation.Cancel(); err != nil { + t.Fatalf("unable to cancel reservation: %v", err) + } + + // Those outpoints should no longer be locked. + lockedOutPoints = lnwallet.wallet.LockedOutpoints() + if len(lockedOutPoints) != 0 { + t.Fatalf("outpoints still locked") + } + + // Reservation ID should now longer be tracked. + _, ok := lnwallet.fundingLimbo[chanReservation.reservationID] + if ok { + t.Fatalf("funding reservation still in map") + } + + // TODO(roasbeef): create method like Balance that ignores locked + // outpoints, will let us fail early/fast instead of querying and + // attempting coin selection. + + // Request to fund a new channel should now succeeed. + _, err = lnwallet.InitChannelReservation(fundingAmount, SIGHASH) + if err != nil { + t.Fatalf("unable to initialize funding reservation: %v", err) + } } -func TestFundingReservationInsufficientFunds(t *testing.T) { +func TestCancelNonExistantReservation(t *testing.T) { + // Create our test wallet, will have a total of 20 BTC available for + // funding via 5 outputs with 4BTC each. + testDir, lnwallet, err := createTestWallet() + if err != nil { + t.Fatalf("unable to create test ln wallet: %v", err) + } + defer os.RemoveAll(testDir) + defer lnwallet.Stop() + + // Create our own reservation, give it some ID. + res := newChannelReservation(SIGHASH, 1000, 5000, lnwallet, 22) + + // Attempt to cancel this reservation. This should fail, we know + // nothing of it. + if err = res.Cancel(); err == nil { + t.Fatalf("cancelled non-existant reservation") + } } func TestFundingReservationInvalidCounterpartySigs(t *testing.T) {