Skip to content

Commit

Permalink
Finish deposit contract endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
paulhauner committed Sep 7, 2020
1 parent 0046ec5 commit 95c6564
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 31 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 24 additions & 9 deletions beacon_node/eth1/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,34 @@ pub enum Eth1NetworkId {
Custom(u64),
}

impl Into<u64> for Eth1NetworkId {
fn into(self) -> u64 {
match self {
Eth1NetworkId::Mainnet => 1,
Eth1NetworkId::Goerli => 5,
Eth1NetworkId::Custom(id) => id,
}
}
}

impl From<u64> 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<Self, Self::Err> {
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))
}
}

Expand Down
4 changes: 3 additions & 1 deletion beacon_node/eth1/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
1 change: 1 addition & 0 deletions beacon_node/http_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
27 changes: 7 additions & 20 deletions beacon_node/http_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -792,26 +792,13 @@ pub fn serve<T: BeaconChainTypes>(
.and(warp::path::end())
.and(chain_filter.clone())
.and_then(|chain: Arc<BeaconChain<T>>| {
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(),
},
))
})
});

Expand Down
7 changes: 7 additions & 0 deletions beacon_node/http_api/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -1099,5 +1104,7 @@ async fn config_get() {
.test_get_config_fork_schedule()
.await
.test_get_config_spec()
.await
.test_get_deposit_contract()
.await;
}
4 changes: 4 additions & 0 deletions beacon_node/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,10 @@ pub fn get_config<E: EthSpec>(

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 =
Expand Down
3 changes: 2 additions & 1 deletion common/eth2/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
7 changes: 7 additions & 0 deletions consensus/types/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ pub struct ChainSpec {
*/
pub eth1_follow_distance: u64,
pub seconds_per_eth1_block: u64,
pub deposit_contract_address: Address,

/*
* Networking
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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
})
}
Expand Down

0 comments on commit 95c6564

Please sign in to comment.