Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warnings and 80 column fix #50

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
177 changes: 90 additions & 87 deletions src/core/src/consensus_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@ use service::Poet2Service;
use enclave_sgx::WaitCertificate;
use std::collections::VecDeque;
use std::collections::HashMap;
#[macro_use]
use serde_derive;
use serde_json;

/*
* The validator state represents the state for a single
* validator at a point in time. A validator state object contains:
* key_block_claim_count (int): The number of blocks that the validator has
* claimed using the current PoET public key
* poet_public_key (str): The current PoET public key for the validator
* total_block_claim_count (int): The total number of the blocks that the
* validator has claimed
* @param key_block_claim_count (int): The number of blocks that the validator
* has claimed using the current PoET public key.
* @param poet_public_key (str): The current PoET public key for the validator.
* @param total_block_claim_count (int): The total number of blocks that the
* validator has claimed.
*
*/

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
pub struct ValidatorState {
key_block_claim_count : u64,
Expand All @@ -28,8 +27,8 @@ pub struct ValidatorState {
* The population sample represents the information
* we need to create the population estimate, which in turn is used to compute
* the local mean. A population sample object contains:
* wait_time (float): The duration from a wait certificate/timer
* local_mean (float): The local mean from a wait certificate/timer
* @param wait_time (float): The duration from a wait certificate/timer
* @param local_mean (float): The local mean from a wait certificate/timer
*/

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
Expand All @@ -40,23 +39,22 @@ pub struct PopulationSample {

/*
*
* The population estimate represents what we need
* to help in computing zTest results. A population estimate object contains:
*
* population_estimate (float): The population estimate for the corresponding
* block
* previous_block_id (str): The ID of the block previous to the one that this
* population estimate corresponds to
* validator_id (str): The ID of the validator that won the corresponding
* EstimateInfo represents what we need to help in computing zTest results.
* An Estimate Info object contains:
* @param population_estimate (float): The population estimate for the
* corresponding block
* @param previous_block_id (str): The ID of the block previous to the one that
* this population estimate corresponds to
* @param validator_id (str): The ID of the validator that won the corresponding
* block
*
*/
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
pub struct EstimateInfo {
pub population_estimate : f64,
// Needs to be of type BlockId but encapsulating structure is required to
// Needs to be of type BlockId but encapsulating structure is required to
// to be serializeable & BlockId is not at the sdk
pub previous_block_id: String,//BlockId,
pub previous_block_id: String,
pub validator_id: String
}

Expand All @@ -73,43 +71,40 @@ pub struct ConsensusState {
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
pub struct ValidatorInfo{
// Needs to be of type PeerId but encapsulating structure is required to
pub struct ValidatorInfo {
// Needs to be of type PeerId but encapsulating structure is required to
// to be serializeable & PeerId is not at the sdk
id: String,//PeerId,
id: String,
poet_public_key: String,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
pub struct PoetSettingsView{
pub struct PoetSettingsView {
population_estimate_sample_size: usize,
}

#[derive(Serialize, Deserialize, Clone, Debug, Default)]
struct BlockInfo{
struct BlockInfo {
wait_certificate: Option<WaitCertificate>,
validator_info: Option<ValidatorInfo>,
poet_settings_view: Option<PoetSettingsView>,
}

impl PartialEq for BlockInfo{
impl PartialEq for BlockInfo {
fn eq( &self, other: &BlockInfo) -> bool {
let self_ = self.clone();
let other_ = other.clone();


if (((self.wait_certificate.is_some() && other.wait_certificate.is_some())
if (((self.wait_certificate.is_some() && other.wait_certificate.is_some())
&& (self_.wait_certificate.unwrap() == other_.wait_certificate.unwrap()))
|| (self.wait_certificate.is_none() && other.wait_certificate.is_none()))
&& (((self.validator_info.is_some() && other.validator_info.is_some())
|| (self.wait_certificate.is_none() && other.wait_certificate.is_none()))
&& (((self.validator_info.is_some() && other.validator_info.is_some())
&& (self_.validator_info.unwrap() == other_.validator_info.unwrap()))
|| (self.validator_info.is_none() && other.validator_info.is_none()))
&& (((self.poet_settings_view.is_some() && other.poet_settings_view.is_some())
|| (self.validator_info.is_none() && other.validator_info.is_none()))
&& (((self.poet_settings_view.is_some() && other.poet_settings_view.is_some())
&& (self_.poet_settings_view.unwrap() == other_.poet_settings_view.unwrap()))
|| (self.poet_settings_view.is_none() && other.poet_settings_view.is_none()))

|| (self.poet_settings_view.is_none() && other.poet_settings_view.is_none()))
{ true }
else
else
{ false }
}
}
Expand All @@ -120,86 +115,94 @@ struct Entry{
value: BlockInfo,
}

impl ConsensusState{

const MINIMUM_WAIT_TIME: f64 = 1.0;
pub fn consensus_state_for_block_id(&mut self, block_id: BlockId, svc: &mut Poet2Service) -> Option<ConsensusState>{
impl ConsensusState {
pub fn consensus_state_for_block_id(&mut self, block_id: BlockId,
svc: &mut Poet2Service) -> Option<ConsensusState> {
let mut previous_wait_certificate: Option<WaitCertificate> = None;
let mut consensus_state: Option<ConsensusState> = None;
let mut wait_certificate : Option<WaitCertificate> = None;
let mut blocks: Vec<Entry> = Vec::new();
let mut current_id = block_id;
loop{
let mut block_ = svc.get_block(current_id.clone());
loop {
let block_ = svc.get_block(current_id.clone());
let block: Block;
if block_.is_ok(){
if block_.is_ok() {
block = block_.unwrap();
}
else{
else {
break;
}
/*consensus_state = consensus_state_store.get(current_id.clone());
if consensus_state != None{
break
}*/
let mut payload_vec = block.payload;
let mut payload_str = String::from_utf8(payload_vec).expect("Found Invalid UTF-8");
let mut wait_certificate = Some(serde_json::from_str(&payload_str).unwrap());
let payload_vec = block.payload;
let payload_str = String::from_utf8(payload_vec).expect("Found Invalid UTF-8");
let wait_certificate = Some(serde_json::from_str(&payload_str).unwrap());
if wait_certificate.is_some() {
//TODO
}
else if blocks.is_empty() || previous_wait_certificate.is_some(){
blocks.push(Entry{ key: current_id.clone(), value: BlockInfo{ wait_certificate: None, validator_info: None, poet_settings_view: None} });
else if blocks.is_empty() || previous_wait_certificate.is_some() {
blocks.push(Entry{ key: current_id.clone(), value:
BlockInfo{ wait_certificate: None, validator_info:
None, poet_settings_view: None} });
}
previous_wait_certificate = wait_certificate.clone();
current_id = block.previous_id;
//let mut consensus_state = consensus_state_store_.get(current_id.clone());
if consensus_state.is_none(){
consensus_state = Some(ConsensusState::default());
if consensus_state.is_none() {
consensus_state = Some(ConsensusState::default());
}
for entry in blocks.iter().rev(){
for entry in blocks.iter().rev() {
let mut val = &entry.value;
if val.wait_certificate.is_none(){
consensus_state = Some(ConsensusState::default());
}
else{
self.validator_did_claim_block(&(val.clone().validator_info.unwrap()), &(val.clone().wait_certificate.unwrap()), &(val.clone().poet_settings_view.unwrap()));
}
if val.wait_certificate.is_none() {
consensus_state = Some(ConsensusState::default());
}
else {
self.validator_did_claim_block(&(val.clone().validator_info.unwrap()),
&(val.clone().wait_certificate.unwrap()),
&(val.clone().poet_settings_view.unwrap()));
}
}
}
consensus_state
consensus_state
}

pub fn validator_did_claim_block(&mut self, validator_info: &ValidatorInfo, wait_certificate: &WaitCertificate, poet_settings_view: &PoetSettingsView ) -> (){
pub fn validator_did_claim_block(&mut self, validator_info: &ValidatorInfo,
wait_certificate: &WaitCertificate,
poet_settings_view: &PoetSettingsView ) -> () {
self.aggregate_local_mean += 5.5_f64; //wait_certificate.local_mean;
self.total_block_claim_count += 1;
self.population_samples.push_back( PopulationSample{ wait_time: wait_certificate.wait_time , local_mean: 5.5_f64}); //wait_certificate.local_mean});
while self.population_samples.len() > poet_settings_view.population_estimate_sample_size{
self.population_samples.push_back(
PopulationSample{ wait_time: wait_certificate.wait_time ,
local_mean: 5.5_f64});
while self.population_samples.len() >
poet_settings_view.population_estimate_sample_size {
self.population_samples.pop_front();
}
let mut validator_state = self.get_validator_state(validator_info.clone());
let mut total_block_claim_count = validator_state.total_block_claim_count + 1;
let mut key_block_claim_count = if validator_info.poet_public_key == validator_state.poet_public_key {
validator_state.key_block_claim_count + 1
}
else{
1
let validator_state = self.get_validator_state(validator_info.clone());
let total_block_claim_count = validator_state.total_block_claim_count + 1;
let key_block_claim_count = if validator_info.poet_public_key ==
validator_state.poet_public_key {
validator_state.key_block_claim_count + 1
}
else {
1
};
let peerid_vec = Vec::from(validator_info.clone().id);
let peerid_str = String::from_utf8(peerid_vec).expect("Found Invalid UTF-8");
self.validators.insert(peerid_str, ValidatorState{ key_block_claim_count: key_block_claim_count, poet_public_key: validator_info.clone().poet_public_key, total_block_claim_count: total_block_claim_count});

}
self.validators.insert(peerid_str,
ValidatorState{ key_block_claim_count: key_block_claim_count,
poet_public_key: validator_info.clone().poet_public_key,
total_block_claim_count: total_block_claim_count});
}

pub fn get_validator_state(&mut self, validator_info: ValidatorInfo) -> Box<ValidatorState>{
let peerid_vec = Vec::from(validator_info.clone().id);
let peerid_str = String::from_utf8(peerid_vec).expect("Found Invalid UTF-8");
let mut validator_state = self.validators.get(&peerid_str);
let mut val_state = ValidatorState{ key_block_claim_count: 0, poet_public_key: validator_info.clone().poet_public_key, total_block_claim_count: 0};
if validator_state.is_none(){
return Box::new(val_state);
}
Box::new(validator_state.unwrap().clone())
}
pub fn get_validator_state(&mut self, validator_info: ValidatorInfo) ->
Box<ValidatorState> {
let peerid_vec = Vec::from(validator_info.clone().id);
let peerid_str = String::from_utf8(peerid_vec).expect("Found Invalid UTF-8");
let validator_state = self.validators.get(&peerid_str);
let val_state = ValidatorState{ key_block_claim_count: 0,
poet_public_key: validator_info.clone().poet_public_key,
total_block_claim_count: 0};
if validator_state.is_none() {
return Box::new(val_state);
}
Box::new(validator_state.unwrap().clone())
}
}

7 changes: 3 additions & 4 deletions src/core/src/consensus_state_store.rs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
* ------------------------------------------------------------------------------
*/

use protobuf;
use consensus_state::ConsensusState;
use sawtooth_sdk::consensus::engine::BlockId;
use database::lmdb::{LmdbContext, LmdbDatabase};
use database::lmdb::{LmdbDatabase};
use database::DatabaseError;
use bincode::{serialize, deserialize};

Expand Down Expand Up @@ -57,11 +56,11 @@ impl<'a> ConsensusStateStore<'a> {
pub fn delete(&mut self, block_id: BlockId) -> Result<(), DatabaseError>{
let mut writer = self.consensus_state_db.writer()?;
writer.delete(&Vec::from(block_id))?;

Ok(())
}

pub fn put(&mut self, block_id: BlockId, consensus_state: ConsensusState) -> Result<(), DatabaseError>{
pub fn put(&mut self, block_id: BlockId, consensus_state: ConsensusState) ->
Result<(), DatabaseError>{
let mut writer = self.consensus_state_db.writer()?;
let serialized_state = serialize(&consensus_state).map_err(|err| {
DatabaseError::WriterError(format!("Failed to serialize state: {}", err))
Expand Down
3 changes: 2 additions & 1 deletion src/core/src/enclave_sgx.rs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ impl EnclaveConfig {
info!("wait certificate generated is {:?}", wait_cert);

//release wait certificate
let status = ffi::release_wait_certificate(&mut eid, &mut wait_cert_info);
ffi::release_wait_certificate(&mut eid, &mut wait_cert_info)
.expect("failed to release wait certificate");

(wait_cert, wait_cert_sign)
}
Expand Down
5 changes: 2 additions & 3 deletions src/core/src/enclave_sim.rs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl Default for WaitCertificate {
}
}

static mut last_block_number : u64 = 0_u64;
// static mut last_block_number : u64 = 0_u64;

pub struct PoetCertMap {
poet_block_id : String,
Expand Down Expand Up @@ -141,8 +141,7 @@ pub fn initialize_wait_certificate(
if !in_serialized_prev_block_wait_certificate.is_empty() {
let deserialized_prev_block_wait_certificate =
serde_json::from_str(&in_serialized_prev_block_wait_certificate);
let mut prev_wait_certificate_obj : WaitCertificate =
WaitCertificate::default();
let prev_wait_certificate_obj : WaitCertificate;

if deserialized_prev_block_wait_certificate.is_ok() {
prev_wait_certificate_obj =
Expand Down
Loading