Use E for EthSpec globally (#5264)

* Use `E` for `EthSpec` globally

* Fix tests

* Merge branch 'unstable' into e-ethspec

* Merge branch 'unstable' into e-ethspec

# Conflicts:
#	beacon_node/execution_layer/src/engine_api.rs
#	beacon_node/execution_layer/src/engine_api/http.rs
#	beacon_node/execution_layer/src/engine_api/json_structures.rs
#	beacon_node/execution_layer/src/test_utils/handle_rpc.rs
#	beacon_node/store/src/partial_beacon_state.rs
#	consensus/types/src/beacon_block.rs
#	consensus/types/src/beacon_block_body.rs
#	consensus/types/src/beacon_state.rs
#	consensus/types/src/config_and_preset.rs
#	consensus/types/src/execution_payload.rs
#	consensus/types/src/execution_payload_header.rs
#	consensus/types/src/light_client_optimistic_update.rs
#	consensus/types/src/payload.rs
#	lcli/src/parse_ssz.rs
This commit is contained in:
Mac L
2024-04-03 02:12:25 +11:00
committed by GitHub
parent f8fdb71f50
commit 969d12dc6f
230 changed files with 2743 additions and 2792 deletions

View File

@@ -35,12 +35,12 @@ const GAS_USED: u64 = GAS_LIMIT - 1;
#[derive(Clone, Debug, PartialEq)]
#[allow(clippy::large_enum_variant)] // This struct is only for testing.
pub enum Block<T: EthSpec> {
pub enum Block<E: EthSpec> {
PoW(PoWBlock),
PoS(ExecutionPayload<T>),
PoS(ExecutionPayload<E>),
}
impl<T: EthSpec> Block<T> {
impl<E: EthSpec> Block<E> {
pub fn block_number(&self) -> u64 {
match self {
Block::PoW(block) => block.block_number,
@@ -88,7 +88,7 @@ impl<T: EthSpec> Block<T> {
}
}
pub fn as_execution_block_with_tx(&self) -> Option<ExecutionBlockWithTransactions<T>> {
pub fn as_execution_block_with_tx(&self) -> Option<ExecutionBlockWithTransactions<E>> {
match self {
Block::PoS(payload) => Some(payload.clone().try_into().unwrap()),
Block::PoW(_) => None,
@@ -107,13 +107,13 @@ pub struct PoWBlock {
}
#[derive(Debug, Clone)]
pub struct ExecutionBlockGenerator<T: EthSpec> {
pub struct ExecutionBlockGenerator<E: EthSpec> {
/*
* Common database
*/
head_block: Option<Block<T>>,
head_block: Option<Block<E>>,
finalized_block_hash: Option<ExecutionBlockHash>,
blocks: HashMap<ExecutionBlockHash, Block<T>>,
blocks: HashMap<ExecutionBlockHash, Block<E>>,
block_hashes: HashMap<u64, Vec<ExecutionBlockHash>>,
/*
* PoW block parameters
@@ -124,9 +124,9 @@ pub struct ExecutionBlockGenerator<T: EthSpec> {
/*
* PoS block parameters
*/
pub pending_payloads: HashMap<ExecutionBlockHash, ExecutionPayload<T>>,
pub pending_payloads: HashMap<ExecutionBlockHash, ExecutionPayload<E>>,
pub next_payload_id: u64,
pub payload_ids: HashMap<PayloadId, ExecutionPayload<T>>,
pub payload_ids: HashMap<PayloadId, ExecutionPayload<E>>,
/*
* Post-merge fork triggers
*/
@@ -136,7 +136,7 @@ pub struct ExecutionBlockGenerator<T: EthSpec> {
/*
* deneb stuff
*/
pub blobs_bundles: HashMap<PayloadId, BlobsBundle<T>>,
pub blobs_bundles: HashMap<PayloadId, BlobsBundle<E>>,
pub kzg: Option<Arc<Kzg>>,
rng: Arc<Mutex<StdRng>>,
}
@@ -147,7 +147,7 @@ fn make_rng() -> Arc<Mutex<StdRng>> {
Arc::new(Mutex::new(StdRng::seed_from_u64(0xDEADBEEF0BAD5EEDu64)))
}
impl<T: EthSpec> ExecutionBlockGenerator<T> {
impl<E: EthSpec> ExecutionBlockGenerator<E> {
pub fn new(
terminal_total_difficulty: Uint256,
terminal_block_number: u64,
@@ -181,7 +181,7 @@ impl<T: EthSpec> ExecutionBlockGenerator<T> {
gen
}
pub fn latest_block(&self) -> Option<Block<T>> {
pub fn latest_block(&self) -> Option<Block<E>> {
self.head_block.clone()
}
@@ -190,7 +190,7 @@ impl<T: EthSpec> ExecutionBlockGenerator<T> {
.map(|block| block.as_execution_block(self.terminal_total_difficulty))
}
pub fn block_by_number(&self, number: u64) -> Option<Block<T>> {
pub fn block_by_number(&self, number: u64) -> Option<Block<E>> {
// Get the latest canonical head block
let mut latest_block = self.latest_block()?;
loop {
@@ -223,7 +223,7 @@ impl<T: EthSpec> ExecutionBlockGenerator<T> {
.map(|block| block.as_execution_block(self.terminal_total_difficulty))
}
pub fn block_by_hash(&self, hash: ExecutionBlockHash) -> Option<Block<T>> {
pub fn block_by_hash(&self, hash: ExecutionBlockHash) -> Option<Block<E>> {
self.blocks.get(&hash).cloned()
}
@@ -235,7 +235,7 @@ impl<T: EthSpec> ExecutionBlockGenerator<T> {
pub fn execution_block_with_txs_by_hash(
&self,
hash: ExecutionBlockHash,
) -> Option<ExecutionBlockWithTransactions<T>> {
) -> Option<ExecutionBlockWithTransactions<E>> {
self.block_by_hash(hash)
.and_then(|block| block.as_execution_block_with_tx())
}
@@ -243,7 +243,7 @@ impl<T: EthSpec> ExecutionBlockGenerator<T> {
pub fn execution_block_with_txs_by_number(
&self,
number: u64,
) -> Option<ExecutionBlockWithTransactions<T>> {
) -> Option<ExecutionBlockWithTransactions<E>> {
self.block_by_number(number)
.and_then(|block| block.as_execution_block_with_tx())
}
@@ -368,7 +368,7 @@ impl<T: EthSpec> ExecutionBlockGenerator<T> {
// This does not reject duplicate blocks inserted. This lets us re-use the same execution
// block generator for multiple beacon chains which is useful in testing.
pub fn insert_block(&mut self, block: Block<T>) -> Result<ExecutionBlockHash, String> {
pub fn insert_block(&mut self, block: Block<E>) -> Result<ExecutionBlockHash, String> {
if block.parent_hash() != ExecutionBlockHash::zero()
&& !self.blocks.contains_key(&block.parent_hash())
{
@@ -378,7 +378,7 @@ impl<T: EthSpec> ExecutionBlockGenerator<T> {
Ok(self.insert_block_without_checks(block))
}
pub fn insert_block_without_checks(&mut self, block: Block<T>) -> ExecutionBlockHash {
pub fn insert_block_without_checks(&mut self, block: Block<E>) -> ExecutionBlockHash {
let block_hash = block.block_hash();
self.block_hashes
.entry(block.block_number())
@@ -389,7 +389,7 @@ impl<T: EthSpec> ExecutionBlockGenerator<T> {
block_hash
}
pub fn modify_last_block(&mut self, block_modifier: impl FnOnce(&mut Block<T>)) {
pub fn modify_last_block(&mut self, block_modifier: impl FnOnce(&mut Block<E>)) {
if let Some(last_block_hash) = self
.block_hashes
.iter_mut()
@@ -423,15 +423,15 @@ impl<T: EthSpec> ExecutionBlockGenerator<T> {
}
}
pub fn get_payload(&mut self, id: &PayloadId) -> Option<ExecutionPayload<T>> {
pub fn get_payload(&mut self, id: &PayloadId) -> Option<ExecutionPayload<E>> {
self.payload_ids.get(id).cloned()
}
pub fn get_blobs_bundle(&mut self, id: &PayloadId) -> Option<BlobsBundle<T>> {
pub fn get_blobs_bundle(&mut self, id: &PayloadId) -> Option<BlobsBundle<E>> {
self.blobs_bundles.get(id).cloned()
}
pub fn new_payload(&mut self, payload: ExecutionPayload<T>) -> PayloadStatusV1 {
pub fn new_payload(&mut self, payload: ExecutionPayload<E>) -> PayloadStatusV1 {
let Some(parent) = self.blocks.get(&payload.parent_hash()) else {
return PayloadStatusV1 {
status: PayloadStatusV1Status::Syncing,
@@ -551,10 +551,10 @@ impl<T: EthSpec> ExecutionBlockGenerator<T> {
pub fn build_new_execution_payload(
&mut self,
head_block_hash: ExecutionBlockHash,
parent: &Block<T>,
parent: &Block<E>,
id: PayloadId,
attributes: &PayloadAttributes,
) -> Result<ExecutionPayload<T>, String> {
) -> Result<ExecutionPayload<E>, String> {
let mut execution_payload = match attributes {
PayloadAttributes::V1(pa) => ExecutionPayload::Merge(ExecutionPayloadMerge {
parent_hash: head_block_hash,
@@ -656,7 +656,7 @@ impl<T: EthSpec> ExecutionBlockGenerator<T> {
ForkName::Deneb | ForkName::Electra => {
// get random number between 0 and Max Blobs
let mut rng = self.rng.lock();
let num_blobs = rng.gen::<usize>() % (T::max_blobs_per_block() + 1);
let num_blobs = rng.gen::<usize>() % (E::max_blobs_per_block() + 1);
let (bundle, transactions) = generate_blobs(num_blobs)?;
for tx in Vec::from(transactions) {
execution_payload
@@ -728,7 +728,7 @@ pub fn generate_blobs<E: EthSpec>(
Ok((bundle, transactions.into()))
}
pub fn static_valid_tx<T: EthSpec>() -> Result<Transaction<T::MaxBytesPerTransaction>, String> {
pub fn static_valid_tx<E: EthSpec>() -> Result<Transaction<E::MaxBytesPerTransaction>, String> {
// This is a real transaction hex encoded, but we don't care about the contents of the transaction.
let transaction: EthersTransaction = serde_json::from_str(
r#"{
@@ -757,11 +757,11 @@ fn payload_id_from_u64(n: u64) -> PayloadId {
n.to_le_bytes()
}
pub fn generate_genesis_header<T: EthSpec>(
pub fn generate_genesis_header<E: EthSpec>(
spec: &ChainSpec,
post_transition_merge: bool,
) -> Option<ExecutionPayloadHeader<T>> {
let genesis_fork = spec.fork_name_at_slot::<T>(spec.genesis_slot);
) -> Option<ExecutionPayloadHeader<E>> {
let genesis_fork = spec.fork_name_at_slot::<E>(spec.genesis_slot);
let genesis_block_hash =
generate_genesis_block(spec.terminal_total_difficulty, DEFAULT_TERMINAL_BLOCK)
.ok()
@@ -774,7 +774,7 @@ pub fn generate_genesis_header<T: EthSpec>(
*header.block_hash_mut() = genesis_block_hash.unwrap_or_default();
Some(header)
} else {
Some(ExecutionPayloadHeader::<T>::Merge(<_>::default()))
Some(ExecutionPayloadHeader::<E>::Merge(<_>::default()))
}
}
ForkName::Capella => {

View File

@@ -11,9 +11,9 @@ pub const BAD_PARAMS_ERROR_CODE: i64 = -32602;
pub const UNKNOWN_PAYLOAD_ERROR_CODE: i64 = -38001;
pub const FORK_REQUEST_MISMATCH_ERROR_CODE: i64 = -32000;
pub async fn handle_rpc<T: EthSpec>(
pub async fn handle_rpc<E: EthSpec>(
body: JsonValue,
ctx: Arc<Context<T>>,
ctx: Arc<Context<E>>,
) -> Result<JsonValue, (String, i64)> {
*ctx.previous_request.lock() = Some(body.clone());
@@ -95,26 +95,26 @@ pub async fn handle_rpc<T: EthSpec>(
ENGINE_NEW_PAYLOAD_V1 | ENGINE_NEW_PAYLOAD_V2 | ENGINE_NEW_PAYLOAD_V3 => {
let request = match method {
ENGINE_NEW_PAYLOAD_V1 => JsonExecutionPayload::V1(
get_param::<JsonExecutionPayloadV1<T>>(params, 0)
get_param::<JsonExecutionPayloadV1<E>>(params, 0)
.map_err(|s| (s, BAD_PARAMS_ERROR_CODE))?,
),
ENGINE_NEW_PAYLOAD_V2 => get_param::<JsonExecutionPayloadV2<T>>(params, 0)
ENGINE_NEW_PAYLOAD_V2 => get_param::<JsonExecutionPayloadV2<E>>(params, 0)
.map(|jep| JsonExecutionPayload::V2(jep))
.or_else(|_| {
get_param::<JsonExecutionPayloadV1<T>>(params, 0)
get_param::<JsonExecutionPayloadV1<E>>(params, 0)
.map(|jep| JsonExecutionPayload::V1(jep))
})
.map_err(|s| (s, BAD_PARAMS_ERROR_CODE))?,
ENGINE_NEW_PAYLOAD_V3 => get_param::<JsonExecutionPayloadV4<T>>(params, 0)
ENGINE_NEW_PAYLOAD_V3 => get_param::<JsonExecutionPayloadV4<E>>(params, 0)
.map(|jep| JsonExecutionPayload::V4(jep))
.or_else(|_| {
get_param::<JsonExecutionPayloadV3<T>>(params, 0)
get_param::<JsonExecutionPayloadV3<E>>(params, 0)
.map(|jep| JsonExecutionPayload::V3(jep))
.or_else(|_| {
get_param::<JsonExecutionPayloadV2<T>>(params, 0)
get_param::<JsonExecutionPayloadV2<E>>(params, 0)
.map(|jep| JsonExecutionPayload::V2(jep))
.or_else(|_| {
get_param::<JsonExecutionPayloadV1<T>>(params, 0)
get_param::<JsonExecutionPayloadV1<E>>(params, 0)
.map(|jep| JsonExecutionPayload::V1(jep))
})
})
@@ -543,7 +543,7 @@ pub async fn handle_rpc<T: EthSpec>(
match maybe_block {
Some(block) => {
let transactions = Transactions::<T>::new(
let transactions = Transactions::<E>::new(
block
.transactions()
.iter()
@@ -563,7 +563,7 @@ pub async fn handle_rpc<T: EthSpec>(
)
})?;
response.push(Some(JsonExecutionPayloadBodyV1::<T> {
response.push(Some(JsonExecutionPayloadBodyV1::<E> {
transactions,
withdrawals: block
.withdrawals()

View File

@@ -9,14 +9,14 @@ use kzg::Kzg;
use tempfile::NamedTempFile;
use types::MainnetEthSpec;
pub struct MockExecutionLayer<T: EthSpec> {
pub server: MockServer<T>,
pub el: ExecutionLayer<T>,
pub struct MockExecutionLayer<E: EthSpec> {
pub server: MockServer<E>,
pub el: ExecutionLayer<E>,
pub executor: TaskExecutor,
pub spec: ChainSpec,
}
impl<T: EthSpec> MockExecutionLayer<T> {
impl<E: EthSpec> MockExecutionLayer<E> {
pub fn default_params(executor: TaskExecutor) -> Self {
let mut spec = MainnetEthSpec::default_spec();
spec.terminal_total_difficulty = DEFAULT_TERMINAL_DIFFICULTY.into();
@@ -146,7 +146,7 @@ impl<T: EthSpec> MockExecutionLayer<T> {
.await
.unwrap();
let payload: ExecutionPayload<T> = match block_proposal_content_type {
let payload: ExecutionPayload<E> = match block_proposal_content_type {
BlockProposalContentsType::Full(block) => block.to_payload().into(),
BlockProposalContentsType::Blinded(_) => panic!("Should always be a full payload"),
};
@@ -219,9 +219,9 @@ impl<T: EthSpec> MockExecutionLayer<T> {
}
#[allow(clippy::too_many_arguments)]
pub async fn assert_valid_execution_payload_on_head<Payload: AbstractExecPayload<T>>(
pub async fn assert_valid_execution_payload_on_head<Payload: AbstractExecPayload<E>>(
&self,
payload: ExecutionPayload<T>,
payload: ExecutionPayload<E>,
payload_header: Payload,
block_hash: ExecutionBlockHash,
parent_hash: ExecutionBlockHash,
@@ -307,7 +307,7 @@ impl<T: EthSpec> MockExecutionLayer<T> {
pub async fn with_terminal_block<'a, U, V>(self, func: U) -> Self
where
U: Fn(ChainSpec, ExecutionLayer<T>, Option<ExecutionBlock>) -> V,
U: Fn(ChainSpec, ExecutionLayer<E>, Option<ExecutionBlock>) -> V,
V: Future<Output = ()>,
{
let terminal_block_number = self

View File

@@ -84,14 +84,14 @@ impl Default for MockExecutionConfig {
}
}
pub struct MockServer<T: EthSpec> {
pub struct MockServer<E: EthSpec> {
_shutdown_tx: oneshot::Sender<()>,
listen_socket_addr: SocketAddr,
last_echo_request: Arc<RwLock<Option<Bytes>>>,
pub ctx: Arc<Context<T>>,
pub ctx: Arc<Context<E>>,
}
impl<T: EthSpec> MockServer<T> {
impl<E: EthSpec> MockServer<E> {
pub fn unit_testing() -> Self {
Self::new(
&runtime::Handle::current(),
@@ -133,7 +133,7 @@ impl<T: EthSpec> MockServer<T> {
kzg,
);
let ctx: Arc<Context<T>> = Arc::new(Context {
let ctx: Arc<Context<E>> = Arc::new(Context {
config: server_config,
jwt_key,
log: null_logger().unwrap(),
@@ -211,7 +211,7 @@ impl<T: EthSpec> MockServer<T> {
)
}
pub fn execution_block_generator(&self) -> RwLockWriteGuard<'_, ExecutionBlockGenerator<T>> {
pub fn execution_block_generator(&self) -> RwLockWriteGuard<'_, ExecutionBlockGenerator<E>> {
self.ctx.execution_block_generator.write()
}
@@ -423,7 +423,7 @@ impl<T: EthSpec> MockServer<T> {
.insert_block_without_checks(block);
}
pub fn get_block(&self, block_hash: ExecutionBlockHash) -> Option<Block<T>> {
pub fn get_block(&self, block_hash: ExecutionBlockHash) -> Option<Block<E>> {
self.ctx
.execution_block_generator
.read()
@@ -501,12 +501,12 @@ impl warp::reject::Reject for AuthError {}
/// A wrapper around all the items required to spawn the HTTP server.
///
/// The server will gracefully handle the case where any fields are `None`.
pub struct Context<T: EthSpec> {
pub struct Context<E: EthSpec> {
pub config: Config,
pub jwt_key: JwtKey,
pub log: Logger,
pub last_echo_request: Arc<RwLock<Option<Bytes>>>,
pub execution_block_generator: RwLock<ExecutionBlockGenerator<T>>,
pub execution_block_generator: RwLock<ExecutionBlockGenerator<E>>,
pub preloaded_responses: Arc<Mutex<Vec<serde_json::Value>>>,
pub previous_request: Arc<Mutex<Option<serde_json::Value>>>,
pub static_new_payload_response: Arc<Mutex<Option<StaticNewPayloadResponse>>>,
@@ -525,10 +525,10 @@ pub struct Context<T: EthSpec> {
pub syncing_response: Arc<Mutex<Result<bool, String>>>,
pub engine_capabilities: Arc<RwLock<EngineCapabilities>>,
pub _phantom: PhantomData<T>,
pub _phantom: PhantomData<E>,
}
impl<T: EthSpec> Context<T> {
impl<E: EthSpec> Context<E> {
pub fn get_new_payload_status(
&self,
block_hash: &ExecutionBlockHash,
@@ -637,8 +637,8 @@ async fn handle_rejection(err: Rejection) -> Result<impl warp::Reply, Infallible
///
/// Returns an error if the server is unable to bind or there is another error during
/// configuration.
pub fn serve<T: EthSpec>(
ctx: Arc<Context<T>>,
pub fn serve<E: EthSpec>(
ctx: Arc<Context<E>>,
shutdown: impl Future<Output = ()> + Send + Sync + 'static,
) -> Result<(SocketAddr, impl Future<Output = ()>), Error> {
let config = &ctx.config;
@@ -653,7 +653,7 @@ pub fn serve<T: EthSpec>(
let root = warp::path::end()
.and(warp::body::json())
.and(ctx_filter.clone())
.and_then(|body: serde_json::Value, ctx: Arc<Context<T>>| async move {
.and_then(|body: serde_json::Value, ctx: Arc<Context<E>>| async move {
let id = body
.get("id")
.and_then(serde_json::Value::as_u64)
@@ -700,7 +700,7 @@ pub fn serve<T: EthSpec>(
let echo = warp::path("echo")
.and(warp::body::bytes())
.and(ctx_filter)
.and_then(|bytes: Bytes, ctx: Arc<Context<T>>| async move {
.and_then(|bytes: Bytes, ctx: Arc<Context<E>>| async move {
*ctx.last_echo_request.write() = Some(bytes.clone());
Ok::<_, warp::reject::Rejection>(
warp::http::Response::builder().status(200).body(bytes),