Files
lighthouse/testing/state_transition_vectors/src/main.rs
Eitan Seri-Levi 99e53b88c3 Migrate from ethereum-types to alloy-primitives (#6078)
* Remove use of ethers_core::RlpStream

* Merge branch 'unstable' of https://github.com/sigp/lighthouse into remove_use_of_ethers_core

* Remove old code

* Simplify keccak call

* Remove unused package

* Merge branch 'unstable' of https://github.com/ethDreamer/lighthouse into remove_use_of_ethers_core

* Merge branch 'unstable' into remove_use_of_ethers_core

* Run clippy

* Merge branch 'remove_use_of_ethers_core' of https://github.com/dospore/lighthouse into remove_use_of_ethers_core

* Check all cargo fmt

* migrate to alloy primitives init

* fix deps

* integrate alloy-primitives

* resolve dep issues

* more changes based on dep changes

* add TODOs

* Merge branch 'unstable' of https://github.com/sigp/lighthouse into remove_use_of_ethers_core

* Revert lock

* Add BeaconBlocksByRange v3

* continue migration

* Revert "Add BeaconBlocksByRange v3"

This reverts commit e3ce7fc5ea.

* impl hash256 extended trait

* revert some uneeded diffs

* merge conflict resolved

* fix subnet id rshift calc

* rename to FixedBytesExtended

* debugging

* Merge branch 'unstable' of https://github.com/sigp/lighthouse into migrate-to-alloy-primitives

* fix failed test

* fixing more tests

* Merge branch 'unstable' of https://github.com/sigp/lighthouse into remove_use_of_ethers_core

* introduce a shim to convert between the two u256 types

* move alloy to wrokspace

* align alloy versions

* update

* update web3signer test certs

* refactor

* resolve failing tests

* linting

* fix graffiti string test

* fmt

* fix ef test

* resolve merge conflicts

* remove udep and revert cert

* cargo patch

* cyclic dep

* fix build error

* Merge branch 'unstable' of https://github.com/sigp/lighthouse into migrate-to-alloy-primitives

* resolve conflicts, update deps

* merge unstable

* fmt

* fix deps

* Merge branch 'unstable' of https://github.com/sigp/lighthouse into migrate-to-alloy-primitives

* resolve merge conflicts

* resolve conflicts, make necessary changes

* Remove patch

* fmt

* remove file

* merge conflicts

* sneaking in a smol change

* bump versions

* Merge remote-tracking branch 'origin/unstable' into migrate-to-alloy-primitives

* Updates for peerDAS

* Update ethereum_hashing to prevent dupe

* updated alloy-consensus, removed TODOs

* cargo update

* endianess fix

* Merge branch 'unstable' of https://github.com/sigp/lighthouse into migrate-to-alloy-primitives

* fmt

* fix merge

* fix test

* fixed_bytes crate

* minor fixes

* convert u256 to i64

* panic free mixin to_low_u64_le

* from_str_radix

* computbe_subnet api and ensuring we use big-endian

* Merge branch 'unstable' of https://github.com/sigp/lighthouse into migrate-to-alloy-primitives

* fix test

* Simplify subnet_id test

* Simplify some more tests

* Add tests to fixed_bytes crate

* Merge branch 'unstable' into migrate-to-alloy-primitives
2024-09-02 08:03:24 +00:00

134 lines
4.2 KiB
Rust

#[macro_use]
mod macros;
mod exit;
use beacon_chain::test_utils::{BeaconChainHarness, EphemeralHarnessType};
use ssz::Encode;
use std::env;
use std::fs::{self, File};
use std::io::Write;
use std::path::{Path, PathBuf};
use std::process::exit;
use std::sync::LazyLock;
use types::{
test_utils::generate_deterministic_keypairs, BeaconState, EthSpec, Keypair, SignedBeaconBlock,
};
use types::{FixedBytesExtended, Hash256, MainnetEthSpec, Slot};
type E = MainnetEthSpec;
pub const VALIDATOR_COUNT: usize = 64;
/// The base output directory for test vectors.
pub const BASE_VECTOR_DIR: &str = "vectors";
pub const SLOT_OFFSET: u64 = 1;
/// Writes all known test vectors to `CARGO_MANIFEST_DIR/vectors`.
#[tokio::main]
async fn main() {
match write_all_vectors().await {
Ok(()) => exit(0),
Err(e) => {
eprintln!("Error: {}", e);
exit(1)
}
}
}
/// An abstract definition of a test vector that can be run as a test or exported to disk.
pub struct TestVector {
pub title: String,
pub pre_state: BeaconState<E>,
pub block: SignedBeaconBlock<E>,
pub post_state: Option<BeaconState<E>>,
pub error: Option<String>,
}
/// A cached set of keys.
static KEYPAIRS: LazyLock<Vec<Keypair>> =
LazyLock::new(|| generate_deterministic_keypairs(VALIDATOR_COUNT));
async fn get_harness<E: EthSpec>(
slot: Slot,
validator_count: usize,
) -> BeaconChainHarness<EphemeralHarnessType<E>> {
let harness = BeaconChainHarness::builder(E::default())
.default_spec()
.keypairs(KEYPAIRS[0..validator_count].to_vec())
.fresh_ephemeral_store()
.build();
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(),
)
.await;
}
harness
}
/// Writes all vectors to file.
async fn write_all_vectors() -> Result<(), String> {
write_vectors_to_file("exit", &exit::vectors().await)
}
/// Writes a list of `vectors` to the `title` dir.
fn write_vectors_to_file(title: &str, vectors: &[TestVector]) -> Result<(), String> {
let dir = env::var("CARGO_MANIFEST_DIR")
.map_err(|e| format!("Unable to find manifest dir: {:?}", e))?
.parse::<PathBuf>()
.map_err(|e| format!("Unable to parse manifest dir: {:?}", e))?
.join(BASE_VECTOR_DIR)
.join(title);
if dir.exists() {
fs::remove_dir_all(&dir).map_err(|e| format!("Unable to remove {:?}: {:?}", dir, e))?;
}
fs::create_dir_all(&dir).map_err(|e| format!("Unable to create {:?}: {:?}", dir, e))?;
for vector in vectors {
let dir = dir.clone().join(&vector.title);
if dir.exists() {
fs::remove_dir_all(&dir).map_err(|e| format!("Unable to remove {:?}: {:?}", dir, e))?;
}
fs::create_dir_all(&dir).map_err(|e| format!("Unable to create {:?}: {:?}", dir, e))?;
write_to_ssz_file(&dir.clone().join("pre.ssz"), &vector.pre_state)?;
write_to_ssz_file(&dir.clone().join("block.ssz"), &vector.block)?;
if let Some(post_state) = vector.post_state.as_ref() {
write_to_ssz_file(&dir.clone().join("post.ssz"), post_state)?;
}
if let Some(error) = vector.error.as_ref() {
write_to_file(&dir.clone().join("error.txt"), error.as_bytes())?;
}
}
Ok(())
}
/// Write some SSZ object to file.
fn write_to_ssz_file<T: Encode>(path: &Path, item: &T) -> Result<(), String> {
write_to_file(path, &item.as_ssz_bytes())
}
/// Write some bytes to file.
fn write_to_file(path: &Path, item: &[u8]) -> Result<(), String> {
File::create(path)
.map_err(|e| format!("Unable to create {:?}: {:?}", path, e))
.and_then(|mut file| {
file.write_all(item)
.map(|_| ())
.map_err(|e| format!("Unable to write to {:?}: {:?}", path, e))
})
}