Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 47 additions & 28 deletions beacon_node/beacon_chain/src/beacon_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ use slasher::Slasher;
use slog::{crit, debug, error, info, trace, warn, Logger};
use slot_clock::SlotClock;
use ssz::Encode;
#[cfg(feature = "withdrawals")]
use state_processing::per_block_processing::get_expected_withdrawals;
use state_processing::{
common::{get_attesting_indices_from_state, get_indexed_attestation},
per_block_processing,
Expand Down Expand Up @@ -4105,35 +4107,52 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
return Ok(());
}

let payload_attributes = match self.spec.fork_name_at_epoch(prepare_epoch) {
#[cfg(feature = "withdrawals")]
let head_state = &self.canonical_head.cached_head().snapshot.beacon_state;
#[cfg(feature = "withdrawals")]
let withdrawals = match self.spec.fork_name_at_epoch(prepare_epoch) {
ForkName::Base | ForkName::Altair | ForkName::Merge => {
PayloadAttributes::V1(PayloadAttributesV1 {
timestamp: self
.slot_clock
.start_of(prepare_slot)
.ok_or(Error::InvalidSlot(prepare_slot))?
.as_secs(),
prev_randao: head_random,
suggested_fee_recipient: execution_layer
.get_suggested_fee_recipient(proposer as u64)
.await,
})
}
ForkName::Capella | ForkName::Eip4844 => PayloadAttributes::V2(PayloadAttributesV2 {
timestamp: self
.slot_clock
.start_of(prepare_slot)
.ok_or(Error::InvalidSlot(prepare_slot))?
.as_secs(),
prev_randao: head_random,
suggested_fee_recipient: execution_layer
.get_suggested_fee_recipient(proposer as u64)
.await,
//FIXME(sean)
#[cfg(feature = "withdrawals")]
withdrawals: vec![],
}),
};
None
},
ForkName::Capella | ForkName::Eip4844 => match &head_state {
&BeaconState::Capella(_) | &BeaconState::Eip4844(_) => {
// The head_state is already BeaconState::Capella or later
// FIXME(mark)
// Might implement caching here in the future..
Some(get_expected_withdrawals(head_state, &self.spec))
}
&BeaconState::Base(_) | &BeaconState::Altair(_) | &BeaconState::Merge(_) => {
// We are the Capella transition block proposer, need advanced state
let mut prepare_state = self
.state_at_slot(prepare_slot, StateSkipConfig::WithoutStateRoots)
.or_else(|e| {
error!(self.log, "Capella Transition Proposer"; "Error Advancing State: " => ?e);
Err(e)
})?;
// FIXME(mark)
// Might implement caching here in the future..
Some(get_expected_withdrawals(&prepare_state, &self.spec))
}
},
}.transpose().or_else(|e| {
error!(self.log, "Error preparing beacon proposer"; "while calculating expected withdrawals" => ?e);
Err(e)
}).map(|withdrawals_opt| withdrawals_opt.map(|w| w.into()))
.map_err(Error::PrepareProposerFailed)?;

let payload_attributes = PayloadAttributes::V2(PayloadAttributesV2 {
timestamp: self
.slot_clock
.start_of(prepare_slot)
.ok_or(Error::InvalidSlot(prepare_slot))?
.as_secs(),
prev_randao: head_random,
suggested_fee_recipient: execution_layer
.get_suggested_fee_recipient(proposer as u64)
.await,
#[cfg(feature = "withdrawals")]
withdrawals,
});

debug!(
self.log,
Expand Down
2 changes: 1 addition & 1 deletion beacon_node/beacon_chain/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ pub enum BeaconChainError {
},
AddPayloadLogicError,
ExecutionForkChoiceUpdateFailed(execution_layer::Error),
PrepareProposerBlockingFailed(execution_layer::Error),
PrepareProposerFailed(BlockProcessingError),
ExecutionForkChoiceUpdateInvalid {
status: PayloadStatus,
},
Expand Down
17 changes: 17 additions & 0 deletions beacon_node/beacon_chain/src/execution_payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use fork_choice::{InvalidationOperation, PayloadVerificationStatus};
use proto_array::{Block as ProtoBlock, ExecutionStatus};
use slog::debug;
use slot_clock::SlotClock;
#[cfg(feature = "withdrawals")]
use state_processing::per_block_processing::get_expected_withdrawals;
use state_processing::per_block_processing::{
compute_timestamp_at_slot, is_execution_enabled, is_merge_transition_complete,
partially_verify_execution_payload,
Expand Down Expand Up @@ -362,6 +364,15 @@ pub fn get_execution_payload<
let random = *state.get_randao_mix(current_epoch)?;
let latest_execution_payload_header_block_hash =
state.latest_execution_payload_header()?.block_hash();
#[cfg(feature = "withdrawals")]
let withdrawals = match state {
&BeaconState::Capella(_) | &BeaconState::Eip4844(_) => {
Some(get_expected_withdrawals(state, spec)?.into())
}
&BeaconState::Merge(_) => None,
// These shouldn't happen but they're here to make the pattern irrefutable
&BeaconState::Base(_) | &BeaconState::Altair(_) => None,
};

// Spawn a task to obtain the execution payload from the EL via a series of async calls. The
// `join_handle` can be used to await the result of the function.
Expand All @@ -378,6 +389,8 @@ pub fn get_execution_payload<
proposer_index,
latest_execution_payload_header_block_hash,
builder_params,
#[cfg(feature = "withdrawals")]
withdrawals,
)
.await
},
Expand Down Expand Up @@ -411,6 +424,7 @@ pub async fn prepare_execution_payload<T, Payload>(
proposer_index: u64,
latest_execution_payload_header_block_hash: ExecutionBlockHash,
builder_params: BuilderParams,
#[cfg(feature = "withdrawals")] withdrawals: Option<Vec<Withdrawal>>,
) -> Result<BlockProposalContents<T::EthSpec, Payload>, BlockProductionError>
where
T: BeaconChainTypes,
Expand Down Expand Up @@ -480,6 +494,9 @@ where
proposer_index,
forkchoice_update_params,
builder_params,
fork,
#[cfg(feature = "withdrawals")]
withdrawals,
&chain.spec,
)
.await
Expand Down
8 changes: 4 additions & 4 deletions beacon_node/beacon_chain/tests/payload_invalidation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ use beacon_chain::{
INVALID_JUSTIFIED_PAYLOAD_SHUTDOWN_REASON,
};
use execution_layer::{
json_structures::{JsonForkChoiceStateV1, JsonPayloadAttributesV1},
json_structures::{JsonForkchoiceStateV1, JsonPayloadAttributesV1},
test_utils::ExecutionBlockGenerator,
ExecutionLayer, ForkChoiceState, PayloadAttributes,
ExecutionLayer, ForkchoiceState, PayloadAttributes,
};
use fork_choice::{
CountUnrealized, Error as ForkChoiceError, InvalidationOperation, PayloadVerificationStatus,
Expand Down Expand Up @@ -117,7 +117,7 @@ impl InvalidPayloadRig {
&self.harness.chain.canonical_head
}

fn previous_forkchoice_update_params(&self) -> (ForkChoiceState, PayloadAttributes) {
fn previous_forkchoice_update_params(&self) -> (ForkchoiceState, PayloadAttributes) {
let mock_execution_layer = self.harness.mock_execution_layer.as_ref().unwrap();
let json = mock_execution_layer
.server
Expand All @@ -126,7 +126,7 @@ impl InvalidPayloadRig {
let params = json.get("params").expect("no params");

let fork_choice_state_json = params.get(0).expect("no payload param");
let fork_choice_state: JsonForkChoiceStateV1 =
let fork_choice_state: JsonForkchoiceStateV1 =
serde_json::from_value(fork_choice_state_json.clone()).unwrap();

let payload_param_json = params.get(1).expect("no payload param");
Expand Down
4 changes: 2 additions & 2 deletions beacon_node/execution_layer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]
withdrawals = ["state_processing/withdrawals", "types/withdrawals"]
withdrawals-processing = ["state_processing/withdrawals-processing"]
withdrawals = ["state_processing/withdrawals", "types/withdrawals", "eth2/withdrawals"]
withdrawals-processing = ["state_processing/withdrawals-processing", "eth2/withdrawals-processing"]

[dependencies]
types = { path = "../../consensus/types"}
Expand Down
46 changes: 43 additions & 3 deletions beacon_node/execution_layer/src/engine_api.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::engines::ForkChoiceState;
use crate::engines::ForkchoiceState;
pub use ethers_core::types::Transaction;
use ethers_core::utils::rlp::{Decodable, Rlp};
use http::deposit_methods::RpcError;
Expand All @@ -7,10 +7,11 @@ use reqwest::StatusCode;
use serde::{Deserialize, Serialize};
use strum::IntoStaticStr;
use superstruct::superstruct;
#[cfg(feature = "withdrawals")]
use types::Withdrawal;
pub use types::{
Address, EthSpec, ExecutionBlockHash, ExecutionPayload, ExecutionPayloadHeader, FixedVector,
Hash256, Uint256, VariableList,
ForkName, Hash256, Uint256, VariableList,
};

pub mod auth;
Expand Down Expand Up @@ -44,6 +45,9 @@ pub enum Error {
DeserializeWithdrawals(ssz_types::Error),
BuilderApi(builder_client::Error),
IncorrectStateVariant,
RequiredMethodUnsupported(&'static str),
UnsupportedForkVariant(String),
BadConversion(String),
}

impl From<reqwest::Error> for Error {
Expand Down Expand Up @@ -255,7 +259,29 @@ pub struct PayloadAttributes {
pub suggested_fee_recipient: Address,
#[cfg(feature = "withdrawals")]
#[superstruct(only(V2))]
pub withdrawals: Vec<Withdrawal>,
pub withdrawals: Option<Vec<Withdrawal>>,
}

impl PayloadAttributes {
pub fn downgrade_to_v1(self) -> Result<Self, Error> {
match self {
PayloadAttributes::V1(_) => Ok(self),
PayloadAttributes::V2(v2) => {
#[cfg(features = "withdrawals")]
if v2.withdrawals.is_some() {
return Err(Error::BadConversion(
"Downgrading from PayloadAttributesV2 with non-null withdrawaals"
.to_string(),
));
}
Ok(PayloadAttributes::V1(PayloadAttributesV1 {
timestamp: v2.timestamp,
prev_randao: v2.prev_randao,
suggested_fee_recipient: v2.suggested_fee_recipient,
}))
}
}
}
}

#[derive(Clone, Debug, PartialEq)]
Expand All @@ -277,3 +303,17 @@ pub struct ProposeBlindedBlockResponse {
pub latest_valid_hash: Option<Hash256>,
pub validation_error: Option<String>,
}

// This name is work in progress, it could
// change when this method is actually proposed
// but I'm writing this as it has been described
#[derive(Clone, Copy)]
pub struct SupportedApis {
pub new_payload_v1: bool,
pub new_payload_v2: bool,
pub forkchoice_updated_v1: bool,
pub forkchoice_updated_v2: bool,
pub get_payload_v1: bool,
pub get_payload_v2: bool,
pub exchange_transition_configuration_v1: bool,
}
Loading