Skip to content

Commit

Permalink
Merge pull request #3 from worldcoin/world-chain-builder
Browse files Browse the repository at this point in the history
feat(wc-builder): Implement initial `WorldChainBuilder` logic
  • Loading branch information
0xKitsune authored Sep 6, 2024
2 parents f38f351 + 7d60fdc commit b1034fe
Show file tree
Hide file tree
Showing 6 changed files with 230 additions and 5 deletions.
8 changes: 8 additions & 0 deletions world-chain-builder/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 14 additions & 4 deletions world-chain-builder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,30 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

# reth
reth-cli-util = { git = "https://github.com/paradigmxyz/reth", version = "1.0.6" }
reth-node-builder = { git = "https://github.com/paradigmxyz/reth", version = "1.0.6" }
reth-chainspec = { git = "https://github.com/paradigmxyz/reth", version = "1.0.6" }
reth-evm = { git = "https://github.com/paradigmxyz/reth", version = "1.0.6" }
reth-provider = { git = "https://github.com/paradigmxyz/reth", version = "1.0.6" }
reth-transaction-pool = { git = "https://github.com/paradigmxyz/reth", version = "1.0.6" }
reth-payload-builder = { git = "https://github.com/paradigmxyz/reth", version = "1.0.6" }
reth-basic-payload-builder = { git = "https://github.com/paradigmxyz/reth", version = "1.0.6" }

# reth-optimism
reth-node-optimism = { git = "https://github.com/paradigmxyz/reth", version = "1.0.6", features = [
"optimism",
] }
reth-optimism-cli = { git = "https://github.com/paradigmxyz/reth", version = "1.0.6", features = [
"optimism",
] }
reth-cli-util = { git = "https://github.com/paradigmxyz/reth", version = "1.0.6" }


# other
clap = { version = "4", features = ["derive", "env"] }
eyre = "0.6"


[[bin]]
name = "builder"
path = "bin/builder.rs"
name = "world-chain-builder"
path = "bin/world-chain-builder.rs"
File renamed without changes.
56 changes: 56 additions & 0 deletions world-chain-builder/src/builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use reth_basic_payload_builder::{
BuildArguments, BuildOutcome, MissingPayloadBehaviour, PayloadBuilder, PayloadConfig,
};
use reth_evm::ConfigureEvm;
use reth_node_optimism::{OptimismBuiltPayload, OptimismPayloadBuilderAttributes};
use reth_payload_builder::error::PayloadBuilderError;
use reth_provider::StateProviderFactory;
use reth_transaction_pool::TransactionPool;

/// Priority blockspace for humans builder
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PBHBuilder<EvmConfig> {
// NOTE: do we need this?
// compute_pending_block: bool,
evm_config: EvmConfig,
}

impl<EvmConfig> PBHBuilder<EvmConfig> {
/// `OptimismPayloadBuilder` constructor.
pub const fn new(evm_config: EvmConfig) -> Self {
Self { evm_config }
}
}

/// Implementation of the [`PayloadBuilder`] trait for [`PBHBuilder`].
impl<Pool, Client, EvmConfig> PayloadBuilder<Pool, Client> for PBHBuilder<EvmConfig>
where
Client: StateProviderFactory,
Pool: TransactionPool,
EvmConfig: ConfigureEvm,
{
type Attributes = OptimismPayloadBuilderAttributes;
type BuiltPayload = OptimismBuiltPayload;

fn try_build(
&self,
args: BuildArguments<Pool, Client, OptimismPayloadBuilderAttributes, OptimismBuiltPayload>,
) -> Result<BuildOutcome<OptimismBuiltPayload>, PayloadBuilderError> {
todo!()
}

fn on_missing_payload(
&self,
_args: BuildArguments<Pool, Client, OptimismPayloadBuilderAttributes, OptimismBuiltPayload>,
) -> MissingPayloadBehaviour<Self::BuiltPayload> {
MissingPayloadBehaviour::AwaitInProgress
}

fn build_empty_payload(
&self,
client: &Client,
config: PayloadConfig<Self::Attributes>,
) -> Result<OptimismBuiltPayload, PayloadBuilderError> {
todo!()
}
}
1 change: 1 addition & 0 deletions world-chain-builder/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod builder;
pub mod node;
152 changes: 151 additions & 1 deletion world-chain-builder/src/node.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,152 @@
use reth_basic_payload_builder::{BasicPayloadJobGenerator, BasicPayloadJobGeneratorConfig};
use reth_chainspec::ChainSpec;
use reth_evm::ConfigureEvm;
use reth_node_builder::{
components::{ComponentsBuilder, PayloadServiceBuilder},
BuilderContext, FullNodeTypes, Node, NodeTypes, NodeTypesWithEngine, PayloadBuilderConfig,
};
use reth_node_optimism::{
args::RollupArgs,
node::{
OptimismAddOns, OptimismConsensusBuilder, OptimismExecutorBuilder, OptimismNetworkBuilder,
OptimismPoolBuilder,
},
OptimismEngineTypes, OptimismEvmConfig,
};
use reth_payload_builder::{PayloadBuilderHandle, PayloadBuilderService};
use reth_provider::CanonStateSubscriptions;
use reth_transaction_pool::TransactionPool;

use crate::builder::PBHBuilder;

#[derive(Debug, Clone)]
pub struct WorldChainBuilder {
/// Additional Optimism args
pub args: RollupArgs,
}

impl WorldChainBuilder {
pub const fn new(args: RollupArgs) -> Self {
Self { args }
}

/// Returns the components for the given [`RollupArgs`].
pub fn components<Node>(
args: RollupArgs,
) -> ComponentsBuilder<
Node,
OptimismPoolBuilder,
WorldChainPayloadBuilder,
OptimismNetworkBuilder,
OptimismExecutorBuilder,
OptimismConsensusBuilder,
>
where
Node: FullNodeTypes<
Types: NodeTypesWithEngine<Engine = OptimismEngineTypes, ChainSpec = ChainSpec>,
>,
{
let RollupArgs {
disable_txpool_gossip,
compute_pending_block,
discovery_v4,
..
} = args;
ComponentsBuilder::default()
.node_types::<Node>()
.pool(OptimismPoolBuilder::default())
.payload(WorldChainPayloadBuilder::new(OptimismEvmConfig::default()))
.network(OptimismNetworkBuilder {
disable_txpool_gossip,
disable_discovery_v4: !discovery_v4,
})
.executor(OptimismExecutorBuilder::default())
.consensus(OptimismConsensusBuilder::default())
}
}

impl<N> Node<N> for WorldChainBuilder
where
N: FullNodeTypes<
Types: NodeTypesWithEngine<Engine = OptimismEngineTypes, ChainSpec = ChainSpec>,
>,
{
type ComponentsBuilder = ComponentsBuilder<
N,
OptimismPoolBuilder,
WorldChainPayloadBuilder,
OptimismNetworkBuilder,
OptimismExecutorBuilder,
OptimismConsensusBuilder,
>;

type AddOns = OptimismAddOns;

fn components_builder(&self) -> Self::ComponentsBuilder {
let Self { args } = self;
Self::components(args.clone())
}
}

impl NodeTypes for WorldChainBuilder {
type Primitives = ();
type ChainSpec = ChainSpec;
}

impl NodeTypesWithEngine for WorldChainBuilder {
type Engine = OptimismEngineTypes;
}

#[derive(Debug, Default, Clone)]
pub struct WorldChainPayloadBuilder {}
pub struct WorldChainPayloadBuilder<EVM = OptimismEvmConfig> {
/// The EVM configuration to use for the payload builder.
pub evm_config: EVM,
}

impl<EVM> WorldChainPayloadBuilder<EVM> {
pub const fn new(evm_config: EVM) -> Self {
Self { evm_config }
}
}

impl<Node, EVM, Pool> PayloadServiceBuilder<Node, Pool> for WorldChainPayloadBuilder<EVM>
where
Node: FullNodeTypes<
Types: NodeTypesWithEngine<Engine = OptimismEngineTypes, ChainSpec = ChainSpec>,
>,
Pool: TransactionPool + Unpin + 'static,
EVM: ConfigureEvm,
{
async fn spawn_payload_service(
self,
ctx: &BuilderContext<Node>,
pool: Pool,
) -> eyre::Result<PayloadBuilderHandle<OptimismEngineTypes>> {
let payload_builder = PBHBuilder::new(self.evm_config);

let conf = ctx.payload_builder_config();

let payload_job_config = BasicPayloadJobGeneratorConfig::default()
.interval(conf.interval())
.deadline(conf.deadline())
.max_payload_tasks(conf.max_payload_tasks())
// no extradata for OP
.extradata(Default::default());

let payload_generator = BasicPayloadJobGenerator::with_builder(
ctx.provider().clone(),
pool,
ctx.task_executor().clone(),
payload_job_config,
ctx.chain_spec(),
payload_builder,
);
let (payload_service, payload_builder) =
PayloadBuilderService::new(payload_generator, ctx.provider().canonical_state_stream());

ctx.task_executor()
.spawn_critical("payload builder service", Box::pin(payload_service));

Ok(payload_builder)
}
}

0 comments on commit b1034fe

Please sign in to comment.