script/research/consensud: fmt

This commit is contained in:
aggstam
2022-03-21 15:06:12 +02:00
committed by parazyd
parent 7dd944ab5c
commit 243484f6f9
3 changed files with 20 additions and 23 deletions

View File

@@ -1,13 +1,9 @@
use std::{
net::SocketAddr,
thread,
time,
};
use std::{net::SocketAddr, thread, time};
use easy_parallel::Parallel;
use async_executor::Executor;
use async_std::sync::Arc;
use clap::{IntoApp, Parser};
use easy_parallel::Parallel;
use serde::{Deserialize, Serialize};
use simplelog::{ColorChoice, TermLogger, TerminalMode};
@@ -75,23 +71,22 @@ async fn api_service_init(executor: Arc<Executor<'_>>, config: &ConsensusdConfig
/// 2. Nodes count not hard coded.
/// 3. Proposed block broadcast.
fn consensus_task(config: &ConsensusdConfig) {
let state_path = expand_path(&config.state_path).unwrap();
let id = config.id;
let nodes_count = 1;
println!("Waiting for state initialization...");
thread::sleep(time::Duration::from_secs(10));
// After initialization node should wait for next epoch
let state = State::load_current_state(id, &state_path).unwrap();
let seconds_until_next_epoch = state.get_seconds_until_next_epoch_start();
println!("Waiting for next epoch({:?} sec)...", seconds_until_next_epoch);
thread::sleep(seconds_until_next_epoch);
loop {
let state = State::load_current_state(id, &state_path).unwrap();
let proposed_block =
let state = State::load_current_state(id, &state_path).unwrap();
let proposed_block =
if state.check_if_epoch_leader(nodes_count) { state.propose_block() } else { None };
if proposed_block.is_none() {
println!("Node is not the epoch leader. Sleeping till next epoch...");
@@ -99,11 +94,11 @@ fn consensus_task(config: &ConsensusdConfig) {
// TODO: Proposed block broadcast.
println!("Node is the epoch leader. Proposed block: {:?}", proposed_block);
}
let seconds_until_next_epoch = state.get_seconds_until_next_epoch_start();
println!("Waiting for next epoch({:?} sec)...", seconds_until_next_epoch);
thread::sleep(seconds_until_next_epoch);
};
}
}
const CONFIG_FILE_CONTENTS: &[u8] = include_bytes!("../consensusd_config.toml");
@@ -127,7 +122,7 @@ async fn main() -> Result<()> {
let api_ex = main_ex.clone();
let (signal, shutdown) = async_channel::unbounded::<()>();
let signal1 = signal.clone();
let signal2 = signal.clone();
let signal2 = signal.clone();
let (result, _) = Parallel::new()
// Run the RCP API service future in background.
.add(|| {

View File

@@ -1,14 +1,14 @@
pub mod api_service;
pub mod block;
pub mod blockchain;
pub mod api_service;
pub mod metadata;
pub mod state;
pub mod util;
pub mod vote;
pub use api_service::APIService;
pub use block::Block;
pub use blockchain::Blockchain;
pub use api_service::APIService;
pub use metadata::Metadata;
pub use state::State;
pub use vote::Vote;

View File

@@ -1,3 +1,4 @@
use chrono::{NaiveDateTime, Utc};
use serde::{Deserialize, Serialize};
use std::{
collections::hash_map::DefaultHasher,
@@ -5,7 +6,6 @@ use std::{
path::PathBuf,
time::Duration,
};
use chrono::{NaiveDateTime, Utc};
use super::{
block::Block,
@@ -58,17 +58,19 @@ impl State {
pub fn append_tx(&mut self, tx: String) {
self.unconfirmed_txs.push(tx);
}
/// Node calculates seconds until next epoch starting time.
/// Epochs duration is configured using the delta value.
pub fn get_seconds_until_next_epoch_start(&self) -> Duration {
let delta = 10;
let start_time = NaiveDateTime::from_timestamp(self.genesis_time.0, 0);
let current_epoch = self.get_current_epoch() + 1;
let next_epoch_start_timestamp = (current_epoch * (2 * delta)) + (start_time.timestamp() as u64);
let next_epoch_start = NaiveDateTime::from_timestamp(next_epoch_start_timestamp.try_into().unwrap(), 0);
let current_epoch = self.get_current_epoch() + 1;
let next_epoch_start_timestamp =
(current_epoch * (2 * delta)) + (start_time.timestamp() as u64);
let next_epoch_start =
NaiveDateTime::from_timestamp(next_epoch_start_timestamp.try_into().unwrap(), 0);
let current_time = NaiveDateTime::from_timestamp(Utc::now().timestamp(), 0);
let diff = next_epoch_start - current_time;
let diff = next_epoch_start - current_time;
Duration::new(diff.num_seconds().try_into().unwrap(), 0)
}