Update pool/attestations and committees endpoints (#1899)

## Issue Addressed

Catching up on a few eth2 spec updates:

## Proposed Changes

- adding query params to the `GET pool/attestations` endpoint
- allowing the `POST pool/attestations` endpoint to accept an array of attestations
    - batching attestation submission
- moving `epoch` from a path param to a query param in the `committees` endpoint

## Additional Info


Co-authored-by: realbigsean <seananderson33@gmail.com>
This commit is contained in:
realbigsean
2020-11-18 23:31:39 +00:00
parent 3408de8151
commit 79fd9b32b9
6 changed files with 250 additions and 127 deletions

View File

@@ -6,6 +6,7 @@ use beacon_chain::{
};
use discv5::enr::{CombinedKey, EnrBuilder};
use environment::null_logger;
use eth2::Error;
use eth2::{types::*, BeaconNodeHttpClient, Url};
use eth2_libp2p::{
rpc::methods::MetaData,
@@ -624,14 +625,10 @@ impl ApiTester {
for state_id in self.interesting_state_ids() {
let mut state_opt = self.get_state(state_id);
let epoch = state_opt
.as_ref()
.map(|state| state.current_epoch())
.unwrap_or_else(|| Epoch::new(0));
let epoch_opt = state_opt.as_ref().map(|state| state.current_epoch());
let results = self
.client
.get_beacon_states_committees(state_id, epoch, None, None)
.get_beacon_states_committees(state_id, None, None, epoch_opt)
.await
.unwrap()
.map(|res| res.data);
@@ -641,11 +638,10 @@ impl ApiTester {
}
let state = state_opt.as_mut().expect("result should be none");
state.build_all_committee_caches(&self.chain.spec).unwrap();
let committees = state
.get_beacon_committees_at_epoch(
RelativeEpoch::from_epoch(state.current_epoch(), epoch).unwrap(),
)
.get_beacon_committees_at_epoch(RelativeEpoch::Current)
.unwrap();
for (i, result) in results.unwrap().into_iter().enumerate() {
@@ -886,45 +882,60 @@ impl ApiTester {
}
pub async fn test_post_beacon_pool_attestations_valid(mut self) -> Self {
for attestation in &self.attestations {
self.client
.post_beacon_pool_attestations(attestation)
.await
.unwrap();
self.client
.post_beacon_pool_attestations(self.attestations.as_slice())
.await
.unwrap();
assert!(
self.network_rx.try_recv().is_ok(),
"valid attestation should be sent to network"
);
}
assert!(
self.network_rx.try_recv().is_ok(),
"valid attestation should be sent to network"
);
self
}
pub async fn test_post_beacon_pool_attestations_invalid(mut self) -> Self {
let mut attestations = Vec::new();
for attestation in &self.attestations {
let mut attestation = attestation.clone();
attestation.data.slot += 1;
let mut invalid_attestation = attestation.clone();
invalid_attestation.data.slot += 1;
assert!(self
.client
.post_beacon_pool_attestations(&attestation)
.await
.is_err());
assert!(
self.network_rx.try_recv().is_err(),
"invalid attestation should not be sent to network"
);
// add both to ensure we only fail on invalid attestations
attestations.push(attestation.clone());
attestations.push(invalid_attestation);
}
let err = self
.client
.post_beacon_pool_attestations(attestations.as_slice())
.await
.unwrap_err();
match err {
Error::ServerIndexedMessage(IndexedErrorMessage {
code,
message: _,
failures,
}) => {
assert_eq!(code, 400);
assert_eq!(failures.len(), self.attestations.len());
}
_ => panic!("query did not fail correctly"),
}
assert!(
self.network_rx.try_recv().is_ok(),
"if some attestations are valid, we should send them to the network"
);
self
}
pub async fn test_get_beacon_pool_attestations(self) -> Self {
let result = self
.client
.get_beacon_pool_attestations()
.get_beacon_pool_attestations(None, None)
.await
.unwrap()
.data;