Skip to content

Commit

Permalink
Start writing headers/block_id endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
paulhauner committed Sep 5, 2020
1 parent b4e9438 commit bd3d306
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
30 changes: 30 additions & 0 deletions beacon_node/http_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,35 @@ pub fn serve<T: BeaconChainTypes>(
},
);

// beacon/headers/{block_id}
let beacon_headers_block_id = base_path
.and(warp::path("beacon"))
.and(warp::path("headers"))
.and(warp::path::param::<BlockId>())
.and(chain_filter.clone())
.and_then(|block_id: BlockId, chain: Arc<BeaconChain<T>>| {
blocking_json_task(move || {
let root = block_id.root(&chain)?;
let block = BlockId::from_root(root).block(&chain)?;

let canonical = chain
.block_root_at_slot(block.slot())
.map_err(crate::reject::beacon_chain_error)?
.map_or(false, |canonical| root == canonical);

let data = api_types::BlockHeaderData {
root,
canonical,
header: api_types::BlockHeaderAndSignature {
message: block.message.block_header(),
signature: block.signature.into(),
},
};

Ok(api_types::GenericResponse::from(data))
})
});

/*
* beacon/blocks/{block_id}
*/
Expand Down Expand Up @@ -390,6 +419,7 @@ pub fn serve<T: BeaconChainTypes>(
.or(beacon_state_validators_id)
.or(beacon_state_committees)
.or(beacon_headers)
.or(beacon_headers_block_id)
.or(beacon_block_root)
.recover(crate::reject::handle_rejection);

Expand Down
18 changes: 18 additions & 0 deletions common/eth2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,24 @@ impl BeaconNodeClient {
self.get_opt(path).await
}

/// `GET beacon/headers/{block_id}`
///
/// Returns `Ok(None)` on a 404 error.
pub async fn beacon_headers_block_id(
&self,
block_id: BlockId,
) -> Result<Option<GenericResponse<Vec<BlockHeaderData>>>, Error> {
let mut path = self.server.clone();

path.path_segments_mut()
.expect("path is base")
.push("beacon")
.push("headers")
.push(&block_id.to_string());

self.get_opt(path).await
}

/// `GET beacon/blocks/{block_id}/root`
///
/// Returns `Ok(None)` on a 404 error.
Expand Down

0 comments on commit bd3d306

Please sign in to comment.