Skip to content

Commit ee79e07

Browse files
committed
Issue #765 - Beekeeper | Session timeout and lock_all API call locks wallets one by one so list_wallets can show different statuses during lock
1 parent beef04c commit ee79e07

File tree

11 files changed

+238
-51
lines changed

11 files changed

+238
-51
lines changed

programs/beekeeper/beekeeper/beekeeper_app.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ init_data beekeeper_app::initialize( int argc, char** argv )
171171
return _initialization;
172172
}
173173

174-
api_ptr = std::make_unique<beekeeper::beekeeper_wallet_api>( wallet_manager_ptr, app, unlock_interval );
174+
api_ptr = std::make_unique<beekeeper::beekeeper_wallet_api>( wallet_manager_ptr, mtx_handler, app, unlock_interval );
175175

176176
app.notify_status( "beekeeper is starting" );
177177

@@ -251,7 +251,8 @@ init_data beekeeper_app::save_keys( const boost::program_options::variables_map&
251251
std::shared_ptr<beekeeper::beekeeper_wallet_manager> beekeeper_app::create_wallet( const boost::filesystem::path& cmd_wallet_dir, uint64_t cmd_unlock_timeout, uint32_t cmd_session_limit )
252252
{
253253
instance = std::make_shared<beekeeper_instance>( app, cmd_wallet_dir, notifications_endpoint );
254-
return std::make_shared<beekeeper::beekeeper_wallet_manager>( std::make_shared<session_manager>( notifications_endpoint ), instance,
254+
mtx_handler = std::make_shared<mutex_handler>();
255+
return std::make_shared<beekeeper::beekeeper_wallet_manager>( std::make_shared<session_manager>( notifications_endpoint, mtx_handler ), instance,
255256
cmd_wallet_dir, cmd_unlock_timeout, cmd_session_limit,
256257
[this]() { app.kill(); } );
257258
}

programs/beekeeper/beekeeper/beekeeper_wallet_api.cpp

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
#include <fc/variant_object.hpp>
99
#include <fc/reflect/variant.hpp>
1010

11-
#include<shared_mutex>
12-
1311
namespace beekeeper {
1412

1513
#define HIVE_BEEKEEPER_API_NAME "beekeeper_api"
@@ -26,17 +24,19 @@ class beekeeper_api_impl
2624

2725
extended_api ex_api;
2826

29-
std::shared_mutex mtx;
30-
3127
public_key_type create( const std::string& source )
3228
{
3329
return utility::public_key::create( source, prefix );
3430
}
3531

3632
public:
37-
beekeeper_api_impl( std::shared_ptr<beekeeper::beekeeper_wallet_manager> wallet_mgr, uint64_t unlock_interval )
33+
beekeeper_api_impl( std::shared_ptr<beekeeper::beekeeper_wallet_manager> wallet_mgr, std::shared_ptr<mutex_handler> mtx_handler, appbase::application& app, uint64_t unlock_interval )
3834
: prefix( HIVE_ADDRESS_PREFIX/*At now this is only one allowed prefix, by maybe in the future custom prefixes could be used as well.*/ ),
39-
ex_api( unlock_interval ), _wallet_mgr( wallet_mgr ) {}
35+
ex_api( unlock_interval ), _wallet_mgr( wallet_mgr ), _mtx_handler( mtx_handler )
36+
{
37+
FC_ASSERT( _wallet_mgr );
38+
FC_ASSERT( _mtx_handler );
39+
}
4040

4141
DECLARE_API_IMPL
4242
(
@@ -66,58 +66,59 @@ class beekeeper_api_impl
6666
)
6767

6868
std::shared_ptr<beekeeper::beekeeper_wallet_manager> _wallet_mgr;
69+
std::shared_ptr<mutex_handler> _mtx_handler;
6970
};
7071

7172
DEFINE_API_IMPL( beekeeper_api_impl, create )
7273
{
73-
std::unique_lock guard( mtx );
74+
std::unique_lock guard( _mtx_handler->get_mutex() );
7475

7576
return { _wallet_mgr->create( args.token, args.wallet_name, args.password, args.is_temporary ) };
7677
}
7778

7879
DEFINE_API_IMPL( beekeeper_api_impl, open )
7980
{
80-
std::unique_lock guard( mtx );
81+
std::unique_lock guard( _mtx_handler->get_mutex() );
8182

8283
_wallet_mgr->open( args.token, args.wallet_name );
8384
return open_return();
8485
}
8586

8687
DEFINE_API_IMPL( beekeeper_api_impl, close )
8788
{
88-
std::unique_lock guard( mtx );
89+
std::unique_lock guard( _mtx_handler->get_mutex() );
8990

9091
_wallet_mgr->close( args.token, args.wallet_name );
9192
return close_return();
9293
}
9394

9495
DEFINE_API_IMPL( beekeeper_api_impl, set_timeout )
9596
{
96-
std::unique_lock guard( mtx );
97+
std::unique_lock guard( _mtx_handler->get_mutex() );
9798

9899
_wallet_mgr->set_timeout( args.token, args.seconds );
99100
return set_timeout_return();
100101
}
101102

102103
DEFINE_API_IMPL( beekeeper_api_impl, lock_all )
103104
{
104-
std::unique_lock guard( mtx );
105+
std::unique_lock guard( _mtx_handler->get_mutex() );
105106

106107
_wallet_mgr->lock_all( args.token );
107108
return lock_all_return();
108109
}
109110

110111
DEFINE_API_IMPL( beekeeper_api_impl, lock )
111112
{
112-
std::unique_lock guard( mtx );
113+
std::unique_lock guard( _mtx_handler->get_mutex() );
113114

114115
_wallet_mgr->lock( args.token, args.wallet_name );
115116
return lock_return();
116117
}
117118

118119
DEFINE_API_IMPL( beekeeper_api_impl, unlock )
119120
{
120-
std::unique_lock guard( mtx );
121+
std::unique_lock guard( _mtx_handler->get_mutex() );
121122

122123
if( ex_api.unlock_allowed() )
123124
{
@@ -138,124 +139,125 @@ DEFINE_API_IMPL( beekeeper_api_impl, unlock )
138139

139140
DEFINE_API_IMPL( beekeeper_api_impl, import_key )
140141
{
141-
std::unique_lock guard( mtx );
142+
std::unique_lock guard( _mtx_handler->get_mutex() );
142143

143144
return { _wallet_mgr->import_key( args.token, args.wallet_name, args.wif_key, prefix ) };
144145
}
145146

146147
DEFINE_API_IMPL( beekeeper_api_impl, import_keys )
147148
{
148-
std::unique_lock guard( mtx );
149+
std::unique_lock guard( _mtx_handler->get_mutex() );
149150

150151
return { _wallet_mgr->import_keys( args.token, args.wallet_name, args.wif_keys, prefix ) };
151152
}
152153

153154
DEFINE_API_IMPL( beekeeper_api_impl, remove_key )
154155
{
155-
std::unique_lock guard( mtx );
156+
std::unique_lock guard( _mtx_handler->get_mutex() );
156157

157158
_wallet_mgr->remove_key( args.token, args.wallet_name, create( args.public_key ) );
158159
return remove_key_return();
159160
}
160161

161162
DEFINE_API_IMPL( beekeeper_api_impl, list_wallets )
162163
{
163-
std::shared_lock guard( mtx );
164+
std::shared_lock guard( _mtx_handler->get_mutex() );
164165

165166
return { _wallet_mgr->list_wallets( args.token ) };
166167
}
167168

168169
DEFINE_API_IMPL( beekeeper_api_impl, list_created_wallets )
169170
{
170-
std::shared_lock guard( mtx );
171+
std::shared_lock guard( _mtx_handler->get_mutex() );
171172

172173
return { _wallet_mgr->list_created_wallets( args.token ) };
173174
}
174175

175176
DEFINE_API_IMPL( beekeeper_api_impl, get_public_keys )
176177
{
177-
std::shared_lock guard( mtx );
178+
std::shared_lock guard( _mtx_handler->get_mutex() );
178179

179180
auto _keys = _wallet_mgr->get_public_keys( args.token, args.wallet_name );
180181
return { utility::get_public_keys( _keys ) };
181182
}
182183

183184
DEFINE_API_IMPL( beekeeper_api_impl, sign_digest )
184185
{
185-
std::shared_lock guard( mtx );
186+
std::shared_lock guard( _mtx_handler->get_mutex() );
186187

187188
using namespace beekeeper;
188189
return { _wallet_mgr->sign_digest( args.token, args.wallet_name, args.sig_digest, create( args.public_key ), prefix ) };
189190
}
190191

191192
DEFINE_API_IMPL( beekeeper_api_impl, get_info )
192193
{
193-
std::shared_lock guard( mtx );
194+
std::shared_lock guard( _mtx_handler->get_mutex() );
194195

195196
return _wallet_mgr->get_info( args.token );
196197
}
197198

198199
DEFINE_API_IMPL( beekeeper_api_impl, create_session )
199200
{
200-
std::unique_lock guard( mtx );
201+
std::unique_lock guard( _mtx_handler->get_mutex() );
201202

202203
return { _wallet_mgr->create_session( args.salt, args.notifications_endpoint ) };
203204
}
204205

205206
DEFINE_API_IMPL( beekeeper_api_impl, close_session )
206207
{
207-
std::unique_lock guard( mtx );
208+
std::unique_lock guard( _mtx_handler->get_mutex() );
208209

209210
_wallet_mgr->close_session( args.token );
210211
return close_session_return();
211212
}
212213

213214
DEFINE_API_IMPL( beekeeper_api_impl, has_matching_private_key )
214215
{
215-
std::shared_lock guard( mtx );
216+
std::shared_lock guard( _mtx_handler->get_mutex() );
216217

217218
return { _wallet_mgr->has_matching_private_key( args.token, args.wallet_name, create( args.public_key ) ) };
218219
}
219220

220221
DEFINE_API_IMPL( beekeeper_api_impl, encrypt_data )
221222
{
222-
std::shared_lock guard( mtx );
223+
std::shared_lock guard( _mtx_handler->get_mutex() );
223224

224225
return { _wallet_mgr->encrypt_data( args.token, create( args.from_public_key ), create( args.to_public_key ), args.wallet_name, args.content, args.nonce, prefix ) };
225226
}
226227

227228
DEFINE_API_IMPL( beekeeper_api_impl, decrypt_data )
228229
{
229-
std::shared_lock guard( mtx );
230+
std::shared_lock guard( _mtx_handler->get_mutex() );
230231

231232
return { _wallet_mgr->decrypt_data( args.token, create( args.from_public_key ), create( args.to_public_key ), args.wallet_name, args.encrypted_content ) };
232233
}
233234

234235
DEFINE_API_IMPL( beekeeper_api_impl, get_version )
235236
{
236-
std::shared_lock guard( mtx );
237+
std::shared_lock guard( _mtx_handler->get_mutex() );
237238

238239
return _wallet_mgr->get_version();
239240
}
240241

241242
DEFINE_API_IMPL( beekeeper_api_impl, has_wallet )
242243
{
243-
std::shared_lock guard( mtx );
244+
std::shared_lock guard( _mtx_handler->get_mutex() );
244245

245246
return { _wallet_mgr->has_wallet( args.token, args.wallet_name ) };
246247
}
247248

248249
DEFINE_API_IMPL( beekeeper_api_impl, is_wallet_unlocked )
249250
{
250-
std::shared_lock guard( mtx );
251+
std::shared_lock guard( _mtx_handler->get_mutex() );
251252

252253
return _wallet_mgr->is_wallet_unlocked( args.token, args.wallet_name );
253254
}
254255

255256
} // detail
256257

257-
beekeeper_wallet_api::beekeeper_wallet_api( std::shared_ptr<beekeeper::beekeeper_wallet_manager> wallet_mgr, appbase::application& app, uint64_t unlock_interval )
258-
: my( new detail::beekeeper_api_impl( wallet_mgr, unlock_interval ) )
258+
beekeeper_wallet_api::beekeeper_wallet_api( std::shared_ptr<beekeeper::beekeeper_wallet_manager> wallet_mgr, std::shared_ptr<mutex_handler> mtx_handler,
259+
appbase::application& app, uint64_t unlock_interval )
260+
: my( new detail::beekeeper_api_impl( wallet_mgr, mtx_handler, app, unlock_interval ) )
259261
{
260262
JSON_RPC_REGISTER_API( HIVE_BEEKEEPER_API_NAME );
261263
}

programs/beekeeper/beekeeper/include/beekeeper/beekeeper_app.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <beekeeper/beekeeper_instance.hpp>
66
#include <beekeeper/beekeeper_wallet_api.hpp>
7+
#include <beekeeper/mutex_handler.hpp>
78

89
#include <core/beekeeper_app_base.hpp>
910

@@ -27,6 +28,8 @@ class beekeeper_app: public beekeeper_app_base
2728

2829
appbase::application app;
2930

31+
std::shared_ptr<mutex_handler> mtx_handler;
32+
3033
std::string check_version();
3134
bool save_keys( const std::optional<std::string>& notification, const std::string& wallet_name, const std::string& wallet_password );
3235

programs/beekeeper/beekeeper/include/beekeeper/beekeeper_wallet_api.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include <core/beekeeper_wallet_manager.hpp>
44
#include <core/utilities.hpp>
55

6+
#include <beekeeper/mutex_handler.hpp>
7+
68
#include <hive/plugins/json_rpc/utility.hpp>
79

810
#include <fc/optional.hpp>
@@ -24,7 +26,8 @@ namespace detail
2426
class beekeeper_wallet_api
2527
{
2628
public:
27-
beekeeper_wallet_api( std::shared_ptr<beekeeper::beekeeper_wallet_manager> wallet_mgr, appbase::application& app, uint64_t unlock_interval );
29+
beekeeper_wallet_api( std::shared_ptr<beekeeper::beekeeper_wallet_manager> wallet_mgr, std::shared_ptr<mutex_handler> mtx_handler,
30+
appbase::application& app, uint64_t unlock_interval );
2831
~beekeeper_wallet_api();
2932

3033
DECLARE_API(
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#pragma once
2+
3+
#include<shared_mutex>
4+
5+
namespace beekeeper {
6+
7+
class mutex_handler
8+
{
9+
private:
10+
11+
std::shared_mutex mtx;
12+
13+
public:
14+
15+
inline std::shared_mutex& get_mutex(){ return mtx; }
16+
};
17+
18+
} //beekeeper

programs/beekeeper/beekeeper/include/beekeeper/session_manager.hpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,27 @@
22

33
#include <core/session_manager_base.hpp>
44

5+
#include <beekeeper/mutex_handler.hpp>
6+
57
#include <string>
68

79
namespace beekeeper {
810

911
class session_manager: public session_manager_base
1012
{
13+
private:
14+
15+
std::shared_ptr<mutex_handler> mtx_handler;
16+
1117
protected:
1218

1319
std::shared_ptr<session_base> create_session( const std::optional<std::string>& notifications_endpoint, const std::string& token, std::shared_ptr<time_manager_base> not_used_time, const boost::filesystem::path& wallet_directory ) override;
1420

21+
void lock( const std::string& token ) override;
22+
1523
public:
1624

17-
session_manager( const std::optional<std::string>& notifications_endpoint );
25+
session_manager( const std::optional<std::string>& notifications_endpoint, std::shared_ptr<mutex_handler> mtx_handler );
1826
~session_manager() override {}
1927
};
2028

programs/beekeeper/beekeeper/session_manager.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
namespace beekeeper {
66

7-
session_manager::session_manager( const std::optional<std::string>& notifications_endpoint )
7+
session_manager::session_manager( const std::optional<std::string>& notifications_endpoint, std::shared_ptr<mutex_handler> mtx_handler )
8+
: mtx_handler( mtx_handler )
89
{
910
time = std::make_shared<time_manager>( notifications_endpoint );
1011
}
@@ -14,4 +15,12 @@ std::shared_ptr<session_base> session_manager::create_session( const std::option
1415
return std::make_shared<session>( content_deliverer, notifications_endpoint, token, time, wallet_directory );
1516
}
1617

18+
void session_manager::lock( const std::string& token )
19+
{
20+
FC_ASSERT( mtx_handler );
21+
22+
std::unique_lock guard( mtx_handler->get_mutex() );
23+
session_manager_base::lock( token );
24+
}
25+
1726
} //beekeeper

programs/beekeeper/core/include/core/session_manager_base.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ class session_manager_base
2828

2929
virtual std::shared_ptr<session_base> create_session( const std::optional<std::string>& notifications_endpoint, const std::string& token, std::shared_ptr<time_manager_base> time, const boost::filesystem::path& wallet_directory );
3030

31+
virtual void lock( const std::string& token );
32+
3133
public:
3234

3335
session_manager_base();

0 commit comments

Comments
 (0)