Skip to content

Commit

Permalink
Fix attestation duties bug
Browse files Browse the repository at this point in the history
  • Loading branch information
paulhauner committed Sep 8, 2020
1 parent b8f4d72 commit dea298e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
10 changes: 10 additions & 0 deletions beacon_node/http_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,16 @@ pub fn serve<T: BeaconChainTypes>(
.and_then(
|epoch: Epoch, query: api_types::ValidatorDutiesQuery, chain: Arc<BeaconChain<T>>| {
blocking_json_task(move || {
let current_epoch = chain.epoch().map_err(crate::reject::beacon_chain_error)?;

// Taking advantage of saturating addition on epoch.
if epoch + 1 < current_epoch {
return Err(crate::reject::custom_bad_request(format!(
"request epoch {} is more than one epoch prior to current epoch {}",
epoch, current_epoch
)));
}

let indices = query
.index
.clone()
Expand Down
14 changes: 14 additions & 0 deletions beacon_node/http_api/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,20 @@ impl ApiTester {
for indices in self.interesting_validator_indices() {
let epoch = Epoch::from(epoch);

// The endpoint does not allow getting duties older than the previous epoch.
if epoch + 1 < current_epoch {
assert_eq!(
self.client
.get_validator_duties_attester(epoch, Some(&indices))
.await
.unwrap_err()
.status()
.map(Into::into),
Some(400)
);
continue;
}

let results = self
.client
.get_validator_duties_attester(epoch, Some(&indices))
Expand Down
2 changes: 1 addition & 1 deletion common/eth2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub enum Error {
}

impl Error {
fn status(&self) -> Option<StatusCode> {
pub fn status(&self) -> Option<StatusCode> {
match self {
Error::Reqwest(error) => error.status(),
Error::ServerMessage(msg) => StatusCode::try_from(msg.code).ok(),
Expand Down
11 changes: 9 additions & 2 deletions common/eth2/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,14 +317,18 @@ pub struct ChainHeadData {
pub root: Hash256,
}

#[derive(Clone, Deserialize)]
#[derive(Clone, PartialEq, Debug, Deserialize)]
#[serde(try_from = "String", bound = "T: FromStr")]
pub struct QueryVec<T: FromStr>(pub Vec<T>);

impl<T: FromStr> TryFrom<String> for QueryVec<T> {
type Error = String;

fn try_from(string: String) -> Result<Self, Self::Error> {
if string == "" {
return Ok(Self(vec![]));
}

string
.split(",")
.map(|s| s.parse().map_err(|_| "unable to parse".to_string()))
Expand Down Expand Up @@ -362,6 +366,9 @@ mod tests {

#[test]
fn query_vec() {
assert_eq!("0,1,2".parse::<QueryVec<u64>>().unwrap().0, vec![0, 1, 3]);
assert_eq!(
QueryVec::try_from("0,1,2".to_string()).unwrap(),
QueryVec(vec![0_u64, 1, 2])
);
}
}

0 comments on commit dea298e

Please sign in to comment.