Skip to content

Commit

Permalink
small refactoring (#6531)
Browse files Browse the repository at this point in the history
  • Loading branch information
tcoratger authored Feb 10, 2024
1 parent 1bc75d9 commit 8cfa5ef
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 35 deletions.
18 changes: 6 additions & 12 deletions crates/blockchain-tree/src/block_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,7 @@ impl BlockBuffer {
parent_hash: &BlockHash,
) -> Vec<SealedBlockWithSenders> {
// remove parent block if present
let mut removed = Vec::new();
if let Some(block) = self.remove_block(parent_hash) {
removed.push(block);
}
let mut removed = self.remove_block(parent_hash).into_iter().collect::<Vec<_>>();

removed.extend(self.remove_children(vec![*parent_hash]));
self.metrics.blocks.set(self.blocks.len() as f64);
Expand Down Expand Up @@ -157,14 +154,11 @@ impl BlockBuffer {
/// The block might be missing from other collections, the method will only ensure that it has
/// been removed.
fn remove_block(&mut self, hash: &BlockHash) -> Option<SealedBlockWithSenders> {
if let Some(block) = self.blocks.remove(hash) {
self.remove_from_earliest_blocks(block.number, hash);
self.remove_from_parent(block.parent_hash, hash);
self.lru.pop(hash);
Some(block)
} else {
None
}
let block = self.blocks.remove(hash)?;
self.remove_from_earliest_blocks(block.number, hash);
self.remove_from_parent(block.parent_hash, hash);
self.lru.pop(hash);
Some(block)
}

/// Remove all children and their descendants for the given blocks and return them.
Expand Down
14 changes: 8 additions & 6 deletions crates/blockchain-tree/src/block_indices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,14 @@ impl BlockIndices {
/// Remove chain from indices and return dependent chains that need to be removed.
/// Does the cleaning of the tree and removing blocks from the chain.
pub fn remove_chain(&mut self, chain: &Chain) -> BTreeSet<BlockChainId> {
let mut lose_chains = BTreeSet::new();
for (block_number, block) in chain.blocks().iter() {
let block_hash = block.hash();
lose_chains.extend(self.remove_block(*block_number, block_hash))
}
lose_chains
chain
.blocks()
.iter()
.flat_map(|(block_number, block)| {
let block_hash = block.hash();
self.remove_block(*block_number, block_hash)
})
.collect()
}

/// Remove Blocks from indices.
Expand Down
3 changes: 1 addition & 2 deletions crates/net/common/src/bandwidth_meter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

use crate::stream::HasRemoteAddr;
use std::{
convert::TryFrom as _,
io,
Expand All @@ -38,8 +39,6 @@ use tokio::{
net::TcpStream,
};

use crate::stream::HasRemoteAddr;

/// Meters bandwidth usage of streams
#[derive(Debug)]
struct BandwidthMeterInner {
Expand Down
7 changes: 1 addition & 6 deletions crates/payload/basic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,7 @@ impl<Client, Pool, Tasks, Builder> BasicPayloadJobGenerator<Client, Pool, Tasks,
/// Returns the pre-cached reads for the given parent block if it matches the cached state's
/// block.
fn maybe_pre_cached(&self, parent: B256) -> Option<CachedReads> {
let pre_cached = self.pre_cached.as_ref()?;
if pre_cached.block == parent {
Some(pre_cached.cached.clone())
} else {
None
}
self.pre_cached.as_ref().filter(|pc| pc.block == parent).map(|pc| pc.cached.clone())
}
}

Expand Down
5 changes: 1 addition & 4 deletions crates/rpc/ipc/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,7 @@ impl TransportReceiverT for Receiver {

/// Returns a Future resolving when the server sent us something back.
async fn receive(&mut self) -> Result<ReceivedMessage, Self::Error> {
match self.inner.next().await {
None => Err(IpcError::Closed),
Some(val) => Ok(ReceivedMessage::Text(val?)),
}
self.inner.next().await.map_or(Err(IpcError::Closed), |val| Ok(ReceivedMessage::Text(val?)))
}
}

Expand Down
9 changes: 4 additions & 5 deletions crates/rpc/ipc/src/server/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,10 @@ where

/// Polls to accept a new incoming connection to the endpoint.
pub(crate) fn poll_accept(&mut self, cx: &mut Context<'_>) -> Poll<<Self as Stream>::Item> {
let res = match ready!(self.poll_next_unpin(cx)) {
None => Err(io::Error::new(io::ErrorKind::ConnectionAborted, "ipc connection closed")),
Some(conn) => conn,
};
Poll::Ready(res)
Poll::Ready(ready!(self.poll_next_unpin(cx)).map_or(
Err(io::Error::new(io::ErrorKind::ConnectionAborted, "ipc connection closed")),
|conn| conn,
))
}
}

Expand Down

0 comments on commit 8cfa5ef

Please sign in to comment.