-
Notifications
You must be signed in to change notification settings - Fork 784
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2627463
commit 49ac075
Showing
11 changed files
with
572 additions
and
4 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[package] | ||
name = "http_api" | ||
version = "0.1.0" | ||
authors = ["Paul Hauner <paul@paulhauner.com>"] | ||
edition = "2018" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
warp = "0.2.4" | ||
serde = { version = "1.0.110", features = ["derive"] } | ||
tokio = { version = "0.2.21", features = ["sync"] } | ||
parking_lot = "0.11.0" | ||
types = { path = "../../consensus/types" } | ||
hex = "0.4.2" | ||
beacon_chain = { path = "../beacon_chain" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use beacon_chain::{BeaconChain, BeaconChainTypes}; | ||
use std::str::FromStr; | ||
use types::{Hash256, Slot}; | ||
|
||
#[derive(Debug)] | ||
pub enum BlockId { | ||
Head, | ||
Genesis, | ||
Finalized, | ||
Justified, | ||
Slot(Slot), | ||
Root(Hash256), | ||
} | ||
|
||
impl BlockId { | ||
pub fn root<T: BeaconChainTypes>( | ||
&self, | ||
chain: &BeaconChain<T>, | ||
) -> Result<Hash256, warp::Rejection> { | ||
match self { | ||
BlockId::Head => chain | ||
.head_info() | ||
.map(|head| head.block_root) | ||
.map_err(crate::reject::beacon_chain_error), | ||
BlockId::Genesis => Ok(chain.genesis_block_root), | ||
BlockId::Finalized => chain | ||
.head_info() | ||
.map(|head| head.finalized_checkpoint.root) | ||
.map_err(crate::reject::beacon_chain_error), | ||
BlockId::Justified => chain | ||
.head_info() | ||
.map(|head| head.current_justified_checkpoint.root) | ||
.map_err(crate::reject::beacon_chain_error), | ||
BlockId::Slot(slot) => chain | ||
.block_root_at_slot(*slot) | ||
.map_err(crate::reject::beacon_chain_error) | ||
.and_then(|root_opt| root_opt.ok_or_else(|| warp::reject::not_found())), | ||
BlockId::Root(root) => Ok(*root), | ||
} | ||
} | ||
} | ||
|
||
impl FromStr for BlockId { | ||
type Err = String; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
match s { | ||
"head" => Ok(BlockId::Head), | ||
"genesis" => Ok(BlockId::Genesis), | ||
"finalized" => Ok(BlockId::Finalized), | ||
"justified" => Ok(BlockId::Justified), | ||
other => { | ||
if other.starts_with("0x") { | ||
Hash256::from_str(s) | ||
.map(BlockId::Root) | ||
.map_err(|e| format!("{} cannot be parsed as a root", e)) | ||
} else { | ||
u64::from_str(s) | ||
.map(Slot::new) | ||
.map(BlockId::Slot) | ||
.map_err(|_| format!("{} cannot be parsed as a parameter", s)) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
mod block_id; | ||
mod reject; | ||
mod state_id; | ||
|
||
use beacon_chain::{BeaconChain, BeaconChainTypes}; | ||
use block_id::BlockId; | ||
use state_id::StateId; | ||
use std::sync::Arc; | ||
use warp::Filter; | ||
|
||
const API_PREFIX: &str = "eth"; | ||
const API_VERSION: &str = "v1"; | ||
|
||
pub struct Context<T: BeaconChainTypes> { | ||
pub chain: Option<Arc<BeaconChain<T>>>, | ||
} | ||
|
||
pub async fn serve<T: BeaconChainTypes>(ctx: Arc<Context<T>>) { | ||
let base_path = warp::path(API_PREFIX).and(warp::path(API_VERSION)); | ||
let chain_filter = warp::any() | ||
.map(move || ctx.chain.clone()) | ||
.and_then(|chain| async move { | ||
match chain { | ||
Some(chain) => Ok(chain), | ||
None => Err(warp::reject::not_found()), | ||
} | ||
}); | ||
|
||
/* | ||
* beacon/states | ||
*/ | ||
|
||
let beacon_states_path = base_path | ||
.and(warp::path("beacon")) | ||
.and(warp::path("states")) | ||
.and(warp::path::param::<StateId>()) | ||
.and(chain_filter.clone()); | ||
|
||
let beacon_state_root = beacon_states_path | ||
.clone() | ||
.and(warp::path("root")) | ||
.and(warp::path::end()) | ||
.and_then(|state_id: StateId, chain: Arc<BeaconChain<T>>| async move { | ||
state_id.root(&chain).map(|resp| warp::reply::json(&resp)) | ||
}); | ||
|
||
let beacon_state_fork = beacon_states_path | ||
.clone() | ||
.and(warp::path("fork")) | ||
.and(warp::path::end()) | ||
.and_then(|state_id: StateId, chain: Arc<BeaconChain<T>>| async move { | ||
state_id.root(&chain).map(|resp| warp::reply::json(&resp)) | ||
}); | ||
|
||
/* | ||
* beacon/blocks | ||
*/ | ||
|
||
let beacon_blocks_path = base_path | ||
.and(warp::path("beacon")) | ||
.and(warp::path("blocks")) | ||
.and(warp::path::param::<BlockId>()) | ||
.and(chain_filter.clone()); | ||
|
||
let beacon_block_root = beacon_blocks_path | ||
.clone() | ||
.and(warp::path("root")) | ||
.and(warp::path::end()) | ||
.and_then(|block_id: BlockId, chain: Arc<BeaconChain<T>>| async move { | ||
block_id.root(&chain).map(|resp| warp::reply::json(&resp)) | ||
}); | ||
|
||
let routes = beacon_state_root | ||
.or(beacon_state_fork) | ||
.or(beacon_block_root); | ||
|
||
warp::serve(routes).run(([127, 0, 0, 1], 3030)).await; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
use warp::reject::Reject; | ||
|
||
pub fn beacon_chain_error(e: beacon_chain::BeaconChainError) -> warp::reject::Rejection { | ||
warp::reject::custom(BeaconChainError(e)) | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct BeaconChainError(pub beacon_chain::BeaconChainError); | ||
|
||
impl Reject for BeaconChainError {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use beacon_chain::{BeaconChain, BeaconChainTypes}; | ||
use std::str::FromStr; | ||
use types::{Hash256, Slot}; | ||
|
||
#[derive(Debug)] | ||
pub enum StateId { | ||
Head, | ||
Genesis, | ||
Finalized, | ||
Justified, | ||
Slot(Slot), | ||
Root(Hash256), | ||
} | ||
|
||
impl StateId { | ||
pub fn root<T: BeaconChainTypes>( | ||
&self, | ||
chain: &BeaconChain<T>, | ||
) -> Result<Hash256, warp::Rejection> { | ||
match self { | ||
StateId::Head => chain | ||
.head_info() | ||
.map(|head| head.state_root) | ||
.map_err(crate::reject::beacon_chain_error), | ||
StateId::Genesis => Ok(chain.genesis_state_root), | ||
StateId::Finalized => todo!(), | ||
StateId::Justified => todo!(), | ||
StateId::Slot(_) => todo!(), | ||
StateId::Root(root) => Ok(*root), | ||
} | ||
} | ||
} | ||
|
||
impl FromStr for StateId { | ||
type Err = String; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
match s { | ||
"head" => Ok(StateId::Head), | ||
"genesis" => Ok(StateId::Genesis), | ||
"finalized" => Ok(StateId::Finalized), | ||
"justified" => Ok(StateId::Justified), | ||
other => { | ||
if other.starts_with("0x") { | ||
Hash256::from_str(s) | ||
.map(StateId::Root) | ||
.map_err(|e| format!("{} cannot be parsed as a root", e)) | ||
} else { | ||
u64::from_str(s) | ||
.map(Slot::new) | ||
.map(StateId::Slot) | ||
.map_err(|_| format!("{} cannot be parsed as a slot", s)) | ||
} | ||
} | ||
} | ||
} | ||
} |