mod proto_array; mod ssz_container; use parking_lot::RwLock; use proto_array::ProtoArray; use ssz::{Decode, Encode}; use ssz_container::SszContainer; use ssz_derive::{Decode, Encode}; use std::collections::HashMap; use types::{Epoch, Hash256, Slot}; pub const DEFAULT_PRUNE_THRESHOLD: usize = 256; #[derive(Clone, PartialEq, Debug)] pub enum Error { FinalizedNodeUnknown(Hash256), JustifiedNodeUnknown(Hash256), InvalidFinalizedRootChange, InvalidNodeIndex(usize), InvalidParentIndex(usize), InvalidBestChildIndex(usize), InvalidJustifiedIndex(usize), InvalidBestDescendant(usize), InvalidParentDelta(usize), InvalidNodeDelta(usize), DeltaOverflow(usize), IndexOverflow(&'static str), InvalidDeltaLen { deltas: usize, indices: usize, }, RevertedFinalizedEpoch { current_finalized_epoch: Epoch, new_finalized_epoch: Epoch, }, InvalidBestNode { justified_epoch: Epoch, finalized_epoch: Epoch, node_justified_epoch: Epoch, node_finalized_epoch: Epoch, }, } #[derive(Default, PartialEq, Clone, Encode, Decode)] pub struct VoteTracker { current_root: Hash256, next_root: Hash256, next_epoch: Epoch, } /// A Vec-wrapper which will grow to match any request. /// /// E.g., a `get` or `insert` to an out-of-bounds element will cause the Vec to grow (using /// Default) to the smallest size required to fulfill the request. #[derive(Default, Clone, Debug, PartialEq)] pub struct ElasticList(Vec); impl ElasticList where T: Default, { fn ensure(&mut self, i: usize) { if self.0.len() <= i { self.0.resize_with(i + 1, Default::default); } } pub fn get(&mut self, i: usize) -> &T { self.ensure(i); &self.0[i] } pub fn get_mut(&mut self, i: usize) -> &mut T { self.ensure(i); &mut self.0[i] } pub fn iter_mut(&mut self) -> impl Iterator { self.0.iter_mut() } } pub struct ProtoArrayForkChoice { proto_array: RwLock, votes: RwLock>, balances: RwLock>, } impl PartialEq for ProtoArrayForkChoice { fn eq(&self, other: &Self) -> bool { *self.proto_array.read() == *other.proto_array.read() && *self.votes.read() == *other.votes.read() && *self.balances.read() == *other.balances.read() } } impl ProtoArrayForkChoice { pub fn new( finalized_block_slot: Slot, justified_epoch: Epoch, finalized_epoch: Epoch, finalized_root: Hash256, ) -> Result { let mut proto_array = ProtoArray { prune_threshold: DEFAULT_PRUNE_THRESHOLD, ffg_update_required: false, justified_epoch, finalized_epoch, nodes: Vec::with_capacity(1), indices: HashMap::with_capacity(1), }; proto_array .on_new_block( finalized_block_slot, finalized_root, None, justified_epoch, finalized_epoch, ) .map_err(|e| format!("Failed to add finalized block to proto_array: {:?}", e))?; Ok(Self { proto_array: RwLock::new(proto_array), votes: RwLock::new(ElasticList::default()), balances: RwLock::new(vec![]), }) } pub fn process_attestation( &self, validator_index: usize, block_root: Hash256, block_epoch: Epoch, ) -> Result<(), String> { let mut votes = self.votes.write(); let vote = votes.get_mut(validator_index); if block_epoch > vote.next_epoch || *vote == VoteTracker::default() { vote.next_root = block_root; vote.next_epoch = block_epoch; } Ok(()) } pub fn process_block( &self, slot: Slot, block_root: Hash256, parent_root: Hash256, justified_epoch: Epoch, finalized_epoch: Epoch, ) -> Result<(), String> { self.proto_array .write() .on_new_block( slot, block_root, Some(parent_root), justified_epoch, finalized_epoch, ) .map_err(|e| format!("process_block_error: {:?}", e)) } pub fn find_head( &self, justified_epoch: Epoch, justified_root: Hash256, finalized_epoch: Epoch, justified_state_balances: &[u64], ) -> Result { let mut proto_array = self.proto_array.write(); let mut votes = self.votes.write(); let mut old_balances = self.balances.write(); let new_balances = justified_state_balances; let deltas = compute_deltas( &proto_array.indices, &mut votes, &old_balances, &new_balances, ) .map_err(|e| format!("find_head compute_deltas failed: {:?}", e))?; proto_array .apply_score_changes(deltas, justified_epoch, finalized_epoch) .map_err(|e| format!("find_head apply_score_changes failed: {:?}", e))?; *old_balances = new_balances.to_vec(); proto_array .find_head(&justified_root) .map_err(|e| format!("find_head failed: {:?}", e)) } pub fn update_finalized_root( &self, finalized_epoch: Epoch, finalized_root: Hash256, ) -> Result<(), String> { self.proto_array .write() .maybe_prune(finalized_epoch, finalized_root) .map_err(|e| format!("find_head maybe_prune failed: {:?}", e)) } pub fn set_prune_threshold(&self, prune_threshold: usize) { self.proto_array.write().prune_threshold = prune_threshold; } pub fn len(&self) -> usize { self.proto_array.read().nodes.len() } pub fn contains_block(&self, block_root: &Hash256) -> bool { self.proto_array.read().indices.contains_key(block_root) } pub fn block_slot(&self, block_root: &Hash256) -> Option { let proto_array = self.proto_array.read(); let i = proto_array.indices.get(block_root)?; let block = proto_array.nodes.get(*i)?; Some(block.slot) } pub fn latest_message(&self, validator_index: usize) -> Option<(Hash256, Epoch)> { let votes = self.votes.read(); if validator_index < votes.0.len() { let vote = &votes.0[validator_index]; if *vote == VoteTracker::default() { None } else { Some((vote.next_root, vote.next_epoch)) } } else { None } } pub fn as_bytes(&self) -> Vec { SszContainer::from(self).as_ssz_bytes() } pub fn from_bytes(bytes: &[u8]) -> Result { SszContainer::from_ssz_bytes(bytes) .map(Into::into) .map_err(|e| format!("Failed to decode ProtoArrayForkChoice: {:?}", e)) } } /// Returns a list of `deltas`, where there is one delta for each of the indices in /// `0..indices.len()`. /// /// The deltas are formed by a change between `old_balances` and `new_balances`, and/or a change of vote in `votes`. /// /// ## Errors /// /// - If a value in `indices` is greater to or equal to `indices.len()`. /// - If some `Hash256` in `votes` is not a key in `indices` (except for `Hash256::zero()`, this is /// always valid). fn compute_deltas( indices: &HashMap, votes: &mut ElasticList, old_balances: &[u64], new_balances: &[u64], ) -> Result, Error> { let mut deltas = vec![0_i64; indices.len()]; for (val_index, vote) in votes.iter_mut().enumerate() { // There is no need to create a score change if the validator has never voted or both their // votes are for the zero hash (alias to the genesis block). if vote.current_root == Hash256::zero() && vote.next_root == Hash256::zero() { continue; } // If the validator was not included in the _old_ balances (i.e., it did not exist yet) // then say its balance was zero. let old_balance = old_balances.get(val_index).copied().unwrap_or_else(|| 0); // If the validators vote is not known in the _new_ balances, then use a balance of zero. // // It is possible that there is a vote for an unknown validator if we change our justified // state to a new state with a higher epoch that is on a different fork because that fork may have // on-boarded less validators than the prior fork. let new_balance = new_balances.get(val_index).copied().unwrap_or_else(|| 0); if vote.current_root != vote.next_root || old_balance != new_balance { // We ignore the vote if it is not known in `indices`. We assume that it is outside // of our tree (i.e., pre-finalization) and therefore not interesting. if let Some(current_delta_index) = indices.get(&vote.current_root).copied() { let delta = deltas .get_mut(current_delta_index) .ok_or_else(|| Error::InvalidNodeDelta(current_delta_index))? .checked_sub(old_balance as i64) .ok_or_else(|| Error::DeltaOverflow(current_delta_index))?; // Array access safe due to check on previous line. deltas[current_delta_index] = delta; } // We ignore the vote if it is not known in `indices`. We assume that it is outside // of our tree (i.e., pre-finalization) and therefore not interesting. if let Some(next_delta_index) = indices.get(&vote.next_root).copied() { let delta = deltas .get(next_delta_index) .ok_or_else(|| Error::InvalidNodeDelta(next_delta_index))? .checked_add(new_balance as i64) .ok_or_else(|| Error::DeltaOverflow(next_delta_index))?; // Array access safe due to check on previous line. deltas[next_delta_index] = delta; } vote.current_root = vote.next_root; } } Ok(deltas) } #[cfg(test)] mod test_compute_deltas { use super::*; /// Gives a hash that is not the zero hash (unless i is `usize::max_value)`. fn hash_from_index(i: usize) -> Hash256 { Hash256::from_low_u64_be(i as u64 + 1) } #[test] fn zero_hash() { let validator_count: usize = 16; let mut indices = HashMap::new(); let mut votes = ElasticList::default(); let mut old_balances = vec![]; let mut new_balances = vec![]; for i in 0..validator_count { indices.insert(hash_from_index(i), i); votes.0.push(VoteTracker { current_root: Hash256::zero(), next_root: Hash256::zero(), next_epoch: Epoch::new(0), }); old_balances.push(0); new_balances.push(0); } let deltas = compute_deltas(&indices, &mut votes, &old_balances, &new_balances) .expect("should compute deltas"); assert_eq!( deltas.len(), validator_count, "deltas should have expected length" ); assert_eq!( deltas, vec![0; validator_count], "deltas should all be zero" ); for vote in votes.0 { assert_eq!( vote.current_root, vote.next_root, "the vote shoulds should have been updated" ); } } #[test] fn all_voted_the_same() { const BALANCE: u64 = 42; let validator_count: usize = 16; let mut indices = HashMap::new(); let mut votes = ElasticList::default(); let mut old_balances = vec![]; let mut new_balances = vec![]; for i in 0..validator_count { indices.insert(hash_from_index(i), i); votes.0.push(VoteTracker { current_root: Hash256::zero(), next_root: hash_from_index(0), next_epoch: Epoch::new(0), }); old_balances.push(BALANCE); new_balances.push(BALANCE); } let deltas = compute_deltas(&indices, &mut votes, &old_balances, &new_balances) .expect("should compute deltas"); assert_eq!( deltas.len(), validator_count, "deltas should have expected length" ); for (i, delta) in deltas.into_iter().enumerate() { if i == 0 { assert_eq!( delta, BALANCE as i64 * validator_count as i64, "zero'th root should have a delta" ); } else { assert_eq!(delta, 0, "all other deltas should be zero"); } } for vote in votes.0 { assert_eq!( vote.current_root, vote.next_root, "the vote shoulds should have been updated" ); } } #[test] fn different_votes() { const BALANCE: u64 = 42; let validator_count: usize = 16; let mut indices = HashMap::new(); let mut votes = ElasticList::default(); let mut old_balances = vec![]; let mut new_balances = vec![]; for i in 0..validator_count { indices.insert(hash_from_index(i), i); votes.0.push(VoteTracker { current_root: Hash256::zero(), next_root: hash_from_index(i), next_epoch: Epoch::new(0), }); old_balances.push(BALANCE); new_balances.push(BALANCE); } let deltas = compute_deltas(&indices, &mut votes, &old_balances, &new_balances) .expect("should compute deltas"); assert_eq!( deltas.len(), validator_count, "deltas should have expected length" ); for delta in deltas.into_iter() { assert_eq!( delta, BALANCE as i64, "each root should have the same delta" ); } for vote in votes.0 { assert_eq!( vote.current_root, vote.next_root, "the vote shoulds should have been updated" ); } } #[test] fn moving_votes() { const BALANCE: u64 = 42; let validator_count: usize = 16; let mut indices = HashMap::new(); let mut votes = ElasticList::default(); let mut old_balances = vec![]; let mut new_balances = vec![]; for i in 0..validator_count { indices.insert(hash_from_index(i), i); votes.0.push(VoteTracker { current_root: hash_from_index(0), next_root: hash_from_index(1), next_epoch: Epoch::new(0), }); old_balances.push(BALANCE); new_balances.push(BALANCE); } let deltas = compute_deltas(&indices, &mut votes, &old_balances, &new_balances) .expect("should compute deltas"); assert_eq!( deltas.len(), validator_count, "deltas should have expected length" ); let total_delta = BALANCE as i64 * validator_count as i64; for (i, delta) in deltas.into_iter().enumerate() { if i == 0 { assert_eq!( delta, 0 - total_delta, "zero'th root should have a negative delta" ); } else if i == 1 { assert_eq!(delta, total_delta, "first root should have positive delta"); } else { assert_eq!(delta, 0, "all other deltas should be zero"); } } for vote in votes.0 { assert_eq!( vote.current_root, vote.next_root, "the vote shoulds should have been updated" ); } } #[test] fn move_out_of_tree() { const BALANCE: u64 = 42; let mut indices = HashMap::new(); let mut votes = ElasticList::default(); // There is only one block. indices.insert(hash_from_index(1), 0); // There are two validators. let old_balances = vec![BALANCE; 2]; let new_balances = vec![BALANCE; 2]; // One validator moves their vote from the block to the zero hash. votes.0.push(VoteTracker { current_root: hash_from_index(1), next_root: Hash256::zero(), next_epoch: Epoch::new(0), }); // One validator moves their vote from the block to something outside the tree. votes.0.push(VoteTracker { current_root: hash_from_index(1), next_root: Hash256::from_low_u64_be(1337), next_epoch: Epoch::new(0), }); let deltas = compute_deltas(&indices, &mut votes, &old_balances, &new_balances) .expect("should compute deltas"); assert_eq!(deltas.len(), 1, "deltas should have expected length"); assert_eq!( deltas[0], 0 - BALANCE as i64 * 2, "the block should have lost both balances" ); for vote in votes.0 { assert_eq!( vote.current_root, vote.next_root, "the vote shoulds should have been updated" ); } } #[test] fn changing_balances() { const OLD_BALANCE: u64 = 42; const NEW_BALANCE: u64 = OLD_BALANCE * 2; let validator_count: usize = 16; let mut indices = HashMap::new(); let mut votes = ElasticList::default(); let mut old_balances = vec![]; let mut new_balances = vec![]; for i in 0..validator_count { indices.insert(hash_from_index(i), i); votes.0.push(VoteTracker { current_root: hash_from_index(0), next_root: hash_from_index(1), next_epoch: Epoch::new(0), }); old_balances.push(OLD_BALANCE); new_balances.push(NEW_BALANCE); } let deltas = compute_deltas(&indices, &mut votes, &old_balances, &new_balances) .expect("should compute deltas"); assert_eq!( deltas.len(), validator_count, "deltas should have expected length" ); for (i, delta) in deltas.into_iter().enumerate() { if i == 0 { assert_eq!( delta, 0 - OLD_BALANCE as i64 * validator_count as i64, "zero'th root should have a negative delta" ); } else if i == 1 { assert_eq!( delta, NEW_BALANCE as i64 * validator_count as i64, "first root should have positive delta" ); } else { assert_eq!(delta, 0, "all other deltas should be zero"); } } for vote in votes.0 { assert_eq!( vote.current_root, vote.next_root, "the vote shoulds should have been updated" ); } } #[test] fn validator_appears() { const BALANCE: u64 = 42; let mut indices = HashMap::new(); let mut votes = ElasticList::default(); // There are two blocks. indices.insert(hash_from_index(1), 0); indices.insert(hash_from_index(2), 1); // There is only one validator in the old balances. let old_balances = vec![BALANCE; 1]; // There are two validators in the new balances. let new_balances = vec![BALANCE; 2]; // Both validator move votes from block 1 to block 2. for _ in 0..2 { votes.0.push(VoteTracker { current_root: hash_from_index(1), next_root: hash_from_index(2), next_epoch: Epoch::new(0), }); } let deltas = compute_deltas(&indices, &mut votes, &old_balances, &new_balances) .expect("should compute deltas"); assert_eq!(deltas.len(), 2, "deltas should have expected length"); assert_eq!( deltas[0], 0 - BALANCE as i64, "block 1 should have only lost one balance" ); assert_eq!( deltas[1], 2 * BALANCE as i64, "block 2 should have gained two balances" ); for vote in votes.0 { assert_eq!( vote.current_root, vote.next_root, "the vote shoulds should have been updated" ); } } #[test] fn validator_disappears() { const BALANCE: u64 = 42; let mut indices = HashMap::new(); let mut votes = ElasticList::default(); // There are two blocks. indices.insert(hash_from_index(1), 0); indices.insert(hash_from_index(2), 1); // There are two validators in the old balances. let old_balances = vec![BALANCE; 2]; // There is only one validator in the new balances. let new_balances = vec![BALANCE; 1]; // Both validator move votes from block 1 to block 2. for _ in 0..2 { votes.0.push(VoteTracker { current_root: hash_from_index(1), next_root: hash_from_index(2), next_epoch: Epoch::new(0), }); } let deltas = compute_deltas(&indices, &mut votes, &old_balances, &new_balances) .expect("should compute deltas"); assert_eq!(deltas.len(), 2, "deltas should have expected length"); assert_eq!( deltas[0], 0 - BALANCE as i64 * 2, "block 1 should have lost both balances" ); assert_eq!( deltas[1], BALANCE as i64, "block 2 should have only gained one balance" ); for vote in votes.0 { assert_eq!( vote.current_root, vote.next_root, "the vote shoulds should have been updated" ); } } }