mirror of
https://github.com/sigp/lighthouse.git
synced 2026-05-08 17:26:04 +00:00
Add working tests for publish beacon block
This commit is contained in:
@@ -3,13 +3,13 @@ use crate::response_builder::ResponseBuilder;
|
|||||||
use crate::{ApiError, ApiResult, UrlQuery};
|
use crate::{ApiError, ApiResult, UrlQuery};
|
||||||
use beacon_chain::{BeaconChain, BeaconChainTypes};
|
use beacon_chain::{BeaconChain, BeaconChainTypes};
|
||||||
use hyper::{Body, Request};
|
use hyper::{Body, Request};
|
||||||
use serde::Serialize;
|
use serde::{Deserialize, Serialize};
|
||||||
use ssz_derive::Encode;
|
use ssz_derive::Encode;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use store::Store;
|
use store::Store;
|
||||||
use types::{BeaconBlock, BeaconState, Epoch, EthSpec, Hash256, Slot, Validator};
|
use types::{BeaconBlock, BeaconState, Epoch, EthSpec, Hash256, Slot, Validator};
|
||||||
|
|
||||||
#[derive(Serialize, Encode)]
|
#[derive(Serialize, Deserialize, Encode)]
|
||||||
pub struct HeadResponse {
|
pub struct HeadResponse {
|
||||||
pub slot: Slot,
|
pub slot: Slot,
|
||||||
pub block_root: Hash256,
|
pub block_root: Hash256,
|
||||||
|
|||||||
@@ -1,16 +1,16 @@
|
|||||||
#![cfg(test)]
|
#![cfg(test)]
|
||||||
|
|
||||||
use beacon_chain::{BeaconChain, BeaconChainTypes};
|
use beacon_chain::{BeaconChain, BeaconChainTypes};
|
||||||
use futures::Future;
|
|
||||||
use node_test_rig::{
|
use node_test_rig::{
|
||||||
environment::{Environment, EnvironmentBuilder},
|
environment::{Environment, EnvironmentBuilder},
|
||||||
LocalBeaconNode,
|
LocalBeaconNode,
|
||||||
};
|
};
|
||||||
|
use remote_beacon_node::BeaconBlockPublishStatus;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use tree_hash::TreeHash;
|
use tree_hash::{SignedRoot, TreeHash};
|
||||||
use types::{
|
use types::{
|
||||||
test_utils::generate_deterministic_keypair, ChainSpec, Domain, EthSpec, MinimalEthSpec,
|
test_utils::generate_deterministic_keypair, BeaconBlock, ChainSpec, Domain, EthSpec,
|
||||||
Signature, Slot,
|
MinimalEthSpec, Signature, Slot,
|
||||||
};
|
};
|
||||||
|
|
||||||
type E = MinimalEthSpec;
|
type E = MinimalEthSpec;
|
||||||
@@ -43,7 +43,23 @@ fn get_randao_reveal<T: BeaconChainTypes>(
|
|||||||
Signature::new(&message, domain, &keypair.sk)
|
Signature::new(&message, domain, &keypair.sk)
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/// Signs the given block (assuming the given `beacon_chain` uses deterministic keypairs).
|
||||||
|
fn sign_block<T: BeaconChainTypes>(
|
||||||
|
beacon_chain: Arc<BeaconChain<T>>,
|
||||||
|
block: &mut BeaconBlock<T::EthSpec>,
|
||||||
|
spec: &ChainSpec,
|
||||||
|
) {
|
||||||
|
let fork = beacon_chain.head().beacon_state.fork.clone();
|
||||||
|
let proposer_index = beacon_chain
|
||||||
|
.block_proposer(block.slot)
|
||||||
|
.expect("should get proposer index");
|
||||||
|
let keypair = generate_deterministic_keypair(proposer_index);
|
||||||
|
let epoch = block.slot.epoch(E::slots_per_epoch());
|
||||||
|
let message = block.signed_root();
|
||||||
|
let domain = spec.get_domain(epoch, Domain::BeaconProposer, &fork);
|
||||||
|
block.signature = Signature::new(&message, domain, &keypair.sk);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn validator_block_post() {
|
fn validator_block_post() {
|
||||||
let mut env = build_env();
|
let mut env = build_env();
|
||||||
@@ -61,7 +77,7 @@ fn validator_block_post() {
|
|||||||
let slot = Slot::new(1);
|
let slot = Slot::new(1);
|
||||||
let randao_reveal = get_randao_reveal(beacon_chain.clone(), slot, spec);
|
let randao_reveal = get_randao_reveal(beacon_chain.clone(), slot, spec);
|
||||||
|
|
||||||
let block = env
|
let mut block = env
|
||||||
.runtime()
|
.runtime()
|
||||||
.block_on(
|
.block_on(
|
||||||
remote_node
|
remote_node
|
||||||
@@ -71,18 +87,39 @@ fn validator_block_post() {
|
|||||||
)
|
)
|
||||||
.expect("should fetch block from http api");
|
.expect("should fetch block from http api");
|
||||||
|
|
||||||
assert!(env
|
// Try publishing the block without a signature, ensure it is flagged as invalid.
|
||||||
|
let publish_status = env
|
||||||
.runtime()
|
.runtime()
|
||||||
.block_on(
|
.block_on(remote_node.http.validator().publish_block(block.clone()))
|
||||||
remote_node
|
.expect("should publish block");
|
||||||
.http
|
assert!(
|
||||||
.validator()
|
!publish_status.is_valid(),
|
||||||
.publish_block(block)
|
"the unsigned published block should not be valid"
|
||||||
.and_then(|_| Ok(()))
|
);
|
||||||
)
|
|
||||||
.is_ok());
|
sign_block(beacon_chain.clone(), &mut block, spec);
|
||||||
|
let block_root = block.canonical_root();
|
||||||
|
|
||||||
|
let publish_status = env
|
||||||
|
.runtime()
|
||||||
|
.block_on(remote_node.http.validator().publish_block(block.clone()))
|
||||||
|
.expect("should publish block");
|
||||||
|
assert_eq!(
|
||||||
|
publish_status,
|
||||||
|
BeaconBlockPublishStatus::Valid,
|
||||||
|
"the signed published block should be valid"
|
||||||
|
);
|
||||||
|
|
||||||
|
let head = env
|
||||||
|
.runtime()
|
||||||
|
.block_on(remote_node.http.beacon().get_head())
|
||||||
|
.expect("should get head");
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
head.block_root, block_root,
|
||||||
|
"the published block should become the head block"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn validator_block_get() {
|
fn validator_block_get() {
|
||||||
|
|||||||
@@ -4,8 +4,11 @@
|
|||||||
//! Presently, this is only used for testing but it _could_ become a user-facing library.
|
//! Presently, this is only used for testing but it _could_ become a user-facing library.
|
||||||
|
|
||||||
use futures::{Future, IntoFuture};
|
use futures::{Future, IntoFuture};
|
||||||
use reqwest::r#async::{Client, ClientBuilder, RequestBuilder};
|
use reqwest::{
|
||||||
use serde::Deserialize;
|
r#async::{Client, ClientBuilder, Response},
|
||||||
|
StatusCode,
|
||||||
|
};
|
||||||
|
use serde::{de::DeserializeOwned, Deserialize, Serialize};
|
||||||
use ssz::Encode;
|
use ssz::Encode;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
@@ -14,6 +17,8 @@ use types::{BeaconBlock, BeaconState, EthSpec, Signature};
|
|||||||
use types::{Hash256, Slot};
|
use types::{Hash256, Slot};
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
|
||||||
|
pub use rest_api::HeadResponse;
|
||||||
|
|
||||||
pub const REQUEST_TIMEOUT_SECONDS: u64 = 5;
|
pub const REQUEST_TIMEOUT_SECONDS: u64 = 5;
|
||||||
|
|
||||||
/// Connects to a remote Lighthouse (or compatible) node via HTTP.
|
/// Connects to a remote Lighthouse (or compatible) node via HTTP.
|
||||||
@@ -72,16 +77,51 @@ impl<E: EthSpec> HttpClient<E> {
|
|||||||
self.url.join(path).map_err(|e| e.into())
|
self.url.join(path).map_err(|e| e.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get(&self, path: &str) -> Result<RequestBuilder, Error> {
|
pub fn json_post<T: Serialize>(
|
||||||
// TODO: add timeout
|
&self,
|
||||||
self.url(path)
|
url: Url,
|
||||||
.map(|url| Client::new().get(&url.to_string()))
|
body: T,
|
||||||
|
) -> impl Future<Item = Response, Error = Error> {
|
||||||
|
self.client
|
||||||
|
.post(&url.to_string())
|
||||||
|
.json(&body)
|
||||||
|
.send()
|
||||||
|
.map_err(Error::from)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn post(&self, path: &str) -> Result<RequestBuilder, Error> {
|
pub fn json_get<T: DeserializeOwned>(
|
||||||
// TODO: add timeout
|
&self,
|
||||||
self.url(path)
|
mut url: Url,
|
||||||
.map(|url| Client::new().post(&url.to_string()))
|
query_pairs: Vec<(String, String)>,
|
||||||
|
) -> impl Future<Item = T, Error = Error> {
|
||||||
|
query_pairs.into_iter().for_each(|(key, param)| {
|
||||||
|
url.query_pairs_mut().append_pair(&key, ¶m);
|
||||||
|
});
|
||||||
|
|
||||||
|
self.client
|
||||||
|
.get(&url.to_string())
|
||||||
|
.send()
|
||||||
|
.map_err(Error::from)
|
||||||
|
.and_then(|response| response.error_for_status().map_err(Error::from))
|
||||||
|
.and_then(|mut success| success.json::<T>().map_err(Error::from))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
|
pub enum BeaconBlockPublishStatus {
|
||||||
|
/// The block was valid and has been published to the network.
|
||||||
|
Valid,
|
||||||
|
/// The block was not valid and may or may not have been published to the network.
|
||||||
|
Invalid(String),
|
||||||
|
/// The server responsed with an unknown status code. The block may or may not have been
|
||||||
|
/// published to the network.
|
||||||
|
Unknown,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl BeaconBlockPublishStatus {
|
||||||
|
/// Returns `true` if `*self == BeaconBlockPublishStatus::Valid`.
|
||||||
|
pub fn is_valid(&self) -> bool {
|
||||||
|
*self == BeaconBlockPublishStatus::Valid
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -98,18 +138,28 @@ impl<E: EthSpec> Validator<E> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Posts a block to the beacon node, expecting it to verify it and publish it to the network.
|
/// Posts a block to the beacon node, expecting it to verify it and publish it to the network.
|
||||||
pub fn publish_block(&self, block: BeaconBlock<E>) -> impl Future<Item = (), Error = Error> {
|
pub fn publish_block(
|
||||||
|
&self,
|
||||||
|
block: BeaconBlock<E>,
|
||||||
|
) -> impl Future<Item = BeaconBlockPublishStatus, Error = Error> {
|
||||||
let client = self.0.clone();
|
let client = self.0.clone();
|
||||||
self.url("block")
|
self.url("block")
|
||||||
.into_future()
|
.into_future()
|
||||||
.and_then(move |url| client.post(&url.to_string()))
|
.and_then(move |url| client.json_post::<_>(url, block))
|
||||||
.and_then(move |builder| {
|
.and_then(|mut response| {
|
||||||
builder
|
response
|
||||||
.json(&block)
|
.text()
|
||||||
.send()
|
.map(|text| (response, text))
|
||||||
.map_err(|e| Error::ReqwestError(e))
|
.map_err(Error::from)
|
||||||
|
})
|
||||||
|
.and_then(|(response, text)| match response.status() {
|
||||||
|
StatusCode::OK => Ok(BeaconBlockPublishStatus::Valid),
|
||||||
|
StatusCode::ACCEPTED => Ok(BeaconBlockPublishStatus::Invalid(text)),
|
||||||
|
_ => response
|
||||||
|
.error_for_status()
|
||||||
|
.map_err(Error::from)
|
||||||
|
.map(|_| BeaconBlockPublishStatus::Unknown),
|
||||||
})
|
})
|
||||||
.map(|_response| ())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Requests a new (unsigned) block from the beacon node.
|
/// Requests a new (unsigned) block from the beacon node.
|
||||||
@@ -119,18 +169,15 @@ impl<E: EthSpec> Validator<E> {
|
|||||||
randao_reveal: Signature,
|
randao_reveal: Signature,
|
||||||
) -> impl Future<Item = BeaconBlock<E>, Error = Error> {
|
) -> impl Future<Item = BeaconBlock<E>, Error = Error> {
|
||||||
let client = self.0.clone();
|
let client = self.0.clone();
|
||||||
self.url("block")
|
self.url("block").into_future().and_then(move |url| {
|
||||||
.into_future()
|
client.json_get::<BeaconBlock<E>>(
|
||||||
.and_then(move |mut url| {
|
url,
|
||||||
url.query_pairs_mut()
|
vec![
|
||||||
.append_pair("slot", &format!("{}", slot.as_u64()));
|
("slot".into(), format!("{}", slot.as_u64())),
|
||||||
url.query_pairs_mut()
|
("randao_reveal".into(), signature_as_string(&randao_reveal)),
|
||||||
.append_pair("randao_reveal", &signature_as_string(&randao_reveal));
|
],
|
||||||
client.get(&url.to_string())
|
)
|
||||||
})
|
})
|
||||||
.and_then(|builder| builder.send().map_err(Error::from))
|
|
||||||
.and_then(|response| response.error_for_status().map_err(Error::from))
|
|
||||||
.and_then(|mut success| success.json::<BeaconBlock<E>>().map_err(Error::from))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -146,12 +193,19 @@ impl<E: EthSpec> Beacon<E> {
|
|||||||
.map_err(Into::into)
|
.map_err(Into::into)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_head(&self) -> impl Future<Item = HeadResponse, Error = Error> {
|
||||||
|
let client = self.0.clone();
|
||||||
|
self.url("head")
|
||||||
|
.into_future()
|
||||||
|
.and_then(move |url| client.json_get::<HeadResponse>(url, vec![]))
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns the block and block root at the given slot.
|
/// Returns the block and block root at the given slot.
|
||||||
pub fn get_block_by_slot(
|
pub fn get_block_by_slot(
|
||||||
&self,
|
&self,
|
||||||
slot: Slot,
|
slot: Slot,
|
||||||
) -> impl Future<Item = (BeaconBlock<E>, Hash256), Error = Error> {
|
) -> impl Future<Item = (BeaconBlock<E>, Hash256), Error = Error> {
|
||||||
self.get_block("slot", format!("{}", slot.as_u64()))
|
self.get_block("slot".to_string(), format!("{}", slot.as_u64()))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the block and block root at the given root.
|
/// Returns the block and block root at the given root.
|
||||||
@@ -159,25 +213,21 @@ impl<E: EthSpec> Beacon<E> {
|
|||||||
&self,
|
&self,
|
||||||
root: Hash256,
|
root: Hash256,
|
||||||
) -> impl Future<Item = (BeaconBlock<E>, Hash256), Error = Error> {
|
) -> impl Future<Item = (BeaconBlock<E>, Hash256), Error = Error> {
|
||||||
self.get_block("root", root_as_string(root))
|
self.get_block("root".to_string(), root_as_string(root))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the block and block root at the given slot.
|
/// Returns the block and block root at the given slot.
|
||||||
fn get_block(
|
fn get_block(
|
||||||
&self,
|
&self,
|
||||||
query_key: &'static str,
|
query_key: String,
|
||||||
query_param: String,
|
query_param: String,
|
||||||
) -> impl Future<Item = (BeaconBlock<E>, Hash256), Error = Error> {
|
) -> impl Future<Item = (BeaconBlock<E>, Hash256), Error = Error> {
|
||||||
let client = self.0.clone();
|
let client = self.0.clone();
|
||||||
self.url("block")
|
self.url("block")
|
||||||
.into_future()
|
.into_future()
|
||||||
.and_then(move |mut url| {
|
.and_then(move |url| {
|
||||||
url.query_pairs_mut().append_pair(query_key, &query_param);
|
client.json_get::<BlockResponse<E>>(url, vec![(query_key, query_param)])
|
||||||
client.get(&url.to_string())
|
|
||||||
})
|
})
|
||||||
.and_then(|builder| builder.send().map_err(Error::from))
|
|
||||||
.and_then(|response| response.error_for_status().map_err(Error::from))
|
|
||||||
.and_then(|mut success| success.json::<BlockResponse<E>>().map_err(Error::from))
|
|
||||||
.map(|response| (response.beacon_block, response.root))
|
.map(|response| (response.beacon_block, response.root))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -186,7 +236,7 @@ impl<E: EthSpec> Beacon<E> {
|
|||||||
&self,
|
&self,
|
||||||
slot: Slot,
|
slot: Slot,
|
||||||
) -> impl Future<Item = (BeaconState<E>, Hash256), Error = Error> {
|
) -> impl Future<Item = (BeaconState<E>, Hash256), Error = Error> {
|
||||||
self.get_state("slot", format!("{}", slot.as_u64()))
|
self.get_state("slot".to_string(), format!("{}", slot.as_u64()))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the state and state root at the given root.
|
/// Returns the state and state root at the given root.
|
||||||
@@ -194,25 +244,21 @@ impl<E: EthSpec> Beacon<E> {
|
|||||||
&self,
|
&self,
|
||||||
root: Hash256,
|
root: Hash256,
|
||||||
) -> impl Future<Item = (BeaconState<E>, Hash256), Error = Error> {
|
) -> impl Future<Item = (BeaconState<E>, Hash256), Error = Error> {
|
||||||
self.get_state("root", root_as_string(root))
|
self.get_state("root".to_string(), root_as_string(root))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the state and state root at the given slot.
|
/// Returns the state and state root at the given slot.
|
||||||
fn get_state(
|
fn get_state(
|
||||||
&self,
|
&self,
|
||||||
query_key: &'static str,
|
query_key: String,
|
||||||
query_param: String,
|
query_param: String,
|
||||||
) -> impl Future<Item = (BeaconState<E>, Hash256), Error = Error> {
|
) -> impl Future<Item = (BeaconState<E>, Hash256), Error = Error> {
|
||||||
let client = self.0.clone();
|
let client = self.0.clone();
|
||||||
self.url("state")
|
self.url("state")
|
||||||
.into_future()
|
.into_future()
|
||||||
.and_then(move |mut url| {
|
.and_then(move |url| {
|
||||||
url.query_pairs_mut().append_pair(query_key, &query_param);
|
client.json_get::<StateResponse<E>>(url, vec![(query_key, query_param)])
|
||||||
client.get(&url.to_string())
|
|
||||||
})
|
})
|
||||||
.and_then(|builder| builder.send().map_err(Error::from))
|
|
||||||
.and_then(|response| response.error_for_status().map_err(Error::from))
|
|
||||||
.and_then(|mut success| success.json::<StateResponse<E>>().map_err(Error::from))
|
|
||||||
.map(|response| (response.beacon_state, response.root))
|
.map(|response| (response.beacon_state, response.root))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user