Skip to content

Commit

Permalink
daemon: don't keep the lock
Browse files Browse the repository at this point in the history
  • Loading branch information
Slixe committed Oct 26, 2023
1 parent 0794799 commit 2ea7bc2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 34 deletions.
2 changes: 2 additions & 0 deletions xelis_daemon/src/core/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1162,10 +1162,12 @@ impl<S: Storage> Blockchain<S> {
}

pub async fn build_block_from_header(&self, header: Immutable<BlockHeader>) -> Result<Block, BlockchainError> {
trace!("Searching TXs for block at height {}", header.get_height());
let mut transactions: Vec<Immutable<Transaction>> = Vec::with_capacity(header.get_txs_count());
let storage = self.storage.read().await;
let mempool = self.mempool.read().await;
for hash in header.get_txs_hashes() {
trace!("Searching TX {} for building block", hash);
// at this point, we don't want to lose/remove any tx, we clone it only
let tx = if mempool.contains_tx(hash) {
mempool.get_tx(hash)?
Expand Down
73 changes: 39 additions & 34 deletions xelis_daemon/src/p2p/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1064,45 +1064,50 @@ impl<S: Storage> P2pServer<S> {
let request = request.into_owned();
match &request {
ObjectRequest::Block(hash) => {
let storage = self.blockchain.get_storage().read().await;
if storage.has_block(hash).await? {
let block = storage.get_block(hash).await?;
peer.send_packet(Packet::ObjectResponse(ObjectResponse::Block(Cow::Owned(block)))).await?;
} else {
debug!("{} asked block '{}' but not present in our chain", peer, hash);
peer.send_packet(Packet::ObjectResponse(ObjectResponse::NotFound(request))).await?;
}
debug!("{} asked full block {}", peer, hash);
let block = {
let storage = self.blockchain.get_storage().read().await;
storage.get_block(hash).await
};

match block {
Ok(block) => {
debug!("block {} found, sending it", hash);
peer.send_packet(Packet::ObjectResponse(ObjectResponse::Block(Cow::Borrowed(&block)))).await?;
},
Err(e) => {
debug!("{} asked block '{}' but not present in our chain: {}", peer, hash, e);
peer.send_packet(Packet::ObjectResponse(ObjectResponse::NotFound(request))).await?;
}
};
},
ObjectRequest::BlockHeader(hash) => {
let storage = self.blockchain.get_storage().read().await;
if storage.has_block(hash).await? {
let header = storage.get_block_header_by_hash(hash).await?;
peer.send_packet(Packet::ObjectResponse(ObjectResponse::BlockHeader(Cow::Borrowed(&header)))).await?;
} else {
debug!("{} asked block header '{}' but not present in our chain", peer, hash);
peer.send_packet(Packet::ObjectResponse(ObjectResponse::NotFound(request))).await?;
}
},
ObjectRequest::Transaction(hash) => {
let search_on_disk = {
let mempool = self.blockchain.get_mempool().read().await;
if let Ok(tx) = mempool.view_tx(hash) {
peer.send_packet(Packet::ObjectResponse(ObjectResponse::Transaction(Cow::Borrowed(tx)))).await?;
false
} else {
debug!("{} asked transaction '{}' but not present in our mempool", peer, hash);
true
}
debug!("{} asked block header {}", peer, hash);
let block = {
let storage = self.blockchain.get_storage().read().await;
storage.get_block_header_by_hash(hash).await
};

if search_on_disk {
debug!("Looking on disk for transaction {}", hash);
let storage = self.blockchain.get_storage().read().await;
if storage.has_transaction(hash).await? {
let tx = storage.get_transaction(hash).await?;
match block {
Ok(block) => {
debug!("block header {} found, sending it", hash);
peer.send_packet(Packet::ObjectResponse(ObjectResponse::BlockHeader(Cow::Borrowed(&block)))).await?;
},
Err(e) => {
debug!("{} asked block header '{}' but not present in our chain: {}", peer, hash, e);
peer.send_packet(Packet::ObjectResponse(ObjectResponse::NotFound(request))).await?;
}
};
},
ObjectRequest::Transaction(hash) => {
debug!("{} asked tx {}", peer, hash);
match self.blockchain.get_tx(hash).await {
Ok(tx) => {
debug!("tx {} found, sending it", hash);
peer.send_packet(Packet::ObjectResponse(ObjectResponse::Transaction(Cow::Borrowed(&tx)))).await?;
} else {
debug!("{} asked transaction '{}' but not present in our chain", peer, hash);
},
Err(e) => {
debug!("{} asked tx '{}' but not present in our chain: {}", peer, hash, e);
peer.send_packet(Packet::ObjectResponse(ObjectResponse::NotFound(request))).await?;
}
}
Expand Down

0 comments on commit 2ea7bc2

Please sign in to comment.