merge upstream, fix compile errors

This commit is contained in:
realbigsean
2023-01-11 13:52:58 -05:00
100 changed files with 1304 additions and 505 deletions

View File

@@ -5,7 +5,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]
withdrawals-processing = ["state_processing/withdrawals-processing", "eth2/withdrawals-processing"]
withdrawals-processing = ["state_processing/withdrawals-processing"]
[dependencies]
types = { path = "../../consensus/types"}

View File

@@ -27,7 +27,7 @@ impl From<jsonwebtoken::errors::Error> for Error {
/// Provides wrapper around `[u8; JWT_SECRET_LENGTH]` that implements `Zeroize`.
#[derive(Zeroize, Clone)]
#[zeroize(drop)]
pub struct JwtKey([u8; JWT_SECRET_LENGTH as usize]);
pub struct JwtKey([u8; JWT_SECRET_LENGTH]);
impl JwtKey {
/// Wrap given slice in `Self`. Returns an error if slice.len() != `JWT_SECRET_LENGTH`.

View File

@@ -889,11 +889,11 @@ impl HttpJsonRpc {
pub async fn supported_apis_v1(&self) -> Result<SupportedApis, Error> {
Ok(SupportedApis {
new_payload_v1: true,
new_payload_v2: cfg!(feature = "withdrawals-processing"),
new_payload_v2: cfg!(any(feature = "withdrawals-processing", test)),
forkchoice_updated_v1: true,
forkchoice_updated_v2: cfg!(feature = "withdrawals-processing"),
forkchoice_updated_v2: cfg!(any(feature = "withdrawals-processing", test)),
get_payload_v1: true,
get_payload_v2: cfg!(feature = "withdrawals-processing"),
get_payload_v2: cfg!(any(feature = "withdrawals-processing", test)),
exchange_transition_configuration_v1: true,
})
}

View File

@@ -348,12 +348,14 @@ impl From<Withdrawal> for JsonWithdrawal {
impl From<JsonWithdrawal> for Withdrawal {
fn from(jw: JsonWithdrawal) -> Self {
// This comparison is done to avoid a scenario where the EE gives us too large a number and we
// panic when attempting to cast to a `u64`.
let amount = std::cmp::max(jw.amount / 1000000000, Uint256::from(u64::MAX));
Self {
index: jw.index,
validator_index: jw.validator_index,
address: jw.address,
//FIXME(sean) if EE gives us too large a number this panics
amount: (jw.amount / 1000000000).as_u64(),
amount: amount.as_u64(),
}
}
}

View File

@@ -35,7 +35,7 @@ use tokio::{
time::sleep,
};
use tokio_stream::wrappers::WatchStream;
use types::{AbstractExecPayload, Blob, ExecPayload, KzgCommitment};
use types::{AbstractExecPayload, BeaconStateError, Blob, ExecPayload, KzgCommitment};
use types::{
BlindedPayload, BlockType, ChainSpec, Epoch, ExecutionBlockHash, ForkName,
ProposerPreparationData, PublicKeyBytes, Signature, SignedBeaconBlock, Slot, Uint256,
@@ -95,6 +95,13 @@ pub enum Error {
FeeRecipientUnspecified,
MissingLatestValidHash,
InvalidJWTSecret(String),
BeaconStateError(BeaconStateError),
}
impl From<BeaconStateError> for Error {
fn from(e: BeaconStateError) -> Self {
Error::BeaconStateError(e)
}
}
impl From<ApiError> for Error {
@@ -150,17 +157,17 @@ impl<T: EthSpec, Payload: AbstractExecPayload<T>> BlockProposalContents<T, Paylo
} => payload,
}
}
pub fn default_at_fork(fork_name: ForkName) -> Self {
match fork_name {
pub fn default_at_fork(fork_name: ForkName) -> Result<Self, BeaconStateError> {
Ok(match fork_name {
ForkName::Base | ForkName::Altair | ForkName::Merge | ForkName::Capella => {
BlockProposalContents::Payload(Payload::default_at_fork(fork_name))
BlockProposalContents::Payload(Payload::default_at_fork(fork_name)?)
}
ForkName::Eip4844 => BlockProposalContents::PayloadAndBlobs {
payload: Payload::default_at_fork(fork_name),
payload: Payload::default_at_fork(fork_name)?,
blobs: VariableList::default(),
kzg_commitments: VariableList::default(),
},
}
})
}
}
@@ -805,10 +812,6 @@ impl<T: EthSpec> ExecutionLayer<T> {
spec,
) {
Ok(()) => Ok(ProvenancedPayload::Builder(
//FIXME(sean) the builder API needs to be updated
// NOTE the comment above was removed in the
// rebase with unstable.. I think it goes
// here now?
BlockProposalContents::Payload(relay.data.message.header),
)),
Err(reason) if !reason.payload_invalid() => {
@@ -860,19 +863,11 @@ impl<T: EthSpec> ExecutionLayer<T> {
spec,
) {
Ok(()) => Ok(ProvenancedPayload::Builder(
//FIXME(sean) the builder API needs to be updated
// NOTE the comment above was removed in the
// rebase with unstable.. I think it goes
// here now?
BlockProposalContents::Payload(relay.data.message.header),
)),
// If the payload is valid then use it. The local EE failed
// to produce a payload so we have no alternative.
Err(e) if !e.payload_invalid() => Ok(ProvenancedPayload::Builder(
//FIXME(sean) the builder API needs to be updated
// NOTE the comment above was removed in the
// rebase with unstable.. I think it goes
// here now?
BlockProposalContents::Payload(relay.data.message.header),
)),
Err(reason) => {

View File

@@ -13,7 +13,8 @@ use std::collections::HashMap;
use tree_hash::TreeHash;
use tree_hash_derive::TreeHash;
use types::{
EthSpec, ExecutionBlockHash, ExecutionPayload, ExecutionPayloadMerge, Hash256, Uint256,
EthSpec, ExecutionBlockHash, ExecutionPayload, ExecutionPayloadCapella,
ExecutionPayloadEip4844, ExecutionPayloadMerge, ForkName, Hash256, Uint256,
};
const GAS_LIMIT: u64 = 16384;
@@ -113,6 +114,11 @@ pub struct ExecutionBlockGenerator<T: EthSpec> {
pub pending_payloads: HashMap<ExecutionBlockHash, ExecutionPayload<T>>,
pub next_payload_id: u64,
pub payload_ids: HashMap<PayloadId, ExecutionPayload<T>>,
/*
* Post-merge fork triggers
*/
pub shanghai_time: Option<u64>, // withdrawals
pub eip4844_time: Option<u64>, // 4844
}
impl<T: EthSpec> ExecutionBlockGenerator<T> {
@@ -120,6 +126,8 @@ impl<T: EthSpec> ExecutionBlockGenerator<T> {
terminal_total_difficulty: Uint256,
terminal_block_number: u64,
terminal_block_hash: ExecutionBlockHash,
shanghai_time: Option<u64>,
eip4844_time: Option<u64>,
) -> Self {
let mut gen = Self {
head_block: <_>::default(),
@@ -132,6 +140,8 @@ impl<T: EthSpec> ExecutionBlockGenerator<T> {
pending_payloads: <_>::default(),
next_payload_id: 0,
payload_ids: <_>::default(),
shanghai_time,
eip4844_time,
};
gen.insert_pow_block(0).unwrap();
@@ -163,6 +173,16 @@ impl<T: EthSpec> ExecutionBlockGenerator<T> {
}
}
pub fn get_fork_at_timestamp(&self, timestamp: u64) -> ForkName {
match self.eip4844_time {
Some(fork_time) if timestamp >= fork_time => ForkName::Eip4844,
_ => match self.shanghai_time {
Some(fork_time) if timestamp >= fork_time => ForkName::Capella,
_ => ForkName::Merge,
},
}
}
pub fn execution_block_by_number(&self, number: u64) -> Option<ExecutionBlock> {
self.block_by_number(number)
.map(|block| block.as_execution_block(self.terminal_total_difficulty))
@@ -395,7 +415,9 @@ impl<T: EthSpec> ExecutionBlockGenerator<T> {
}
}
pub fn forkchoice_updated_v1(
// This function expects payload_attributes to already be validated with respect to
// the current fork [obtained by self.get_fork_at_timestamp(payload_attributes.timestamp)]
pub fn forkchoice_updated(
&mut self,
forkchoice_state: ForkchoiceState,
payload_attributes: Option<PayloadAttributes>,
@@ -469,23 +491,65 @@ impl<T: EthSpec> ExecutionBlockGenerator<T> {
transactions: vec![].into(),
}),
PayloadAttributes::V2(pa) => {
// FIXME: think about how to test different forks
ExecutionPayload::Merge(ExecutionPayloadMerge {
parent_hash: forkchoice_state.head_block_hash,
fee_recipient: pa.suggested_fee_recipient,
receipts_root: Hash256::repeat_byte(42),
state_root: Hash256::repeat_byte(43),
logs_bloom: vec![0; 256].into(),
prev_randao: pa.prev_randao,
block_number: parent.block_number() + 1,
gas_limit: GAS_LIMIT,
gas_used: GAS_USED,
timestamp: pa.timestamp,
extra_data: "block gen was here".as_bytes().to_vec().into(),
base_fee_per_gas: Uint256::one(),
block_hash: ExecutionBlockHash::zero(),
transactions: vec![].into(),
})
match self.get_fork_at_timestamp(pa.timestamp) {
ForkName::Merge => ExecutionPayload::Merge(ExecutionPayloadMerge {
parent_hash: forkchoice_state.head_block_hash,
fee_recipient: pa.suggested_fee_recipient,
receipts_root: Hash256::repeat_byte(42),
state_root: Hash256::repeat_byte(43),
logs_bloom: vec![0; 256].into(),
prev_randao: pa.prev_randao,
block_number: parent.block_number() + 1,
gas_limit: GAS_LIMIT,
gas_used: GAS_USED,
timestamp: pa.timestamp,
extra_data: "block gen was here".as_bytes().to_vec().into(),
base_fee_per_gas: Uint256::one(),
block_hash: ExecutionBlockHash::zero(),
transactions: vec![].into(),
}),
ForkName::Capella => {
ExecutionPayload::Capella(ExecutionPayloadCapella {
parent_hash: forkchoice_state.head_block_hash,
fee_recipient: pa.suggested_fee_recipient,
receipts_root: Hash256::repeat_byte(42),
state_root: Hash256::repeat_byte(43),
logs_bloom: vec![0; 256].into(),
prev_randao: pa.prev_randao,
block_number: parent.block_number() + 1,
gas_limit: GAS_LIMIT,
gas_used: GAS_USED,
timestamp: pa.timestamp,
extra_data: "block gen was here".as_bytes().to_vec().into(),
base_fee_per_gas: Uint256::one(),
block_hash: ExecutionBlockHash::zero(),
transactions: vec![].into(),
withdrawals: pa.withdrawals.as_ref().unwrap().clone().into(),
})
}
ForkName::Eip4844 => {
ExecutionPayload::Eip4844(ExecutionPayloadEip4844 {
parent_hash: forkchoice_state.head_block_hash,
fee_recipient: pa.suggested_fee_recipient,
receipts_root: Hash256::repeat_byte(42),
state_root: Hash256::repeat_byte(43),
logs_bloom: vec![0; 256].into(),
prev_randao: pa.prev_randao,
block_number: parent.block_number() + 1,
gas_limit: GAS_LIMIT,
gas_used: GAS_USED,
timestamp: pa.timestamp,
extra_data: "block gen was here".as_bytes().to_vec().into(),
base_fee_per_gas: Uint256::one(),
// FIXME(4844): maybe this should be set to something?
excess_data_gas: Uint256::one(),
block_hash: ExecutionBlockHash::zero(),
transactions: vec![].into(),
withdrawals: pa.withdrawals.as_ref().unwrap().clone().into(),
})
}
_ => unreachable!(),
}
}
};
@@ -576,6 +640,8 @@ mod test {
TERMINAL_DIFFICULTY.into(),
TERMINAL_BLOCK,
ExecutionBlockHash::zero(),
None,
None,
);
for i in 0..=TERMINAL_BLOCK {

View File

@@ -82,17 +82,40 @@ pub async fn handle_rpc<T: EthSpec>(
ENGINE_NEW_PAYLOAD_V2 => {
JsonExecutionPayload::V2(get_param::<JsonExecutionPayloadV2<T>>(params, 0)?)
}
// TODO(4844) add that here..
_ => unreachable!(),
};
let fork = match request {
JsonExecutionPayload::V1(_) => ForkName::Merge,
JsonExecutionPayload::V2(ref payload) => {
if payload.withdrawals.is_none() {
ForkName::Merge
} else {
ForkName::Capella
let fork = ctx
.execution_block_generator
.read()
.get_fork_at_timestamp(*request.timestamp());
// validate method called correctly according to shanghai fork time
match fork {
ForkName::Merge => {
if request.withdrawals().is_ok() && request.withdrawals().unwrap().is_some() {
return Err(format!(
"{} called with `withdrawals` before capella fork!",
method
));
}
}
ForkName::Capella => {
if method == ENGINE_NEW_PAYLOAD_V1 {
return Err(format!("{} called after capella fork!", method));
}
if request.withdrawals().is_err()
|| (request.withdrawals().is_ok()
&& request.withdrawals().unwrap().is_none())
{
return Err(format!(
"{} called without `withdrawals` after capella fork!",
method
));
}
}
// TODO(4844) add 4844 error checking here
_ => unreachable!(),
};
// Canned responses set by block hash take priority.
@@ -125,7 +148,7 @@ pub async fn handle_rpc<T: EthSpec>(
Ok(serde_json::to_value(JsonPayloadStatusV1::from(response)).unwrap())
}
ENGINE_GET_PAYLOAD_V1 => {
ENGINE_GET_PAYLOAD_V1 | ENGINE_GET_PAYLOAD_V2 => {
let request: JsonPayloadIdRequest = get_param(params, 0)?;
let id = request.into();
@@ -135,12 +158,76 @@ pub async fn handle_rpc<T: EthSpec>(
.get_payload(&id)
.ok_or_else(|| format!("no payload for id {:?}", id))?;
Ok(serde_json::to_value(JsonExecutionPayloadV1::try_from(response).unwrap()).unwrap())
// validate method called correctly according to shanghai fork time
if ctx
.execution_block_generator
.read()
.get_fork_at_timestamp(response.timestamp())
== ForkName::Capella
&& method == ENGINE_GET_PAYLOAD_V1
{
return Err(format!("{} called after capella fork!", method));
}
// TODO(4844) add 4844 error checking here
match method {
ENGINE_GET_PAYLOAD_V1 => Ok(serde_json::to_value(
JsonExecutionPayloadV1::try_from(response).unwrap(),
)
.unwrap()),
ENGINE_GET_PAYLOAD_V2 => Ok(serde_json::to_value(JsonGetPayloadResponse {
execution_payload: JsonExecutionPayloadV2::try_from(response).unwrap(),
})
.unwrap()),
_ => unreachable!(),
}
}
// FIXME(capella): handle fcu version 2
ENGINE_FORKCHOICE_UPDATED_V1 => {
ENGINE_FORKCHOICE_UPDATED_V1 | ENGINE_FORKCHOICE_UPDATED_V2 => {
let forkchoice_state: JsonForkchoiceStateV1 = get_param(params, 0)?;
let payload_attributes: Option<JsonPayloadAttributes> = get_param(params, 1)?;
let payload_attributes = match method {
ENGINE_FORKCHOICE_UPDATED_V1 => {
let jpa1: Option<JsonPayloadAttributesV1> = get_param(params, 1)?;
jpa1.map(JsonPayloadAttributes::V1)
}
ENGINE_FORKCHOICE_UPDATED_V2 => {
let jpa2: Option<JsonPayloadAttributesV2> = get_param(params, 1)?;
jpa2.map(JsonPayloadAttributes::V2)
}
_ => unreachable!(),
};
// validate method called correctly according to shanghai fork time
if let Some(pa) = payload_attributes.as_ref() {
match ctx
.execution_block_generator
.read()
.get_fork_at_timestamp(*pa.timestamp())
{
ForkName::Merge => {
if pa.withdrawals().is_ok() && pa.withdrawals().unwrap().is_some() {
return Err(format!(
"{} called with `withdrawals` before capella fork!",
method
));
}
}
ForkName::Capella => {
if method == ENGINE_FORKCHOICE_UPDATED_V1 {
return Err(format!("{} called after capella fork!", method));
}
if pa.withdrawals().is_err()
|| (pa.withdrawals().is_ok() && pa.withdrawals().unwrap().is_none())
{
return Err(format!(
"{} called without `withdrawals` after capella fork!",
method
));
}
}
// TODO(4844) add 4844 error checking here
_ => unreachable!(),
};
}
if let Some(hook_response) = ctx
.hook
@@ -161,13 +248,10 @@ pub async fn handle_rpc<T: EthSpec>(
return Ok(serde_json::to_value(response).unwrap());
}
let mut response = ctx
.execution_block_generator
.write()
.forkchoice_updated_v1(
forkchoice_state.into(),
payload_attributes.map(|json| json.into()),
)?;
let mut response = ctx.execution_block_generator.write().forkchoice_updated(
forkchoice_state.into(),
payload_attributes.map(|json| json.into()),
)?;
if let Some(mut status) = ctx.static_forkchoice_updated_response.lock().clone() {
if status.status == PayloadStatusV1Status::Valid {

View File

@@ -26,17 +26,22 @@ impl<T: EthSpec> MockExecutionLayer<T> {
DEFAULT_TERMINAL_BLOCK,
ExecutionBlockHash::zero(),
Epoch::new(0),
None,
None,
Some(JwtKey::from_slice(&DEFAULT_JWT_SECRET).unwrap()),
None,
)
}
#[allow(clippy::too_many_arguments)]
pub fn new(
executor: TaskExecutor,
terminal_total_difficulty: Uint256,
terminal_block: u64,
terminal_block_hash: ExecutionBlockHash,
terminal_block_hash_activation_epoch: Epoch,
shanghai_time: Option<u64>,
eip4844_time: Option<u64>,
jwt_key: Option<JwtKey>,
builder_url: Option<SensitiveUrl>,
) -> Self {
@@ -54,6 +59,8 @@ impl<T: EthSpec> MockExecutionLayer<T> {
terminal_total_difficulty,
terminal_block,
terminal_block_hash,
shanghai_time,
eip4844_time,
);
let url = SensitiveUrl::parse(&server.url()).unwrap();

View File

@@ -45,6 +45,8 @@ pub struct MockExecutionConfig {
pub terminal_difficulty: Uint256,
pub terminal_block: u64,
pub terminal_block_hash: ExecutionBlockHash,
pub shanghai_time: Option<u64>,
pub eip4844_time: Option<u64>,
}
impl Default for MockExecutionConfig {
@@ -55,6 +57,8 @@ impl Default for MockExecutionConfig {
terminal_block: DEFAULT_TERMINAL_BLOCK,
terminal_block_hash: ExecutionBlockHash::zero(),
server_config: Config::default(),
shanghai_time: None,
eip4844_time: None,
}
}
}
@@ -74,6 +78,8 @@ impl<T: EthSpec> MockServer<T> {
DEFAULT_TERMINAL_DIFFICULTY.into(),
DEFAULT_TERMINAL_BLOCK,
ExecutionBlockHash::zero(),
None, // FIXME(capella): should this be the default?
None, // FIXME(eip4844): should this be the default?
)
}
@@ -84,11 +90,18 @@ impl<T: EthSpec> MockServer<T> {
terminal_block,
terminal_block_hash,
server_config,
shanghai_time,
eip4844_time,
} = config;
let last_echo_request = Arc::new(RwLock::new(None));
let preloaded_responses = Arc::new(Mutex::new(vec![]));
let execution_block_generator =
ExecutionBlockGenerator::new(terminal_difficulty, terminal_block, terminal_block_hash);
let execution_block_generator = ExecutionBlockGenerator::new(
terminal_difficulty,
terminal_block,
terminal_block_hash,
shanghai_time,
eip4844_time,
);
let ctx: Arc<Context<T>> = Arc::new(Context {
config: server_config,
@@ -140,6 +153,8 @@ impl<T: EthSpec> MockServer<T> {
terminal_difficulty: Uint256,
terminal_block: u64,
terminal_block_hash: ExecutionBlockHash,
shanghai_time: Option<u64>,
eip4844_time: Option<u64>,
) -> Self {
Self::new_with_config(
handle,
@@ -149,6 +164,8 @@ impl<T: EthSpec> MockServer<T> {
terminal_difficulty,
terminal_block,
terminal_block_hash,
shanghai_time,
eip4844_time,
},
)
}