Skip to content

Commit

Permalink
test importAccount scenario
Browse files Browse the repository at this point in the history
Signed-off-by: Ononiwu Maureen <59079323+Chinwendu20@users.noreply.github.com>
  • Loading branch information
Chinwendu20 authored and kaloudis committed Aug 21, 2024
1 parent 617aa69 commit 41ce280
Show file tree
Hide file tree
Showing 14 changed files with 1,164 additions and 802 deletions.
80 changes: 62 additions & 18 deletions itest/lnd_wallet_import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func psbtSendFromImportedAccount(ht *lntest.HarnessTest, srcNode, destNode,
default:
ht.Fatalf("unsupported addr type %v", accountAddrType)
}
changeUtxoAmt := confBalance - destAmt - expTxFee
changeUtxoAmt := 100000000 - destAmt - expTxFee

// If the transaction was created from the default imported account,
// then any change produced is moved to the default wallet account.
Expand All @@ -217,11 +217,11 @@ func psbtSendFromImportedAccount(ht *lntest.HarnessTest, srcNode, destNode,
accountWithBalance = defaultAccount
}
ht.AssertWalletAccountBalance(
srcNode, accountWithBalance, 0, changeUtxoAmt,
srcNode, accountWithBalance, 8000, changeUtxoAmt,
)
ht.MineBlocksAndAssertNumTxes(1, 1)
ht.AssertWalletAccountBalance(
srcNode, accountWithBalance, changeUtxoAmt, 0,
srcNode, accountWithBalance, changeUtxoAmt+8000, 0,
)

// Finally, assert that the transaction has the expected change address
Expand Down Expand Up @@ -520,7 +520,11 @@ func testWalletImportAccountScenario(ht *lntest.HarnessTest,
// NOTE: we won't use standby nodes here since the test will change
// each of the node's wallet state.
carol := ht.NewNode("carol", nil)
dave := ht.NewNode("dave", nil)

dave, _, _ := ht.NewNodeWithSeed("dave",
[]string{"--reset-wallet-transactions"},
[]byte("bensonidahosa"),
false)

runWalletImportAccountScenario(ht, addrType, carol, dave)
}
Expand All @@ -538,11 +542,33 @@ func runWalletImportAccountScenario(ht *lntest.HarnessTest,
require.Len(ht, listResp.Accounts, 1)
carolAccount := listResp.Accounts[0]

// Generate an address for carol and send coins to it,
// when we import carol's account into dave's,
// we would generate this address in dave's node and test that we can
// recover the funds that we sent to this address after a restart of
// Dave's node.
r := carol.RPC.NewAddress(&lnrpc.NewAddressRequest{
Type: walletToLNAddrType(ht.T, addrType),
})

ht.FundCoins(100000, ht.Alice)

req := &lnrpc.SendCoinsRequest{
Addr: r.Address,
Amount: 8000,
SatPerByte: 1,
}
ht.Alice.RPC.SendCoins(req)
ht.MineBlocks(6)

ht.AssertWalletAccountBalance(carol, defaultAccount, 8000, 0)

const importedAccount = "carol"
importReq := &walletrpc.ImportAccountRequest{
Name: importedAccount,
ExtendedPublicKey: carolAccount.ExtendedPublicKey,
AddressType: addrType,
BirthdayHeight: 5,
}
dave.RPC.ImportAccount(importReq)

Expand Down Expand Up @@ -574,25 +600,43 @@ func runWalletImportAccountScenario(ht *lntest.HarnessTest,
err := dave.RPC.ImportAccountAssertErr(importReq)
require.ErrorContains(ht, err, errAccountExists)

// externalAddr := newExternalAddr(
// ht, dave, carol, importedAccount, addrType,
// )

// We'll generate an address for Carol from Dave's node to receive some
// funds.
externalAddr := newExternalAddr(
ht, dave, carol, importedAccount, addrType,
)
// funds. This account should be the same as the address that we
// generated in carol's node.
resp := dave.RPC.NewAddress(&lnrpc.NewAddressRequest{
Type: walletToLNAddrType(ht.T, addrType),
Account: importedAccount,
})

externalAddr := resp.Address

require.Equal(ht, r.Address, externalAddr)

// We should only be able to recover the coins in this address after a
// restart.
ht.AssertWalletAccountBalance(dave, importedAccount, 0, 0)

ht.RestartNode(dave)

ht.AssertWalletAccountBalance(dave, importedAccount, 8000, 0)

// Send coins to Carol's address and confirm them, making sure the
// balance updates accordingly.
alice := ht.Alice
req := &lnrpc.SendCoinsRequest{
req = &lnrpc.SendCoinsRequest{
Addr: externalAddr,
Amount: utxoAmt,
SatPerByte: 1,
}
alice.RPC.SendCoins(req)

ht.AssertWalletAccountBalance(dave, importedAccount, 0, utxoAmt)
ht.AssertWalletAccountBalance(dave, importedAccount, 8000, utxoAmt)
ht.MineBlocksAndAssertNumTxes(1, 1)
ht.AssertWalletAccountBalance(dave, importedAccount, utxoAmt, 0)
ht.AssertWalletAccountBalance(dave, importedAccount, utxoAmt+8000, 0)

// To ensure that Dave can use Carol's account as watch-only, we'll
// construct a PSBT that sends funds to Alice, which we'll then hand
Expand Down Expand Up @@ -631,13 +675,13 @@ func runWalletImportAccountScenario(ht *lntest.HarnessTest,
dave, importedAccount, confBalance+utxoAmt, 0,
)

// Now that we have enough funds, it's time to fund the channel, make a
// test payment, and close it. This contains several balance assertions
// along the way.
fundChanAndCloseFromImportedAccount(
ht, dave, alice, carol, importedAccount, addrType, utxoAmt,
int64(funding.MaxBtcFundingAmount),
)
// // Now that we have enough funds, it's time to fund the channel, make a
// // test payment, and close it. This contains several balance assertions
// // along the way.
// fundChanAndCloseFromImportedAccount(
// ht, dave, alice, carol, importedAccount, addrType, utxoAmt,
// int64(funding.MaxBtcFundingAmount),
// )
}

// testWalletImportPubKey tests that an imported public keys can fund
Expand Down
Loading

0 comments on commit 41ce280

Please sign in to comment.