Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Start transaction signature earily in thread pool
Browse files Browse the repository at this point in the history
  • Loading branch information
heifner committed Dec 12, 2018
1 parent 659fc92 commit 7d3e5c6
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class transaction_metadata {

const flat_set<public_key_type>& recover_keys( const chain_id_type& chain_id );

static void create_signing_keys_future( transaction_metadata_ptr& mtrx,
static void create_signing_keys_future( const transaction_metadata_ptr& mtrx,
boost::asio::thread_pool& thread_pool, const chain_id_type& chain_id );

uint32_t total_actions()const { return trx.context_free_actions.size() + trx.actions.size(); }
Expand Down
5 changes: 3 additions & 2 deletions libraries/chain/transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,10 @@ flat_set<public_key_type> transaction::get_signature_keys( const vector<signatur
public_key_type recov;
if( use_cache ) {
recovery_cache_type::index<by_sig>::type::iterator it = recovery_cache.get<by_sig>().find( sig );
if( it == recovery_cache.get<by_sig>().end() || it->trx_id != id()) {
const auto& tid = id();
if( it == recovery_cache.get<by_sig>().end() || it->trx_id != tid) {
recov = public_key_type( sig, digest );
recovery_cache.emplace_back(cached_pub_key{id(), recov, sig} ); //could fail on dup signatures; not a problem
recovery_cache.emplace_back(cached_pub_key{tid, recov, sig} ); //could fail on dup signatures; not a problem
} else {
recov = it->pub_key;
}
Expand Down
8 changes: 7 additions & 1 deletion libraries/chain/transaction_metadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@ const flat_set<public_key_type>& transaction_metadata::recover_keys( const chain
return signing_keys->second;
}

void transaction_metadata::create_signing_keys_future( transaction_metadata_ptr& mtrx,
void transaction_metadata::create_signing_keys_future( const transaction_metadata_ptr& mtrx,
boost::asio::thread_pool& thread_pool, const chain_id_type& chain_id ) {
if( mtrx->signing_keys && mtrx->signing_keys->first == chain_id )
return;

if( mtrx->signing_keys.valid() ) // already created
return;

std::weak_ptr<transaction_metadata> mtrx_wp = mtrx;
mtrx->signing_keys_future = async_thread_pool( thread_pool, [chain_id, mtrx_wp]() {
auto mtrx = mtrx_wp.lock();
Expand Down
16 changes: 14 additions & 2 deletions plugins/producer_plugin/producer_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,18 @@ class producer_plugin_impl : public std::enable_shared_from_this<producer_plugin
std::deque<std::tuple<transaction_metadata_ptr, bool, next_function<transaction_trace_ptr>>> _pending_incoming_transactions;

void on_incoming_transaction_async(const transaction_metadata_ptr& trx, bool persist_until_expired, next_function<transaction_trace_ptr> next) {
chain::controller& chain = chain_plug->chain();
transaction_metadata::create_signing_keys_future( trx, chain.get_thread_pool(), chain.get_chain_id() );
boost::asio::post( chain.get_thread_pool(), [self = this, trx, persist_until_expired, next]() {
if( trx->signing_keys_future.valid() )
trx->signing_keys_future.wait();
app().get_io_service().post( [self, trx, persist_until_expired, next]() {
self->process_incoming_transaction_async( trx, persist_until_expired, next );
});
});
}

void process_incoming_transaction_async(const transaction_metadata_ptr& trx, bool persist_until_expired, next_function<transaction_trace_ptr> next) {
chain::controller& chain = chain_plug->chain();
if (!chain.pending_block_state()) {
_pending_incoming_transactions.emplace_back(trx, persist_until_expired, next);
Expand Down Expand Up @@ -1245,7 +1257,7 @@ producer_plugin_impl::start_block_result producer_plugin_impl::start_block(bool
_pending_incoming_transactions.pop_front();
--orig_pending_txn_size;
_incoming_trx_weight -= 1.0;
on_incoming_transaction_async(std::get<0>(e), std::get<1>(e), std::get<2>(e));
process_incoming_transaction_async(std::get<0>(e), std::get<1>(e), std::get<2>(e));
}

if (block_time <= fc::time_point::now()) {
Expand Down Expand Up @@ -1308,7 +1320,7 @@ producer_plugin_impl::start_block_result producer_plugin_impl::start_block(bool
auto e = _pending_incoming_transactions.front();
_pending_incoming_transactions.pop_front();
--orig_pending_txn_size;
on_incoming_transaction_async(std::get<0>(e), std::get<1>(e), std::get<2>(e));
process_incoming_transaction_async(std::get<0>(e), std::get<1>(e), std::get<2>(e));
if (block_time <= fc::time_point::now()) return start_block_result::exhausted;
}
}
Expand Down

0 comments on commit 7d3e5c6

Please sign in to comment.