Skip to content
This repository has been archived by the owner on Sep 25, 2023. It is now read-only.

Commit

Permalink
fix: fix dependency imports
Browse files Browse the repository at this point in the history
Signed-off-by: Joseph Livesey <[email protected]>
  • Loading branch information
suchapalaver committed Feb 1, 2023
1 parent 84959ef commit 75b11f3
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 63 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ name = "sawtooth-devmode-engine-rust"
version = "1.2.5"
authors = ["Intel Corporation"]
description = "Hyperledger Sawtooth DevMode Rust consensus engine"
edition = "2018"

[[bin]]
name = "devmode-engine-rust"
Expand Down
96 changes: 51 additions & 45 deletions src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,19 @@
* ------------------------------------------------------------------------------
*/

use std::fmt::{self, Write};
use std::str::FromStr;
use std::sync::mpsc::{Receiver, RecvTimeoutError};
use std::thread::sleep;
use std::time;

use rand;
use rand::Rng;

use sawtooth_sdk::consensus::{engine::*, service::Service};
use std::{
fmt::{self, Write},
str::FromStr,
sync::mpsc::{Receiver, RecvTimeoutError},
thread::sleep,
time,
};

use rand::{thread_rng, Rng};
use sawtooth_sdk::consensus::{
engine::{Block, BlockId, Engine, Error, PeerId, StartupState, Update},
service::Service,
};

const DEFAULT_WAIT_TIME: u64 = 0;
const NULL_BLOCK_IDENTIFIER: [u8; 8] = [0, 0, 0, 0, 0, 0, 0, 0];
Expand All @@ -49,15 +52,15 @@ impl DevmodeService {
}

fn get_chain_head(&mut self) -> Block {
debug!("Getting chain head");
log::debug!("Getting chain head");
self.service
.get_chain_head()
.expect("Failed to get chain head")
}

#[allow(clippy::ptr_arg)]
fn get_block(&mut self, block_id: &BlockId) -> Block {
debug!("Getting block {}", to_hex(block_id));
log::debug!("Getting block {}", to_hex(block_id));
self.service
.get_blocks(vec![block_id.clone()])
.expect("Failed to get block")
Expand All @@ -66,40 +69,40 @@ impl DevmodeService {
}

fn initialize_block(&mut self) {
debug!("Initializing block");
log::debug!("Initializing block");
self.service
.initialize_block(None)
.expect("Failed to initialize");
}

fn finalize_block(&mut self) -> BlockId {
debug!("Finalizing block");
log::debug!("Finalizing block");
let mut summary = self.service.summarize_block();
while let Err(Error::BlockNotReady) = summary {
if !self.log_guard.not_ready_to_summarize {
self.log_guard.not_ready_to_summarize = true;
debug!("Block not ready to summarize");
log::debug!("Block not ready to summarize");
}
sleep(time::Duration::from_secs(1));
summary = self.service.summarize_block();
}
self.log_guard.not_ready_to_summarize = false;
let summary = summary.expect("Failed to summarize block");
debug!("Block has been summarized successfully");
log::debug!("Block has been summarized successfully");

let consensus: Vec<u8> = create_consensus(&summary);
let mut block_id = self.service.finalize_block(consensus.clone());
while let Err(Error::BlockNotReady) = block_id {
if !self.log_guard.not_ready_to_finalize {
self.log_guard.not_ready_to_finalize = true;
debug!("Block not ready to finalize");
log::debug!("Block not ready to finalize");
}
sleep(time::Duration::from_secs(1));
block_id = self.service.finalize_block(consensus.clone());
}
self.log_guard.not_ready_to_finalize = false;
let block_id = block_id.expect("Failed to finalize block");
debug!(
log::debug!(
"Block has been finalized successfully: {}",
to_hex(&block_id)
);
Expand All @@ -108,35 +111,35 @@ impl DevmodeService {
}

fn check_block(&mut self, block_id: BlockId) {
debug!("Checking block {}", to_hex(&block_id));
log::debug!("Checking block {}", to_hex(&block_id));
self.service
.check_blocks(vec![block_id])
.expect("Failed to check block");
}

fn fail_block(&mut self, block_id: BlockId) {
debug!("Failing block {}", to_hex(&block_id));
log::debug!("Failing block {}", to_hex(&block_id));
self.service
.fail_block(block_id)
.expect("Failed to fail block");
}

fn ignore_block(&mut self, block_id: BlockId) {
debug!("Ignoring block {}", to_hex(&block_id));
log::debug!("Ignoring block {}", to_hex(&block_id));
self.service
.ignore_block(block_id)
.expect("Failed to ignore block")
}

fn commit_block(&mut self, block_id: BlockId) {
debug!("Committing block {}", to_hex(&block_id));
log::debug!("Committing block {}", to_hex(&block_id));
self.service
.commit_block(block_id)
.expect("Failed to commit block");
}

fn cancel_block(&mut self) {
debug!("Canceling block");
log::debug!("Canceling block");
match self.service.cancel_block() {
Ok(_) => {}
Err(Error::InvalidState(_)) => {}
Expand All @@ -147,7 +150,7 @@ impl DevmodeService {
}

fn broadcast_published_block(&mut self, block_id: BlockId) {
debug!("Broadcasting published block: {}", to_hex(&block_id));
log::debug!("Broadcasting published block: {}", to_hex(&block_id));
self.service
.broadcast("published", block_id)
.expect("Failed to broadcast published block");
Expand Down Expand Up @@ -195,18 +198,18 @@ impl DevmodeService {
let min_wait_time: u64 = ints[0];
let max_wait_time: u64 = ints[1];

debug!("Min: {:?} -- Max: {:?}", min_wait_time, max_wait_time);
log::debug!("Min: {:?} -- Max: {:?}", min_wait_time, max_wait_time);

if min_wait_time >= max_wait_time {
DEFAULT_WAIT_TIME
} else {
rand::thread_rng().gen_range(min_wait_time, max_wait_time)
thread_rng().gen_range(min_wait_time, max_wait_time)
}
} else {
DEFAULT_WAIT_TIME
};

info!("Wait time: {:?}", wait_time);
log::info!("Wait time: {:?}", wait_time);

time::Duration::from_secs(wait_time)
}
Expand Down Expand Up @@ -246,25 +249,25 @@ impl Engine for DevmodeEngine {

match incoming_message {
Ok(update) => {
debug!("Received message: {}", message_type(&update));
log::debug!("Received message: {}", message_type(&update));

match update {
Update::Shutdown => {
break;
}
Update::BlockNew(block) => {
info!("Checking consensus data: {}", DisplayBlock(&block));
log::info!("Checking consensus data: {}", DisplayBlock(&block));

if block.previous_id == NULL_BLOCK_IDENTIFIER {
warn!("Received genesis block; ignoring");
log::warn!("Received genesis block; ignoring");
continue;
}

if check_consensus(&block) {
info!("Passed consensus check: {}", DisplayBlock(&block));
log::info!("Passed consensus check: {}", DisplayBlock(&block));
service.check_block(block.block_id);
} else {
info!("Failed consensus check: {}", DisplayBlock(&block));
log::info!("Failed consensus check: {}", DisplayBlock(&block));
service.fail_block(block.block_id);
}
}
Expand All @@ -276,7 +279,7 @@ impl Engine for DevmodeEngine {

chain_head = service.get_chain_head();

info!(
log::info!(
"Choosing between chain heads -- current: {} -- new: {}",
DisplayBlock(&chain_head),
DisplayBlock(&block)
Expand All @@ -286,10 +289,10 @@ impl Engine for DevmodeEngine {
if block.block_num > chain_head.block_num
&& block.block_num == chain_head.block_num + 1
{
info!("Committing {}", DisplayBlock(&block));
log::info!("Committing {}", DisplayBlock(&block));
service.commit_block(block_id);
} else {
info!("Ignoring {}", DisplayBlock(&block));
log::info!("Ignoring {}", DisplayBlock(&block));
service.ignore_block(block_id);
}
} else {
Expand All @@ -298,7 +301,7 @@ impl Engine for DevmodeEngine {
|| (block.block_num == chain_head.block_num
&& block.block_id > chain_head.block_id)
{
info!("Committing {}", DisplayBlock(&block));
log::info!("Committing {}", DisplayBlock(&block));
service.commit_block(block_id);
} else if block.block_num < chain_head.block_num {
let mut chain_block = chain_head;
Expand All @@ -309,14 +312,17 @@ impl Engine for DevmodeEngine {
}
}
if block.block_id > chain_block.block_id {
info!("Switching to new fork {}", DisplayBlock(&block));
log::info!(
"Switching to new fork {}",
DisplayBlock(&block)
);
service.commit_block(block_id);
} else {
info!("Ignoring fork {}", DisplayBlock(&block));
log::info!("Ignoring fork {}", DisplayBlock(&block));
service.ignore_block(block_id);
}
} else {
info!("Ignoring {}", DisplayBlock(&block));
log::info!("Ignoring {}", DisplayBlock(&block));
service.ignore_block(block_id);
}
}
Expand All @@ -325,7 +331,7 @@ impl Engine for DevmodeEngine {
// The chain head was updated, so abandon the
// block in progress and start a new one.
Update::BlockCommit(new_chain_head) => {
info!(
log::info!(
"Chain head updated to {}, abandoning block in progress",
to_hex(&new_chain_head)
);
Expand All @@ -344,15 +350,15 @@ impl Engine for DevmodeEngine {
.unwrap()
{
DevmodeMessage::Published => {
info!(
log::info!(
"Received block published message from {}: {}",
to_hex(&sender_id),
to_hex(&message.content)
);
}

DevmodeMessage::Received => {
info!(
log::info!(
"Received block received message from {}: {}",
to_hex(&sender_id),
to_hex(&message.content)
Expand All @@ -361,7 +367,7 @@ impl Engine for DevmodeEngine {
}

DevmodeMessage::Ack => {
info!(
log::info!(
"Received ack message from {}: {}",
to_hex(&sender_id),
to_hex(&message.content)
Expand All @@ -377,15 +383,15 @@ impl Engine for DevmodeEngine {
}

Err(RecvTimeoutError::Disconnected) => {
error!("Disconnected from validator");
log::error!("Disconnected from validator");
break;
}

Err(RecvTimeoutError::Timeout) => {}
}

if !published_at_height && time::Instant::now().duration_since(start) > wait_time {
info!("Timer expired -- publishing block");
log::info!("Timer expired -- publishing block");
let new_block_id = service.finalize_block();
published_at_height = true;

Expand Down
31 changes: 13 additions & 18 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,19 @@
* ------------------------------------------------------------------------------
*/

#[macro_use]
extern crate clap;
#[macro_use]
extern crate log;
extern crate log4rs;
extern crate rand;
extern crate sawtooth_sdk;

mod engine;

use std::process;

use log::LevelFilter;
use log4rs::append::console::ConsoleAppender;
use log4rs::config::{Appender, Config, Root};
use log4rs::encode::pattern::PatternEncoder;

use clap::{clap_app, crate_version};
use engine::DevmodeEngine;
use log::LevelFilter;
use log4rs::{
append::console::ConsoleAppender,
config::{Appender, Root},
encode::pattern::PatternEncoder,
init_config, Config,
};
use sawtooth_sdk::consensus::zmq_driver::ZmqDriver;

fn main() {
Expand Down Expand Up @@ -66,20 +61,20 @@ fn main() {
.appender(Appender::builder().build("stdout", Box::new(stdout)))
.build(Root::builder().appender("stdout").build(console_log_level))
.unwrap_or_else(|err| {
error!("{}", err);
process::exit(1);
log::error!("{}", err);
process::exit(1)
});

log4rs::init_config(config).unwrap_or_else(|err| {
error!("{}", err);
init_config(config).unwrap_or_else(|err| {
log::error!("{}", err);
process::exit(1);
});

let (driver, _stop) = ZmqDriver::new();
driver
.start(endpoint, DevmodeEngine::new())
.unwrap_or_else(|err| {
error!("{}", err);
log::error!("{}", err);
process::exit(1);
});
}

0 comments on commit 75b11f3

Please sign in to comment.