Skip to content

Commit 2f76ac0

Browse files
committed
test,gui: decouple chain and wallet initialization from test case
Prepare ground for legacy watch-only test.
1 parent cd98b71 commit 2f76ac0

File tree

1 file changed

+50
-37
lines changed

1 file changed

+50
-37
lines changed

src/qt/test/wallettests.cpp

Lines changed: 50 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,39 @@ void VerifyUseAvailableBalance(SendCoinsDialog& sendCoinsDialog, const WalletMod
173173
QVERIFY(send_entry->getValue().amount == sum_selected_coins);
174174
}
175175

176+
void SyncUpWallet(const std::shared_ptr<CWallet>& wallet, interfaces::Node& node)
177+
{
178+
WalletRescanReserver reserver(*wallet);
179+
reserver.reserve();
180+
CWallet::ScanResult result = wallet->ScanForWalletTransactions(Params().GetConsensus().hashGenesisBlock, /*start_height=*/0, /*max_height=*/{}, reserver, /*fUpdate=*/true, /*save_progress=*/false);
181+
QCOMPARE(result.status, CWallet::ScanResult::SUCCESS);
182+
QCOMPARE(result.last_scanned_block, WITH_LOCK(node.context()->chainman->GetMutex(), return node.context()->chainman->ActiveChain().Tip()->GetBlockHash()));
183+
QVERIFY(result.last_failed_block.IsNull());
184+
}
185+
186+
std::shared_ptr<CWallet> SetupDescriptorsWallet(interfaces::Node& node, TestChain100Setup& test)
187+
{
188+
std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(node.context()->chain.get(), "", CreateMockWalletDatabase());
189+
wallet->LoadWallet();
190+
LOCK(wallet->cs_wallet);
191+
wallet->SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
192+
wallet->SetupDescriptorScriptPubKeyMans();
193+
194+
// Add the coinbase key
195+
FlatSigningProvider provider;
196+
std::string error;
197+
std::unique_ptr<Descriptor> desc = Parse("combo(" + EncodeSecret(test.coinbaseKey) + ")", provider, error, /* require_checksum=*/ false);
198+
assert(desc);
199+
WalletDescriptor w_desc(std::move(desc), 0, 0, 1, 1);
200+
if (!wallet->AddWalletDescriptor(w_desc, provider, "", false)) assert(false);
201+
CTxDestination dest = GetDestinationForKey(test.coinbaseKey.GetPubKey(), wallet->m_default_address_type);
202+
wallet->SetAddressBook(dest, "", "receive");
203+
wallet->SetLastBlockProcessed(105, WITH_LOCK(node.context()->chainman->GetMutex(), return node.context()->chainman->ActiveChain().Tip()->GetBlockHash()));
204+
SyncUpWallet(wallet, node);
205+
wallet->SetBroadcastTransactions(true);
206+
return wallet;
207+
}
208+
176209
//! Simple qt wallet tests.
177210
//
178211
// Test widgets can be debugged interactively calling show() on them and
@@ -186,44 +219,8 @@ void VerifyUseAvailableBalance(SendCoinsDialog& sendCoinsDialog, const WalletMod
186219
// QT_QPA_PLATFORM=xcb src/qt/test/test_bitcoin-qt # Linux
187220
// QT_QPA_PLATFORM=windows src/qt/test/test_bitcoin-qt # Windows
188221
// QT_QPA_PLATFORM=cocoa src/qt/test/test_bitcoin-qt # macOS
189-
void TestGUI(interfaces::Node& node)
222+
void TestGUI(interfaces::Node& node, const std::shared_ptr<CWallet>& wallet)
190223
{
191-
// Set up wallet and chain with 105 blocks (5 mature blocks for spending).
192-
TestChain100Setup test;
193-
for (int i = 0; i < 5; ++i) {
194-
test.CreateAndProcessBlock({}, GetScriptForRawPubKey(test.coinbaseKey.GetPubKey()));
195-
}
196-
auto wallet_loader = interfaces::MakeWalletLoader(*test.m_node.chain, *Assert(test.m_node.args));
197-
test.m_node.wallet_loader = wallet_loader.get();
198-
node.setContext(&test.m_node);
199-
const std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(node.context()->chain.get(), "", CreateMockWalletDatabase());
200-
wallet->LoadWallet();
201-
wallet->SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
202-
{
203-
LOCK(wallet->cs_wallet);
204-
wallet->SetupDescriptorScriptPubKeyMans();
205-
206-
// Add the coinbase key
207-
FlatSigningProvider provider;
208-
std::string error;
209-
std::unique_ptr<Descriptor> desc = Parse("combo(" + EncodeSecret(test.coinbaseKey) + ")", provider, error, /* require_checksum=*/ false);
210-
assert(desc);
211-
WalletDescriptor w_desc(std::move(desc), 0, 0, 1, 1);
212-
if (!wallet->AddWalletDescriptor(w_desc, provider, "", false)) assert(false);
213-
CTxDestination dest = GetDestinationForKey(test.coinbaseKey.GetPubKey(), wallet->m_default_address_type);
214-
wallet->SetAddressBook(dest, "", "receive");
215-
wallet->SetLastBlockProcessed(105, WITH_LOCK(node.context()->chainman->GetMutex(), return node.context()->chainman->ActiveChain().Tip()->GetBlockHash()));
216-
}
217-
{
218-
WalletRescanReserver reserver(*wallet);
219-
reserver.reserve();
220-
CWallet::ScanResult result = wallet->ScanForWalletTransactions(Params().GetConsensus().hashGenesisBlock, /*start_height=*/0, /*max_height=*/{}, reserver, /*fUpdate=*/true, /*save_progress=*/false);
221-
QCOMPARE(result.status, CWallet::ScanResult::SUCCESS);
222-
QCOMPARE(result.last_scanned_block, WITH_LOCK(node.context()->chainman->GetMutex(), return node.context()->chainman->ActiveChain().Tip()->GetBlockHash()));
223-
QVERIFY(result.last_failed_block.IsNull());
224-
}
225-
wallet->SetBroadcastTransactions(true);
226-
227224
// Create widgets for sending coins and listing transactions.
228225
std::unique_ptr<const PlatformStyle> platformStyle(PlatformStyle::instantiate("other"));
229226
SendCoinsDialog sendCoinsDialog(platformStyle.get());
@@ -353,6 +350,22 @@ void TestGUI(interfaces::Node& node)
353350
QCOMPARE(walletModel.wallet().getAddressReceiveRequests().size(), size_t{0});
354351
}
355352

353+
void TestGUI(interfaces::Node& node)
354+
{
355+
// Set up wallet and chain with 105 blocks (5 mature blocks for spending).
356+
TestChain100Setup test;
357+
for (int i = 0; i < 5; ++i) {
358+
test.CreateAndProcessBlock({}, GetScriptForRawPubKey(test.coinbaseKey.GetPubKey()));
359+
}
360+
auto wallet_loader = interfaces::MakeWalletLoader(*test.m_node.chain, *Assert(test.m_node.args));
361+
test.m_node.wallet_loader = wallet_loader.get();
362+
node.setContext(&test.m_node);
363+
364+
// "Full" GUI tests, use descriptor wallet
365+
const std::shared_ptr<CWallet>& desc_wallet = SetupDescriptorsWallet(node, test);
366+
TestGUI(node, desc_wallet);
367+
}
368+
356369
} // namespace
357370

358371
void WalletTests::walletTests()

0 commit comments

Comments
 (0)