lnd_test: extend on chain recovery test to assert utxos

This commit is contained in:
Olaoluwa Osuntokun 2019-02-11 13:02:00 -08:00 committed by Conner Fromknecht
parent cb2723236f
commit 932fafd7cd
No known key found for this signature in database
GPG Key ID: E7D737B67FA592C7

@ -10,6 +10,7 @@ import (
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"math"
"os" "os"
"path/filepath" "path/filepath"
"reflect" "reflect"
@ -643,8 +644,8 @@ func testOnchainFundRecovery(net *lntest.NetworkHarness, t *harnessTest) {
// method takes the expected value of Carol's balance when using the // method takes the expected value of Carol's balance when using the
// given recovery window. Additionally, the caller can specify an action // given recovery window. Additionally, the caller can specify an action
// to perform on the restored node before the node is shutdown. // to perform on the restored node before the node is shutdown.
restoreCheckBalance := func(expAmount int64, recoveryWindow int32, restoreCheckBalance := func(expAmount int64, expectedNumUTXOs int,
fn func(*lntest.HarnessNode)) { recoveryWindow int32, fn func(*lntest.HarnessNode)) {
// Restore Carol, passing in the password, mnemonic, and // Restore Carol, passing in the password, mnemonic, and
// desired recovery window. // desired recovery window.
@ -655,8 +656,12 @@ func testOnchainFundRecovery(net *lntest.NetworkHarness, t *harnessTest) {
t.Fatalf("unable to restore node: %v", err) t.Fatalf("unable to restore node: %v", err)
} }
// Query carol for her current wallet balance. // Query carol for her current wallet balance, and also that we
var currBalance int64 // gain the expected number of UTXOs.
var (
currBalance int64
currNumUTXOs uint32
)
err = lntest.WaitPredicate(func() bool { err = lntest.WaitPredicate(func() bool {
req := &lnrpc.WalletBalanceRequest{} req := &lnrpc.WalletBalanceRequest{}
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout) ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
@ -673,12 +678,27 @@ func testOnchainFundRecovery(net *lntest.NetworkHarness, t *harnessTest) {
return false return false
} }
utxoReq := &lnrpc.ListUnspentRequest{
MaxConfs: math.MaxInt32,
}
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
utxoResp, err := node.ListUnspent(ctxt, utxoReq)
if err != nil {
t.Fatalf("unable to query utxos: %v", err)
}
currNumUTXOs := len(utxoResp.Utxos)
if currNumUTXOs != expectedNumUTXOs {
return false
}
return true return true
}, 15*time.Second) }, 15*time.Second)
if err != nil { if err != nil {
t.Fatalf("expected restored node to have %d satoshis, "+ t.Fatalf("expected restored node to have %d satoshis, "+
"instead has %d satoshis", expAmount, "instead has %d satoshis, expected %d utxos "+
currBalance) "instead has %d", expAmount, currBalance,
expectedNumUTXOs, currNumUTXOs)
} }
// If the user provided a callback, execute the commands against // If the user provided a callback, execute the commands against
@ -750,37 +770,40 @@ func testOnchainFundRecovery(net *lntest.NetworkHarness, t *harnessTest) {
// //
// After, one BTC is sent to both her first external P2WKH and NP2WKH // After, one BTC is sent to both her first external P2WKH and NP2WKH
// addresses. // addresses.
restoreCheckBalance(0, 0, skipAndSend(0)) restoreCheckBalance(0, 0, 0, skipAndSend(0))
// Check that restoring without a look-ahead results in having no funds // Check that restoring without a look-ahead results in having no funds
// in the wallet, even though they exist on-chain. // in the wallet, even though they exist on-chain.
restoreCheckBalance(0, 0, nil) restoreCheckBalance(0, 0, 0, nil)
// Now, check that using a look-ahead of 1 recovers the balance from the // Now, check that using a look-ahead of 1 recovers the balance from
// two transactions above. // the two transactions above. We should also now have 2 UTXOs in the
// wallet at the end of the recovery attempt.
// //
// After, we will generate and skip 9 P2WKH and NP2WKH addresses, and // After, we will generate and skip 9 P2WKH and NP2WKH addresses, and
// send another BTC to the subsequent 10th address in each derivation // send another BTC to the subsequent 10th address in each derivation
// path. // path.
restoreCheckBalance(2*btcutil.SatoshiPerBitcoin, 1, skipAndSend(9)) restoreCheckBalance(2*btcutil.SatoshiPerBitcoin, 2, 1, skipAndSend(9))
// Check that using a recovery window of 9 does not find the two most // Check that using a recovery window of 9 does not find the two most
// recent txns. // recent txns.
restoreCheckBalance(2*btcutil.SatoshiPerBitcoin, 9, nil) restoreCheckBalance(2*btcutil.SatoshiPerBitcoin, 2, 9, nil)
// Extending our recovery window to 10 should find the most recent // Extending our recovery window to 10 should find the most recent
// transactions, leaving the wallet with 4 BTC total. // transactions, leaving the wallet with 4 BTC total. We should also
// learn of the two additional UTXOs created above.
// //
// After, we will skip 19 more addrs, sending to the 20th address past // After, we will skip 19 more addrs, sending to the 20th address past
// our last found address, and repeat the same checks. // our last found address, and repeat the same checks.
restoreCheckBalance(4*btcutil.SatoshiPerBitcoin, 10, skipAndSend(19)) restoreCheckBalance(4*btcutil.SatoshiPerBitcoin, 4, 10, skipAndSend(19))
// Check that recovering with a recovery window of 19 fails to find the // Check that recovering with a recovery window of 19 fails to find the
// most recent transactions. // most recent transactions.
restoreCheckBalance(4*btcutil.SatoshiPerBitcoin, 19, nil) restoreCheckBalance(4*btcutil.SatoshiPerBitcoin, 4, 19, nil)
// Ensure that using a recovery window of 20 succeeds. // Ensure that using a recovery window of 20 succeeds with all UTXOs
restoreCheckBalance(6*btcutil.SatoshiPerBitcoin, 20, nil) // found and the final balance reflected.
restoreCheckBalance(6*btcutil.SatoshiPerBitcoin, 6, 20, nil)
} }
// testBasicChannelFunding performs a test exercising expected behavior from a // testBasicChannelFunding performs a test exercising expected behavior from a