Merge remote-tracking branch 'origin/unstable' into tree-states

This commit is contained in:
Michael Sproul
2022-09-14 13:51:23 +10:00
404 changed files with 28947 additions and 12000 deletions

View File

@@ -12,3 +12,4 @@ types = { path = "../../consensus/types" }
eth2_ssz = "0.4.1"
beacon_chain = { path = "../../beacon_node/beacon_chain" }
lazy_static = "1.4.0"
tokio = { version = "1.14.0", features = ["rt-multi-thread"] }

View File

@@ -15,6 +15,7 @@ struct ExitTest {
validator_index: u64,
exit_epoch: Epoch,
state_epoch: Epoch,
#[allow(clippy::type_complexity)]
state_modifier: Box<dyn FnOnce(&mut BeaconState<E>)>,
#[allow(clippy::type_complexity)]
block_modifier:
@@ -37,11 +38,12 @@ impl Default for ExitTest {
}
impl ExitTest {
fn block_and_pre_state(self) -> (SignedBeaconBlock<E>, BeaconState<E>) {
async fn block_and_pre_state(self) -> (SignedBeaconBlock<E>, BeaconState<E>) {
let harness = get_harness::<E>(
self.state_epoch.start_slot(E::slots_per_epoch()),
VALIDATOR_COUNT,
);
)
.await;
let mut state = harness.get_current_state();
(self.state_modifier)(&mut state);
@@ -49,11 +51,12 @@ impl ExitTest {
let validator_index = self.validator_index;
let exit_epoch = self.exit_epoch;
let (signed_block, state) =
harness.make_block_with_modifier(state.clone(), state.slot() + 1, |block| {
let (signed_block, state) = harness
.make_block_with_modifier(state.clone(), state.slot() + 1, |block| {
harness.add_voluntary_exit(block, validator_index, exit_epoch);
block_modifier(&harness, block);
});
})
.await;
(signed_block, state)
}
@@ -73,12 +76,12 @@ impl ExitTest {
}
#[cfg(all(test, not(debug_assertions)))]
fn run(self) -> BeaconState<E> {
async fn run(self) -> BeaconState<E> {
let spec = &E::default_spec();
let expected = self.expected.clone();
assert_eq!(STATE_EPOCH, spec.shard_committee_period);
let (block, mut state) = self.block_and_pre_state();
let (block, mut state) = self.block_and_pre_state().await;
let result = Self::process(&block, &mut state);
@@ -87,8 +90,8 @@ impl ExitTest {
state
}
fn test_vector(self, title: String) -> TestVector {
let (block, pre_state) = self.block_and_pre_state();
async fn test_vector(self, title: String) -> TestVector {
let (block, pre_state) = self.block_and_pre_state().await;
let mut post_state = pre_state.clone();
let (post_state, error) = match Self::process(&block, &mut post_state) {
Ok(_) => (Some(post_state), None),
@@ -344,14 +347,14 @@ mod custom_tests {
);
}
#[test]
fn valid() {
let state = ExitTest::default().run();
#[tokio::test]
async fn valid() {
let state = ExitTest::default().run().await;
assert_exited(&state, VALIDATOR_INDEX as usize);
}
#[test]
fn valid_three() {
#[tokio::test]
async fn valid_three() {
let state = ExitTest {
block_modifier: Box::new(|harness, block| {
harness.add_voluntary_exit(block, 1, STATE_EPOCH);
@@ -359,7 +362,8 @@ mod custom_tests {
}),
..ExitTest::default()
}
.run();
.run()
.await;
for i in &[VALIDATOR_INDEX, 1, 2] {
assert_exited(&state, *i as usize);

View File

@@ -4,11 +4,11 @@
/// - `mod tests`: runs all the test vectors locally.
macro_rules! vectors_and_tests {
($($name: ident, $test: expr),*) => {
pub fn vectors() -> Vec<TestVector> {
pub async fn vectors() -> Vec<TestVector> {
let mut vec = vec![];
$(
vec.push($test.test_vector(stringify!($name).into()));
vec.push($test.test_vector(stringify!($name).into()).await);
)*
vec
@@ -18,9 +18,9 @@ macro_rules! vectors_and_tests {
mod tests {
use super::*;
$(
#[test]
fn $name() {
$test.run();
#[tokio::test]
async fn $name() {
$test.run().await;
}
)*
}

View File

@@ -25,8 +25,9 @@ pub const BASE_VECTOR_DIR: &str = "vectors";
pub const SLOT_OFFSET: u64 = 1;
/// Writes all known test vectors to `CARGO_MANIFEST_DIR/vectors`.
fn main() {
match write_all_vectors() {
#[tokio::main]
async fn main() {
match write_all_vectors().await {
Ok(()) => exit(0),
Err(e) => {
eprintln!("Error: {}", e);
@@ -49,7 +50,7 @@ lazy_static! {
static ref KEYPAIRS: Vec<Keypair> = generate_deterministic_keypairs(VALIDATOR_COUNT);
}
fn get_harness<E: EthSpec>(
async fn get_harness<E: EthSpec>(
slot: Slot,
validator_count: usize,
) -> BeaconChainHarness<EphemeralHarnessType<E>> {
@@ -61,23 +62,25 @@ fn get_harness<E: EthSpec>(
let skip_to_slot = slot - SLOT_OFFSET;
if skip_to_slot > Slot::new(0) {
let state = harness.get_current_state();
harness.add_attested_blocks_at_slots(
state,
Hash256::zero(),
(skip_to_slot.as_u64()..slot.as_u64())
.map(Slot::new)
.collect::<Vec<_>>()
.as_slice(),
(0..validator_count).collect::<Vec<_>>().as_slice(),
);
harness
.add_attested_blocks_at_slots(
state,
Hash256::zero(),
(skip_to_slot.as_u64()..slot.as_u64())
.map(Slot::new)
.collect::<Vec<_>>()
.as_slice(),
(0..validator_count).collect::<Vec<_>>().as_slice(),
)
.await;
}
harness
}
/// Writes all vectors to file.
fn write_all_vectors() -> Result<(), String> {
write_vectors_to_file("exit", &exit::vectors())
async fn write_all_vectors() -> Result<(), String> {
write_vectors_to_file("exit", &exit::vectors().await)
}
/// Writes a list of `vectors` to the `title` dir.