From 85b2b52a5fdd048269e7d85463ea392fde58442d Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Mon, 26 Sep 2016 12:16:12 -0700 Subject: [PATCH] lnwallet: add concrete type for coin selection fail during funding workflow --- lnwallet/interface_test.go | 4 ++-- lnwallet/wallet.go | 22 +++++++++++++++------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/lnwallet/interface_test.go b/lnwallet/interface_test.go index ff9b5489..8687ad93 100644 --- a/lnwallet/interface_test.go +++ b/lnwallet/interface_test.go @@ -523,7 +523,7 @@ func testFundingTransactionLockedOutputs(miner *rpctest.Harness, if err == nil { t.Fatalf("not error returned, should fail on coin selection") } - if err != lnwallet.ErrInsufficientFunds { + if _, ok := err.(*lnwallet.ErrInsufficientFunds); !ok { t.Fatalf("error not coinselect error: %v", err) } if failedReservation != nil { @@ -545,7 +545,7 @@ func testFundingCancellationNotEnoughFunds(miner *rpctest.Harness, // Attempt to create another channel with 44 BTC, this should fail. _, err = wallet.InitChannelReservation(fundingAmount, fundingAmount, testHdSeed, numReqConfs, 4) - if err != lnwallet.ErrInsufficientFunds { + if _, ok := err.(*lnwallet.ErrInsufficientFunds); !ok { t.Fatalf("coin selection succeded should have insufficient funds: %v", err) } diff --git a/lnwallet/wallet.go b/lnwallet/wallet.go index b7d555b5..ec05964b 100644 --- a/lnwallet/wallet.go +++ b/lnwallet/wallet.go @@ -1,7 +1,6 @@ package lnwallet import ( - "errors" "fmt" "sync" "sync/atomic" @@ -40,9 +39,6 @@ const ( ) var ( - // Error types - ErrInsufficientFunds = errors.New("not enough available outputs to " + - "create funding transaction") // Namespace bucket keys. lightningNamespaceKey = []byte("ln-wallet") @@ -50,6 +46,20 @@ var ( wtxmgrNamespaceKey = []byte("wtxmgr") ) +// ErrInsufficientFunds is a type matching the error interface which is +// returned when coin selection for a new funding transaction fails to due +// having an insufficient amount of confirmed funds. +type ErrInsufficientFunds struct { + amountAvailable btcutil.Amount + amountSelected btcutil.Amount +} + +func (e *ErrInsufficientFunds) Error() string { + return fmt.Sprintf("not enough outputs to create funding transaction,"+ + " need %v only have %v available", e.amountAvailable, + e.amountSelected) +} + // initFundingReserveReq is the first message sent to initiate the workflow // required to open a payment channel with a remote peer. The initial required // paramters are configurable accross channels. These paramters are to be chosen @@ -1283,9 +1293,7 @@ func selectInputs(amt btcutil.Amount, coins []*Utxo) (btcutil.Amount, []*wire.Ou // If we're about to go past the number of available coins, // then exit with an error. if i > len(coins)-1 { - return 0, nil, fmt.Errorf("not enough outputs to create "+ - "funding transaction, need %v only have %v "+ - "available", amt, satSelected) + return 0, nil, &ErrInsufficientFunds{amt, satSelected} } // Otherwise, collect this new coin as it may be used for final