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

Integrate old shasper runtime

parent 0f36291c
Branches
Tags
No related merge requests found
......@@ -58,12 +58,16 @@ use runtime_primitives::{
use client::{
block_builder::api as block_builder_api, runtime_api as client_api
};
use srml_support::StorageMap;
use srml_support::storage::unhashed::StorageVec;
use consensus_primitives::api as consensus_api;
use version::RuntimeVersion;
#[cfg(feature = "std")]
use version::NativeVersion;
use parity_codec::Encode;
use keccak_hasher::KeccakHasher;
use spec::SpecHeader;
use ssz_hash::SpecHash;
// A few exports that help ease life for downstream crates.
#[cfg(any(feature = "std", test))]
......@@ -154,7 +158,24 @@ impl_runtime_apis! {
impl block_builder_api::BlockBuilder<Block, BasicInherentData> for Runtime {
fn apply_extrinsic(extrinsic: <Block as BlockT>::Extrinsic) -> ApplyResult {
let _extrinsic_index = <storage::UncheckedExtrinsics>::count();
let extrinsic_index = <storage::UncheckedExtrinsics>::count();
if extrinsic_index == consts::TIMESTAMP_POSITION {
<storage::Timestamp>::put(extrinsic.clone().timestamp().expect("Invalid timestamp"));
} else if extrinsic_index == consts::SLOT_POSITION {
let parent_slot = <storage::Slot>::get();
<storage::ParentSlot>::put(parent_slot);
<storage::Slot>::put(extrinsic.clone().slot().expect("Invalid slot"));
} else if extrinsic_index == consts::RANDAO_REVEAL_POSITION {
<storage::RandaoReveal>::put(extrinsic.clone().randao_reveal().expect("Invalid randao reveal"));
} else if extrinsic_index == consts::POW_CHAIN_REF_POSITION {
<storage::PowChainRef>::put(extrinsic.clone().pow_chain_ref().expect("Invalid pow chain ref"));
} else {
let attestation = extrinsic.clone().attestation().expect("Invalid attestation");
let mut attestations = <storage::Attestations>::items();
attestations.push(attestation);
<storage::Attestations>::set_items(attestations);
}
let mut extrinsics = <storage::UncheckedExtrinsics>::items();
extrinsics.push(extrinsic);
......@@ -164,6 +185,7 @@ impl_runtime_apis! {
<storage::ExtrinsicsRoot>::put(H256::from(extrinsics_root));
<storage::UncheckedExtrinsics>::set_items(extrinsics);
Ok(ApplyOutcome::Success)
}
......@@ -174,6 +196,54 @@ impl_runtime_apis! {
let extrinsics_root = <storage::ExtrinsicsRoot>::take();
let parent_hash = <storage::ParentHash>::take();
let digest = <storage::Digest>::take();
let _timestamp = <storage::Timestamp>::take();
let slot = <storage::Slot>::get();
let parent_slot = <storage::ParentSlot>::get();
let parent_header_hash = <storage::LastHeaderHash>::get();
let randao_reveal = <storage::RandaoReveal>::take();
let pow_chain_ref = <storage::PowChainRef>::take();
let attestations = <storage::Attestations>::items();
<storage::Attestations>::set_count(0);
let mut active_state = <storage::Active>::get();
let mut crystallized_state = <storage::Crystallized>::get();
validation::validate_block_pre_processing_conditions();
active_state.update_recent_block_hashes(parent_slot, slot, parent_header_hash);
validation::process_block::<storage::BlockHashesBySlot, storage::BlockVoteCache>(
slot,
parent_slot,
&crystallized_state,
&mut active_state,
&attestations
);
validation::process_cycle_transitions::<storage::BlockHashesBySlot, storage::BlockVoteCache>(
slot,
parent_header_hash,
&mut crystallized_state,
&mut active_state
);
let active_state_root = active_state.spec_hash::<KeccakHasher>();
let crystallized_state_root = crystallized_state.spec_hash::<KeccakHasher>();
let spec_header = SpecHeader {
randao_reveal, attestations, pow_chain_ref,
active_state_root, crystallized_state_root,
slot_number: slot,
parent_hash: parent_header_hash,
};
let block_hash = ssz_hash::SpecHash::spec_hash::<KeccakHasher>(&spec_header);
<storage::BlockHashesBySlot>::insert(slot, block_hash);
<storage::Active>::put(&active_state);
<storage::ActiveRoot>::put(&active_state_root);
<storage::Crystallized>::put(&crystallized_state);
<storage::CrystallizedRoot>::put(&crystallized_state_root);
<storage::LastHeaderHash>::put(&block_hash);
let state_root = BlakeTwo256::storage_root();
......
use primitives::H256;
use rstd::prelude::Vec;
use keccak_hasher::KeccakHasher;
use super::{AttestationRecord, Block};
use consts;
use super::AttestationRecord;
#[derive(Clone, PartialEq, Eq, Default, SszEncode, SszDecode, SszHash)]
#[cfg_attr(feature = "std", derive(Debug))]
......@@ -16,34 +14,3 @@ pub struct SpecHeader {
pub active_state_root: H256,
pub crystallized_state_root: H256,
}
pub trait SpecBlockExt {
fn header_spec_hash(&self, active_state_root: H256, crystallized_state_root: H256) -> H256;
}
impl SpecBlockExt for Block {
fn header_spec_hash(&self, active_state_root: H256, crystallized_state_root: H256) -> H256 {
let slot_number = self.extrinsics[consts::SLOT_POSITION as usize].clone().slot().expect("Invalid slot extrinsic");
let randao_reveal = self.extrinsics[consts::RANDAO_REVEAL_POSITION as usize].clone().randao_reveal().expect("Invalid randao reveal extrinsic");
let pow_chain_ref = self.extrinsics[consts::POW_CHAIN_REF_POSITION as usize].clone().pow_chain_ref().expect("Invalid pow chain ref extrinsic");
let attestations = (&self.extrinsics[consts::ATTESTATION_START_POSITION as usize..])
.iter()
.cloned()
.map(|extrinsic| extrinsic.attestation().expect("Invalid attestation extrinsic"))
.collect();
let header = &self.header;
let spec_header = SpecHeader {
parent_hash: header.parent_hash,
slot_number: slot_number,
randao_reveal: randao_reveal,
attestations: attestations,
pow_chain_ref: pow_chain_ref,
active_state_root: active_state_root,
crystallized_state_root: crystallized_state_root,
};
ssz_hash::SpecHash::spec_hash::<KeccakHasher>(&spec_header)
}
}
......@@ -2,6 +2,7 @@ use primitives::{H256, BlockNumber, Hash, ValidatorId};
use primitives::storage::well_known_keys;
use srml_support::storage::unhashed;
use state::{ActiveState, CrystallizedState, BlockVoteInfo};
use attestation::AttestationRecord;
use super::UncheckedExtrinsic;
use super::Digest as DigestT;
......@@ -10,6 +11,13 @@ storage_items! {
pub ParentHash: b"sys:parenthash" => default Hash;
pub ExtrinsicsRoot: b"sys:extrinsicsroot" => default Hash;
pub Digest: b"sys:digest" => default DigestT;
pub Timestamp: b"sys:timestamp" => default u64;
pub Slot: b"sys:slot" => default u64;
pub ParentSlot: b"sys:parentslot" => default u64;
pub LastHeaderHash: b"sys:lasthash" => default H256;
pub RandaoReveal: b"sys:randaoreveal" => default H256;
pub PowChainRef: b"sys:powchainref" => default H256;
pub BlockHashesBySlot: b"sys:blockhashesbyslot" => map [ u64 => H256 ];
pub Active: b"sys:active" => default ActiveState;
pub ActiveRoot: b"sys:activeroot" => default H256;
......@@ -29,3 +37,9 @@ impl unhashed::StorageVec for Authorities {
type Item = ValidatorId;
const PREFIX: &'static [u8] = well_known_keys::AUTHORITY_PREFIX;
}
pub struct Attestations;
impl unhashed::StorageVec for Attestations {
type Item = AttestationRecord;
const PREFIX: &'static [u8] = b"sys:attestations";
}
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