Skip to content

Commit

Permalink
fatp: maintenance on every block imported
Browse files Browse the repository at this point in the history
This should reduce the number of empty blocks, which are built when
block builder is building upon blocks that are unknown to transaction
pool.

The ligh-maintain fallback will also be implemented.
  • Loading branch information
michalkucharczyk committed Jun 21, 2024
1 parent d60b9a3 commit 4b2148e
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion substrate/client/api/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ impl<B: BlockT> TryFrom<BlockImportNotification<B>> for ChainEvent<B> {
if n.is_new_best {
Ok(Self::NewBestBlock { hash: n.hash, tree_route: n.tree_route })
} else {
Err(())
Ok(Self::NewBlock { hash: n.hash })
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion substrate/client/transaction-pool/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,11 @@ impl<T> ReadyTransactions for std::iter::Empty<T> {
/// Events that the transaction pool listens for.
#[derive(Debug)]
pub enum ChainEvent<B: BlockT> {
/// New block have been added to the chain.
NewBlock {
/// Hash of the block.
hash: B::Hash,
},
/// New best block have been added to the chain.
NewBestBlock {
/// Hash of the block.
Expand All @@ -372,7 +377,9 @@ impl<B: BlockT> ChainEvent<B> {
/// Returns the block hash associated to the event.
pub fn hash(&self) -> B::Hash {
match self {
Self::NewBestBlock { hash, .. } | Self::Finalized { hash, .. } => *hash,
Self::NewBlock { hash, .. } |
Self::NewBestBlock { hash, .. } |
Self::Finalized { hash, .. } => *hash,
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ where
/// fallback when tree_route cannot be computed.
pub fn force_update(&mut self, event: &ChainEvent<Block>) {
match event {
ChainEvent::NewBestBlock { hash, .. } => self.recent_best_block = *hash,
ChainEvent::NewBlock { hash, .. } | ChainEvent::NewBestBlock { hash, .. } =>
self.recent_best_block = *hash,
ChainEvent::Finalized { hash, .. } => self.recent_finalized_block = *hash,
};
log::debug!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1124,7 +1124,7 @@ where
};

match event {
ChainEvent::NewBestBlock { .. } => {},
ChainEvent::NewBestBlock { .. } | ChainEvent::NewBlock { .. }=> {},
ChainEvent::Finalized { hash, ref tree_route } => {
self.handle_finalized(hash, tree_route).await;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,10 @@ where
PoolApi: 'static + graph::ChainApi<Block = Block>,
{
async fn maintain(&self, event: ChainEvent<Self::Block>) {
if matches!(event, ChainEvent::NewBlock{..}) {
return
}

let prev_finalized_block = self.enactment_state.lock().recent_finalized_block();
let compute_tree_route = |from, to| -> Result<TreeRoute<Block>, String> {
match self.api.tree_route(from, to) {
Expand Down

0 comments on commit 4b2148e

Please sign in to comment.