Skip to content

Commit

Permalink
perf: mine blocks in the context of a blocking task
Browse files Browse the repository at this point in the history
Signed-off-by: ljedrz <[email protected]>
  • Loading branch information
ljedrz committed Nov 17, 2021
1 parent 5ff6147 commit 727bd91
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/network/ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ impl<N: Network, E: Environment> Ledger<N, E> {
}
LedgerRequest::Mine(local_ip, recipient, ledger_router) => {
// Process the request to mine the next block.
self.mine_next_block(local_ip, recipient, ledger_router);
self.mine_next_block(local_ip, recipient, ledger_router).await;
}
LedgerRequest::Ping(peer_ip, block_height, block_hash) => {
// Determine if the peer is on a fork (or unknown).
Expand Down Expand Up @@ -419,7 +419,7 @@ impl<N: Network, E: Environment> Ledger<N, E> {
///
/// Mines a new block and adds it to the canon blocks.
///
fn mine_next_block(&self, local_ip: SocketAddr, recipient: Address<N>, ledger_router: LedgerRouter<N, E>) {
async fn mine_next_block(&self, local_ip: SocketAddr, recipient: Address<N>, ledger_router: LedgerRouter<N, E>) {
// If the node type is not a miner, it should not be mining.
if E::NODE_TYPE != NodeType::Miner {
return;
Expand All @@ -445,21 +445,25 @@ impl<N: Network, E: Environment> Ledger<N, E> {

task::spawn(async move {
// Mine the next block.
let result = canon.mine_next_block(recipient, &unconfirmed_transactions, &terminator, &mut thread_rng());
let result = task::spawn_blocking(move || {
canon.mine_next_block(recipient, &unconfirmed_transactions, &terminator, &mut thread_rng())
})
.await
.map_err(|e| e.into());

// Set the status to `Ready`.
status.store(Status::Ready as u8, Ordering::SeqCst);

match result {
Ok(block) => {
Ok(Ok(block)) => {
trace!("Miner has found the next block");
// Broadcast the next block.
let request = LedgerRequest::UnconfirmedBlock(local_ip, block);
if let Err(error) = ledger_router.send(request).await {
warn!("Failed to broadcast mined block: {}", error);
}
}
Err(error) => trace!("{}", error),
Ok(Err(error)) | Err(error) => trace!("{}", error),
}
});
}
Expand Down

0 comments on commit 727bd91

Please sign in to comment.