Merge remote-tracking branch 'origin/unstable' into tree-states

This commit is contained in:
Michael Sproul
2022-03-01 16:03:41 +11:00
209 changed files with 6107 additions and 1362 deletions

View File

@@ -1,7 +1,7 @@
[package]
name = "execution_layer"
version = "0.1.0"
edition = "2018"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View File

@@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize};
pub const LATEST_TAG: &str = "latest";
use crate::engines::ForkChoiceState;
pub use types::{Address, EthSpec, ExecutionPayload, Hash256, Uint256};
pub use types::{Address, EthSpec, ExecutionBlockHash, ExecutionPayload, Hash256, Uint256};
pub mod http;
pub mod json_structures;
@@ -17,14 +17,15 @@ pub enum Error {
Reqwest(reqwest::Error),
BadResponse(String),
RequestFailed(String),
InvalidExecutePayloadResponse(&'static str),
JsonRpc(RpcError),
Json(serde_json::Error),
ServerMessage { code: i64, message: String },
Eip155Failure,
IsSyncing,
ExecutionBlockNotFound(Hash256),
ExecutionBlockNotFound(ExecutionBlockHash),
ExecutionHeadBlockNotFound,
ParentHashEqualsBlockHash(Hash256),
ParentHashEqualsBlockHash(ExecutionBlockHash),
PayloadIdUnavailable,
}
@@ -52,13 +53,13 @@ pub trait EngineApi {
async fn get_block_by_hash<'a>(
&self,
block_hash: Hash256,
block_hash: ExecutionBlockHash,
) -> Result<Option<ExecutionBlock>, Error>;
async fn execute_payload_v1<T: EthSpec>(
async fn new_payload_v1<T: EthSpec>(
&self,
execution_payload: ExecutionPayload<T>,
) -> Result<ExecutePayloadResponse, Error>;
) -> Result<PayloadStatusV1, Error>;
async fn get_payload_v1<T: EthSpec>(
&self,
@@ -73,16 +74,19 @@ pub trait EngineApi {
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum ExecutePayloadResponseStatus {
pub enum PayloadStatusV1Status {
Valid,
Invalid,
Syncing,
Accepted,
InvalidBlockHash,
InvalidTerminalBlock,
}
#[derive(Clone, Debug, PartialEq)]
pub struct ExecutePayloadResponse {
pub status: ExecutePayloadResponseStatus,
pub latest_valid_hash: Option<Hash256>,
pub struct PayloadStatusV1 {
pub status: PayloadStatusV1Status,
pub latest_valid_hash: Option<ExecutionBlockHash>,
pub validation_error: Option<String>,
}
@@ -96,10 +100,10 @@ pub enum BlockByNumberQuery<'a> {
#[serde(rename_all = "camelCase")]
pub struct ExecutionBlock {
#[serde(rename = "hash")]
pub block_hash: Hash256,
pub block_hash: ExecutionBlockHash,
#[serde(rename = "number", with = "eth2_serde_utils::u64_hex_be")]
pub block_number: u64,
pub parent_hash: Hash256,
pub parent_hash: ExecutionBlockHash,
pub total_difficulty: Uint256,
}
@@ -110,13 +114,8 @@ pub struct PayloadAttributes {
pub suggested_fee_recipient: Address,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum ForkchoiceUpdatedResponseStatus {
Success,
Syncing,
}
#[derive(Clone, Debug, PartialEq)]
pub struct ForkchoiceUpdatedResponse {
pub status: ForkchoiceUpdatedResponseStatus,
pub payload_status: PayloadStatusV1,
pub payload_id: Option<PayloadId>,
}

View File

@@ -27,8 +27,8 @@ pub const ETH_GET_BLOCK_BY_HASH_TIMEOUT: Duration = Duration::from_secs(1);
pub const ETH_SYNCING: &str = "eth_syncing";
pub const ETH_SYNCING_TIMEOUT: Duration = Duration::from_millis(250);
pub const ENGINE_EXECUTE_PAYLOAD_V1: &str = "engine_executePayloadV1";
pub const ENGINE_EXECUTE_PAYLOAD_TIMEOUT: Duration = Duration::from_secs(2);
pub const ENGINE_NEW_PAYLOAD_V1: &str = "engine_newPayloadV1";
pub const ENGINE_NEW_PAYLOAD_TIMEOUT: Duration = Duration::from_secs(2);
pub const ENGINE_GET_PAYLOAD_V1: &str = "engine_getPayloadV1";
pub const ENGINE_GET_PAYLOAD_TIMEOUT: Duration = Duration::from_secs(2);
@@ -125,7 +125,7 @@ impl EngineApi for HttpJsonRpc {
async fn get_block_by_hash<'a>(
&self,
block_hash: Hash256,
block_hash: ExecutionBlockHash,
) -> Result<Option<ExecutionBlock>, Error> {
let params = json!([block_hash, RETURN_FULL_TRANSACTION_OBJECTS]);
@@ -133,18 +133,14 @@ impl EngineApi for HttpJsonRpc {
.await
}
async fn execute_payload_v1<T: EthSpec>(
async fn new_payload_v1<T: EthSpec>(
&self,
execution_payload: ExecutionPayload<T>,
) -> Result<ExecutePayloadResponse, Error> {
) -> Result<PayloadStatusV1, Error> {
let params = json!([JsonExecutionPayloadV1::from(execution_payload)]);
let response: JsonExecutePayloadV1Response = self
.rpc_request(
ENGINE_EXECUTE_PAYLOAD_V1,
params,
ENGINE_EXECUTE_PAYLOAD_TIMEOUT,
)
let response: JsonPayloadStatusV1 = self
.rpc_request(ENGINE_NEW_PAYLOAD_V1, params, ENGINE_NEW_PAYLOAD_TIMEOUT)
.await?;
Ok(response.into())
@@ -417,7 +413,9 @@ mod test {
Tester::new()
.assert_request_equals(
|client| async move {
let _ = client.get_block_by_hash(Hash256::repeat_byte(1)).await;
let _ = client
.get_block_by_hash(ExecutionBlockHash::repeat_byte(1))
.await;
},
json!({
"id": STATIC_ID,
@@ -437,9 +435,9 @@ mod test {
let _ = client
.forkchoice_updated_v1(
ForkChoiceState {
head_block_hash: Hash256::repeat_byte(1),
safe_block_hash: Hash256::repeat_byte(1),
finalized_block_hash: Hash256::zero(),
head_block_hash: ExecutionBlockHash::repeat_byte(1),
safe_block_hash: ExecutionBlockHash::repeat_byte(1),
finalized_block_hash: ExecutionBlockHash::zero(),
},
Some(PayloadAttributes {
timestamp: 5,
@@ -486,16 +484,16 @@ mod test {
}
#[tokio::test]
async fn execute_payload_v1_request() {
async fn new_payload_v1_request() {
Tester::new()
.assert_request_equals(
|client| async move {
let _ = client
.execute_payload_v1::<MainnetEthSpec>(ExecutionPayload {
parent_hash: Hash256::repeat_byte(0),
.new_payload_v1::<MainnetEthSpec>(ExecutionPayload {
parent_hash: ExecutionBlockHash::repeat_byte(0),
fee_recipient: Address::repeat_byte(1),
state_root: Hash256::repeat_byte(1),
receipt_root: Hash256::repeat_byte(0),
receipts_root: Hash256::repeat_byte(0),
logs_bloom: vec![1; 256].into(),
random: Hash256::repeat_byte(1),
block_number: 0,
@@ -504,7 +502,7 @@ mod test {
timestamp: 42,
extra_data: vec![].into(),
base_fee_per_gas: Uint256::from(1),
block_hash: Hash256::repeat_byte(1),
block_hash: ExecutionBlockHash::repeat_byte(1),
transactions: vec![].into(),
})
.await;
@@ -512,7 +510,7 @@ mod test {
json!({
"id": STATIC_ID,
"jsonrpc": JSONRPC_VERSION,
"method": ENGINE_EXECUTE_PAYLOAD_V1,
"method": ENGINE_NEW_PAYLOAD_V1,
"params": [{
"parentHash": HASH_00,
"feeRecipient": ADDRESS_01,
@@ -542,9 +540,9 @@ mod test {
let _ = client
.forkchoice_updated_v1(
ForkChoiceState {
head_block_hash: Hash256::repeat_byte(0),
safe_block_hash: Hash256::repeat_byte(0),
finalized_block_hash: Hash256::repeat_byte(1),
head_block_hash: ExecutionBlockHash::repeat_byte(0),
safe_block_hash: ExecutionBlockHash::repeat_byte(0),
finalized_block_hash: ExecutionBlockHash::repeat_byte(1),
},
None,
)
@@ -592,9 +590,9 @@ mod test {
let _ = client
.forkchoice_updated_v1(
ForkChoiceState {
head_block_hash: Hash256::from_str("0x3b8fb240d288781d4aac94d3fd16809ee413bc99294a085798a589dae51ddd4a").unwrap(),
safe_block_hash: Hash256::from_str("0x3b8fb240d288781d4aac94d3fd16809ee413bc99294a085798a589dae51ddd4a").unwrap(),
finalized_block_hash: Hash256::zero(),
head_block_hash: ExecutionBlockHash::from_str("0x3b8fb240d288781d4aac94d3fd16809ee413bc99294a085798a589dae51ddd4a").unwrap(),
safe_block_hash: ExecutionBlockHash::from_str("0x3b8fb240d288781d4aac94d3fd16809ee413bc99294a085798a589dae51ddd4a").unwrap(),
finalized_block_hash: ExecutionBlockHash::zero(),
},
Some(PayloadAttributes {
timestamp: 5,
@@ -627,7 +625,11 @@ mod test {
"id": STATIC_ID,
"jsonrpc": JSONRPC_VERSION,
"result": {
"status": "SUCCESS",
"payloadStatus": {
"status": "VALID",
"latestValidHash": HASH_00,
"validationError": ""
},
"payloadId": "0xa247243752eb10b4"
}
})],
@@ -635,9 +637,9 @@ mod test {
let response = client
.forkchoice_updated_v1(
ForkChoiceState {
head_block_hash: Hash256::from_str("0x3b8fb240d288781d4aac94d3fd16809ee413bc99294a085798a589dae51ddd4a").unwrap(),
safe_block_hash: Hash256::from_str("0x3b8fb240d288781d4aac94d3fd16809ee413bc99294a085798a589dae51ddd4a").unwrap(),
finalized_block_hash: Hash256::zero(),
head_block_hash: ExecutionBlockHash::from_str("0x3b8fb240d288781d4aac94d3fd16809ee413bc99294a085798a589dae51ddd4a").unwrap(),
safe_block_hash: ExecutionBlockHash::from_str("0x3b8fb240d288781d4aac94d3fd16809ee413bc99294a085798a589dae51ddd4a").unwrap(),
finalized_block_hash: ExecutionBlockHash::zero(),
},
Some(PayloadAttributes {
timestamp: 5,
@@ -648,7 +650,11 @@ mod test {
.await
.unwrap();
assert_eq!(response, ForkchoiceUpdatedResponse {
status: ForkchoiceUpdatedResponseStatus::Success,
payload_status: PayloadStatusV1 {
status: PayloadStatusV1Status::Valid,
latest_valid_hash: Some(ExecutionBlockHash::zero()),
validation_error: Some(String::new()),
},
payload_id:
Some(str_to_payload_id("0xa247243752eb10b4")),
});
@@ -683,12 +689,12 @@ mod test {
"logsBloom": LOGS_BLOOM_00,
"random": HASH_00,
"blockNumber":"0x1",
"gasLimit":"0x1c9c380",
"gasLimit":"0x1c95111",
"gasUsed":"0x0",
"timestamp":"0x5",
"extraData":"0x",
"baseFeePerGas":"0x7",
"blockHash":"0x3559e851470f6e7bbed1db474980683e8c315bfce99b2a6ef47c057c04de7858",
"blockHash":"0x6359b8381a370e2f54072a5784ddd78b6ed024991558c511d4452eb4f6ac898c",
"transactions":[]
}
})],
@@ -699,19 +705,19 @@ mod test {
.unwrap();
let expected = ExecutionPayload {
parent_hash: Hash256::from_str("0x3b8fb240d288781d4aac94d3fd16809ee413bc99294a085798a589dae51ddd4a").unwrap(),
parent_hash: ExecutionBlockHash::from_str("0x3b8fb240d288781d4aac94d3fd16809ee413bc99294a085798a589dae51ddd4a").unwrap(),
fee_recipient: Address::from_str("0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b").unwrap(),
state_root: Hash256::from_str("0xca3149fa9e37db08d1cd49c9061db1002ef1cd58db2210f2115c8c989b2bdf45").unwrap(),
receipt_root: Hash256::from_str("0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421").unwrap(),
receipts_root: Hash256::from_str("0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421").unwrap(),
logs_bloom: vec![0; 256].into(),
random: Hash256::zero(),
block_number: 1,
gas_limit: u64::from_str_radix("1c9c380",16).unwrap(),
gas_limit: u64::from_str_radix("1c95111",16).unwrap(),
gas_used: 0,
timestamp: 5,
extra_data: vec![].into(),
base_fee_per_gas: Uint256::from(7),
block_hash: Hash256::from_str("0x3559e851470f6e7bbed1db474980683e8c315bfce99b2a6ef47c057c04de7858").unwrap(),
block_hash: ExecutionBlockHash::from_str("0x6359b8381a370e2f54072a5784ddd78b6ed024991558c511d4452eb4f6ac898c").unwrap(),
transactions: vec![].into(),
};
@@ -720,14 +726,14 @@ mod test {
)
.await
.assert_request_equals(
// engine_executePayloadV1 REQUEST validation
// engine_newPayloadV1 REQUEST validation
|client| async move {
let _ = client
.execute_payload_v1::<MainnetEthSpec>(ExecutionPayload {
parent_hash: Hash256::from_str("0x3b8fb240d288781d4aac94d3fd16809ee413bc99294a085798a589dae51ddd4a").unwrap(),
.new_payload_v1::<MainnetEthSpec>(ExecutionPayload {
parent_hash: ExecutionBlockHash::from_str("0x3b8fb240d288781d4aac94d3fd16809ee413bc99294a085798a589dae51ddd4a").unwrap(),
fee_recipient: Address::from_str("0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b").unwrap(),
state_root: Hash256::from_str("0xca3149fa9e37db08d1cd49c9061db1002ef1cd58db2210f2115c8c989b2bdf45").unwrap(),
receipt_root: Hash256::from_str("0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421").unwrap(),
receipts_root: Hash256::from_str("0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421").unwrap(),
logs_bloom: vec![0; 256].into(),
random: Hash256::zero(),
block_number: 1,
@@ -736,7 +742,7 @@ mod test {
timestamp: 5,
extra_data: vec![].into(),
base_fee_per_gas: Uint256::from(7),
block_hash: Hash256::from_str("0x3559e851470f6e7bbed1db474980683e8c315bfce99b2a6ef47c057c04de7858").unwrap(),
block_hash: ExecutionBlockHash::from_str("0x3559e851470f6e7bbed1db474980683e8c315bfce99b2a6ef47c057c04de7858").unwrap(),
transactions: vec![].into(),
})
.await;
@@ -744,7 +750,7 @@ mod test {
json!({
"id": STATIC_ID,
"jsonrpc": JSONRPC_VERSION,
"method": ENGINE_EXECUTE_PAYLOAD_V1,
"method": ENGINE_NEW_PAYLOAD_V1,
"params": [{
"parentHash":"0x3b8fb240d288781d4aac94d3fd16809ee413bc99294a085798a589dae51ddd4a",
"feeRecipient":"0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b",
@@ -765,26 +771,27 @@ mod test {
)
.await
.with_preloaded_responses(
// engine_executePayloadV1 RESPONSE validation
// engine_newPayloadV1 RESPONSE validation
vec![json!({
"jsonrpc": JSONRPC_VERSION,
"id": STATIC_ID,
"result":{
"status":"VALID",
"latestValidHash":"0x3559e851470f6e7bbed1db474980683e8c315bfce99b2a6ef47c057c04de7858"
"latestValidHash":"0x3559e851470f6e7bbed1db474980683e8c315bfce99b2a6ef47c057c04de7858",
"validationError":"",
}
})],
|client| async move {
let response = client
.execute_payload_v1::<MainnetEthSpec>(ExecutionPayload::default())
.new_payload_v1::<MainnetEthSpec>(ExecutionPayload::default())
.await
.unwrap();
assert_eq!(response,
ExecutePayloadResponse {
status: ExecutePayloadResponseStatus::Valid,
latest_valid_hash: Some(Hash256::from_str("0x3559e851470f6e7bbed1db474980683e8c315bfce99b2a6ef47c057c04de7858").unwrap()),
validation_error: None
PayloadStatusV1 {
status: PayloadStatusV1Status::Valid,
latest_valid_hash: Some(ExecutionBlockHash::from_str("0x3559e851470f6e7bbed1db474980683e8c315bfce99b2a6ef47c057c04de7858").unwrap()),
validation_error: Some(String::new()),
}
);
},
@@ -796,9 +803,9 @@ mod test {
let _ = client
.forkchoice_updated_v1(
ForkChoiceState {
head_block_hash: Hash256::from_str("0x3559e851470f6e7bbed1db474980683e8c315bfce99b2a6ef47c057c04de7858").unwrap(),
safe_block_hash: Hash256::from_str("0x3559e851470f6e7bbed1db474980683e8c315bfce99b2a6ef47c057c04de7858").unwrap(),
finalized_block_hash: Hash256::from_str("0x3b8fb240d288781d4aac94d3fd16809ee413bc99294a085798a589dae51ddd4a").unwrap(),
head_block_hash: ExecutionBlockHash::from_str("0x3559e851470f6e7bbed1db474980683e8c315bfce99b2a6ef47c057c04de7858").unwrap(),
safe_block_hash: ExecutionBlockHash::from_str("0x3559e851470f6e7bbed1db474980683e8c315bfce99b2a6ef47c057c04de7858").unwrap(),
finalized_block_hash: ExecutionBlockHash::from_str("0x3b8fb240d288781d4aac94d3fd16809ee413bc99294a085798a589dae51ddd4a").unwrap(),
},
None,
)
@@ -819,14 +826,15 @@ mod test {
.await
.with_preloaded_responses(
// engine_forkchoiceUpdatedV1 RESPONSE validation
//
// Note: this test was modified to provide `null` rather than `0x`. The geth vectors
// are invalid.
vec![json!({
"jsonrpc": JSONRPC_VERSION,
"id": STATIC_ID,
"result": {
"status":"SUCCESS",
"payloadStatus": {
"status": "VALID",
"latestValidHash": HASH_00,
"validationError": ""
},
"payloadId": JSON_NULL,
}
})],
@@ -834,16 +842,20 @@ mod test {
let response = client
.forkchoice_updated_v1(
ForkChoiceState {
head_block_hash: Hash256::from_str("0x3559e851470f6e7bbed1db474980683e8c315bfce99b2a6ef47c057c04de7858").unwrap(),
safe_block_hash: Hash256::from_str("0x3559e851470f6e7bbed1db474980683e8c315bfce99b2a6ef47c057c04de7858").unwrap(),
finalized_block_hash: Hash256::from_str("0x3b8fb240d288781d4aac94d3fd16809ee413bc99294a085798a589dae51ddd4a").unwrap(),
head_block_hash: ExecutionBlockHash::from_str("0x3559e851470f6e7bbed1db474980683e8c315bfce99b2a6ef47c057c04de7858").unwrap(),
safe_block_hash: ExecutionBlockHash::from_str("0x3559e851470f6e7bbed1db474980683e8c315bfce99b2a6ef47c057c04de7858").unwrap(),
finalized_block_hash: ExecutionBlockHash::from_str("0x3b8fb240d288781d4aac94d3fd16809ee413bc99294a085798a589dae51ddd4a").unwrap(),
},
None,
)
.await
.unwrap();
assert_eq!(response, ForkchoiceUpdatedResponse {
status: ForkchoiceUpdatedResponseStatus::Success,
payload_status: PayloadStatusV1 {
status: PayloadStatusV1Status::Valid,
latest_valid_hash: Some(ExecutionBlockHash::zero()),
validation_error: Some(String::new()),
},
payload_id: None,
});
},

View File

@@ -1,7 +1,7 @@
use super::*;
use serde::{Deserialize, Serialize};
use ssz_types::FixedVector;
use types::{EthSpec, Transaction, Unsigned, VariableList};
use types::{EthSpec, ExecutionBlockHash, Transaction, Unsigned, VariableList};
#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
@@ -59,7 +59,7 @@ pub struct JsonPayloadIdResponse {
#[derive(Debug, PartialEq, Default, Serialize, Deserialize)]
#[serde(bound = "T: EthSpec", rename_all = "camelCase")]
pub struct JsonExecutionPayloadV1<T: EthSpec> {
pub parent_hash: Hash256,
pub parent_hash: ExecutionBlockHash,
pub fee_recipient: Address,
pub state_root: Hash256,
pub receipts_root: Hash256,
@@ -77,7 +77,7 @@ pub struct JsonExecutionPayloadV1<T: EthSpec> {
#[serde(with = "ssz_types::serde_utils::hex_var_list")]
pub extra_data: VariableList<u8, T::MaxExtraDataBytes>,
pub base_fee_per_gas: Uint256,
pub block_hash: Hash256,
pub block_hash: ExecutionBlockHash,
#[serde(with = "serde_transactions")]
pub transactions:
VariableList<Transaction<T::MaxBytesPerTransaction>, T::MaxTransactionsPerPayload>,
@@ -90,7 +90,7 @@ impl<T: EthSpec> From<ExecutionPayload<T>> for JsonExecutionPayloadV1<T> {
parent_hash,
fee_recipient,
state_root,
receipt_root,
receipts_root,
logs_bloom,
random,
block_number,
@@ -107,7 +107,7 @@ impl<T: EthSpec> From<ExecutionPayload<T>> for JsonExecutionPayloadV1<T> {
parent_hash,
fee_recipient,
state_root,
receipts_root: receipt_root,
receipts_root,
logs_bloom,
random,
block_number,
@@ -146,7 +146,7 @@ impl<T: EthSpec> From<JsonExecutionPayloadV1<T>> for ExecutionPayload<T> {
parent_hash,
fee_recipient,
state_root,
receipt_root: receipts_root,
receipts_root,
logs_bloom,
random,
block_number,
@@ -207,9 +207,9 @@ impl From<JsonPayloadAttributesV1> for PayloadAttributes {
#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct JsonForkChoiceStateV1 {
pub head_block_hash: Hash256,
pub safe_block_hash: Hash256,
pub finalized_block_hash: Hash256,
pub head_block_hash: ExecutionBlockHash,
pub safe_block_hash: ExecutionBlockHash,
pub finalized_block_hash: ExecutionBlockHash,
}
impl From<ForkChoiceState> for JsonForkChoiceStateV1 {
@@ -248,47 +248,60 @@ impl From<JsonForkChoiceStateV1> for ForkChoiceState {
#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum JsonExecutePayloadV1ResponseStatus {
pub enum JsonPayloadStatusV1Status {
Valid,
Invalid,
Syncing,
Accepted,
InvalidBlockHash,
InvalidTerminalBlock,
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct JsonExecutePayloadV1Response {
pub status: JsonExecutePayloadV1ResponseStatus,
pub latest_valid_hash: Option<Hash256>,
pub struct JsonPayloadStatusV1 {
pub status: JsonPayloadStatusV1Status,
pub latest_valid_hash: Option<ExecutionBlockHash>,
pub validation_error: Option<String>,
}
impl From<ExecutePayloadResponseStatus> for JsonExecutePayloadV1ResponseStatus {
fn from(e: ExecutePayloadResponseStatus) -> Self {
impl From<PayloadStatusV1Status> for JsonPayloadStatusV1Status {
fn from(e: PayloadStatusV1Status) -> Self {
match e {
ExecutePayloadResponseStatus::Valid => JsonExecutePayloadV1ResponseStatus::Valid,
ExecutePayloadResponseStatus::Invalid => JsonExecutePayloadV1ResponseStatus::Invalid,
ExecutePayloadResponseStatus::Syncing => JsonExecutePayloadV1ResponseStatus::Syncing,
PayloadStatusV1Status::Valid => JsonPayloadStatusV1Status::Valid,
PayloadStatusV1Status::Invalid => JsonPayloadStatusV1Status::Invalid,
PayloadStatusV1Status::Syncing => JsonPayloadStatusV1Status::Syncing,
PayloadStatusV1Status::Accepted => JsonPayloadStatusV1Status::Accepted,
PayloadStatusV1Status::InvalidBlockHash => JsonPayloadStatusV1Status::InvalidBlockHash,
PayloadStatusV1Status::InvalidTerminalBlock => {
JsonPayloadStatusV1Status::InvalidTerminalBlock
}
}
}
}
impl From<JsonExecutePayloadV1ResponseStatus> for ExecutePayloadResponseStatus {
fn from(j: JsonExecutePayloadV1ResponseStatus) -> Self {
impl From<JsonPayloadStatusV1Status> for PayloadStatusV1Status {
fn from(j: JsonPayloadStatusV1Status) -> Self {
match j {
JsonExecutePayloadV1ResponseStatus::Valid => ExecutePayloadResponseStatus::Valid,
JsonExecutePayloadV1ResponseStatus::Invalid => ExecutePayloadResponseStatus::Invalid,
JsonExecutePayloadV1ResponseStatus::Syncing => ExecutePayloadResponseStatus::Syncing,
JsonPayloadStatusV1Status::Valid => PayloadStatusV1Status::Valid,
JsonPayloadStatusV1Status::Invalid => PayloadStatusV1Status::Invalid,
JsonPayloadStatusV1Status::Syncing => PayloadStatusV1Status::Syncing,
JsonPayloadStatusV1Status::Accepted => PayloadStatusV1Status::Accepted,
JsonPayloadStatusV1Status::InvalidBlockHash => PayloadStatusV1Status::InvalidBlockHash,
JsonPayloadStatusV1Status::InvalidTerminalBlock => {
PayloadStatusV1Status::InvalidTerminalBlock
}
}
}
}
impl From<ExecutePayloadResponse> for JsonExecutePayloadV1Response {
fn from(e: ExecutePayloadResponse) -> Self {
impl From<PayloadStatusV1> for JsonPayloadStatusV1 {
fn from(p: PayloadStatusV1) -> Self {
// Use this verbose deconstruction pattern to ensure no field is left unused.
let ExecutePayloadResponse {
let PayloadStatusV1 {
status,
latest_valid_hash,
validation_error,
} = e;
} = p;
Self {
status: status.into(),
@@ -298,10 +311,10 @@ impl From<ExecutePayloadResponse> for JsonExecutePayloadV1Response {
}
}
impl From<JsonExecutePayloadV1Response> for ExecutePayloadResponse {
fn from(j: JsonExecutePayloadV1Response) -> Self {
impl From<JsonPayloadStatusV1> for PayloadStatusV1 {
fn from(j: JsonPayloadStatusV1) -> Self {
// Use this verbose deconstruction pattern to ensure no field is left unused.
let JsonExecutePayloadV1Response {
let JsonPayloadStatusV1 {
status,
latest_valid_hash,
validation_error,
@@ -315,50 +328,23 @@ impl From<JsonExecutePayloadV1Response> for ExecutePayloadResponse {
}
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum JsonForkchoiceUpdatedV1ResponseStatus {
Success,
Syncing,
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct JsonForkchoiceUpdatedV1Response {
pub status: JsonForkchoiceUpdatedV1ResponseStatus,
pub payload_status: JsonPayloadStatusV1,
pub payload_id: Option<TransparentJsonPayloadId>,
}
impl From<JsonForkchoiceUpdatedV1ResponseStatus> for ForkchoiceUpdatedResponseStatus {
fn from(j: JsonForkchoiceUpdatedV1ResponseStatus) -> Self {
match j {
JsonForkchoiceUpdatedV1ResponseStatus::Success => {
ForkchoiceUpdatedResponseStatus::Success
}
JsonForkchoiceUpdatedV1ResponseStatus::Syncing => {
ForkchoiceUpdatedResponseStatus::Syncing
}
}
}
}
impl From<ForkchoiceUpdatedResponseStatus> for JsonForkchoiceUpdatedV1ResponseStatus {
fn from(f: ForkchoiceUpdatedResponseStatus) -> Self {
match f {
ForkchoiceUpdatedResponseStatus::Success => {
JsonForkchoiceUpdatedV1ResponseStatus::Success
}
ForkchoiceUpdatedResponseStatus::Syncing => {
JsonForkchoiceUpdatedV1ResponseStatus::Syncing
}
}
}
}
impl From<JsonForkchoiceUpdatedV1Response> for ForkchoiceUpdatedResponse {
fn from(j: JsonForkchoiceUpdatedV1Response) -> Self {
// Use this verbose deconstruction pattern to ensure no field is left unused.
let JsonForkchoiceUpdatedV1Response { status, payload_id } = j;
let JsonForkchoiceUpdatedV1Response {
payload_status: status,
payload_id,
} = j;
Self {
status: status.into(),
payload_status: status.into(),
payload_id: payload_id.map(Into::into),
}
}
@@ -366,10 +352,13 @@ impl From<JsonForkchoiceUpdatedV1Response> for ForkchoiceUpdatedResponse {
impl From<ForkchoiceUpdatedResponse> for JsonForkchoiceUpdatedV1Response {
fn from(f: ForkchoiceUpdatedResponse) -> Self {
// Use this verbose deconstruction pattern to ensure no field is left unused.
let ForkchoiceUpdatedResponse { status, payload_id } = f;
let ForkchoiceUpdatedResponse {
payload_status: status,
payload_id,
} = f;
Self {
status: status.into(),
payload_status: status.into(),
payload_id: payload_id.map(Into::into),
}
}

View File

@@ -1,12 +1,14 @@
//! Provides generic behaviour for multiple execution engines, specifically fallback behaviour.
use crate::engine_api::{EngineApi, Error as EngineApiError, PayloadAttributes, PayloadId};
use crate::engine_api::{
EngineApi, Error as EngineApiError, ForkchoiceUpdatedResponse, PayloadAttributes, PayloadId,
};
use futures::future::join_all;
use lru::LruCache;
use slog::{crit, debug, info, warn, Logger};
use std::future::Future;
use tokio::sync::{Mutex, RwLock};
use types::{Address, Hash256};
use types::{Address, ExecutionBlockHash, Hash256};
/// The number of payload IDs that will be stored for each `Engine`.
///
@@ -23,9 +25,9 @@ enum EngineState {
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct ForkChoiceState {
pub head_block_hash: Hash256,
pub safe_block_hash: Hash256,
pub finalized_block_hash: Hash256,
pub head_block_hash: ExecutionBlockHash,
pub safe_block_hash: ExecutionBlockHash,
pub finalized_block_hash: ExecutionBlockHash,
}
/// Used to enable/disable logging on some tasks.
@@ -46,7 +48,7 @@ impl Logging {
#[derive(Hash, PartialEq, std::cmp::Eq)]
struct PayloadIdCacheKey {
pub head_block_hash: Hash256,
pub head_block_hash: ExecutionBlockHash,
pub timestamp: u64,
pub random: Hash256,
pub suggested_fee_recipient: Address,
@@ -73,7 +75,7 @@ impl<T> Engine<T> {
pub async fn get_payload_id(
&self,
head_block_hash: Hash256,
head_block_hash: ExecutionBlockHash,
timestamp: u64,
random: Hash256,
suggested_fee_recipient: Address,
@@ -97,7 +99,7 @@ impl<T: EngineApi> Engine<T> {
forkchoice_state: ForkChoiceState,
payload_attributes: Option<PayloadAttributes>,
log: &Logger,
) -> Result<Option<PayloadId>, EngineApiError> {
) -> Result<ForkchoiceUpdatedResponse, EngineApiError> {
let response = self
.api
.forkchoice_updated_v1(forkchoice_state, payload_attributes)
@@ -117,7 +119,7 @@ impl<T: EngineApi> Engine<T> {
}
}
Ok(response.payload_id)
Ok(response)
}
}

View File

@@ -7,6 +7,7 @@
use engine_api::{Error as ApiError, *};
use engines::{Engine, EngineError, Engines, ForkChoiceState, Logging};
use lru::LruCache;
use payload_status::process_multiple_payload_statuses;
use sensitive_url::SensitiveUrl;
use slog::{crit, debug, error, info, Logger};
use slot_clock::SlotClock;
@@ -19,12 +20,14 @@ use tokio::{
sync::{Mutex, MutexGuard},
time::{sleep, sleep_until, Instant},
};
use types::{ChainSpec, Epoch, ProposerPreparationData};
use types::{ChainSpec, Epoch, ExecutionBlockHash, ProposerPreparationData};
pub use engine_api::{http::HttpJsonRpc, ExecutePayloadResponseStatus};
pub use engine_api::{http::HttpJsonRpc, PayloadAttributes, PayloadStatusV1Status};
pub use payload_status::PayloadStatus;
mod engine_api;
mod engines;
mod payload_status;
pub mod test_utils;
/// Each time the `ExecutionLayer` retrieves a block from an execution node, it stores that block
@@ -49,6 +52,8 @@ pub enum Error {
NotSynced,
ShuttingDown,
FeeRecipientUnspecified,
ConsensusFailure,
MissingLatestValidHash,
}
impl From<ApiError> for Error {
@@ -67,7 +72,7 @@ struct Inner {
engines: Engines<HttpJsonRpc>,
suggested_fee_recipient: Option<Address>,
proposer_preparation_data: Mutex<HashMap<u64, ProposerPreparationDataEntry>>,
execution_blocks: Mutex<LruCache<Hash256, ExecutionBlock>>,
execution_blocks: Mutex<LruCache<ExecutionBlockHash, ExecutionBlock>>,
executor: TaskExecutor,
log: Logger,
}
@@ -136,7 +141,9 @@ impl ExecutionLayer {
}
/// Note: this function returns a mutex guard, be careful to avoid deadlocks.
async fn execution_blocks(&self) -> MutexGuard<'_, LruCache<Hash256, ExecutionBlock>> {
async fn execution_blocks(
&self,
) -> MutexGuard<'_, LruCache<ExecutionBlockHash, ExecutionBlock>> {
self.inner.execution_blocks.lock().await
}
@@ -249,7 +256,7 @@ impl ExecutionLayer {
}
/// Performs a single execution of the watchdog routine.
async fn watchdog_task(&self) {
pub async fn watchdog_task(&self) {
// Disable logging since this runs frequently and may get annoying.
self.engines().upcheck_not_synced(Logging::Disabled).await;
}
@@ -383,10 +390,10 @@ impl ExecutionLayer {
/// will be contacted.
pub async fn get_payload<T: EthSpec>(
&self,
parent_hash: Hash256,
parent_hash: ExecutionBlockHash,
timestamp: u64,
random: Hash256,
finalized_block_hash: Hash256,
finalized_block_hash: ExecutionBlockHash,
proposer_index: u64,
) -> Result<ExecutionPayload<T>, Error> {
let suggested_fee_recipient = self.get_suggested_fee_recipient(proposer_index).await;
@@ -431,8 +438,18 @@ impl ExecutionLayer {
Some(payload_attributes),
self.log(),
)
.await?
.ok_or(ApiError::PayloadIdUnavailable)?
.await
.map(|response| response.payload_id)?
.ok_or_else(|| {
error!(
self.log(),
"Exec engine unable to produce payload";
"msg" => "No payload ID, the engine is likely syncing. \
This has the potential to cause a missed block proposal.",
);
ApiError::PayloadIdUnavailable
})?
};
engine.api.get_payload_v1(payload_id).await
@@ -441,7 +458,7 @@ impl ExecutionLayer {
.map_err(Error::EngineErrors)
}
/// Maps to the `engine_executePayload` JSON-RPC call.
/// Maps to the `engine_newPayload` JSON-RPC call.
///
/// ## Fallback Behaviour
///
@@ -449,17 +466,18 @@ impl ExecutionLayer {
/// failure) from all nodes and then return based on the first of these conditions which
/// returns true:
///
/// - Error::ConsensusFailure if some nodes return valid and some return invalid
/// - Valid, if any nodes return valid.
/// - Invalid, if any nodes return invalid.
/// - Syncing, if any nodes return syncing.
/// - An error, if all nodes return an error.
pub async fn execute_payload<T: EthSpec>(
pub async fn notify_new_payload<T: EthSpec>(
&self,
execution_payload: &ExecutionPayload<T>,
) -> Result<(ExecutePayloadResponseStatus, Option<Hash256>), Error> {
) -> Result<PayloadStatus, Error> {
debug!(
self.log(),
"Issuing engine_executePayload";
"Issuing engine_newPayload";
"parent_hash" => ?execution_payload.parent_hash,
"block_hash" => ?execution_payload.block_hash,
"block_number" => execution_payload.block_number,
@@ -467,70 +485,14 @@ impl ExecutionLayer {
let broadcast_results = self
.engines()
.broadcast(|engine| engine.api.execute_payload_v1(execution_payload.clone()))
.broadcast(|engine| engine.api.new_payload_v1(execution_payload.clone()))
.await;
let mut errors = vec![];
let mut valid = 0;
let mut invalid = 0;
let mut syncing = 0;
let mut invalid_latest_valid_hash = vec![];
for result in broadcast_results {
match result.map(|response| (response.latest_valid_hash, response.status)) {
Ok((Some(latest_hash), ExecutePayloadResponseStatus::Valid)) => {
if latest_hash == execution_payload.block_hash {
valid += 1;
} else {
invalid += 1;
errors.push(EngineError::Api {
id: "unknown".to_string(),
error: engine_api::Error::BadResponse(
format!(
"execute_payload: response.status = Valid but invalid latest_valid_hash. Expected({:?}) Found({:?})",
execution_payload.block_hash,
latest_hash,
)
),
});
invalid_latest_valid_hash.push(latest_hash);
}
}
Ok((Some(latest_hash), ExecutePayloadResponseStatus::Invalid)) => {
invalid += 1;
invalid_latest_valid_hash.push(latest_hash);
}
Ok((_, ExecutePayloadResponseStatus::Syncing)) => syncing += 1,
Ok((None, status)) => errors.push(EngineError::Api {
id: "unknown".to_string(),
error: engine_api::Error::BadResponse(format!(
"execute_payload: status {:?} returned with null latest_valid_hash",
status
)),
}),
Err(e) => errors.push(e),
}
}
if valid > 0 && invalid > 0 {
crit!(
self.log(),
"Consensus failure between execution nodes";
"method" => "execute_payload"
);
}
if valid > 0 {
Ok((
ExecutePayloadResponseStatus::Valid,
Some(execution_payload.block_hash),
))
} else if invalid > 0 {
Ok((ExecutePayloadResponseStatus::Invalid, None))
} else if syncing > 0 {
Ok((ExecutePayloadResponseStatus::Syncing, None))
} else {
Err(Error::EngineErrors(errors))
}
process_multiple_payload_statuses(
execution_payload.block_hash,
broadcast_results.into_iter(),
self.log(),
)
}
/// Maps to the `engine_consensusValidated` JSON-RPC call.
@@ -541,14 +503,17 @@ impl ExecutionLayer {
/// failure) from all nodes and then return based on the first of these conditions which
/// returns true:
///
/// - Ok, if any node returns successfully.
/// - Error::ConsensusFailure if some nodes return valid and some return invalid
/// - Valid, if any nodes return valid.
/// - Invalid, if any nodes return invalid.
/// - Syncing, if any nodes return syncing.
/// - An error, if all nodes return an error.
pub async fn notify_forkchoice_updated(
&self,
head_block_hash: Hash256,
finalized_block_hash: Hash256,
head_block_hash: ExecutionBlockHash,
finalized_block_hash: ExecutionBlockHash,
payload_attributes: Option<PayloadAttributes>,
) -> Result<(), Error> {
) -> Result<PayloadStatus, Error> {
debug!(
self.log(),
"Issuing engine_forkchoiceUpdated";
@@ -577,15 +542,13 @@ impl ExecutionLayer {
})
.await;
if broadcast_results.iter().any(Result::is_ok) {
Ok(())
} else {
let errors = broadcast_results
process_multiple_payload_statuses(
head_block_hash,
broadcast_results
.into_iter()
.filter_map(Result::err)
.collect();
Err(Error::EngineErrors(errors))
}
.map(|result| result.map(|response| response.payload_status)),
self.log(),
)
}
/// Used during block production to determine if the merge has been triggered.
@@ -598,12 +561,12 @@ impl ExecutionLayer {
pub async fn get_terminal_pow_block_hash(
&self,
spec: &ChainSpec,
) -> Result<Option<Hash256>, Error> {
) -> Result<Option<ExecutionBlockHash>, Error> {
let hash_opt = self
.engines()
.first_success(|engine| async move {
let terminal_block_hash = spec.terminal_block_hash;
if terminal_block_hash != Hash256::zero() {
if terminal_block_hash != ExecutionBlockHash::zero() {
if self
.get_pow_block(engine, terminal_block_hash)
.await?
@@ -647,7 +610,7 @@ impl ExecutionLayer {
&self,
engine: &Engine<HttpJsonRpc>,
spec: &ChainSpec,
) -> Result<Option<Hash256>, ApiError> {
) -> Result<Option<ExecutionBlockHash>, ApiError> {
let mut block = engine
.api
.get_block_by_number(BlockByNumberQuery::Tag(LATEST_TAG))
@@ -659,7 +622,7 @@ impl ExecutionLayer {
loop {
let block_reached_ttd = block.total_difficulty >= spec.terminal_total_difficulty;
if block_reached_ttd {
if block.parent_hash == Hash256::zero() {
if block.parent_hash == ExecutionBlockHash::zero() {
return Ok(Some(block.block_hash));
}
let parent = self
@@ -707,7 +670,7 @@ impl ExecutionLayer {
/// https://github.com/ethereum/consensus-specs/blob/v1.1.0/specs/merge/fork-choice.md
pub async fn is_valid_terminal_pow_block_hash(
&self,
block_hash: Hash256,
block_hash: ExecutionBlockHash,
spec: &ChainSpec,
) -> Result<Option<bool>, Error> {
let broadcast_results = self
@@ -786,7 +749,7 @@ impl ExecutionLayer {
async fn get_pow_block(
&self,
engine: &Engine<HttpJsonRpc>,
hash: Hash256,
hash: ExecutionBlockHash,
) -> Result<Option<ExecutionBlock>, ApiError> {
if let Some(cached) = self.execution_blocks().await.get(&hash).copied() {
// The block was in the cache, no need to request it from the execution
@@ -880,7 +843,7 @@ mod test {
MockExecutionLayer::default_params()
.move_to_terminal_block()
.with_terminal_block(|spec, el, _| async move {
let missing_terminal_block = Hash256::repeat_byte(42);
let missing_terminal_block = ExecutionBlockHash::repeat_byte(42);
assert_eq!(
el.is_valid_terminal_pow_block_hash(missing_terminal_block, &spec)

View File

@@ -0,0 +1,191 @@
use crate::engine_api::{Error as ApiError, PayloadStatusV1, PayloadStatusV1Status};
use crate::engines::EngineError;
use crate::Error;
use slog::{crit, warn, Logger};
use types::ExecutionBlockHash;
/// Provides a simpler, easier to parse version of `PayloadStatusV1` for upstream users.
///
/// It primarily ensures that the `latest_valid_hash` is always present when relevant.
#[derive(Debug, Clone, PartialEq)]
pub enum PayloadStatus {
Valid,
Invalid {
latest_valid_hash: ExecutionBlockHash,
validation_error: Option<String>,
},
Syncing,
Accepted,
InvalidBlockHash {
validation_error: Option<String>,
},
InvalidTerminalBlock {
validation_error: Option<String>,
},
}
/// Processes the responses from multiple execution engines, finding the "best" status and returning
/// it (if any).
///
/// This function has the following basic goals:
///
/// - Detect a consensus failure between nodes.
/// - Find the most-synced node by preferring a definite response (valid/invalid) over a
/// syncing/accepted response or error.
///
/// # Details
///
/// - If there are conflicting valid/invalid responses, always return an error.
/// - If there are syncing/accepted responses but valid/invalid responses exist, return the
/// valid/invalid responses since they're definite.
/// - If there are multiple valid responses, return the first one processed.
/// - If there are multiple invalid responses, return the first one processed.
/// - Syncing/accepted responses are grouped, if there are multiple of them, return the first one
/// processed.
/// - If there are no responses (only errors or nothing), return an error.
pub fn process_multiple_payload_statuses(
head_block_hash: ExecutionBlockHash,
statuses: impl Iterator<Item = Result<PayloadStatusV1, EngineError>>,
log: &Logger,
) -> Result<PayloadStatus, Error> {
let mut errors = vec![];
let mut valid_statuses = vec![];
let mut invalid_statuses = vec![];
let mut other_statuses = vec![];
for status in statuses {
match status {
Err(e) => errors.push(e),
Ok(response) => match &response.status {
PayloadStatusV1Status::Valid => {
if response
.latest_valid_hash
.map_or(false, |h| h == head_block_hash)
{
// The response is only valid if `latest_valid_hash` is not `null` and
// equal to the provided `block_hash`.
valid_statuses.push(PayloadStatus::Valid)
} else {
errors.push(EngineError::Api {
id: "unknown".to_string(),
error: ApiError::BadResponse(
format!(
"new_payload: response.status = VALID but invalid latest_valid_hash. Expected({:?}) Found({:?})",
head_block_hash,
response.latest_valid_hash,
)
),
});
}
}
PayloadStatusV1Status::Invalid => {
if let Some(latest_valid_hash) = response.latest_valid_hash {
// The response is only valid if `latest_valid_hash` is not `null`.
invalid_statuses.push(PayloadStatus::Invalid {
latest_valid_hash,
validation_error: response.validation_error.clone(),
})
} else {
errors.push(EngineError::Api {
id: "unknown".to_string(),
error: ApiError::BadResponse(
"new_payload: response.status = INVALID but null latest_valid_hash"
.to_string(),
),
});
}
}
PayloadStatusV1Status::InvalidBlockHash => {
// In the interests of being liberal with what we accept, only raise a
// warning here.
if response.latest_valid_hash.is_some() {
warn!(
log,
"Malformed response from execution engine";
"msg" => "expected a null latest_valid_hash",
"status" => ?response.status
)
}
invalid_statuses.push(PayloadStatus::InvalidBlockHash {
validation_error: response.validation_error.clone(),
});
}
PayloadStatusV1Status::InvalidTerminalBlock => {
// In the interests of being liberal with what we accept, only raise a
// warning here.
if response.latest_valid_hash.is_some() {
warn!(
log,
"Malformed response from execution engine";
"msg" => "expected a null latest_valid_hash",
"status" => ?response.status
)
}
invalid_statuses.push(PayloadStatus::InvalidTerminalBlock {
validation_error: response.validation_error.clone(),
});
}
PayloadStatusV1Status::Syncing => {
// In the interests of being liberal with what we accept, only raise a
// warning here.
if response.latest_valid_hash.is_some() {
warn!(
log,
"Malformed response from execution engine";
"msg" => "expected a null latest_valid_hash",
"status" => ?response.status
)
}
other_statuses.push(PayloadStatus::Syncing)
}
PayloadStatusV1Status::Accepted => {
// In the interests of being liberal with what we accept, only raise a
// warning here.
if response.latest_valid_hash.is_some() {
warn!(
log,
"Malformed response from execution engine";
"msg" => "expected a null latest_valid_hash",
"status" => ?response.status
)
}
other_statuses.push(PayloadStatus::Accepted)
}
},
}
}
if !valid_statuses.is_empty() && !invalid_statuses.is_empty() {
crit!(
log,
"Consensus failure between execution nodes";
"invalid_statuses" => ?invalid_statuses,
"valid_statuses" => ?valid_statuses,
);
// Choose to exit and ignore the valid response. This preferences correctness over
// liveness.
return Err(Error::ConsensusFailure);
}
// Log any errors to assist with troubleshooting.
for error in &errors {
warn!(
log,
"Error whilst processing payload status";
"error" => ?error,
);
}
valid_statuses
.first()
.or_else(|| invalid_statuses.first())
.or_else(|| other_statuses.first())
.cloned()
.map(Result::Ok)
.unwrap_or_else(|| Err(Error::EngineErrors(errors)))
}

View File

@@ -1,13 +1,15 @@
use crate::engine_api::{
ExecutePayloadResponse, ExecutePayloadResponseStatus, ExecutionBlock, PayloadAttributes,
PayloadId,
json_structures::{
JsonForkchoiceUpdatedV1Response, JsonPayloadStatusV1, JsonPayloadStatusV1Status,
},
ExecutionBlock, PayloadAttributes, PayloadId, PayloadStatusV1, PayloadStatusV1Status,
};
use crate::engines::ForkChoiceState;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use tree_hash::TreeHash;
use tree_hash_derive::TreeHash;
use types::{EthSpec, ExecutionPayload, Hash256, Uint256};
use types::{EthSpec, ExecutionBlockHash, ExecutionPayload, Hash256, Uint256};
const GAS_LIMIT: u64 = 16384;
const GAS_USED: u64 = GAS_LIMIT - 1;
@@ -27,14 +29,14 @@ impl<T: EthSpec> Block<T> {
}
}
pub fn parent_hash(&self) -> Hash256 {
pub fn parent_hash(&self) -> ExecutionBlockHash {
match self {
Block::PoW(block) => block.parent_hash,
Block::PoS(payload) => payload.parent_hash,
}
}
pub fn block_hash(&self) -> Hash256 {
pub fn block_hash(&self) -> ExecutionBlockHash {
match self {
Block::PoW(block) => block.block_hash,
Block::PoS(payload) => payload.block_hash,
@@ -70,8 +72,8 @@ impl<T: EthSpec> Block<T> {
#[serde(rename_all = "camelCase")]
pub struct PoWBlock {
pub block_number: u64,
pub block_hash: Hash256,
pub parent_hash: Hash256,
pub block_hash: ExecutionBlockHash,
pub parent_hash: ExecutionBlockHash,
pub total_difficulty: Uint256,
}
@@ -79,18 +81,18 @@ pub struct ExecutionBlockGenerator<T: EthSpec> {
/*
* Common database
*/
blocks: HashMap<Hash256, Block<T>>,
block_hashes: HashMap<u64, Hash256>,
blocks: HashMap<ExecutionBlockHash, Block<T>>,
block_hashes: HashMap<u64, ExecutionBlockHash>,
/*
* PoW block parameters
*/
pub terminal_total_difficulty: Uint256,
pub terminal_block_number: u64,
pub terminal_block_hash: Hash256,
pub terminal_block_hash: ExecutionBlockHash,
/*
* PoS block parameters
*/
pub pending_payloads: HashMap<Hash256, ExecutionPayload<T>>,
pub pending_payloads: HashMap<ExecutionBlockHash, ExecutionPayload<T>>,
pub next_payload_id: u64,
pub payload_ids: HashMap<PayloadId, ExecutionPayload<T>>,
}
@@ -99,7 +101,7 @@ impl<T: EthSpec> ExecutionBlockGenerator<T> {
pub fn new(
terminal_total_difficulty: Uint256,
terminal_block_number: u64,
terminal_block_hash: Hash256,
terminal_block_hash: ExecutionBlockHash,
) -> Self {
let mut gen = Self {
blocks: <_>::default(),
@@ -142,11 +144,11 @@ impl<T: EthSpec> ExecutionBlockGenerator<T> {
.map(|block| block.as_execution_block(self.terminal_total_difficulty))
}
pub fn block_by_hash(&self, hash: Hash256) -> Option<Block<T>> {
pub fn block_by_hash(&self, hash: ExecutionBlockHash) -> Option<Block<T>> {
self.blocks.get(&hash).cloned()
}
pub fn execution_block_by_hash(&self, hash: Hash256) -> Option<ExecutionBlock> {
pub fn execution_block_by_hash(&self, hash: ExecutionBlockHash) -> Option<ExecutionBlock> {
self.block_by_hash(hash)
.map(|block| block.as_execution_block(self.terminal_total_difficulty))
}
@@ -188,7 +190,7 @@ impl<T: EthSpec> ExecutionBlockGenerator<T> {
pub fn insert_pow_block(&mut self, block_number: u64) -> Result<(), String> {
let parent_hash = if block_number == 0 {
Hash256::zero()
ExecutionBlockHash::zero()
} else if let Some(hash) = self.block_hashes.get(&(block_number - 1)) {
*hash
} else {
@@ -232,23 +234,23 @@ impl<T: EthSpec> ExecutionBlockGenerator<T> {
}
pub fn get_payload(&mut self, id: &PayloadId) -> Option<ExecutionPayload<T>> {
self.payload_ids.remove(id)
self.payload_ids.get(id).cloned()
}
pub fn execute_payload(&mut self, payload: ExecutionPayload<T>) -> ExecutePayloadResponse {
pub fn new_payload(&mut self, payload: ExecutionPayload<T>) -> PayloadStatusV1 {
let parent = if let Some(parent) = self.blocks.get(&payload.parent_hash) {
parent
} else {
return ExecutePayloadResponse {
status: ExecutePayloadResponseStatus::Syncing,
return PayloadStatusV1 {
status: PayloadStatusV1Status::Syncing,
latest_valid_hash: None,
validation_error: None,
};
};
if payload.block_number != parent.block_number() + 1 {
return ExecutePayloadResponse {
status: ExecutePayloadResponseStatus::Invalid,
return PayloadStatusV1 {
status: PayloadStatusV1Status::Invalid,
latest_valid_hash: Some(parent.block_hash()),
validation_error: Some("invalid block number".to_string()),
};
@@ -257,8 +259,8 @@ impl<T: EthSpec> ExecutionBlockGenerator<T> {
let valid_hash = payload.block_hash;
self.pending_payloads.insert(payload.block_hash, payload);
ExecutePayloadResponse {
status: ExecutePayloadResponseStatus::Valid,
PayloadStatusV1 {
status: PayloadStatusV1Status::Valid,
latest_valid_hash: Some(valid_hash),
validation_error: None,
}
@@ -268,39 +270,35 @@ impl<T: EthSpec> ExecutionBlockGenerator<T> {
&mut self,
forkchoice_state: ForkChoiceState,
payload_attributes: Option<PayloadAttributes>,
) -> Result<Option<PayloadId>, String> {
) -> Result<JsonForkchoiceUpdatedV1Response, String> {
if let Some(payload) = self
.pending_payloads
.remove(&forkchoice_state.head_block_hash)
{
self.insert_block(Block::PoS(payload))?;
}
if !self.blocks.contains_key(&forkchoice_state.head_block_hash) {
return Err(format!(
"block hash {:?} unknown",
forkchoice_state.head_block_hash
));
}
if !self.blocks.contains_key(&forkchoice_state.safe_block_hash) {
return Err(format!(
"block hash {:?} unknown",
forkchoice_state.head_block_hash
));
}
if forkchoice_state.finalized_block_hash != Hash256::zero()
let unknown_head_block_hash = !self.blocks.contains_key(&forkchoice_state.head_block_hash);
let unknown_safe_block_hash = !self.blocks.contains_key(&forkchoice_state.safe_block_hash);
let unknown_finalized_block_hash = forkchoice_state.finalized_block_hash
!= ExecutionBlockHash::zero()
&& !self
.blocks
.contains_key(&forkchoice_state.finalized_block_hash)
{
return Err(format!(
"finalized block hash {:?} is unknown",
forkchoice_state.finalized_block_hash
));
.contains_key(&forkchoice_state.finalized_block_hash);
if unknown_head_block_hash || unknown_safe_block_hash || unknown_finalized_block_hash {
return Ok(JsonForkchoiceUpdatedV1Response {
payload_status: JsonPayloadStatusV1 {
status: JsonPayloadStatusV1Status::Syncing,
latest_valid_hash: None,
validation_error: None,
},
payload_id: None,
});
}
match payload_attributes {
None => Ok(None),
let id = match payload_attributes {
None => None,
Some(attributes) => {
if !self.blocks.iter().any(|(_, block)| {
block.block_hash() == self.terminal_block_hash
@@ -325,7 +323,7 @@ impl<T: EthSpec> ExecutionBlockGenerator<T> {
let mut execution_payload = ExecutionPayload {
parent_hash: forkchoice_state.head_block_hash,
fee_recipient: attributes.suggested_fee_recipient,
receipt_root: Hash256::repeat_byte(42),
receipts_root: Hash256::repeat_byte(42),
state_root: Hash256::repeat_byte(43),
logs_bloom: vec![0; 256].into(),
random: attributes.random,
@@ -335,17 +333,27 @@ impl<T: EthSpec> ExecutionBlockGenerator<T> {
timestamp: attributes.timestamp,
extra_data: "block gen was here".as_bytes().to_vec().into(),
base_fee_per_gas: Uint256::one(),
block_hash: Hash256::zero(),
block_hash: ExecutionBlockHash::zero(),
transactions: vec![].into(),
};
execution_payload.block_hash = execution_payload.tree_hash_root();
execution_payload.block_hash =
ExecutionBlockHash::from_root(execution_payload.tree_hash_root());
self.payload_ids.insert(id, execution_payload);
Ok(Some(id))
Some(id)
}
}
};
Ok(JsonForkchoiceUpdatedV1Response {
payload_status: JsonPayloadStatusV1 {
status: JsonPayloadStatusV1Status::Valid,
latest_valid_hash: Some(forkchoice_state.head_block_hash),
validation_error: None,
},
payload_id: id.map(Into::into),
})
}
}
@@ -357,7 +365,7 @@ pub fn generate_pow_block(
terminal_total_difficulty: Uint256,
terminal_block_number: u64,
block_number: u64,
parent_hash: Hash256,
parent_hash: ExecutionBlockHash,
) -> Result<PoWBlock, String> {
if block_number > terminal_block_number {
return Err(format!(
@@ -379,12 +387,12 @@ pub fn generate_pow_block(
let mut block = PoWBlock {
block_number,
block_hash: Hash256::zero(),
block_hash: ExecutionBlockHash::zero(),
parent_hash,
total_difficulty,
};
block.block_hash = block.tree_hash_root();
block.block_hash = ExecutionBlockHash::from_root(block.tree_hash_root());
Ok(block)
}
@@ -403,7 +411,7 @@ mod test {
let mut generator: ExecutionBlockGenerator<MainnetEthSpec> = ExecutionBlockGenerator::new(
TERMINAL_DIFFICULTY.into(),
TERMINAL_BLOCK,
Hash256::zero(),
ExecutionBlockHash::zero(),
);
for i in 0..=TERMINAL_BLOCK {
@@ -421,7 +429,7 @@ mod test {
let expected_parent = i
.checked_sub(1)
.map(|i| generator.block_by_number(i).unwrap().block_hash())
.unwrap_or_else(Hash256::zero);
.unwrap_or_else(ExecutionBlockHash::zero);
assert_eq!(block.parent_hash(), expected_parent);
assert_eq!(

View File

@@ -1,5 +1,5 @@
use super::Context;
use crate::engine_api::{http::*, ExecutePayloadResponse, ExecutePayloadResponseStatus};
use crate::engine_api::{http::*, *};
use crate::json_structures::*;
use serde::de::DeserializeOwned;
use serde_json::Value as JsonValue;
@@ -54,30 +54,33 @@ pub async fn handle_rpc<T: EthSpec>(
)
.unwrap())
}
ENGINE_EXECUTE_PAYLOAD_V1 => {
ENGINE_NEW_PAYLOAD_V1 => {
let request: JsonExecutionPayloadV1<T> = get_param(params, 0)?;
let response = if let Some(status) = *ctx.static_execute_payload_response.lock() {
match status {
ExecutePayloadResponseStatus::Valid => ExecutePayloadResponse {
status,
latest_valid_hash: Some(request.block_hash),
validation_error: None,
},
ExecutePayloadResponseStatus::Syncing => ExecutePayloadResponse {
status,
latest_valid_hash: None,
validation_error: None,
},
_ => unimplemented!("invalid static executePayloadResponse"),
}
let (static_response, should_import) =
if let Some(mut response) = ctx.static_new_payload_response.lock().clone() {
if response.status.status == PayloadStatusV1Status::Valid {
response.status.latest_valid_hash = Some(request.block_hash)
}
(Some(response.status), response.should_import)
} else {
(None, true)
};
let dynamic_response = if should_import {
Some(
ctx.execution_block_generator
.write()
.new_payload(request.into()),
)
} else {
ctx.execution_block_generator
.write()
.execute_payload(request.into())
None
};
Ok(serde_json::to_value(JsonExecutePayloadV1Response::from(response)).unwrap())
let response = static_response.or(dynamic_response).unwrap();
Ok(serde_json::to_value(JsonPayloadStatusV1::from(response)).unwrap())
}
ENGINE_GET_PAYLOAD_V1 => {
let request: JsonPayloadIdRequest = get_param(params, 0)?;
@@ -94,7 +97,8 @@ pub async fn handle_rpc<T: EthSpec>(
ENGINE_FORKCHOICE_UPDATED_V1 => {
let forkchoice_state: JsonForkChoiceStateV1 = get_param(params, 0)?;
let payload_attributes: Option<JsonPayloadAttributesV1> = get_param(params, 1)?;
let id = ctx
let response = ctx
.execution_block_generator
.write()
.forkchoice_updated_v1(
@@ -102,11 +106,7 @@ pub async fn handle_rpc<T: EthSpec>(
payload_attributes.map(|json| json.into()),
)?;
Ok(serde_json::to_value(JsonForkchoiceUpdatedV1Response {
status: JsonForkchoiceUpdatedV1ResponseStatus::Success,
payload_id: id.map(Into::into),
})
.unwrap())
Ok(serde_json::to_value(response).unwrap())
}
other => Err(format!(
"The method {} does not exist/is not available",

View File

@@ -58,7 +58,7 @@ impl<T: EthSpec> MockExecutionLayer<T> {
Self::new(
DEFAULT_TERMINAL_DIFFICULTY.into(),
DEFAULT_TERMINAL_BLOCK,
Hash256::zero(),
ExecutionBlockHash::zero(),
Epoch::new(0),
)
}
@@ -66,7 +66,7 @@ impl<T: EthSpec> MockExecutionLayer<T> {
pub fn new(
terminal_total_difficulty: Uint256,
terminal_block: u64,
terminal_block_hash: Hash256,
terminal_block_hash: ExecutionBlockHash,
terminal_block_hash_activation_epoch: Epoch,
) -> Self {
let el_runtime = ExecutionLayerRuntime::default();
@@ -117,7 +117,7 @@ impl<T: EthSpec> MockExecutionLayer<T> {
self.el
.notify_forkchoice_updated(
parent_hash,
Hash256::zero(),
ExecutionBlockHash::zero(),
Some(PayloadAttributes {
timestamp,
random,
@@ -145,13 +145,11 @@ impl<T: EthSpec> MockExecutionLayer<T> {
assert_eq!(payload.timestamp, timestamp);
assert_eq!(payload.random, random);
let (payload_response, latest_valid_hash) =
self.el.execute_payload(&payload).await.unwrap();
assert_eq!(payload_response, ExecutePayloadResponseStatus::Valid);
assert_eq!(latest_valid_hash, Some(payload.block_hash));
let status = self.el.notify_new_payload(&payload).await.unwrap();
assert_eq!(status, PayloadStatus::Valid);
self.el
.notify_forkchoice_updated(block_hash, Hash256::zero(), None)
.notify_forkchoice_updated(block_hash, ExecutionBlockHash::zero(), None)
.await
.unwrap();

View File

@@ -1,7 +1,6 @@
//! Provides a mock execution engine HTTP JSON-RPC API for use in testing.
use crate::engine_api::http::JSONRPC_VERSION;
use crate::engine_api::ExecutePayloadResponseStatus;
use crate::engine_api::{http::JSONRPC_VERSION, PayloadStatusV1, PayloadStatusV1Status};
use bytes::Bytes;
use environment::null_logger;
use execution_block_generator::{Block, PoWBlock};
@@ -15,7 +14,7 @@ use std::marker::PhantomData;
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};
use std::sync::Arc;
use tokio::{runtime, sync::oneshot};
use types::{EthSpec, Hash256, Uint256};
use types::{EthSpec, ExecutionBlockHash, Uint256};
use warp::Filter;
pub use execution_block_generator::{generate_pow_block, ExecutionBlockGenerator};
@@ -41,7 +40,7 @@ impl<T: EthSpec> MockServer<T> {
&runtime::Handle::current(),
DEFAULT_TERMINAL_DIFFICULTY.into(),
DEFAULT_TERMINAL_BLOCK,
Hash256::zero(),
ExecutionBlockHash::zero(),
)
}
@@ -49,7 +48,7 @@ impl<T: EthSpec> MockServer<T> {
handle: &runtime::Handle,
terminal_difficulty: Uint256,
terminal_block: u64,
terminal_block_hash: Hash256,
terminal_block_hash: ExecutionBlockHash,
) -> Self {
let last_echo_request = Arc::new(RwLock::new(None));
let preloaded_responses = Arc::new(Mutex::new(vec![]));
@@ -62,7 +61,7 @@ impl<T: EthSpec> MockServer<T> {
last_echo_request: last_echo_request.clone(),
execution_block_generator: RwLock::new(execution_block_generator),
preloaded_responses,
static_execute_payload_response: <_>::default(),
static_new_payload_response: <_>::default(),
_phantom: PhantomData,
});
@@ -117,14 +116,54 @@ impl<T: EthSpec> MockServer<T> {
}
pub fn all_payloads_valid(&self) {
*self.ctx.static_execute_payload_response.lock() = Some(ExecutePayloadResponseStatus::Valid)
let response = StaticNewPayloadResponse {
status: PayloadStatusV1 {
status: PayloadStatusV1Status::Valid,
latest_valid_hash: None,
validation_error: None,
},
should_import: true,
};
*self.ctx.static_new_payload_response.lock() = Some(response)
}
/// Setting `should_import = true` simulates an EE that initially returns `SYNCING` but obtains
/// the block via it's own means (e.g., devp2p).
pub fn all_payloads_syncing(&self, should_import: bool) {
let response = StaticNewPayloadResponse {
status: PayloadStatusV1 {
status: PayloadStatusV1Status::Syncing,
latest_valid_hash: None,
validation_error: None,
},
should_import,
};
*self.ctx.static_new_payload_response.lock() = Some(response)
}
pub fn all_payloads_invalid(&self, latest_valid_hash: ExecutionBlockHash) {
let response = StaticNewPayloadResponse {
status: PayloadStatusV1 {
status: PayloadStatusV1Status::Invalid,
latest_valid_hash: Some(latest_valid_hash),
validation_error: Some("static response".into()),
},
should_import: true,
};
*self.ctx.static_new_payload_response.lock() = Some(response)
}
/// Disables any static payload response so the execution block generator will do its own
/// verification.
pub fn full_payload_verification(&self) {
*self.ctx.static_new_payload_response.lock() = None
}
pub fn insert_pow_block(
&self,
block_number: u64,
block_hash: Hash256,
parent_hash: Hash256,
block_hash: ExecutionBlockHash,
parent_hash: ExecutionBlockHash,
total_difficulty: Uint256,
) {
let block = Block::PoW(PoWBlock {
@@ -143,7 +182,7 @@ impl<T: EthSpec> MockServer<T> {
.unwrap()
}
pub fn get_block(&self, block_hash: Hash256) -> Option<Block<T>> {
pub fn get_block(&self, block_hash: ExecutionBlockHash) -> Option<Block<T>> {
self.ctx
.execution_block_generator
.read()
@@ -178,6 +217,12 @@ struct MissingIdField;
impl warp::reject::Reject for MissingIdField {}
#[derive(Debug, Clone, PartialEq)]
pub struct StaticNewPayloadResponse {
status: PayloadStatusV1,
should_import: bool,
}
/// A wrapper around all the items required to spawn the HTTP server.
///
/// The server will gracefully handle the case where any fields are `None`.
@@ -187,7 +232,7 @@ pub struct Context<T: EthSpec> {
pub last_echo_request: Arc<RwLock<Option<Bytes>>>,
pub execution_block_generator: RwLock<ExecutionBlockGenerator<T>>,
pub preloaded_responses: Arc<Mutex<Vec<serde_json::Value>>>,
pub static_execute_payload_response: Arc<Mutex<Option<ExecutePayloadResponseStatus>>>,
pub static_new_payload_response: Arc<Mutex<Option<StaticNewPayloadResponse>>>,
pub _phantom: PhantomData<T>,
}