Skip to content

Commit

Permalink
Add all routes to mock server
Browse files Browse the repository at this point in the history
  • Loading branch information
paulhauner committed Sep 29, 2021
1 parent 57f6a9b commit 3a73cde
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 10 deletions.
4 changes: 2 additions & 2 deletions beacon_node/execution_layer/src/engine_api/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,9 @@ pub struct JsonPreparePayloadRequest {

#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(transparent, rename_all = "camelCase")]
struct JsonPayloadId {
pub struct JsonPayloadId {
#[serde(with = "eth2_serde_utils::u64_hex_be")]
payload_id: u64,
pub payload_id: u64,
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::engine_api::{http::JsonPreparePayloadRequest, ExecutePayloadResponse, ExecutionBlock};
use crate::engine_api::{
http::JsonPreparePayloadRequest, ConsensusStatus, ExecutePayloadResponse, ExecutionBlock,
};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use tree_hash::TreeHash;
Expand Down Expand Up @@ -186,17 +188,26 @@ impl<T: EthSpec> ExecutionBlockGenerator<T> {

block.block_hash = block.tree_hash_root();

self.insert_block(Block::PoW(block))
}

pub fn insert_block(&mut self, block: Block<T>) -> Result<(), String> {
if self.blocks.contains_key(&block.block_hash()) {
return Err(format!("{:?} is already known", block.block_hash()));
} else if block.parent_hash() != Hash256::zero()
&& !self.blocks.contains_key(&block.parent_hash())
{
return Err(format!("parent block {:?} is unknown", block.parent_hash()));
}

self.block_hashes
.insert(block.block_number, block.block_hash);
self.blocks.insert(block.block_hash, Block::PoW(block));
.insert(block.block_number(), block.block_hash());
self.blocks.insert(block.block_hash(), block);

Ok(())
}

pub fn prepare_payload_id(
&mut self,
payload: JsonPreparePayloadRequest,
) -> Result<u64, String> {
pub fn prepare_payload(&mut self, payload: JsonPreparePayloadRequest) -> Result<u64, String> {
if !self
.blocks
.iter()
Expand Down Expand Up @@ -256,6 +267,22 @@ impl<T: EthSpec> ExecutionBlockGenerator<T> {

ExecutePayloadResponse::Valid
}

pub fn consensus_validated(
&mut self,
block_hash: Hash256,
status: ConsensusStatus,
) -> Result<(), String> {
let payload = self
.pending_payloads
.remove(&block_hash)
.ok_or_else(|| format!("no pending payload for {:?}", block_hash))?;

match status {
ConsensusStatus::Valid => self.insert_block(Block::PoS(payload)),
ConsensusStatus::Invalid => Ok(()),
}
}
}

#[cfg(test)]
Expand All @@ -273,7 +300,9 @@ mod test {
ExecutionBlockGenerator::new(TERMINAL_DIFFICULTY, TERMINAL_BLOCK);

for i in 0..=TERMINAL_BLOCK {
generator.insert_pow_block(i).unwrap();
if i > 0 {
generator.insert_pow_block(i).unwrap();
}

/*
* Generate a block, inspect it.
Expand Down
45 changes: 45 additions & 0 deletions beacon_node/execution_layer/src/test_utils/handle_rpc.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
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;
Expand Down Expand Up @@ -54,6 +55,40 @@ pub async fn handle_rpc<T: EthSpec>(
)
.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 => Ok(JsonValue::Null),
ENGINE_FORKCHOICE_UPDATED => Ok(JsonValue::Null),
other => Err(format!(
Expand All @@ -62,3 +97,13 @@ pub async fn handle_rpc<T: EthSpec>(
)),
}
}

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))
})
}

0 comments on commit 3a73cde

Please sign in to comment.