Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the block builder pool a block building algorithm #141

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from

Conversation

ferranbt
Copy link
Contributor

@ferranbt ferranbt commented Aug 22, 2024

📝 Summary

Right now there is an ad-hoc BlockBuilderPool entity that lives inside the live_builder and manages a list of BlockBuildingAlgorithm trait implementations. The pool takes the input to build a block and spawns different algorithms with the same sink destination.

The live_builder uses a concrete reference of the BlockBuilderPool and initialises it with a list of builders.

This PR converts the BlockBuilderPool in an implementation of the BlockBuildingAlgorithm trait. Then, the live_builder does not need to have a concrete reference of the BlockBuilderPool but just a dyn BlockBuildingAlgorithm reference which can be a single block building algorithm or a pool of them.

💡 Motivation and Context


✅ I have completed the following steps:

  • Run make lint
  • Run make test
  • Added tests (if applicable)

@ferranbt ferranbt added enhancement New feature or request A-builder Issues related to the core block building protocol labels Aug 22, 2024
Copy link

github-actions bot commented Aug 22, 2024

Benchmark results for e64e682

Report: https://flashbots-rbuilder-ci-stats.s3.us-east-2.amazonaws.com/benchmark/e64e682-b84a0d1/report/index.html

Date (UTC) 2024-08-23T09:46:58+00:00
Commit e64e6829eeb92363fae5dc5ea277a5ceed473894
Base SHA b84a0d1940dd3392f3ea5d069e1dc54d828b9c45

Significant changes

None

tokio::spawn(multiplex_job(simulations_for_block.orders, broadcast_input));

if let Some(builder) = self.builder.as_ref() {
builder.build_blocks(input);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even though you know that BlockBuildingPool is not blocking, builders are expected to be blocking. We should tokio::task::spawn_blocking this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which one do you want as spawn_blocking?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

builder.build_blocks(input);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check again, I think this is resolved now.

Comment on lines 232 to 274
// This was done before in block building pool
{
let max_time_to_build = time_until_slot_end.try_into().unwrap_or_default();
let block_cancellation = self.global_cancellation.clone().child_token();

let cancel = block_cancellation.clone();
tokio::spawn(async move {
tokio::time::sleep(max_time_to_build).await;
cancel.cancel();
});

let (orders_for_block, sink) = OrdersForBlock::new_with_sink();
// add OrderReplacementManager to manage replacements and cancellations
let order_replacement_manager = OrderReplacementManager::new(Box::new(sink));
// sink removal is automatic via OrderSink::is_alive false
let _block_sub = orderpool_subscriber.add_sink(
block_ctx.block_env.number.to(),
Box::new(order_replacement_manager),
);

let simulations_for_block = order_simulation_pool.spawn_simulation_job(
block_ctx.clone(),
orders_for_block,
block_cancellation.clone(),
);

let (broadcast_input, _) = broadcast::channel(10_000);
let builder_sink = sink_factory.create_sink(payload, block_cancellation.clone());

let input = BlockBuildingAlgorithmInput::<DB> {
provider_factory: self.provider_factory.provider_factory_unchecked(),
ctx: block_ctx,
sink: builder_sink,
input: broadcast_input.subscribe(),
cancel: block_cancellation,
};

tokio::spawn(multiplex_job(simulations_for_block.orders, broadcast_input));

if let Some(builder) = self.builder.as_ref() {
builder.build_blocks(input);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you put this into a func so the code is more readable like before?

Copy link
Contributor

@ZanCorDX ZanCorDX left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Due to type restrictions we didn't have time to polish (BlockBuildingAlgorithmInput having a very specific communication method: broadcast::Receiver) you were forced to add an unnecesary background task on LiveBuilder::run():

tokio::spawn(multiplex_job(simulations_for_block.orders, broadcast_input));

I think this can be avoided by using a trait since the only functions called are try_recv and recv, both implemented on broadcast::Receiver and mpsc::Receiver.
You could either add wrapper objects or extend mpsc::Receiver/broadcast/Receiver (this is cheaper).

@ferranbt
Copy link
Contributor Author

You could either add wrapper objects or extend mpsc::Receiver/broadcast/Receiver (this is cheaper)

Any hints on how to do this?

@ferranbt
Copy link
Contributor Author

Shot out to @liamaharon for the help on the fixes.

@@ -74,7 +79,7 @@ pub struct LiveBuilder<DB, BlocksSourceType: SlotSource> {
pub global_cancellation: CancellationToken,

pub sink_factory: Box<dyn UnfinishedBlockBuildingSinkFactory>,
pub builders: Vec<Arc<dyn BlockBuildingAlgorithm<DB>>>,
pub builder: Arc<dyn BlockBuildingAlgorithm<DB>>, // doing the Option because there is a fuunction that creates the live_builder without the builder.
Copy link
Contributor

@liamaharon liamaharon Aug 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason dyn was chosen over making the builder algorithm generic?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-builder Issues related to the core block building protocol enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants