From 74a25cebdbc8f1b488bb5c5655da714510b8671c Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Fri, 24 Sep 2021 16:13:24 +1000 Subject: [PATCH] Finish adding tests --- beacon_node/execution_layer/src/engine_api.rs | 4 +- .../execution_layer/src/engine_api/http.rs | 146 +++++++++++++++++- consensus/types/src/lib.rs | 4 +- 3 files changed, 146 insertions(+), 8 deletions(-) diff --git a/beacon_node/execution_layer/src/engine_api.rs b/beacon_node/execution_layer/src/engine_api.rs index e86265835a..2892d62412 100644 --- a/beacon_node/execution_layer/src/engine_api.rs +++ b/beacon_node/execution_layer/src/engine_api.rs @@ -69,7 +69,7 @@ pub trait EngineApi { } #[derive(Debug, PartialEq, Serialize, Deserialize)] -#[serde(rename = "SCREAMING_SNAKE_CASE")] +#[serde(rename_all = "SCREAMING_SNAKE_CASE")] pub enum ExecutePayloadResponse { Valid, Invalid, @@ -77,7 +77,7 @@ pub enum ExecutePayloadResponse { } #[derive(Debug, PartialEq, Serialize, Deserialize)] -#[serde(rename = "SCREAMING_SNAKE_CASE")] +#[serde(rename_all = "SCREAMING_SNAKE_CASE")] pub enum ConsensusStatus { Valid, Invalid, diff --git a/beacon_node/execution_layer/src/engine_api/http.rs b/beacon_node/execution_layer/src/engine_api/http.rs index 99ded08f1a..c7674a882c 100644 --- a/beacon_node/execution_layer/src/engine_api/http.rs +++ b/beacon_node/execution_layer/src/engine_api/http.rs @@ -116,7 +116,7 @@ impl EngineApi for HttpJsonRpc { fee_recipient }]); - let response: JsonPreparePayloadResponse = self + let response: JsonPayloadId = self .rpc_request( ENGINE_PREPARE_PAYLOAD, params, @@ -145,7 +145,7 @@ impl EngineApi for HttpJsonRpc { &self, payload_id: PayloadId, ) -> Result, Error> { - let params = json!([payload_id]); + let params = json!([JsonPayloadId { payload_id }]); self.rpc_request(ENGINE_GET_PAYLOAD, params, ENGINE_GET_PAYLOAD_TIMEOUT) .await @@ -215,13 +215,13 @@ struct JsonPreparePayloadRequest { #[derive(Debug, PartialEq, Serialize, Deserialize)] #[serde(transparent, rename_all = "camelCase")] -struct JsonPreparePayloadResponse { +struct JsonPayloadId { #[serde(with = "eth2_serde_utils::u64_hex_be")] payload_id: u64, } #[derive(Debug, PartialEq, Serialize, Deserialize)] -#[serde(bound = "T: EthSpec")] +#[serde(bound = "T: EthSpec", rename_all = "camelCase")] pub struct JsonExecutionPayload { pub parent_hash: Hash256, pub coinbase: Address, @@ -239,6 +239,7 @@ pub struct JsonExecutionPayload { #[serde(with = "eth2_serde_utils::u64_hex_be")] pub timestamp: u64, pub base_fee_per_gas: Hash256, + // FIXME(paul): add extraData pub block_hash: Hash256, // FIXME(paul): add transaction parsing. #[serde(default)] @@ -351,6 +352,143 @@ mod test { const HASH_00: &str = "0x0000000000000000000000000000000000000000000000000000000000000000"; const HASH_01: &str = "0x0101010101010101010101010101010101010101010101010101010101010101"; + const ADDRESS_00: &str = "0x0000000000000000000000000000000000000000"; + const ADDRESS_01: &str = "0x0101010101010101010101010101010101010101"; + + const LOGS_BLOOM_01: &str = "0x01010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101"; + + #[tokio::test] + async fn prepare_payload_request() { + Tester::new() + .assert_request_equals( + |client| async move { + let _ = client + .prepare_payload( + Hash256::repeat_byte(0), + 42, + Hash256::repeat_byte(1), + Address::repeat_byte(0), + ) + .await; + }, + json!({ + "id": STATIC_ID, + "jsonrpc": JSONRPC_VERSION, + "method": ENGINE_PREPARE_PAYLOAD, + "params": [{ + "parentHash": HASH_00, + "timestamp": "0x2a", + "random": HASH_01, + "feeRecipient": ADDRESS_00, + }] + }), + ) + .await; + } + + #[tokio::test] + async fn get_payload_request() { + Tester::new() + .assert_request_equals( + |client| async move { + let _ = client.get_payload::(42).await; + }, + json!({ + "id": STATIC_ID, + "jsonrpc": JSONRPC_VERSION, + "method": ENGINE_GET_PAYLOAD, + "params": ["0x2a"] + }), + ) + .await; + } + + #[tokio::test] + async fn execute_payload_request() { + Tester::new() + .assert_request_equals( + |client| async move { + let _ = client + .execute_payload::(ExecutionPayload { + parent_hash: Hash256::repeat_byte(0), + coinbase: Address::repeat_byte(1), + state_root: Hash256::repeat_byte(1), + receipt_root: Hash256::repeat_byte(0), + logs_bloom: vec![01; 256].into(), + random: Hash256::repeat_byte(1), + block_number: 0, + gas_limit: 1, + gas_used: 2, + timestamp: 42, + base_fee_per_gas: Hash256::repeat_byte(0), + block_hash: Hash256::repeat_byte(1), + transactions: vec![].into(), + }) + .await; + }, + json!({ + "id": STATIC_ID, + "jsonrpc": JSONRPC_VERSION, + "method": ENGINE_EXECUTE_PAYLOAD, + "params": [{ + "parentHash": HASH_00, + "coinbase": ADDRESS_01, + "stateRoot": HASH_01, + "receiptRoot": HASH_00, + "logsBloom": LOGS_BLOOM_01, + "random": HASH_01, + "blockNumber": "0x0", + "gasLimit": "0x1", + "gasUsed": "0x2", + "timestamp": "0x2a", + "baseFeePerGas": HASH_00, + "blockHash": HASH_01, + "transactions": [], + }] + }), + ) + .await; + } + + #[tokio::test] + async fn consensus_validated_request() { + Tester::new() + .assert_request_equals( + |client| async move { + let _ = client + .consensus_validated(Hash256::repeat_byte(0), ConsensusStatus::Valid) + .await; + }, + json!({ + "id": STATIC_ID, + "jsonrpc": JSONRPC_VERSION, + "method": ENGINE_CONSENSUS_VALIDATED, + "params": [{ + "blockHash": HASH_00, + "status": "VALID", + }] + }), + ) + .await + .assert_request_equals( + |client| async move { + let _ = client + .consensus_validated(Hash256::repeat_byte(1), ConsensusStatus::Invalid) + .await; + }, + json!({ + "id": STATIC_ID, + "jsonrpc": JSONRPC_VERSION, + "method": ENGINE_CONSENSUS_VALIDATED, + "params": [{ + "blockHash": HASH_01, + "status": "INVALID", + }] + }), + ) + .await; + } + #[tokio::test] async fn forkchoice_updated_request() { Tester::new() diff --git a/consensus/types/src/lib.rs b/consensus/types/src/lib.rs index 0172995eeb..0c3c6b9256 100644 --- a/consensus/types/src/lib.rs +++ b/consensus/types/src/lib.rs @@ -60,7 +60,7 @@ pub mod signed_voluntary_exit; pub mod signing_data; pub mod sync_committee_subscription; pub mod sync_duty; -pub mod transition_store; +// pub mod transition_store; pub mod validator; pub mod validator_subscription; pub mod voluntary_exit; @@ -153,7 +153,7 @@ pub use crate::sync_committee_subscription::SyncCommitteeSubscription; pub use crate::sync_duty::SyncDuty; pub use crate::sync_selection_proof::SyncSelectionProof; pub use crate::sync_subnet_id::SyncSubnetId; -pub use crate::transition_store::TransitionStore; +// pub use crate::transition_store::TransitionStore; pub use crate::validator::Validator; pub use crate::validator_subscription::ValidatorSubscription; pub use crate::voluntary_exit::VoluntaryExit;