epee: better scope leaver

1. Doesn't allocate a `boost::shared_ptr`
2. Doesn't use a vtable
3. Doesn't use `std::function` wrapper
4. Doesn't necessarily copy functional object
5. Doesn't need a factory function for simple declaration (yay CTAD)
6. Isolated to its own header
7. Name doesn't suck as much
This commit is contained in:
jeffro256 2025-04-08 01:46:46 -05:00
parent b591866fcf
commit 76a8e3160f
No known key found for this signature in database
GPG key ID: 6F79797A6E392442
4 changed files with 79 additions and 30 deletions

View file

@ -29,8 +29,13 @@
#pragma once
#include <boost/utility/value_init.hpp>
#include <boost/shared_ptr.hpp>
#include <algorithm>
#include <functional>
#include <vector>
#include "scope_guard.h"
namespace epee
{
#define AUTO_VAL_INIT(v) boost::value_initialized<decltype(v)>()
@ -72,32 +77,16 @@ namespace misc_utils
/* */
/************************************************************************/
struct call_befor_die_base
{
virtual ~call_befor_die_base() = default;
};
typedef std::shared_ptr<call_befor_die_base> 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<std::function<void()>>;
// name exists for backwards compatibility
template<class t_scope_leave_handler>
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<class t_scope_leave_handler>
auto_scope_leave_caller create_scope_leave_handler(t_scope_leave_handler f)
{
auto_scope_leave_caller slc = std::make_shared<call_befor_die<t_scope_leave_handler>>(f);
return slc;
return auto_scope_leave_caller(std::forward<t_scope_leave_handler>(f));
}
template<typename T> struct struct_init: T

View file

@ -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 <utility>
namespace epee
{
/**
* @brief: scope_guard - RAII pattern to call action on scope leave
*/
template <class F>
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>(f)) {}
// call action
~scope_guard() noexcept { try { m_f(); } catch (...) { /* ignore exception in destructor */ } }
private:
F m_f;
};
} //namespace epee

View file

@ -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]);

View file

@ -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);