Skip to content

Commit fa31622

Browse files
committed
Reduce the scope of the mutex lock and other minor changes.
1 parent a1bb03a commit fa31622

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

libraries/chain/include/eosio/chain/peer_keys_db.hpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#pragma once
22

33
#include <eosio/chain/controller.hpp>
4-
#include <boost/smart_ptr/atomic_shared_ptr.hpp>
54
#include <boost/unordered/unordered_flat_map.hpp>
5+
#include <fc/mutex.hpp>
66

77
namespace eosio::chain {
88

@@ -21,15 +21,17 @@ class peer_keys_db_t {
2121
size_t update_peer_keys(const controller& chain, uint32_t lib_number);
2222

2323
// safe to be called from any thread
24-
std::optional<public_key_type> operator[](name n) const;
24+
std::optional<public_key_type> get_peer_key(name n) const;
25+
26+
// safe to be called from any thread
2527
size_t size() const;
2628

2729
private:
2830
std::optional<uint64_t> _get_version(const chainbase::database& db);
2931

30-
mutable std::mutex _m;
32+
mutable fc::mutex _m;
3133
uint32_t _block_num = 0; // below map includes keys registered up to _block_num (inclusive)
32-
peer_key_map_t _peer_key_map;
34+
peer_key_map_t _peer_key_map GUARDED_BY(_m);
3335
};
3436

3537
} // namespace eosio::chain

libraries/chain/peer_keys_db.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace eosio::chain {
55

66
peer_keys_db_t::peer_keys_db_t() {}
77

8-
std::optional<public_key_type> peer_keys_db_t::operator[](name n) const {
8+
std::optional<public_key_type> peer_keys_db_t::get_peer_key(name n) const {
99
std::lock_guard g(_m);
1010
if (auto it = _peer_key_map.find(n); it != _peer_key_map.end())
1111
return std::optional<public_key_type>(it->second);
@@ -20,7 +20,6 @@ size_t peer_keys_db_t::size() const {
2020
// we update the keys that were registered up to lib_number (inclusive)
2121
// --------------------------------------------------------------------
2222
size_t peer_keys_db_t::update_peer_keys(const controller& chain, uint32_t lib_number) {
23-
std::lock_guard g(_m);
2423
size_t num_updated = 0;
2524
try {
2625
if (lib_number <= _block_num)
@@ -42,6 +41,8 @@ size_t peer_keys_db_t::update_peer_keys(const controller& chain, uint32_t lib_nu
4241
return num_updated;
4342
}
4443

44+
std::lock_guard g(_m); // we only need to protect access to _peer_key_map
45+
4546
for (auto itr = lower; itr != upper; ++itr) {
4647
try {
4748
const auto* itr2 =

0 commit comments

Comments
 (0)