Skip to content
Snippets Groups Projects
Commit 4db98365 authored by Wei Tang's avatar Wei Tang
Browse files

Handle block vote cache

parent 1ee4a0d9
Branches
No related merge requests found
......@@ -22,7 +22,7 @@ pub struct ActiveState {
pub recent_block_hashes: Vec<H256>,
}
#[derive(Encode, Decode)]
#[derive(Encode, Decode, Default)]
pub struct BlockVoteInfo {
pub voter_indices: Vec<usize>,
pub total_voter_deposits: u128,
......
use primitives::H256;
use runtime_support::storage::StorageMap;
use state::{ActiveState, CrystallizedState};
use state::{ActiveState, CrystallizedState, BlockVoteInfo};
use attestation::AttestationRecord;
use consts::CYCLE_LENGTH;
......@@ -48,3 +48,28 @@ pub fn validate_attestation<JustifiedBlockHashes: StorageMap<u64, H256, Query=Op
attestation.verify_signatures(&parent_hashes, &pubkeys);
}
pub fn update_block_vote_cache<BlockVoteCache: StorageMap<H256, BlockVoteInfo, Query=Option<BlockVoteInfo>>>(
slot: u64,
crystallized_state: &CrystallizedState,
active_state: &ActiveState,
attestation: &AttestationRecord
) {
let parent_hashes = active_state.signed_parent_block_hashes(slot, attestation);
let attestation_indices = crystallized_state.attestation_indices(attestation);
for parent_hash in parent_hashes {
if attestation.oblique_parent_hashes.contains(&parent_hash) {
continue;
}
let mut info = BlockVoteCache::get(&parent_hash).unwrap_or_default();
for (i, index) in attestation_indices.iter().enumerate() {
if attestation.attester_bitfield.has_voted(i) && !info.voter_indices.contains(index) {
info.voter_indices.push(*index);
info.total_voter_deposits += crystallized_state.validators[*index].balance;
}
}
BlockVoteCache::insert(parent_hash, info);
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment