[Merge] Implement execution_layer (#2635)

* Checkout serde_utils from rayonism

* Make eth1::http functions pub

* Add bones of execution_layer

* Modify decoding

* Expose Transaction, cargo fmt

* Add executePayload

* Add all minimal spec endpoints

* Start adding json rpc wrapper

* Finish custom JSON response handler

* Switch to new rpc sending method

* Add first test

* Fix camelCase

* Finish adding tests

* Begin threading execution layer into BeaconChain

* Fix clippy lints

* Fix clippy lints

* Thread execution layer into ClientBuilder

* Add CLI flags

* Add block processing methods to ExecutionLayer

* Add block_on to execution_layer

* Integrate execute_payload

* Add extra_data field

* Begin implementing payload handle

* Send consensus valid/invalid messages

* Fix minor type in task_executor

* Call forkchoiceUpdated

* Add search for TTD block

* Thread TTD into execution layer

* Allow producing block with execution payload

* Add LRU cache for execution blocks

* Remove duplicate 0x on ssz_types serialization

* Add tests for block getter methods

* Add basic block generator impl

* Add is_valid_terminal_block to EL

* Verify merge block in block_verification

* Partially implement --terminal-block-hash-override

* Add terminal_block_hash to ChainSpec

* Remove Option from terminal_block_hash in EL

* Revert merge changes to consensus/fork_choice

* Remove commented-out code

* Add bones for handling RPC methods on test server

* Add first ExecutionLayer tests

* Add testing for finding terminal block

* Prevent infinite loops

* Add insert_merge_block to block gen

* Add block gen test for pos blocks

* Start adding payloads to block gen

* Fix clippy lints

* Add execution payload to block gen

* Add execute_payload to block_gen

* Refactor block gen

* Add all routes to mock server

* Use Uint256 for base_fee_per_gas

* Add working execution chain build

* Remove unused var

* Revert "Use Uint256 for base_fee_per_gas"

This reverts commit 6c88f19ac4.

* Fix base_fee_for_gas Uint256

* Update execute payload handle

* Improve testing, fix bugs

* Fix default fee-recipient

* Fix fee-recipient address (again)

* Add check for terminal block, add comments, tidy

* Apply suggestions from code review

Co-authored-by: realbigsean <seananderson33@GMAIL.com>

* Fix is_none on handle Drop

* Remove commented-out tests

Co-authored-by: realbigsean <seananderson33@GMAIL.com>
This commit is contained in:
Paul Hauner
2021-09-30 08:14:15 +10:00
parent 1563bce905
commit d8623cfc4f
38 changed files with 3239 additions and 114 deletions

View File

@@ -0,0 +1,125 @@
use super::Context;
use crate::engine_api::http::*;
use serde::de::DeserializeOwned;
use serde_json::Value as JsonValue;
use std::sync::Arc;
use types::EthSpec;
pub async fn handle_rpc<T: EthSpec>(
body: JsonValue,
ctx: Arc<Context<T>>,
) -> Result<JsonValue, String> {
let method = body
.get("method")
.and_then(JsonValue::as_str)
.ok_or_else(|| "missing/invalid method field".to_string())?;
let params = body
.get("params")
.ok_or_else(|| "missing/invalid params field".to_string())?;
match method {
ETH_SYNCING => Ok(JsonValue::Bool(false)),
ETH_GET_BLOCK_BY_NUMBER => {
let tag = params
.get(0)
.and_then(JsonValue::as_str)
.ok_or_else(|| "missing/invalid params[0] value".to_string())?;
match tag {
"latest" => Ok(serde_json::to_value(
ctx.execution_block_generator
.read()
.await
.latest_execution_block(),
)
.unwrap()),
other => Err(format!("The tag {} is not supported", other)),
}
}
ETH_GET_BLOCK_BY_HASH => {
let hash = params
.get(0)
.and_then(JsonValue::as_str)
.ok_or_else(|| "missing/invalid params[0] value".to_string())
.and_then(|s| {
s.parse()
.map_err(|e| format!("unable to parse hash: {:?}", e))
})?;
Ok(serde_json::to_value(
ctx.execution_block_generator
.read()
.await
.execution_block_by_hash(hash),
)
.unwrap())
}
ENGINE_PREPARE_PAYLOAD => {
let request = get_param_0(params)?;
let payload_id = ctx
.execution_block_generator
.write()
.await
.prepare_payload(request)?;
Ok(serde_json::to_value(JsonPayloadId { payload_id }).unwrap())
}
ENGINE_EXECUTE_PAYLOAD => {
let request: JsonExecutionPayload<T> = get_param_0(params)?;
let response = ctx
.execution_block_generator
.write()
.await
.execute_payload(request.into());
Ok(serde_json::to_value(response).unwrap())
}
ENGINE_GET_PAYLOAD => {
let request: JsonPayloadId = get_param_0(params)?;
let id = request.payload_id;
let response = ctx
.execution_block_generator
.write()
.await
.get_payload(id)
.ok_or_else(|| format!("no payload for id {}", id))?;
Ok(serde_json::to_value(JsonExecutionPayload::from(response)).unwrap())
}
ENGINE_CONSENSUS_VALIDATED => {
let request: JsonConsensusValidatedRequest = get_param_0(params)?;
ctx.execution_block_generator
.write()
.await
.consensus_validated(request.block_hash, request.status)?;
Ok(JsonValue::Null)
}
ENGINE_FORKCHOICE_UPDATED => {
let request: JsonForkChoiceUpdatedRequest = get_param_0(params)?;
ctx.execution_block_generator
.write()
.await
.forkchoice_updated(request.head_block_hash, request.finalized_block_hash)?;
Ok(JsonValue::Null)
}
other => Err(format!(
"The method {} does not exist/is not available",
other
)),
}
}
fn get_param_0<T: DeserializeOwned>(params: &JsonValue) -> Result<T, String> {
params
.get(0)
.ok_or_else(|| "missing/invalid params[0] value".to_string())
.and_then(|param| {
serde_json::from_value(param.clone())
.map_err(|e| format!("failed to deserialize param[0]: {:?}", e))
})
}