From 130124bb796a878052697a0f51814d4400e20175 Mon Sep 17 00:00:00 2001 From: Wilmer Paulino Date: Thu, 9 Aug 2018 21:16:20 -0700 Subject: [PATCH] lntest: add SendCoinsUnconfimed method In this commit, we add a new method to the network harness that allows us to send funds to another party without confirming the transaction. This will be useful for testing funding channels with outputs that have not been confirmed yet. --- lntest/harness.go | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/lntest/harness.go b/lntest/harness.go index d31c5592f..1a6ed8674 100644 --- a/lntest/harness.go +++ b/lntest/harness.go @@ -1104,12 +1104,26 @@ func (n *NetworkHarness) DumpLogs(node *HarnessNode) (string, error) { } // SendCoins attempts to send amt satoshis from the internal mining node to the -// targeted lightning node using a P2WKH address. +// targeted lightning node using a P2WKH address. 6 blocks are mined after in +// order to confirm the transaction. func (n *NetworkHarness) SendCoins(ctx context.Context, amt btcutil.Amount, target *HarnessNode) error { return n.sendCoins( ctx, amt, target, lnrpc.NewAddressRequest_WITNESS_PUBKEY_HASH, + true, + ) +} + +// SendCoinsUnconfirmed sends coins from the internal mining node to the target +// lightning node using a P2WPKH address. No blocks are mined after, so the +// transaction remains unconfirmed. +func (n *NetworkHarness) SendCoinsUnconfirmed(ctx context.Context, + amt btcutil.Amount, target *HarnessNode) error { + + return n.sendCoins( + ctx, amt, target, lnrpc.NewAddressRequest_WITNESS_PUBKEY_HASH, + false, ) } @@ -1120,14 +1134,16 @@ func (n *NetworkHarness) SendCoinsNP2WKH(ctx context.Context, return n.sendCoins( ctx, amt, target, lnrpc.NewAddressRequest_NESTED_PUBKEY_HASH, + true, ) } // sendCoins attempts to send amt satoshis from the internal mining node to the -// targeted lightning node. +// targeted lightning node. The confirmed boolean indicates whether the +// transaction that pays to the target should confirm. func (n *NetworkHarness) sendCoins(ctx context.Context, amt btcutil.Amount, - target *HarnessNode, - addrType lnrpc.NewAddressRequest_AddressType) error { + target *HarnessNode, addrType lnrpc.NewAddressRequest_AddressType, + confirmed bool) error { balReq := &lnrpc.WalletBalanceRequest{} initialBalance, err := target.WalletBalance(ctx, balReq) @@ -1165,8 +1181,17 @@ func (n *NetworkHarness) sendCoins(ctx context.Context, amt btcutil.Amount, return err } - // Finally, generate 6 new blocks to ensure the output gains a - // sufficient number of confirmations. + // If the transaction should remain unconfirmed, then we'll wait until + // the target node's unconfirmed balance reflects the expected balance + // and exit. + if !confirmed { + expectedBalance := initialBalance.UnconfirmedBalance + int64(amt) + return target.WaitForBalance(expectedBalance, false) + } + + // Otherwise, we'll generate 6 new blocks to ensure the output gains a + // sufficient number of confirmations and wait for the balance to + // reflect what's expected. if _, err := n.Miner.Node.Generate(6); err != nil { return err }