diff --git a/Cargo.lock b/Cargo.lock index ee5ad6a78b3..2b0771b1e20 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2299,6 +2299,7 @@ version = "0.1.0" dependencies = [ "beacon_chain", "environment", + "eth1", "eth2", "eth2_libp2p", "hex 0.4.2", diff --git a/beacon_node/eth1/src/http.rs b/beacon_node/eth1/src/http.rs index 6dffdaa7c6d..e8f7d23a026 100644 --- a/beacon_node/eth1/src/http.rs +++ b/beacon_node/eth1/src/http.rs @@ -39,19 +39,34 @@ pub enum Eth1NetworkId { Custom(u64), } +impl Into for Eth1NetworkId { + fn into(self) -> u64 { + match self { + Eth1NetworkId::Mainnet => 1, + Eth1NetworkId::Goerli => 5, + Eth1NetworkId::Custom(id) => id, + } + } +} + +impl From for Eth1NetworkId { + fn from(id: u64) -> Self { + let into = |x: Eth1NetworkId| -> u64 { x.into() }; + match id { + id if id == into(Eth1NetworkId::Mainnet) => Eth1NetworkId::Mainnet, + id if id == into(Eth1NetworkId::Goerli) => Eth1NetworkId::Goerli, + id => Eth1NetworkId::Custom(id), + } + } +} + impl FromStr for Eth1NetworkId { type Err = String; fn from_str(s: &str) -> Result { - match s { - "1" => Ok(Eth1NetworkId::Mainnet), - "5" => Ok(Eth1NetworkId::Goerli), - custom => { - let network_id = u64::from_str_radix(custom, 10) - .map_err(|e| format!("Failed to parse eth1 network id {}", e))?; - Ok(Eth1NetworkId::Custom(network_id)) - } - } + u64::from_str_radix(s, 10) + .map(Into::into) + .map_err(|e| format!("Failed to parse eth1 network id {}", e)) } } diff --git a/beacon_node/eth1/src/lib.rs b/beacon_node/eth1/src/lib.rs index f5f018bd17b..a7aba85a28a 100644 --- a/beacon_node/eth1/src/lib.rs +++ b/beacon_node/eth1/src/lib.rs @@ -13,4 +13,6 @@ pub use block_cache::{BlockCache, Eth1Block}; pub use deposit_cache::DepositCache; pub use deposit_log::DepositLog; pub use inner::SszEth1Cache; -pub use service::{BlockCacheUpdateOutcome, Config, DepositCacheUpdateOutcome, Error, Service}; +pub use service::{ + BlockCacheUpdateOutcome, Config, DepositCacheUpdateOutcome, Error, Service, DEFAULT_NETWORK_ID, +}; diff --git a/beacon_node/http_api/Cargo.toml b/beacon_node/http_api/Cargo.toml index d5d59dbb4c6..7f7a7ed8690 100644 --- a/beacon_node/http_api/Cargo.toml +++ b/beacon_node/http_api/Cargo.toml @@ -18,6 +18,7 @@ eth2 = { path = "../../common/eth2" } slog = "2.5.2" network = { path = "../network" } eth2_libp2p = { path = "../eth2_libp2p" } +eth1 = { path = "../eth1" } [dev-dependencies] store = { path = "../store" } diff --git a/beacon_node/http_api/src/lib.rs b/beacon_node/http_api/src/lib.rs index 5ec4034f4b3..7462148e128 100644 --- a/beacon_node/http_api/src/lib.rs +++ b/beacon_node/http_api/src/lib.rs @@ -792,26 +792,13 @@ pub fn serve( .and(warp::path::end()) .and(chain_filter.clone()) .and_then(|chain: Arc>| { - blocking_json_task(move || match chain.eth1_chain.as_ref() { - Some(eth1) => { - let address = eth1.deposit_contract_address().parse().map_err(|e| { - crate::reject::custom_server_error(format!( - "internal contract address is invalid: {:?}", - e - )) - })?; - - Ok(api_types::GenericResponse::from( - api_types::DepositContractData { - address, - chain_id: eth1.deposit_contract_chain_id(), - }, - )) - } - // TODO: figure out how to return the real value here. - None => Err(crate::reject::custom_not_found( - "node is not syncing the eth1 chain".to_string(), - )), + blocking_json_task(move || { + Ok(api_types::GenericResponse::from( + api_types::DepositContractData { + address: chain.spec.deposit_contract_address, + chain_id: eth1::DEFAULT_NETWORK_ID.into(), + }, + )) }) }); diff --git a/beacon_node/http_api/tests/tests.rs b/beacon_node/http_api/tests/tests.rs index 19fd832cc65..9f4627d3e76 100644 --- a/beacon_node/http_api/tests/tests.rs +++ b/beacon_node/http_api/tests/tests.rs @@ -938,6 +938,11 @@ impl ApiTester { .unwrap() .data; + let expected = DepositContractData { + address: self.chain.spec.deposit_contract_address, + chain_id: eth1::DEFAULT_NETWORK_ID.into(), + }; + assert_eq!(result, expected); self @@ -1099,5 +1104,7 @@ async fn config_get() { .test_get_config_fork_schedule() .await .test_get_config_spec() + .await + .test_get_deposit_contract() .await; } diff --git a/beacon_node/src/config.rs b/beacon_node/src/config.rs index 395ec32e9cc..cd26fac1a60 100644 --- a/beacon_node/src/config.rs +++ b/beacon_node/src/config.rs @@ -236,6 +236,10 @@ pub fn get_config( client_config.eth1.deposit_contract_address = format!("{:?}", eth2_testnet_config.deposit_contract_address()?); + if client_config.eth1.deposit_contract_address != spec.deposit_contract_address { + return Error("Testnet contract address does not match spec".into()); + } + client_config.eth1.deposit_contract_deploy_block = eth2_testnet_config.deposit_contract_deploy_block; client_config.eth1.lowest_cached_block_number = diff --git a/common/eth2/src/types.rs b/common/eth2/src/types.rs index 30d0467ca04..bcebfd20b7a 100644 --- a/common/eth2/src/types.rs +++ b/common/eth2/src/types.rs @@ -295,6 +295,7 @@ pub struct BlockHeaderData { #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct DepositContractData { - pub chain_id: U256, + #[serde(with = "serde_utils::quoted")] + pub chain_id: u64, pub address: Address, } diff --git a/consensus/types/src/chain_spec.rs b/consensus/types/src/chain_spec.rs index d79002c461e..8fca05c44ea 100644 --- a/consensus/types/src/chain_spec.rs +++ b/consensus/types/src/chain_spec.rs @@ -107,6 +107,7 @@ pub struct ChainSpec { */ pub eth1_follow_distance: u64, pub seconds_per_eth1_block: u64, + pub deposit_contract_address: Address, /* * Networking @@ -317,6 +318,9 @@ impl ChainSpec { */ eth1_follow_distance: 1_024, seconds_per_eth1_block: 14, + deposit_contract_address: "1234567890123456789012345678901234567890" + .parse() + .expect("chain spec deposit contract address"), /* * Network specific @@ -517,6 +521,7 @@ pub struct YamlConfig { random_subnets_per_validator: u64, epochs_per_random_subnet_subscription: u64, seconds_per_eth1_block: u64, + deposit_contract_address: Address, } impl Default for YamlConfig { @@ -597,6 +602,7 @@ impl YamlConfig { random_subnets_per_validator: spec.random_subnets_per_validator, epochs_per_random_subnet_subscription: spec.epochs_per_random_subnet_subscription, seconds_per_eth1_block: spec.seconds_per_eth1_block, + deposit_contract_address: spec.deposit_contract_address, } } @@ -671,6 +677,7 @@ impl YamlConfig { boot_nodes: chain_spec.boot_nodes.clone(), genesis_fork_version: self.genesis_fork_version, eth1_follow_distance: self.eth1_follow_distance, + deposit_contract_address: self.deposit_contract_address, ..*chain_spec }) }