2019-02-19 12:20:45 +00:00
|
|
|
// Tests the available fork-choice algorithms
|
2019-02-18 06:32:13 +00:00
|
|
|
|
|
|
|
extern crate beacon_chain;
|
|
|
|
extern crate bls;
|
|
|
|
extern crate db;
|
2019-02-19 12:06:35 +00:00
|
|
|
//extern crate env_logger; // for debugging
|
2019-02-18 06:32:13 +00:00
|
|
|
extern crate fork_choice;
|
2019-02-19 12:06:35 +00:00
|
|
|
extern crate hex;
|
2019-02-18 06:32:13 +00:00
|
|
|
extern crate log;
|
|
|
|
extern crate slot_clock;
|
|
|
|
extern crate types;
|
|
|
|
extern crate yaml_rust;
|
|
|
|
|
|
|
|
pub use beacon_chain::BeaconChain;
|
2019-03-11 00:33:35 +00:00
|
|
|
use bls::Signature;
|
2019-02-18 06:32:13 +00:00
|
|
|
use db::stores::{BeaconBlockStore, BeaconStateStore};
|
|
|
|
use db::MemoryDB;
|
2019-02-19 12:06:35 +00:00
|
|
|
//use env_logger::{Builder, Env};
|
2019-02-20 00:56:58 +00:00
|
|
|
use fork_choice::{BitwiseLMDGhost, ForkChoice, ForkChoiceAlgorithm, LongestChain, SlowLMDGhost};
|
2019-02-18 06:32:13 +00:00
|
|
|
use ssz::ssz_encode;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::sync::Arc;
|
2019-02-19 00:58:17 +00:00
|
|
|
use std::{fs::File, io::prelude::*, path::PathBuf};
|
2019-03-11 00:33:35 +00:00
|
|
|
use types::test_utils::TestingBeaconStateBuilder;
|
|
|
|
use types::{BeaconBlock, BeaconBlockBody, ChainSpec, Eth1Data, Hash256, Slot};
|
2019-02-18 06:32:13 +00:00
|
|
|
use yaml_rust::yaml;
|
|
|
|
|
2019-02-19 12:06:35 +00:00
|
|
|
// Note: We Assume the block Id's are hex-encoded.
|
|
|
|
|
2019-02-18 06:32:13 +00:00
|
|
|
#[test]
|
2019-02-20 00:56:58 +00:00
|
|
|
fn test_bitwise_lmd_ghost() {
|
2019-02-19 12:06:35 +00:00
|
|
|
// set up logging
|
|
|
|
//Builder::from_env(Env::default().default_filter_or("trace")).init();
|
|
|
|
|
2019-02-19 03:37:17 +00:00
|
|
|
test_yaml_vectors(
|
2019-02-20 00:56:58 +00:00
|
|
|
ForkChoiceAlgorithm::BitwiseLMDGhost,
|
|
|
|
"tests/bitwise_lmd_ghost_test_vectors.yaml",
|
2019-02-19 03:37:17 +00:00
|
|
|
100,
|
2019-02-19 04:08:55 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_slow_lmd_ghost() {
|
|
|
|
test_yaml_vectors(
|
|
|
|
ForkChoiceAlgorithm::SlowLMDGhost,
|
|
|
|
"tests/lmd_ghost_test_vectors.yaml",
|
|
|
|
100,
|
2019-02-19 03:37:17 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-02-19 04:22:35 +00:00
|
|
|
#[test]
|
|
|
|
fn test_longest_chain() {
|
|
|
|
test_yaml_vectors(
|
|
|
|
ForkChoiceAlgorithm::LongestChain,
|
|
|
|
"tests/longest_chain_test_vectors.yaml",
|
|
|
|
100,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-02-19 03:37:17 +00:00
|
|
|
// run a generic test over given YAML test vectors
|
|
|
|
fn test_yaml_vectors(
|
|
|
|
fork_choice_algo: ForkChoiceAlgorithm,
|
|
|
|
yaml_file_path: &str,
|
2019-02-19 12:06:35 +00:00
|
|
|
emulated_validators: usize, // the number of validators used to give weights.
|
2019-02-19 03:37:17 +00:00
|
|
|
) {
|
|
|
|
// load test cases from yaml
|
|
|
|
let test_cases = load_test_cases_from_yaml(yaml_file_path);
|
2019-02-18 06:32:13 +00:00
|
|
|
|
|
|
|
// default vars
|
2019-02-19 00:58:17 +00:00
|
|
|
let spec = ChainSpec::foundation();
|
2019-02-18 06:32:13 +00:00
|
|
|
let zero_hash = Hash256::zero();
|
|
|
|
let eth1_data = Eth1Data {
|
|
|
|
deposit_root: zero_hash.clone(),
|
|
|
|
block_hash: zero_hash.clone(),
|
|
|
|
};
|
|
|
|
let randao_reveal = Signature::empty_signature();
|
|
|
|
let signature = Signature::empty_signature();
|
|
|
|
let body = BeaconBlockBody {
|
|
|
|
proposer_slashings: vec![],
|
|
|
|
attester_slashings: vec![],
|
|
|
|
attestations: vec![],
|
|
|
|
deposits: vec![],
|
2019-03-07 01:53:15 +00:00
|
|
|
voluntary_exits: vec![],
|
|
|
|
transfers: vec![],
|
2019-02-18 06:32:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// process the tests
|
|
|
|
for test_case in test_cases {
|
2019-02-19 12:06:35 +00:00
|
|
|
// setup a fresh test
|
|
|
|
let (mut fork_choice, block_store, state_root) =
|
|
|
|
setup_inital_state(&fork_choice_algo, emulated_validators);
|
|
|
|
|
|
|
|
// keep a hashmap of block_id's to block_hashes (random hashes to abstract block_id)
|
|
|
|
//let mut block_id_map: HashMap<String, Hash256> = HashMap::new();
|
|
|
|
// keep a list of hash to slot
|
|
|
|
let mut block_slot: HashMap<Hash256, Slot> = HashMap::new();
|
2019-02-18 06:32:13 +00:00
|
|
|
// assume the block tree is given to us in order.
|
2019-02-19 12:06:35 +00:00
|
|
|
let mut genesis_hash = None;
|
2019-02-18 06:32:13 +00:00
|
|
|
for block in test_case["blocks"].clone().into_vec().unwrap() {
|
|
|
|
let block_id = block["id"].as_str().unwrap().to_string();
|
2019-02-19 12:06:35 +00:00
|
|
|
let parent_id = block["parent"].as_str().unwrap().to_string();
|
2019-02-18 06:32:13 +00:00
|
|
|
|
|
|
|
// default params for genesis
|
2019-02-19 12:06:35 +00:00
|
|
|
let block_hash = id_to_hash(&block_id);
|
2019-02-19 00:58:17 +00:00
|
|
|
let mut slot = spec.genesis_slot;
|
2019-02-19 12:06:35 +00:00
|
|
|
let parent_root = id_to_hash(&parent_id);
|
2019-02-18 06:32:13 +00:00
|
|
|
|
|
|
|
// set the slot and parent based off the YAML. Start with genesis;
|
2019-02-19 12:06:35 +00:00
|
|
|
// if not the genesis, update slot
|
2019-02-18 06:32:13 +00:00
|
|
|
if parent_id != block_id {
|
2019-02-19 12:06:35 +00:00
|
|
|
// find parent slot
|
2019-02-18 06:32:13 +00:00
|
|
|
slot = *(block_slot
|
|
|
|
.get(&parent_root)
|
|
|
|
.expect("Parent should have a slot number"))
|
|
|
|
+ 1;
|
2019-02-19 12:06:35 +00:00
|
|
|
} else {
|
|
|
|
genesis_hash = Some(block_hash);
|
2019-02-18 06:32:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// update slot mapping
|
|
|
|
block_slot.insert(block_hash, slot);
|
|
|
|
|
|
|
|
// build the BeaconBlock
|
|
|
|
let beacon_block = BeaconBlock {
|
|
|
|
slot,
|
|
|
|
parent_root,
|
|
|
|
state_root: state_root.clone(),
|
|
|
|
randao_reveal: randao_reveal.clone(),
|
|
|
|
eth1_data: eth1_data.clone(),
|
|
|
|
signature: signature.clone(),
|
|
|
|
body: body.clone(),
|
|
|
|
};
|
|
|
|
|
2019-02-19 12:06:35 +00:00
|
|
|
// Store the block.
|
2019-02-18 06:32:13 +00:00
|
|
|
block_store
|
|
|
|
.put(&block_hash, &ssz_encode(&beacon_block)[..])
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// run add block for fork choice if not genesis
|
|
|
|
if parent_id != block_id {
|
2019-02-19 00:58:17 +00:00
|
|
|
fork_choice
|
|
|
|
.add_block(&beacon_block, &block_hash, &spec)
|
|
|
|
.unwrap();
|
2019-02-18 06:32:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// add the weights (attestations)
|
|
|
|
let mut current_validator = 0;
|
|
|
|
for id_map in test_case["weights"].clone().into_vec().unwrap() {
|
|
|
|
// get the block id and weights
|
|
|
|
for (map_id, map_weight) in id_map.as_hash().unwrap().iter() {
|
|
|
|
let id = map_id.as_str().unwrap();
|
2019-02-19 12:06:35 +00:00
|
|
|
let block_root = id_to_hash(&id.to_string());
|
2019-02-18 06:32:13 +00:00
|
|
|
let weight = map_weight.as_i64().unwrap();
|
|
|
|
// we assume a validator has a value 1 and add an attestation for to achieve the
|
|
|
|
// correct weight
|
|
|
|
for _ in 0..weight {
|
2019-02-19 12:06:35 +00:00
|
|
|
assert!(
|
|
|
|
current_validator <= emulated_validators,
|
|
|
|
"Not enough validators to emulate weights"
|
|
|
|
);
|
2019-02-18 06:32:13 +00:00
|
|
|
fork_choice
|
2019-02-19 00:58:17 +00:00
|
|
|
.add_attestation(current_validator as u64, &block_root, &spec)
|
2019-02-18 06:32:13 +00:00
|
|
|
.unwrap();
|
|
|
|
current_validator += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// everything is set up, run the fork choice, using genesis as the head
|
2019-02-19 12:06:35 +00:00
|
|
|
let head = fork_choice
|
|
|
|
.find_head(&genesis_hash.unwrap(), &spec)
|
2019-02-19 00:58:17 +00:00
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// compare the result to the expected test
|
|
|
|
let success = test_case["heads"]
|
|
|
|
.clone()
|
|
|
|
.into_vec()
|
|
|
|
.unwrap()
|
|
|
|
.iter()
|
2019-02-19 12:06:35 +00:00
|
|
|
.find(|heads| id_to_hash(&heads["id"].as_str().unwrap().to_string()) == head)
|
2019-02-19 00:58:17 +00:00
|
|
|
.is_some();
|
2019-02-18 06:32:13 +00:00
|
|
|
|
2019-02-19 12:06:35 +00:00
|
|
|
println!("Head found: {}", head);
|
2019-02-19 00:58:17 +00:00
|
|
|
assert!(success, "Did not find one of the possible heads");
|
2019-02-18 06:32:13 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-19 03:37:17 +00:00
|
|
|
|
|
|
|
// loads the test_cases from the supplied yaml file
|
|
|
|
fn load_test_cases_from_yaml(file_path: &str) -> Vec<yaml_rust::Yaml> {
|
|
|
|
// load the yaml
|
|
|
|
let mut file = {
|
|
|
|
let mut file_path_buf = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
|
|
|
file_path_buf.push(file_path);
|
|
|
|
File::open(file_path_buf).unwrap()
|
|
|
|
};
|
|
|
|
let mut yaml_str = String::new();
|
|
|
|
file.read_to_string(&mut yaml_str).unwrap();
|
|
|
|
let docs = yaml::YamlLoader::load_from_str(&yaml_str).unwrap();
|
|
|
|
let doc = &docs[0];
|
|
|
|
doc["test_cases"].as_vec().unwrap().clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
// initialise a single validator and state. All blocks will reference this state root.
|
|
|
|
fn setup_inital_state(
|
2019-02-19 12:06:35 +00:00
|
|
|
fork_choice_algo: &ForkChoiceAlgorithm,
|
2019-02-19 03:37:17 +00:00
|
|
|
no_validators: usize,
|
|
|
|
) -> (Box<ForkChoice>, Arc<BeaconBlockStore<MemoryDB>>, Hash256) {
|
|
|
|
let db = Arc::new(MemoryDB::open());
|
|
|
|
let block_store = Arc::new(BeaconBlockStore::new(db.clone()));
|
|
|
|
let state_store = Arc::new(BeaconStateStore::new(db.clone()));
|
|
|
|
|
|
|
|
// the fork choice instantiation
|
|
|
|
let fork_choice: Box<ForkChoice> = match fork_choice_algo {
|
2019-02-20 00:56:58 +00:00
|
|
|
ForkChoiceAlgorithm::BitwiseLMDGhost => Box::new(BitwiseLMDGhost::new(
|
2019-02-19 03:37:17 +00:00
|
|
|
block_store.clone(),
|
|
|
|
state_store.clone(),
|
|
|
|
)),
|
|
|
|
ForkChoiceAlgorithm::SlowLMDGhost => {
|
|
|
|
Box::new(SlowLMDGhost::new(block_store.clone(), state_store.clone()))
|
|
|
|
}
|
|
|
|
ForkChoiceAlgorithm::LongestChain => Box::new(LongestChain::new(block_store.clone())),
|
|
|
|
};
|
|
|
|
|
|
|
|
let spec = ChainSpec::foundation();
|
|
|
|
|
2019-03-11 00:33:35 +00:00
|
|
|
let state_builder = TestingBeaconStateBuilder::new(no_validators, &spec);
|
|
|
|
let (state, _keypairs) = state_builder.build();
|
2019-02-19 03:37:17 +00:00
|
|
|
|
|
|
|
let state_root = state.canonical_root();
|
|
|
|
state_store
|
|
|
|
.put(&state_root, &ssz_encode(&state)[..])
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// return initialised vars
|
|
|
|
(fork_choice, block_store, state_root)
|
|
|
|
}
|
2019-02-19 12:06:35 +00:00
|
|
|
|
|
|
|
// convert a block_id into a Hash256 -- assume input is hex encoded;
|
|
|
|
fn id_to_hash(id: &String) -> Hash256 {
|
|
|
|
let bytes = hex::decode(id).expect("Block ID should be hex");
|
|
|
|
|
|
|
|
let len = std::cmp::min(bytes.len(), 32);
|
|
|
|
let mut fixed_bytes = [0u8; 32];
|
|
|
|
for (index, byte) in bytes.iter().take(32).enumerate() {
|
|
|
|
fixed_bytes[32 - len + index] = *byte;
|
|
|
|
}
|
|
|
|
Hash256::from(fixed_bytes)
|
|
|
|
}
|