mirror of
https://github.com/sigp/lighthouse.git
synced 2026-04-19 13:58:28 +00:00
Add working tests for rest_api validator endpoint
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
use super::{AggregateSignature, AttestationData, BitList, EthSpec};
|
||||
use super::{
|
||||
AggregateSignature, AttestationData, BitList, ChainSpec, Domain, EthSpec, Fork, SecretKey,
|
||||
Signature,
|
||||
};
|
||||
use crate::test_utils::TestRandom;
|
||||
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
@@ -7,6 +10,12 @@ use test_random_derive::TestRandom;
|
||||
use tree_hash::TreeHash;
|
||||
use tree_hash_derive::{SignedRoot, TreeHash};
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum Error {
|
||||
SszTypesError(ssz_types::Error),
|
||||
AlreadySigned(usize),
|
||||
}
|
||||
|
||||
/// Details an attestation that can be slashable.
|
||||
///
|
||||
/// Spec v0.9.1
|
||||
@@ -48,6 +57,37 @@ impl<T: EthSpec> Attestation<T> {
|
||||
self.aggregation_bits = self.aggregation_bits.union(&other.aggregation_bits);
|
||||
self.signature.add_aggregate(&other.signature);
|
||||
}
|
||||
|
||||
/// Signs `self`, setting the `committee_position`'th bit of `aggregation_bits` to `true`.
|
||||
///
|
||||
/// Returns an `AlreadySigned` error if the `committee_position`'th bit is already `true`.
|
||||
pub fn sign(
|
||||
&mut self,
|
||||
secret_key: &SecretKey,
|
||||
committee_position: usize,
|
||||
fork: &Fork,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
if self
|
||||
.aggregation_bits
|
||||
.get(committee_position)
|
||||
.map_err(|e| Error::SszTypesError(e))?
|
||||
{
|
||||
Err(Error::AlreadySigned(committee_position))
|
||||
} else {
|
||||
self.aggregation_bits
|
||||
.set(committee_position, true)
|
||||
.map_err(|e| Error::SszTypesError(e))?;
|
||||
|
||||
let message = self.data.tree_hash_root();
|
||||
let domain = spec.get_domain(self.data.target.epoch, Domain::BeaconAttester, fork);
|
||||
|
||||
self.signature
|
||||
.add(&Signature::new(&message, domain, secret_key));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -39,7 +39,7 @@ pub mod validator;
|
||||
|
||||
use ethereum_types::{H160, H256};
|
||||
|
||||
pub use crate::attestation::Attestation;
|
||||
pub use crate::attestation::{Attestation, Error as AttestationError};
|
||||
pub use crate::attestation_data::AttestationData;
|
||||
pub use crate::attestation_duty::AttestationDuty;
|
||||
pub use crate::attester_slashing::AttesterSlashing;
|
||||
|
||||
@@ -14,7 +14,8 @@ use std::marker::PhantomData;
|
||||
use std::net::SocketAddr;
|
||||
use std::time::Duration;
|
||||
use types::{
|
||||
Attestation, BeaconBlock, BeaconState, Epoch, EthSpec, Hash256, PublicKey, Signature, Slot,
|
||||
Attestation, BeaconBlock, BeaconState, CommitteeIndex, Epoch, EthSpec, Hash256, PublicKey,
|
||||
Signature, Slot,
|
||||
};
|
||||
use url::Url;
|
||||
|
||||
@@ -134,20 +135,20 @@ fn error_for_status(
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub enum BeaconBlockPublishStatus {
|
||||
/// The block was valid and has been published to the network.
|
||||
pub enum PublishStatus {
|
||||
/// The object 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.
|
||||
/// The object 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
|
||||
/// The server responsed with an unknown status code. The object may or may not have been
|
||||
/// published to the network.
|
||||
Unknown,
|
||||
}
|
||||
|
||||
impl BeaconBlockPublishStatus {
|
||||
/// Returns `true` if `*self == BeaconBlockPublishStatus::Valid`.
|
||||
impl PublishStatus {
|
||||
/// Returns `true` if `*self == PublishStatus::Valid`.
|
||||
pub fn is_valid(&self) -> bool {
|
||||
*self == BeaconBlockPublishStatus::Valid
|
||||
*self == PublishStatus::Valid
|
||||
}
|
||||
}
|
||||
|
||||
@@ -167,18 +168,11 @@ impl<E: EthSpec> Validator<E> {
|
||||
pub fn produce_attestation(
|
||||
&self,
|
||||
slot: Slot,
|
||||
shard: u64,
|
||||
validator_pubkey: &PublicKey,
|
||||
poc_bit: bool,
|
||||
committee_index: CommitteeIndex,
|
||||
) -> impl Future<Item = Attestation<E>, Error = Error> {
|
||||
let query_params = vec![
|
||||
("slot".into(), format!("{}", slot)),
|
||||
("shard".into(), format!("{}", shard)),
|
||||
("poc_bit".into(), format!("{}", poc_bit as u8)),
|
||||
(
|
||||
"validator_pubkey".into(),
|
||||
pubkey_as_string(validator_pubkey),
|
||||
),
|
||||
("committee_index".into(), format!("{}", committee_index)),
|
||||
];
|
||||
|
||||
let client = self.0.clone();
|
||||
@@ -187,6 +181,31 @@ impl<E: EthSpec> Validator<E> {
|
||||
.and_then(move |url| client.json_get(url, query_params))
|
||||
}
|
||||
|
||||
/// Posts an attestation to the beacon node, expecting it to verify it and publish it to the network.
|
||||
pub fn publish_attestation(
|
||||
&self,
|
||||
attestation: Attestation<E>,
|
||||
) -> impl Future<Item = PublishStatus, Error = Error> {
|
||||
let client = self.0.clone();
|
||||
self.url("attestation")
|
||||
.into_future()
|
||||
.and_then(move |url| client.json_post::<_>(url, attestation))
|
||||
.and_then(|mut response| {
|
||||
response
|
||||
.text()
|
||||
.map(|text| (response, text))
|
||||
.map_err(Error::from)
|
||||
})
|
||||
.and_then(|(response, text)| match response.status() {
|
||||
StatusCode::OK => Ok(PublishStatus::Valid),
|
||||
StatusCode::ACCEPTED => Ok(PublishStatus::Invalid(text)),
|
||||
_ => response
|
||||
.error_for_status()
|
||||
.map_err(Error::from)
|
||||
.map(|_| PublishStatus::Unknown),
|
||||
})
|
||||
}
|
||||
|
||||
/// Returns the duties required of the given validator pubkeys in the given epoch.
|
||||
pub fn get_duties(
|
||||
&self,
|
||||
@@ -213,7 +232,7 @@ impl<E: EthSpec> Validator<E> {
|
||||
pub fn publish_block(
|
||||
&self,
|
||||
block: BeaconBlock<E>,
|
||||
) -> impl Future<Item = BeaconBlockPublishStatus, Error = Error> {
|
||||
) -> impl Future<Item = PublishStatus, Error = Error> {
|
||||
let client = self.0.clone();
|
||||
self.url("block")
|
||||
.into_future()
|
||||
@@ -225,12 +244,12 @@ impl<E: EthSpec> Validator<E> {
|
||||
.map_err(Error::from)
|
||||
})
|
||||
.and_then(|(response, text)| match response.status() {
|
||||
StatusCode::OK => Ok(BeaconBlockPublishStatus::Valid),
|
||||
StatusCode::ACCEPTED => Ok(BeaconBlockPublishStatus::Invalid(text)),
|
||||
StatusCode::OK => Ok(PublishStatus::Valid),
|
||||
StatusCode::ACCEPTED => Ok(PublishStatus::Invalid(text)),
|
||||
_ => response
|
||||
.error_for_status()
|
||||
.map_err(Error::from)
|
||||
.map(|_| BeaconBlockPublishStatus::Unknown),
|
||||
.map(|_| PublishStatus::Unknown),
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user