Skip to content

Commit

Permalink
Merge pull request sigp#3 from wilbarnes/shard_state_processing
Browse files Browse the repository at this point in the history
Shard state processing
  • Loading branch information
villanuevawill committed Sep 25, 2019
2 parents fba0ecf + b856a4e commit 937322c
Show file tree
Hide file tree
Showing 19 changed files with 323 additions and 96 deletions.
File renamed without changes.
10 changes: 10 additions & 0 deletions eth2/shard_state_processing/Cargo.toml
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" }

15 changes: 15 additions & 0 deletions eth2/shard_state_processing/src/lib.rs
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,
};
24 changes: 24 additions & 0 deletions eth2/shard_state_processing/src/macros.rs
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 eth2/shard_state_processing/src/per_shard_block_processing.rs
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(())
}
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(())
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use types::*;

#[derive(Debug, PartialEq)]
pub enum Error {
BlockProcessingError,
}
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(())
}
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 eth2/shard_state_processing/src/per_shard_slot_processing.rs
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(())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use types::*;

#[derive(Debug, PartialEq)]
pub enum Error {
ShardStateError,
}
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;
}
}
6 changes: 0 additions & 6 deletions eth2/state_processing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ pub mod common;
pub mod get_genesis_state;
pub mod per_block_processing;
pub mod per_epoch_processing;
pub mod per_shard_block_processing;
pub mod per_shard_slot_processing;
pub mod per_slot_processing;

pub use get_genesis_state::get_genesis_beacon_state;
Expand All @@ -15,8 +13,4 @@ pub use per_block_processing::{
per_block_processing, per_block_processing_without_verifying_block_signature,
};
pub use per_epoch_processing::{errors::EpochProcessingError, per_epoch_processing};
pub use per_shard_block_processing::{
per_shard_block_processing, Error as ShardBlockProcessingError,
};
pub use per_shard_slot_processing::{per_shard_slot_processing, Error as ShardSlotProcessingError};
pub use per_slot_processing::{per_slot_processing, Error as SlotProcessingError};
28 changes: 0 additions & 28 deletions eth2/state_processing/src/per_shard_block_processing.rs

This file was deleted.

Loading

0 comments on commit 937322c

Please sign in to comment.