Skip to content

Commit 531ce40

Browse files
committed
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
1 parent c548baa commit 531ce40

File tree

4 files changed

+78
-30
lines changed

4 files changed

+78
-30
lines changed

contrib/epee/include/misc_language.h

+12-24
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,12 @@
2929
#pragma once
3030

3131
#include <boost/utility/value_init.hpp>
32-
#include <boost/shared_ptr.hpp>
32+
33+
#include <algorithm>
3334
#include <vector>
35+
36+
#include "scope_guard.h"
37+
3438
namespace epee
3539
{
3640
#define AUTO_VAL_INIT(v) boost::value_initialized<decltype(v)>()
@@ -72,32 +76,16 @@ namespace misc_utils
7276
/* */
7377
/************************************************************************/
7478

75-
struct call_befor_die_base
76-
{
77-
virtual ~call_befor_die_base() = default;
78-
};
79-
80-
typedef std::shared_ptr<call_befor_die_base> auto_scope_leave_caller;
81-
82-
83-
template<class t_scope_leave_handler>
84-
struct call_befor_die: public call_befor_die_base
85-
{
86-
t_scope_leave_handler m_func;
87-
call_befor_die(t_scope_leave_handler f):m_func(f)
88-
{}
89-
~call_befor_die()
90-
{
91-
try { m_func(); }
92-
catch (...) { /* ignore */ }
93-
}
94-
};
79+
// name exists for backwards compatibility
80+
template <class F>
81+
using auto_scope_leave_caller = scope_guard<F>;
9582

83+
// name exists for backwards compatibility
9684
template<class t_scope_leave_handler>
97-
auto_scope_leave_caller create_scope_leave_handler(t_scope_leave_handler f)
85+
auto create_scope_leave_handler(t_scope_leave_handler &&f)
86+
-> decltype(scope_guard(std::forward<t_scope_leave_handler>(f)))
9887
{
99-
auto_scope_leave_caller slc = std::make_shared<call_befor_die<t_scope_leave_handler>>(f);
100-
return slc;
88+
return scope_guard(std::forward<t_scope_leave_handler>(f));
10189
}
10290

10391
template<typename T> struct struct_init: T

contrib/epee/include/scope_guard.h

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (c) 2025, The Monero Project
2+
//
3+
// All rights reserved.
4+
//
5+
// Redistribution and use in source and binary forms, with or without modification, are
6+
// permitted provided that the following conditions are met:
7+
//
8+
// 1. Redistributions of source code must retain the above copyright notice, this list of
9+
// conditions and the following disclaimer.
10+
//
11+
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
12+
// of conditions and the following disclaimer in the documentation and/or other
13+
// materials provided with the distribution.
14+
//
15+
// 3. Neither the name of the copyright holder nor the names of its contributors may be
16+
// used to endorse or promote products derived from this software without specific
17+
// prior written permission.
18+
//
19+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20+
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21+
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22+
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24+
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26+
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27+
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28+
29+
#pragma once
30+
31+
#include <utility>
32+
33+
namespace epee
34+
{
35+
36+
/**
37+
* @brief: scope_guard - RAII pattern to call action on scope leave
38+
*/
39+
template <class F>
40+
class scope_guard final
41+
{
42+
public:
43+
// no copy
44+
scope_guard(const scope_guard&) = delete;
45+
scope_guard& operator=(const scope_guard&) = delete;
46+
47+
// constructor with CTAD
48+
scope_guard(F &&f): m_f(std::forward<F>(f)) {}
49+
50+
// call action
51+
~scope_guard() { try { m_f(); } catch (...) { /* ignore exception in destructor */ } }
52+
53+
private:
54+
F m_f;
55+
};
56+
57+
} //namespace epee

src/blockchain_utilities/blockchain_blackball.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,11 @@ static uint64_t find_first_diverging_transaction(const std::string &first_filena
529529
MDB_val k;
530530
MDB_val v[2];
531531

532-
epee::misc_utils::auto_scope_leave_caller txn_dtor[2];
532+
epee::scope_guard txn_dtor([&tx_active, &txn](){
533+
for (int i = 0; i < 2; ++i)
534+
if (tx_active[i])
535+
mdb_txn_abort(txn[i]);
536+
});
533537
for (int i = 0; i < 2; ++i)
534538
{
535539
dbr = mdb_env_create(&env[i]);
@@ -543,7 +547,6 @@ static uint64_t find_first_diverging_transaction(const std::string &first_filena
543547

544548
dbr = mdb_txn_begin(env[i], NULL, MDB_RDONLY, &txn[i]);
545549
if (dbr) throw std::runtime_error("Failed to create LMDB transaction: " + std::string(mdb_strerror(dbr)));
546-
txn_dtor[i] = epee::misc_utils::create_scope_leave_handler([&, i](){if (tx_active[i]) mdb_txn_abort(txn[i]);});
547550
tx_active[i] = true;
548551

549552
dbr = mdb_dbi_open(txn[i], "txs_pruned", MDB_INTEGERKEY, &dbi[i]);

src/wallet/wallet2.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -14096,11 +14096,11 @@ void wallet2::stop_background_sync(const epee::wipeable_string &wallet_password,
1409614096

1409714097
// Set the plaintext spend key
1409814098
m_account.set_spend_key(recovered_spend_key);
14099+
if (is_key_encryption_enabled())
14100+
this->encrypt_keys(wallet_password);
1409914101

14100-
// Encrypt the spend key when done if needed
14101-
epee::misc_utils::auto_scope_leave_caller keys_reencryptor;
14102-
if (m_ask_password == AskPasswordToDecrypt && !m_unattended && !m_watch_only)
14103-
keys_reencryptor = epee::misc_utils::create_scope_leave_handler([&, this]{encrypt_keys(wallet_password);});
14102+
// Decrypt now, then re-encrypt the spend key when done if needed
14103+
wallet_keys_unlocker unlocker(*this, &wallet_password);
1410414104

1410514105
// Now we can use the decrypted spend key to process background cache
1410614106
process_background_cache(background_sync_data, background_synced_chain, last_block_reward);

0 commit comments

Comments
 (0)