From ec2d77abedb983ba9b5b429fbcbc5656a0ef885a Mon Sep 17 00:00:00 2001 From: Michael Vandeberg Date: Fri, 20 Dec 2024 11:39:40 -0700 Subject: [PATCH] Add locking on all mempool functions --- src/koinos/mempool/mempool.cpp | 11 +++++++++++ src/koinos/mempool/mempool.hpp | 1 + 2 files changed, 12 insertions(+) diff --git a/src/koinos/mempool/mempool.cpp b/src/koinos/mempool/mempool.cpp index aef6db6..269ea69 100644 --- a/src/koinos/mempool/mempool.cpp +++ b/src/koinos/mempool/mempool.cpp @@ -747,17 +747,20 @@ mempool::~mempool() = default; bool mempool::has_pending_transaction( const transaction_id_type& id, std::optional< crypto::multihash > block_id ) const { + std::lock_guard< std::mutex > lock( _mutex ); return _my->has_pending_transaction( id, block_id ); } std::vector< rpc::mempool::pending_transaction > mempool::get_pending_transactions( uint64_t limit, std::optional< crypto::multihash > block_id ) { + std::lock_guard< std::mutex > lock( _mutex ); return _my->get_pending_transactions( limit, block_id ); } uint64_t mempool::get_reserved_account_rc( const account_type& account ) const { + std::lock_guard< std::mutex > lock( _mutex ); return _my->get_reserved_account_rc( account ); } @@ -766,6 +769,7 @@ bool mempool::check_pending_account_resources( const account_type& payer, uint64_t trx_resource_limit, std::optional< crypto::multihash > block_id ) const { + std::lock_guard< std::mutex > lock( _mutex ); return _my->check_pending_account_resources( payer, max_payer_resources, trx_resource_limit, block_id ); } @@ -773,11 +777,13 @@ bool mempool::check_account_nonce( const account_type& payee, const std::string& nonce, std::optional< crypto::multihash > block_id ) const { + std::lock_guard< std::mutex > lock( _mutex ); return _my->check_account_nonce( payee, nonce, block_id ); } std::string mempool::get_pending_nonce( const std::string& account, std::optional< crypto::multihash > block_id ) const { + std::lock_guard< std::mutex > lock( _mutex ); return _my->get_pending_nonce( account, block_id ); } @@ -788,6 +794,7 @@ uint64_t mempool::add_pending_transaction( const protocol::transaction& transact uint64_t network_bandwidth_used, uint64_t compute_bandwidth_used ) { + std::lock_guard< std::mutex > lock( _mutex ); return _my->add_pending_transaction( transaction, time, max_payer_rc, @@ -798,21 +805,25 @@ uint64_t mempool::add_pending_transaction( const protocol::transaction& transact uint64_t mempool::remove_pending_transactions( const std::vector< transaction_id_type >& ids ) { + std::lock_guard< std::mutex > lock( _mutex ); return _my->remove_pending_transactions( ids ); } uint64_t mempool::prune( std::chrono::seconds expiration, std::chrono::system_clock::time_point now ) { + std::lock_guard< std::mutex > lock( _mutex ); return _my->prune( expiration, now ); } bool mempool::handle_block( const koinos::broadcast::block_accepted& bam ) { + std::lock_guard< std::mutex > lock( _mutex ); return _my->handle_block( bam ); } void mempool::handle_irreversibility( const koinos::broadcast::block_irreversible& bi ) { + std::lock_guard< std::mutex > lock( _mutex ); _my->handle_irreversibility( bi ); } diff --git a/src/koinos/mempool/mempool.hpp b/src/koinos/mempool/mempool.hpp index a5684da..c1e0e5d 100644 --- a/src/koinos/mempool/mempool.hpp +++ b/src/koinos/mempool/mempool.hpp @@ -39,6 +39,7 @@ class mempool final { private: std::unique_ptr< detail::mempool_impl > _my; + mutable std::mutex _mutex; public: mempool( state_db::fork_resolution_algorithm algo = state_db::fork_resolution_algorithm::fifo );