Skip to content

Commit

Permalink
first working version that tracks hbbft messages as JSON.
Browse files Browse the repository at this point in the history
  • Loading branch information
SurfingNerd committed Aug 4, 2021
1 parent 945b4d7 commit 2210e3e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 19 deletions.
2 changes: 1 addition & 1 deletion crates/ethcore/src/engines/hbbft/hbbft_engine.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::block_reward_hbbft::BlockRewardContract;
use block::ExecutedBlock;
use client::traits::{EngineClient, ForceUpdateSealing, TransactionRequest};
use client::traits::{EngineClient, ForceUpdateSealing};
use crypto::publickey::Signature;
use engines::{
default_system_or_code_call, signer::EngineSigner, Engine, EngineError, ForkChoice, Seal,
Expand Down
61 changes: 43 additions & 18 deletions crates/ethcore/src/engines/hbbft/hbbft_message_memorium.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@ use hbbft::honey_badger::{self};

// use threshold_crypto::{SignatureShare};
use engines::hbbft::NodeId;
use hbbft::honey_badger::Message;
// use hbbft::honey_badger::Message;
use serde::{Deserialize, Serialize};
use serde_json::{json, Result, Value};
use std::{borrow::Borrow, collections::BTreeMap};
use std::{
borrow::Borrow,
collections::BTreeMap,
fs::{self, create_dir_all, File},
io::Write,
path::{Path, PathBuf},
};

pub type HbMessage = honey_badger::Message<NodeId>;

Expand Down Expand Up @@ -34,44 +40,63 @@ pub(crate) struct HbbftMessageMemorium {
// HbMessage: message
// */
agreements: BTreeMap<u64, Vec<(NodeId, NodeId, HbMessage)>>,

message_tracking_id: u64,
}

impl HbbftMessageMemorium {
pub fn new() -> Self {
HbbftMessageMemorium {
signature_shares: BTreeMap::new(),
agreements: BTreeMap::new(),
message_tracking_id: 0,
}
}

pub fn on_message_string_received(&self, message: String, epoch: u64) {
pub fn on_message_string_received(&mut self, message_json: String, epoch: u64) {
self.message_tracking_id += 1;
let mut path_buf = PathBuf::from(format!(
"data/messages/{}/message_{}.json",
epoch, message_tracking_id
));
if let Err(e) = create_dir_all(path_buf.as_path()) {
warn!("Error creating key directory: {:?}", e);
return;
};

path_buf.push(format!("{}", self.message_tracking_id));

let path = path_buf.as_path();
let mut file = match File::create(&path) {
Ok(file) => file,
Err(e) => {
warn!(target: "consensus", "Error creating hbbft memorial file: {:?}", e);
return;
}
};

// if (message.contains("")
if let Err(e) = file.write(message_json.as_bytes()) {
warn!(target: "consensus", "Error writing hbbft memorial file: {:?}", e);
}
}

pub fn on_message_received(&self, message: &HbMessage) {
//performance: dispatcher pattern could improve performance a lot.
let message_string = format!("{:?}", message);
pub fn on_message_received(&mut self, message: &HbMessage) {
//performance: dispatcher pattern + multithreading could improve performance a lot.

let epoch = message.epoch();

match serde_json::to_string(message) {
Ok(jsonString) => {
debug!(target: "consensus", "{}", jsonString);
Ok(json_string) => {
debug!(target: "consensus", "{}", json_string);
self.on_message_string_received(json_string, epoch);
}
Err(e) => {
error!(target: "consensus", "could not create json.");
}
}
}

// how to figure out proposer ?

self.on_message_string_received(message_string, epoch);

//the details are imprisionated
// todo implementation.

// let Message(share) = message;
// let bytes = share.to_bytes();
pub fn free_epoch_memory(&mut self, epoch: u64) {
self.signature_shares.remove(&epoch);
}
}

0 comments on commit 2210e3e

Please sign in to comment.