diff --git a/contrib/epee/include/misc_language.h b/contrib/epee/include/misc_language.h index 4abc2f9824..f74ac10532 100644 --- a/contrib/epee/include/misc_language.h +++ b/contrib/epee/include/misc_language.h @@ -29,8 +29,13 @@ #pragma once #include -#include + +#include +#include #include + +#include "scope_guard.h" + namespace epee { #define AUTO_VAL_INIT(v) boost::value_initialized() @@ -72,32 +77,16 @@ namespace misc_utils /* */ /************************************************************************/ - struct call_befor_die_base - { - virtual ~call_befor_die_base() = default; - }; - - typedef std::shared_ptr auto_scope_leave_caller; - + /// name exists for backwards compatibility. if writing new code, you shouldn't + /// use this type unless you *need* the scope leaver to be polymorphic because the std::function + /// adds unnecessary overhead if you know the type of the functional object at compile-time + using auto_scope_leave_caller = scope_guard>; + // name exists for backwards compatibility template - struct call_befor_die: public call_befor_die_base + auto_scope_leave_caller create_scope_leave_handler(t_scope_leave_handler &&f) { - t_scope_leave_handler m_func; - call_befor_die(t_scope_leave_handler f):m_func(f) - {} - ~call_befor_die() - { - try { m_func(); } - catch (...) { /* ignore */ } - } - }; - - template - auto_scope_leave_caller create_scope_leave_handler(t_scope_leave_handler f) - { - auto_scope_leave_caller slc = std::make_shared>(f); - return slc; + return auto_scope_leave_caller(std::forward(f)); } template struct struct_init: T diff --git a/contrib/epee/include/scope_guard.h b/contrib/epee/include/scope_guard.h new file mode 100644 index 0000000000..7b7243a51d --- /dev/null +++ b/contrib/epee/include/scope_guard.h @@ -0,0 +1,57 @@ +// Copyright (c) 2025, The Monero Project +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other +// materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its contributors may be +// used to endorse or promote products derived from this software without specific +// prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#pragma once + +#include + +namespace epee +{ + +/** + * @brief: scope_guard - RAII pattern to call action on scope leave + */ +template +class scope_guard final +{ +public: + // no copy + scope_guard(const scope_guard&) = delete; + scope_guard& operator=(const scope_guard&) = delete; + + // constructor with CTAD + scope_guard(F &&f): m_f(std::forward(f)) {} + + // call action + ~scope_guard() noexcept { try { m_f(); } catch (...) { /* ignore exception in destructor */ } } + +private: + F m_f; +}; + +} //namespace epee diff --git a/src/blockchain_utilities/blockchain_blackball.cpp b/src/blockchain_utilities/blockchain_blackball.cpp index cf41134d15..38c4706623 100644 --- a/src/blockchain_utilities/blockchain_blackball.cpp +++ b/src/blockchain_utilities/blockchain_blackball.cpp @@ -528,7 +528,11 @@ static uint64_t find_first_diverging_transaction(const std::string &first_filena MDB_val k; MDB_val v[2]; - epee::misc_utils::auto_scope_leave_caller txn_dtor[2]; + epee::scope_guard txn_dtor([&tx_active, &txn](){ + for (int i = 0; i < 2; ++i) + if (tx_active[i]) + mdb_txn_abort(txn[i]); + }); for (int i = 0; i < 2; ++i) { dbr = mdb_env_create(&env[i]); @@ -542,7 +546,6 @@ static uint64_t find_first_diverging_transaction(const std::string &first_filena dbr = mdb_txn_begin(env[i], NULL, MDB_RDONLY, &txn[i]); if (dbr) throw std::runtime_error("Failed to create LMDB transaction: " + std::string(mdb_strerror(dbr))); - txn_dtor[i] = epee::misc_utils::create_scope_leave_handler([&, i](){if (tx_active[i]) mdb_txn_abort(txn[i]);}); tx_active[i] = true; dbr = mdb_dbi_open(txn[i], "txs_pruned", MDB_INTEGERKEY, &dbi[i]); diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index aabb06521d..b9f0cb3c2a 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -13987,11 +13987,11 @@ void wallet2::stop_background_sync(const epee::wipeable_string &wallet_password, // Set the plaintext spend key m_account.set_spend_key(recovered_spend_key); + if (is_key_encryption_enabled()) + this->encrypt_keys(wallet_password); - // Encrypt the spend key when done if needed - epee::misc_utils::auto_scope_leave_caller keys_reencryptor; - if (m_ask_password == AskPasswordToDecrypt && !m_unattended && !m_watch_only) - keys_reencryptor = epee::misc_utils::create_scope_leave_handler([&, this]{encrypt_keys(wallet_password);}); + // Decrypt now, then re-encrypt the spend key when done if needed + wallet_keys_unlocker unlocker(*this, &wallet_password); // Now we can use the decrypted spend key to process background cache process_background_cache(background_sync_data, background_synced_chain, last_block_reward);