forked from sigp/lighthouse
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request sigp#3 from wilbarnes/shard_state_processing
Shard state processing
- Loading branch information
Showing
19 changed files
with
323 additions
and
96 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
name = "shard_state_processing" | ||
version = "0.1.0" | ||
authors = ["Will Villanueva", "Wil Barnes"] | ||
edition = "2018" | ||
|
||
[dependencies] | ||
tree_hash = { path = "../utils/tree_hash" } | ||
types = { path = "../types" } | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#[macro_use] | ||
mod macros; | ||
|
||
pub mod per_shard_block_processing; | ||
pub mod per_shard_slot_processing; | ||
|
||
pub use per_shard_block_processing::{ | ||
errors::{Error as ShardBlockProcessingError}, | ||
per_shard_block_processing, process_shard_block_header, | ||
}; | ||
|
||
pub use per_shard_slot_processing::{ | ||
errors::{Error as ShardSlotProcessingError}, | ||
per_shard_slot_processing, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
macro_rules! verify { | ||
($condition: expr, $result: expr) => { | ||
if !$condition { | ||
return Err(Error::Invalid($result)); | ||
} | ||
}; | ||
} | ||
|
||
macro_rules! invalid { | ||
($result: expr) => { | ||
return Err(Error::Invalid($result)); | ||
}; | ||
} | ||
|
||
macro_rules! safe_add_assign { | ||
($a: expr, $b: expr) => { | ||
$a = $a.saturating_add($b); | ||
}; | ||
} | ||
macro_rules! safe_sub_assign { | ||
($a: expr, $b: expr) => { | ||
$a = $a.saturating_sub($b); | ||
}; | ||
} |
129 changes: 129 additions & 0 deletions
129
eth2/shard_state_processing/src/per_shard_block_processing.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
use crate::*; | ||
use types::*; | ||
use errors::Error; | ||
|
||
pub mod errors; | ||
|
||
pub fn per_shard_block_processing<T: ShardSpec, U: EthSpec>( | ||
beacon_state: &BeaconState<U>, | ||
state: &mut ShardState<T>, | ||
block: &ShardBlock, | ||
spec: &ChainSpec, | ||
) -> Result<(), Error> { | ||
process_shard_block_header(beacon_state, state, block, spec); | ||
// process_shard_attestations(state, beacon_state, block); | ||
// process_shard_block_data_fees(state, beacon_state, block); | ||
Ok(()) | ||
} | ||
|
||
pub fn process_shard_block_header<T: ShardSpec, U: EthSpec>( | ||
beacon_state: &BeaconState<U>, | ||
state: &mut ShardState<T>, | ||
block: &ShardBlock, | ||
spec: &ChainSpec, | ||
) -> Result<(), Error> { | ||
state.latest_block_header = block.temporary_block_header(spec); | ||
|
||
Ok(()) | ||
|
||
// below in progress logic that follows actual spec: | ||
// | ||
// verify!(block.slot == state.slot, ShardBlockProcessingError); | ||
// verify!(block.parent_root == signing_root(state.latest_block_header), ShardBlockProcessingError); | ||
|
||
// state.latest_block_header = block.block_header(); | ||
|
||
// let proposer_idx = get_shard_proposer_index(beacon_state, state.shard, block.slot); | ||
// let pubkey = beacon_state.validator_registry[proposer_idx].pubkey; | ||
|
||
// // perhaps the compute_epoch_of_shard_slot() function here is not correct, find the correct one | ||
// let domain = get_domain(beacon_state, spec.domain_shard_proposer, compute_epoch_of_shard_slot(block.slot)); | ||
// let proposer = &state.validator_registry[proposer_idx]; | ||
|
||
// // update the error here at some point in the near future | ||
// verify!(!proposer.slashed, ShardBlockProcessingError); | ||
|
||
// verify_block_signature(&state, &beacon_state, &block, &spec); | ||
|
||
// Ok(()) | ||
} | ||
|
||
pub fn verify_block_signature<T: ShardSpec>( | ||
state: &ShardState<T>, | ||
block: &ShardBlock, | ||
spec: &ChainSpec, | ||
) -> Result<(), Error> { | ||
// below in progress to follow actual spec | ||
// let block_proposer = &state.validator_registry | ||
// [beacon_state.get_shard_proposer_index(block.slot, RelativeEpoch::Current, spec)?]; | ||
|
||
// let domain = spec.get_domain( | ||
// block.slot.epoch(T::slots_per_epoch()), | ||
// Domain::ShardProposer, | ||
// &beacon_state.fork, | ||
// ); | ||
|
||
// verify!( | ||
// block | ||
// .signature | ||
// .verify(&block.signed_root()[..], domain, &block_proposer.pubkey) | ||
// ); | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn process_shard_attestations<T: ShardSpec, U: EthSpec>( | ||
state: &mut ShardState<T>, | ||
beacon_state: &BeaconState<U>, | ||
attestations: &[Attestation], | ||
spec: &ChainSpec, | ||
) -> Result<(), Error> { | ||
// below in progress to follow actual spec | ||
// verify!( | ||
// attestations.len() as u64 <= spec.max_attestations, | ||
// BlockProcessingError | ||
// ); | ||
|
||
// attestations | ||
// .par_iter() | ||
// .enumerate() | ||
// .try_for_each(|(i, attestation)| { | ||
// validate_shard_attestation(state, attestation, spec).map_err(|e| e.into_with_index(i)) | ||
// })?; | ||
|
||
// let shard_committee = beacon_state.get_shard_committee(state.current_epoch(), state.shard); | ||
// for (i, validator_idx) in shard_committee.iter().enumerate() { | ||
// verify_block_signature(&state, &beacon_state, ) | ||
// } | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn process_shard_block_data_fees<T: ShardSpec, U: EthSpec>( | ||
state: &mut ShardState<T>, | ||
beacon_state: &BeaconState<U>, | ||
block: &ShardBlock, | ||
spec: &ChainSpec, | ||
) -> Result<(), Error> { | ||
// below in progress to follow actual spec | ||
// let base_reward = get_shard_base_reward(beacon_state); | ||
|
||
// add_fee(state, beacon_state, proposer_index); | ||
|
||
// // NOTE: incorrect spec value | ||
// let quotient = spec.base_reward_quotient; | ||
|
||
// if block.body.len < spec.shard_block_size { | ||
// state.basefee += Gwei(cmp::max(1, state.basefee * block.body.len - spec.shard_block_size_target) / quotient) | ||
// } else { | ||
// state.basefee -= Gwei(cmp::min((1, spec.effective_balance_increment | ||
// / spec.epochs_per_shard_period | ||
// / spec.shard_slots_per_epoch) | ||
// ) | ||
// ); | ||
// }; | ||
|
||
// state.basefee = Gwei(); | ||
|
||
Ok(()) | ||
} |
28 changes: 28 additions & 0 deletions
28
eth2/shard_state_processing/src/per_shard_block_processing/add_fee.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use crate::*; | ||
use types::*; | ||
|
||
pub fn add_fee<T: ShardState, U: BeaconState>( | ||
state: &ShardState, | ||
beacon_state: &BeaconState, | ||
index: u64, | ||
) -> Result<(), Error> { | ||
// let epoch = self.current_epoch(); | ||
// | ||
// let earlier_committee = &self | ||
// .get_period_committee(RelativePeriod::Previous, shard)? | ||
// .committee; | ||
|
||
// let later_committee = &self | ||
// .get_period_committee(RelativePeriod::Current, shard)? | ||
// .committee; | ||
|
||
// if index in earlier_committee { | ||
// state.earlier_committee_fees[earlier_committee.index(index)] += delta | ||
// } else { | ||
// state.later_committee_fees[later_committee.index(index)] += delta | ||
// }; | ||
|
||
Ok(()) | ||
} | ||
|
||
|
6 changes: 6 additions & 0 deletions
6
eth2/shard_state_processing/src/per_shard_block_processing/errors.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
use types::*; | ||
|
||
#[derive(Debug, PartialEq)] | ||
pub enum Error { | ||
BlockProcessingError, | ||
} |
19 changes: 19 additions & 0 deletions
19
eth2/shard_state_processing/src/per_shard_block_processing/process_shard_block_header.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use crate::*; | ||
use types::*; | ||
|
||
pub fn get_shard_proposer_index<T:EthSpec>( | ||
beacon_state: &BeaconState<T>, | ||
shard: u64, | ||
epoch: Epoch, | ||
) -> Result<u64, Error> { | ||
// let epoch = get_current_epoch(beacon_state); | ||
// let persistent_committee = get_period_committee() | ||
} | ||
|
||
pub fn get_persistent_committee<T: EthSpec>( | ||
beacon_state: &BeaconSTate<T>, | ||
shard: u64, | ||
epoch: Epoch, | ||
) -> Result<(), Error> { | ||
Ok(()) | ||
} |
24 changes: 24 additions & 0 deletions
24
eth2/shard_state_processing/src/per_shard_block_processing/validate_shard_attestation.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use crate::common::convert_to_indexed; | ||
use types::*; | ||
|
||
pub fn validate_attestation<T: ShardSpec>( | ||
state: &ShardState<T>, | ||
attestation: &Attestation, | ||
spec: &ChainSpec, | ||
) -> Result<(), Error> { | ||
// validate_attestation_parametric(state, attestation, spec, true, false); | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn validate_attestation_parametric<T: ShardSpec>( | ||
state: &ShardState<T>, | ||
attestation: &Attestation, | ||
spec: &ChainSpec, | ||
verify_signature: bool, | ||
time_independent_only: bool, | ||
) -> Result<(), Error> { | ||
// let attestation_slot = state.get_attestation_slot(&attestation.data)?; | ||
|
||
Ok(()) | ||
} |
29 changes: 29 additions & 0 deletions
29
eth2/shard_state_processing/src/per_shard_slot_processing.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
use crate::*; | ||
use tree_hash::TreeHash; | ||
use types::*; | ||
|
||
use process_shard_slot::process_shard_slot; | ||
|
||
pub mod errors; | ||
pub mod process_shard_slot; | ||
|
||
pub fn per_shard_slot_processing<T: ShardSpec>( | ||
state: &mut ShardState<T>, | ||
spec: &ChainSpec, | ||
) -> Result<(), Error> { | ||
if (state | ||
.slot | ||
.epoch(spec.slots_per_epoch, spec.shard_slots_per_beacon_slot) | ||
+ 1) | ||
% spec.epochs_per_shard_period | ||
== 0 | ||
{ | ||
// include period processing here :) | ||
} | ||
|
||
process_shard_slot(state, spec); | ||
|
||
state.slot += 1; | ||
|
||
Ok(()) | ||
} |
6 changes: 6 additions & 0 deletions
6
eth2/shard_state_processing/src/per_shard_slot_processing/errors.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
use types::*; | ||
|
||
#[derive(Debug, PartialEq)] | ||
pub enum Error { | ||
ShardStateError, | ||
} |
20 changes: 20 additions & 0 deletions
20
eth2/shard_state_processing/src/per_shard_slot_processing/process_shard_slot.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
use crate::*; | ||
use tree_hash::TreeHash; | ||
use types::*; | ||
|
||
pub fn process_shard_slot<T: ShardSpec>(state: &mut ShardState<T>, spec: &ChainSpec) -> () { | ||
let previous_state_root = Hash256::from_slice(&state.tree_hash_root()[..]); | ||
|
||
if state.latest_block_header.state_root == spec.zero_hash { | ||
state.latest_block_header.state_root = previous_state_root; | ||
} | ||
|
||
let mut depth = 0; | ||
|
||
while (state.slot.as_u64() % u64::pow(2, depth as u32) == 0 as u64) | ||
&& (depth < T::history_accumulator_depth() as u64) | ||
{ | ||
state.history_accumulator[depth as usize] = previous_state_root; | ||
depth += 1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.