Skip to content

Commit

Permalink
feature: move node-api example into examples (paradigmxyz#6390)
Browse files Browse the repository at this point in the history
Signed-off-by: jsvisa <[email protected]>
Co-authored-by: Matthias Seitz <[email protected]>
  • Loading branch information
jsvisa and mattsse authored Feb 5, 2024
1 parent 35ab1b1 commit b7e511d
Show file tree
Hide file tree
Showing 6 changed files with 246 additions and 141 deletions.
20 changes: 19 additions & 1 deletion Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ members = [
"examples/additional-rpc-namespace-in-cli/",
"examples/beacon-api-sse/",
"examples/cli-extension-event-hooks/",
"examples/custom-node/",
"examples/custom-payload-builder/",
"examples/manual-p2p/",
"examples/rpc-db/",
"examples/trace-transaction-cli/",
"examples/custom-payload-builder/",
"examples/polygon-p2p/",
"testing/ef-tests/",
]
Expand Down
6 changes: 1 addition & 5 deletions crates/node-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,4 @@ revm-primitives.workspace = true
thiserror.workspace = true

# io
serde.workspace = true

[dev-dependencies]
# for examples
reth-payload-builder.workspace = true
serde.workspace = true
134 changes: 0 additions & 134 deletions crates/node-api/src/engine/mod.rs
Original file line number Diff line number Diff line change
@@ -1,138 +1,4 @@
//! This contains the [EngineTypes] trait and implementations for ethereum mainnet types.
//!
//! The [EngineTypes] trait can be implemented to configure the engine to work with custom types,
//! as long as those types implement certain traits.
//!
//! Custom payload attributes can be supported by implementing two main traits:
//!
//! [PayloadAttributes] can be implemented for payload attributes types that are used as
//! arguments to the `engine_forkchoiceUpdated` method. This type should be used to define and
//! _spawn_ payload jobs.
//!
//! [PayloadBuilderAttributes] can be implemented for payload attributes types that _describe_
//! running payload jobs.
//!
//! Once traits are implemented and custom types are defined, the [EngineTypes] trait can be
//! implemented:
//! ```no_run
//! # use reth_rpc_types::engine::{PayloadAttributes as EthPayloadAttributes, PayloadId};
//! # use reth_rpc_types::Withdrawal;
//! # use reth_primitives::{B256, ChainSpec, Address, Withdrawals};
//! # use reth_node_api::{EngineTypes, EngineApiMessageVersion, validate_version_specific_fields, AttributesValidationError, PayloadAttributes, PayloadBuilderAttributes, PayloadOrAttributes};
//! # use reth_payload_builder::{EthPayloadBuilderAttributes, EthBuiltPayload};
//! # use serde::{Deserialize, Serialize};
//! # use thiserror::Error;
//! # use std::convert::Infallible;
//!
//! /// A custom payload attributes type.
//! #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
//! pub struct CustomPayloadAttributes {
//! /// An inner payload type
//! #[serde(flatten)]
//! pub inner: EthPayloadAttributes,
//! /// A custom field
//! pub custom: u64,
//! }
//!
//! /// Custom error type used in payload attributes validation
//! #[derive(Debug, Error)]
//! pub enum CustomError {
//! #[error("Custom field is not zero")]
//! CustomFieldIsNotZero,
//! }
//!
//! impl PayloadAttributes for CustomPayloadAttributes {
//! fn timestamp(&self) -> u64 {
//! self.inner.timestamp()
//! }
//!
//! fn withdrawals(&self) -> Option<&Vec<Withdrawal>> {
//! self.inner.withdrawals()
//! }
//!
//! fn parent_beacon_block_root(&self) -> Option<B256> {
//! self.inner.parent_beacon_block_root()
//! }
//!
//! fn ensure_well_formed_attributes(
//! &self,
//! chain_spec: &ChainSpec,
//! version: EngineApiMessageVersion,
//! ) -> Result<(), AttributesValidationError> {
//! validate_version_specific_fields(chain_spec, version, self.into())?;
//!
//! // custom validation logic - ensure that the custom field is not zero
//! if self.custom == 0 {
//! return Err(AttributesValidationError::invalid_params(
//! CustomError::CustomFieldIsNotZero,
//! ))
//! }
//!
//! Ok(())
//! }
//! }
//!
//! /// Newtype around the payload builder attributes type
//! #[derive(Clone, Debug, PartialEq, Eq)]
//! pub struct CustomPayloadBuilderAttributes(EthPayloadBuilderAttributes);
//!
//! impl PayloadBuilderAttributes for CustomPayloadBuilderAttributes {
//! type RpcPayloadAttributes = CustomPayloadAttributes;
//! type Error = Infallible;
//!
//! fn try_new(parent: B256, attributes: CustomPayloadAttributes) -> Result<Self, Infallible> {
//! Ok(Self(EthPayloadBuilderAttributes::new(parent, attributes.inner)))
//! }
//!
//! fn parent(&self) -> B256 {
//! self.0.parent
//! }
//!
//! fn payload_id(&self) -> PayloadId {
//! self.0.id
//! }
//!
//! fn timestamp(&self) -> u64 {
//! self.0.timestamp
//! }
//!
//! fn parent_beacon_block_root(&self) -> Option<B256> {
//! self.0.parent_beacon_block_root
//! }
//!
//! fn suggested_fee_recipient(&self) -> Address {
//! self.0.suggested_fee_recipient
//! }
//!
//! fn prev_randao(&self) -> B256 {
//! self.0.prev_randao
//! }
//!
//! fn withdrawals(&self) -> &Withdrawals {
//! &self.0.withdrawals
//! }
//! }
//!
//! /// Custom engine types - uses a custom payload attributes RPC type, but uses the default
//! /// payload builder attributes type.
//! #[derive(Clone, Debug, Default, serde::Deserialize)]
//! #[non_exhaustive]
//! pub struct CustomEngineTypes;
//!
//! impl EngineTypes for CustomEngineTypes {
//! type PayloadAttributes = CustomPayloadAttributes;
//! type PayloadBuilderAttributes = CustomPayloadBuilderAttributes;
//! type BuiltPayload = EthBuiltPayload;
//!
//! fn validate_version_specific_fields(
//! chain_spec: &ChainSpec,
//! version: EngineApiMessageVersion,
//! payload_or_attrs: PayloadOrAttributes<'_, CustomPayloadAttributes>,
//! ) -> Result<(), AttributesValidationError> {
//! validate_version_specific_fields(chain_spec, version, payload_or_attrs)
//! }
//! }
//! ```
use reth_primitives::{ChainSpec, Hardfork};

Expand Down
24 changes: 24 additions & 0 deletions examples/custom-node/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "custom-node"
version = "0.0.0"
publish = false
edition.workspace = true
license.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
reth.workspace = true
reth-rpc-api.workspace = true
reth-rpc-types.workspace = true
reth-node-api.workspace = true
reth-node-core.workspace = true
reth-primitives.workspace = true
reth-payload-builder.workspace = true

alloy-chains.workspace = true
jsonrpsee.workspace = true
eyre.workspace = true
tokio.workspace = true
thiserror.workspace = true
serde.workspace = true
Loading

0 comments on commit b7e511d

Please sign in to comment.