Created
September 16, 2023 17:23
-
-
Save ralexstokes/2ae4203c6f6379d956ec588e8744daf0 to your computer and use it in GitHub Desktop.
Sketch of a fork-agnostic execution engine type for `ethereum-consensus`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mod bellatrix { | |
mod execution_engine { | |
use crate::ExecutionEngine; | |
pub struct ExecutionPayload<const A: usize> {} | |
pub struct NewPayloadRequest<const A: usize>(ExecutionPayload<A>); | |
#[derive(Default)] | |
pub struct DefaultExecutionEngine<const A: usize>; | |
impl<const A: usize> DefaultExecutionEngine<A> { | |
fn is_valid_block_hash(&self, payload: &ExecutionPayload<A>) -> bool { | |
true | |
} | |
fn notify_new_payload(&self, payload: &ExecutionPayload<A>) -> bool { | |
true | |
} | |
} | |
impl<const A: usize> ExecutionEngine for DefaultExecutionEngine<A> { | |
type NewPayloadRequest = NewPayloadRequest<A>; | |
fn verify_and_notify_new_payload( | |
&self, | |
new_payload_request: &Self::NewPayloadRequest, | |
) -> bool { | |
if !self.is_valid_block_hash(&new_payload_request.0) { | |
return false; | |
} | |
self.notify_new_payload(&new_payload_request.0) | |
} | |
} | |
pub trait BellatrixExecutionEngine<const A: usize>: | |
ExecutionEngine<NewPayloadRequest = NewPayloadRequest<A>> | |
{ | |
} | |
impl<const A: usize> BellatrixExecutionEngine<A> for DefaultExecutionEngine<A> {} | |
pub fn process_payload<const A: usize, E: BellatrixExecutionEngine<A>>( | |
payload: ExecutionPayload<A>, | |
execution_engine: &E, | |
) { | |
let request = NewPayloadRequest(payload); | |
execution_engine.verify_and_notify_new_payload(&request); | |
} | |
} | |
pub mod spec { | |
pub use crate::bellatrix::execution_engine::*; | |
} | |
pub mod mainnet { | |
use crate::bellatrix::spec; | |
pub use spec::*; | |
pub type ExecutionEngine = spec::ExecutionPayload<4>; | |
} | |
} | |
mod deneb { | |
mod execution_engine { | |
use crate::ExecutionEngine; | |
pub struct ExecutionPayload<const A: usize> {} | |
pub struct NewPayloadRequest<const A: usize> { | |
payload: ExecutionPayload<A>, | |
versioned_hashes: Vec<[u8; 32]>, | |
parent_beacon_block_root: [u8; 32], | |
} | |
#[derive(Default)] | |
pub struct DefaultExecutionEngine<const A: usize>; | |
impl<const A: usize> DefaultExecutionEngine<A> { | |
fn is_valid_versioned_hashes( | |
&self, | |
new_payload_request: &NewPayloadRequest<A>, | |
) -> bool { | |
true | |
} | |
fn is_valid_block_hash( | |
&self, | |
payload: &ExecutionPayload<A>, | |
parent_beacon_block_root: &[u8; 32], | |
) -> bool { | |
true | |
} | |
fn notify_new_payload( | |
&self, | |
payload: &ExecutionPayload<A>, | |
parent_beacon_block_root: &[u8; 32], | |
) -> bool { | |
true | |
} | |
} | |
impl<const A: usize> ExecutionEngine for DefaultExecutionEngine<A> { | |
type NewPayloadRequest = NewPayloadRequest<A>; | |
fn verify_and_notify_new_payload( | |
&self, | |
new_payload_request: &Self::NewPayloadRequest, | |
) -> bool { | |
if !self.is_valid_block_hash( | |
&new_payload_request.payload, | |
&new_payload_request.parent_beacon_block_root, | |
) { | |
return false; | |
} | |
if !self.is_valid_versioned_hashes(new_payload_request) { | |
return false; | |
} | |
self.notify_new_payload( | |
&new_payload_request.payload, | |
&new_payload_request.parent_beacon_block_root, | |
) | |
} | |
} | |
pub trait DenebExecutionEngine<const A: usize>: | |
ExecutionEngine<NewPayloadRequest = NewPayloadRequest<A>> | |
{ | |
} | |
impl<const A: usize> DenebExecutionEngine<A> for DefaultExecutionEngine<A> {} | |
pub fn process_payload<const A: usize, E: DenebExecutionEngine<A>>( | |
payload: ExecutionPayload<A>, | |
execution_engine: &E, | |
) { | |
let request = NewPayloadRequest { | |
payload, | |
versioned_hashes: Default::default(), | |
parent_beacon_block_root: Default::default(), | |
}; | |
execution_engine.verify_and_notify_new_payload(&request); | |
} | |
} | |
pub mod spec { | |
pub use crate::deneb::execution_engine::*; | |
} | |
pub mod mainnet { | |
use crate::deneb::spec; | |
pub use spec::*; | |
pub type ExecutionEngine = spec::ExecutionPayload<4>; | |
} | |
} | |
pub trait ExecutionEngine { | |
type NewPayloadRequest; | |
fn verify_and_notify_new_payload(&self, new_payload_request: &Self::NewPayloadRequest) -> bool; | |
} | |
fn main() { | |
use bellatrix::mainnet as spec; | |
// use deneb::mainnet as spec; | |
let payload = spec::ExecutionEngine {}; | |
let execution_engine = spec::DefaultExecutionEngine::default(); | |
spec::process_payload(payload, &execution_engine); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment