Skip to content

Commit 9646198

Browse files
committed
refactor: const shared_ptrs
Introduce convention to use const shared pointers everywhere, unless the shared pointer is modified at some point, which it very rarely is. We want this convention, as it helps alleviate the misconception that a const shared pointer somehow results in a pointer to an immutable object, which is false.
1 parent 04437ee commit 9646198

File tree

6 files changed

+17
-17
lines changed

6 files changed

+17
-17
lines changed

src/qt/test/addressbooktests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ void TestAddAddressesToSendBook(interfaces::Node& node)
6363
auto wallet_client = interfaces::MakeWalletClient(*test.m_node.chain, *Assert(test.m_node.args));
6464
test.m_node.wallet_client = wallet_client.get();
6565
node.setContext(&test.m_node);
66-
std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(node.context()->chain.get(), "", CreateMockWalletDatabase());
66+
const std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(node.context()->chain.get(), "", CreateMockWalletDatabase());
6767
wallet->LoadWallet();
6868
wallet->SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
6969
{

src/qt/test/wallettests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ void TestGUI(interfaces::Node& node)
141141
auto wallet_client = interfaces::MakeWalletClient(*test.m_node.chain, *Assert(test.m_node.args));
142142
test.m_node.wallet_client = wallet_client.get();
143143
node.setContext(&test.m_node);
144-
std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(node.context()->chain.get(), "", CreateMockWalletDatabase());
144+
const std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(node.context()->chain.get(), "", CreateMockWalletDatabase());
145145
wallet->LoadWallet();
146146
wallet->SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
147147
{

src/wallet/rpcwallet.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ std::shared_ptr<CWallet> GetWalletForJSONRPCRequest(const JSONRPCRequest& reques
103103

104104
std::string wallet_name;
105105
if (GetWalletNameFromJSONRPCRequest(request, wallet_name)) {
106-
std::shared_ptr<CWallet> pwallet = GetWallet(context, wallet_name);
106+
const std::shared_ptr<CWallet> pwallet = GetWallet(context, wallet_name);
107107
if (!pwallet) throw JSONRPCError(RPC_WALLET_NOT_FOUND, "Requested wallet does not exist or is not loaded");
108108
return pwallet;
109109
}
@@ -2840,7 +2840,7 @@ static RPCHelpMan createwallet()
28402840
options.create_passphrase = passphrase;
28412841
bilingual_str error;
28422842
std::optional<bool> load_on_start = request.params[6].isNull() ? std::nullopt : std::optional<bool>(request.params[6].get_bool());
2843-
std::shared_ptr<CWallet> wallet = CreateWallet(context, request.params[0].get_str(), load_on_start, options, status, error, warnings);
2843+
const std::shared_ptr<CWallet> wallet = CreateWallet(context, request.params[0].get_str(), load_on_start, options, status, error, warnings);
28442844
if (!wallet) {
28452845
RPCErrorCode code = status == DatabaseStatus::FAILED_ENCRYPT ? RPC_WALLET_ENCRYPTION_FAILED : RPC_WALLET_ERROR;
28462846
throw JSONRPCError(code, error.original);

src/wallet/test/wallet_tests.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ static_assert(WALLET_INCREMENTAL_RELAY_FEE >= DEFAULT_INCREMENTAL_RELAY_FEE, "wa
4141

4242
BOOST_FIXTURE_TEST_SUITE(wallet_tests, WalletTestingSetup)
4343

44-
static std::shared_ptr<CWallet> TestLoadWallet(WalletContext& context)
44+
static const std::shared_ptr<CWallet> TestLoadWallet(WalletContext& context)
4545
{
4646
DatabaseOptions options;
4747
options.create_flags = WALLET_FLAG_DESCRIPTORS;
@@ -208,7 +208,7 @@ BOOST_FIXTURE_TEST_CASE(importmulti_rescan, TestChain100Setup)
208208
// before the missing block, and success for a key whose creation time is
209209
// after.
210210
{
211-
std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(m_node.chain.get(), "", CreateDummyWalletDatabase());
211+
const std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(m_node.chain.get(), "", CreateDummyWalletDatabase());
212212
wallet->SetupLegacyScriptPubKeyMan();
213213
WITH_LOCK(wallet->cs_wallet, wallet->SetLastBlockProcessed(newTip->nHeight, newTip->GetBlockHash()));
214214
WalletContext context;
@@ -274,7 +274,7 @@ BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup)
274274
{
275275
WalletContext context;
276276
context.args = &gArgs;
277-
std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(m_node.chain.get(), "", CreateDummyWalletDatabase());
277+
const std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(m_node.chain.get(), "", CreateDummyWalletDatabase());
278278
{
279279
auto spk_man = wallet->GetOrCreateLegacyScriptPubKeyMan();
280280
LOCK2(wallet->cs_wallet, spk_man->cs_KeyStore);
@@ -296,7 +296,7 @@ BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup)
296296
// Call importwallet RPC and verify all blocks with timestamps >= BLOCK_TIME
297297
// were scanned, and no prior blocks were scanned.
298298
{
299-
std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(m_node.chain.get(), "", CreateDummyWalletDatabase());
299+
const std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(m_node.chain.get(), "", CreateDummyWalletDatabase());
300300
LOCK(wallet->cs_wallet);
301301
wallet->SetupLegacyScriptPubKeyMan();
302302

@@ -606,7 +606,7 @@ BOOST_FIXTURE_TEST_CASE(ListCoinsTest, ListCoinsTestingSetup)
606606
BOOST_FIXTURE_TEST_CASE(wallet_disableprivkeys, TestChain100Setup)
607607
{
608608
{
609-
std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(m_node.chain.get(), "", CreateDummyWalletDatabase());
609+
const std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(m_node.chain.get(), "", CreateDummyWalletDatabase());
610610
wallet->SetupLegacyScriptPubKeyMan();
611611
wallet->SetMinVersion(FEATURE_LATEST);
612612
wallet->SetWalletFlag(WALLET_FLAG_DISABLE_PRIVATE_KEYS);
@@ -616,7 +616,7 @@ BOOST_FIXTURE_TEST_CASE(wallet_disableprivkeys, TestChain100Setup)
616616
BOOST_CHECK(!wallet->GetNewDestination(OutputType::BECH32, "", dest, error));
617617
}
618618
{
619-
std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(m_node.chain.get(), "", CreateDummyWalletDatabase());
619+
const std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(m_node.chain.get(), "", CreateDummyWalletDatabase());
620620
LOCK(wallet->cs_wallet);
621621
wallet->SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
622622
wallet->SetMinVersion(FEATURE_LATEST);

src/wallet/wallet.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ std::shared_ptr<CWallet> LoadWalletInternal(WalletContext& context, const std::s
221221
}
222222

223223
context.chain->initMessage(_("Loading wallet…").translated);
224-
std::shared_ptr<CWallet> wallet = CWallet::Create(context, name, std::move(database), options.create_flags, error, warnings);
224+
const std::shared_ptr<CWallet> wallet = CWallet::Create(context, name, std::move(database), options.create_flags, error, warnings);
225225
if (!wallet) {
226226
error = Untranslated("Wallet loading failed.") + Untranslated(" ") + error;
227227
status = DatabaseStatus::FAILED_LOAD;
@@ -301,7 +301,7 @@ std::shared_ptr<CWallet> CreateWallet(WalletContext& context, const std::string&
301301

302302
// Make the wallet
303303
context.chain->initMessage(_("Loading wallet…").translated);
304-
std::shared_ptr<CWallet> wallet = CWallet::Create(context, name, std::move(database), wallet_creation_flags, error, warnings);
304+
const std::shared_ptr<CWallet> wallet = CWallet::Create(context, name, std::move(database), wallet_creation_flags, error, warnings);
305305
if (!wallet) {
306306
error = Untranslated("Wallet creation failed.") + Untranslated(" ") + error;
307307
status = DatabaseStatus::FAILED_CREATE;
@@ -2540,7 +2540,7 @@ std::shared_ptr<CWallet> CWallet::Create(WalletContext& context, const std::stri
25402540
int64_t nStart = GetTimeMillis();
25412541
// TODO: Can't use std::make_shared because we need a custom deleter but
25422542
// should be possible to use std::allocate_shared.
2543-
std::shared_ptr<CWallet> walletInstance(new CWallet(chain, name, std::move(database)), ReleaseWallet);
2543+
const std::shared_ptr<CWallet> walletInstance(new CWallet(chain, name, std::move(database)), ReleaseWallet);
25442544
bool rescan_required = false;
25452545
DBErrors nLoadWalletRet = walletInstance->LoadWallet();
25462546
if (nLoadWalletRet != DBErrors::LOAD_OK) {

src/wallet/wallettool.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ static void WalletCreate(CWallet* wallet_instance, uint64_t wallet_creation_flag
4040
wallet_instance->TopUpKeyPool();
4141
}
4242

43-
static std::shared_ptr<CWallet> MakeWallet(const std::string& name, const fs::path& path, DatabaseOptions options)
43+
static const std::shared_ptr<CWallet> MakeWallet(const std::string& name, const fs::path& path, DatabaseOptions options)
4444
{
4545
DatabaseStatus status;
4646
bilingual_str error;
@@ -151,15 +151,15 @@ bool ExecuteWalletToolFunc(const ArgsManager& args, const std::string& command)
151151
options.require_format = DatabaseFormat::SQLITE;
152152
}
153153

154-
std::shared_ptr<CWallet> wallet_instance = MakeWallet(name, path, options);
154+
const std::shared_ptr<CWallet> wallet_instance = MakeWallet(name, path, options);
155155
if (wallet_instance) {
156156
WalletShowInfo(wallet_instance.get());
157157
wallet_instance->Close();
158158
}
159159
} else if (command == "info") {
160160
DatabaseOptions options;
161161
options.require_existing = true;
162-
std::shared_ptr<CWallet> wallet_instance = MakeWallet(name, path, options);
162+
const std::shared_ptr<CWallet> wallet_instance = MakeWallet(name, path, options);
163163
if (!wallet_instance) return false;
164164
WalletShowInfo(wallet_instance.get());
165165
wallet_instance->Close();
@@ -184,7 +184,7 @@ bool ExecuteWalletToolFunc(const ArgsManager& args, const std::string& command)
184184
} else if (command == "dump") {
185185
DatabaseOptions options;
186186
options.require_existing = true;
187-
std::shared_ptr<CWallet> wallet_instance = MakeWallet(name, path, options);
187+
const std::shared_ptr<CWallet> wallet_instance = MakeWallet(name, path, options);
188188
if (!wallet_instance) return false;
189189
bilingual_str error;
190190
bool ret = DumpWallet(*wallet_instance, error);

0 commit comments

Comments
 (0)