Skip to content

Commit

Permalink
Add tests for block roots
Browse files Browse the repository at this point in the history
  • Loading branch information
paulhauner committed Sep 5, 2020
1 parent 02026b9 commit 002be83
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 59 deletions.
175 changes: 116 additions & 59 deletions beacon_node/http_api/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,57 @@ impl ApiTester {
}
}

pub async fn test_beacon_state_root(self, state_ids: &[StateId]) -> Self {
fn get_state(&self, state_id: StateId) -> Option<BeaconState<E>> {
match state_id {
StateId::Head => Some(self.chain.head().unwrap().beacon_state),
StateId::Genesis => self
.chain
.get_state(&self.chain.genesis_state_root, None)
.unwrap(),
StateId::Finalized => {
let finalized_slot = self
.chain
.head_info()
.unwrap()
.finalized_checkpoint
.epoch
.start_slot(E::slots_per_epoch());

let root = self
.chain
.state_root_at_slot(finalized_slot)
.unwrap()
.unwrap();

self.chain.get_state(&root, Some(finalized_slot)).unwrap()
}
StateId::Justified => {
let justified_slot = self
.chain
.head_info()
.unwrap()
.current_justified_checkpoint
.epoch
.start_slot(E::slots_per_epoch());

let root = self
.chain
.state_root_at_slot(justified_slot)
.unwrap()
.unwrap();

self.chain.get_state(&root, Some(justified_slot)).unwrap()
}
StateId::Slot(slot) => {
let root = self.chain.state_root_at_slot(slot).unwrap().unwrap();

self.chain.get_state(&root, Some(slot)).unwrap()
}
StateId::Root(root) => self.chain.get_state(&root, None).unwrap(),
}
}

pub async fn test_beacon_states_root(self, state_ids: &[StateId]) -> Self {
for &state_id in state_ids {
let result = self
.client
Expand Down Expand Up @@ -144,7 +194,7 @@ impl ApiTester {
self
}

pub async fn test_beacon_state_fork(self, state_ids: &[StateId]) -> Self {
pub async fn test_beacon_states_fork(self, state_ids: &[StateId]) -> Self {
for &state_id in state_ids {
let result = self
.client
Expand All @@ -161,57 +211,7 @@ impl ApiTester {
self
}

fn get_state(&self, state_id: StateId) -> Option<BeaconState<E>> {
match state_id {
StateId::Head => Some(self.chain.head().unwrap().beacon_state),
StateId::Genesis => self
.chain
.get_state(&self.chain.genesis_state_root, None)
.unwrap(),
StateId::Finalized => {
let finalized_slot = self
.chain
.head_info()
.unwrap()
.finalized_checkpoint
.epoch
.start_slot(E::slots_per_epoch());

let root = self
.chain
.state_root_at_slot(finalized_slot)
.unwrap()
.unwrap();

self.chain.get_state(&root, Some(finalized_slot)).unwrap()
}
StateId::Justified => {
let justified_slot = self
.chain
.head_info()
.unwrap()
.current_justified_checkpoint
.epoch
.start_slot(E::slots_per_epoch());

let root = self
.chain
.state_root_at_slot(justified_slot)
.unwrap()
.unwrap();

self.chain.get_state(&root, Some(justified_slot)).unwrap()
}
StateId::Slot(slot) => {
let root = self.chain.state_root_at_slot(slot).unwrap().unwrap();

self.chain.get_state(&root, Some(slot)).unwrap()
}
StateId::Root(root) => self.chain.get_state(&root, None).unwrap(),
}
}

pub async fn test_beacon_state_finality_checkpoints(self, state_ids: &[StateId]) -> Self {
pub async fn test_beacon_states_finality_checkpoints(self, state_ids: &[StateId]) -> Self {
for &state_id in state_ids {
let result = self
.client
Expand All @@ -233,6 +233,40 @@ impl ApiTester {

self
}

fn get_block_root(&self, block_id: BlockId) -> Option<Hash256> {
match block_id {
BlockId::Head => Some(self.chain.head_info().unwrap().block_root),
BlockId::Genesis => Some(self.chain.genesis_block_root),
BlockId::Finalized => Some(self.chain.head_info().unwrap().finalized_checkpoint.root),
BlockId::Justified => Some(
self.chain
.head_info()
.unwrap()
.current_justified_checkpoint
.root,
),
BlockId::Slot(slot) => self.chain.block_root_at_slot(slot).unwrap(),
BlockId::Root(root) => Some(root),
}
}

pub async fn test_beacon_blocks_root(self, block_ids: &[BlockId]) -> Self {
for &block_id in block_ids {
let result = self
.client
.beacon_blocks_root(block_id)
.await
.unwrap()
.map(|res| res.data.root);

let expected = self.get_block_root(block_id);

assert_eq!(result, expected, "{:?}", block_id);
}

self
}
}

fn interesting_state_ids() -> Vec<StateId> {
Expand All @@ -251,23 +285,46 @@ fn interesting_state_ids() -> Vec<StateId> {
]
}

fn interesting_block_ids() -> Vec<BlockId> {
vec![
BlockId::Head,
BlockId::Genesis,
BlockId::Finalized,
BlockId::Justified,
BlockId::Slot(Slot::new(0)),
BlockId::Slot(Slot::new(32)),
BlockId::Slot(Slot::from(SKIPPED_SLOTS[0])),
BlockId::Slot(Slot::from(SKIPPED_SLOTS[1])),
BlockId::Slot(Slot::from(SKIPPED_SLOTS[2])),
BlockId::Slot(Slot::from(SKIPPED_SLOTS[3])),
BlockId::Root(Hash256::zero()),
]
}

#[tokio::test(core_threads = 2)]
async fn beacon_states_root() {
ApiTester::new()
.test_beacon_states_root(&interesting_state_ids())
.await;
}

#[tokio::test(core_threads = 2)]
async fn beacon_state_root() {
async fn beacon_states_fork() {
ApiTester::new()
.test_beacon_state_root(&interesting_state_ids())
.test_beacon_states_fork(&interesting_state_ids())
.await;
}

#[tokio::test(core_threads = 2)]
async fn beacon_state_fork() {
async fn beacon_states_finality_checkpoints() {
ApiTester::new()
.test_beacon_state_fork(&interesting_state_ids())
.test_beacon_states_finality_checkpoints(&interesting_state_ids())
.await;
}

#[tokio::test(core_threads = 2)]
async fn beacon_state_finality_checkpoints() {
async fn beacon_blocks_root() {
ApiTester::new()
.test_beacon_state_finality_checkpoints(&interesting_state_ids())
.test_beacon_blocks_root(&interesting_block_ids())
.await;
}
11 changes: 11 additions & 0 deletions common/eth2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,15 @@ impl BeaconNodeClient {
self.get_opt(&format!("beacon/states/{}/finality_checkpoints", state_id))
.await
}

/// `GET beacon/blocks/{block_id}/root`
///
/// Returns `Ok(None)` on a 404 error.
pub async fn beacon_blocks_root(
&self,
block_id: BlockId,
) -> Result<Option<GenericResponse<RootData>>, Error> {
self.get_opt(&format!("beacon/blocks/{}/root", block_id))
.await
}
}

0 comments on commit 002be83

Please sign in to comment.