From 75b11f34b4d527828495a32376f5f480a8d9360e Mon Sep 17 00:00:00 2001 From: Joseph Livesey Date: Tue, 31 Jan 2023 19:36:53 -0500 Subject: [PATCH] fix: fix dependency imports Signed-off-by: Joseph Livesey --- Cargo.toml | 1 + src/engine.rs | 96 +++++++++++++++++++++++++++------------------------ src/main.rs | 31 +++++++---------- 3 files changed, 65 insertions(+), 63 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c21d2986..62c54e51 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/engine.rs b/src/engine.rs index 248689be..0e9b04f3 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -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]; @@ -49,7 +52,7 @@ 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") @@ -57,7 +60,7 @@ impl DevmodeService { #[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") @@ -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 = 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) ); @@ -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(_)) => {} @@ -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"); @@ -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) } @@ -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); } } @@ -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) @@ -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 { @@ -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; @@ -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); } } @@ -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) ); @@ -344,7 +350,7 @@ impl Engine for DevmodeEngine { .unwrap() { DevmodeMessage::Published => { - info!( + log::info!( "Received block published message from {}: {}", to_hex(&sender_id), to_hex(&message.content) @@ -352,7 +358,7 @@ impl Engine for DevmodeEngine { } DevmodeMessage::Received => { - info!( + log::info!( "Received block received message from {}: {}", to_hex(&sender_id), to_hex(&message.content) @@ -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) @@ -377,7 +383,7 @@ impl Engine for DevmodeEngine { } Err(RecvTimeoutError::Disconnected) => { - error!("Disconnected from validator"); + log::error!("Disconnected from validator"); break; } @@ -385,7 +391,7 @@ impl Engine for DevmodeEngine { } 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; diff --git a/src/main.rs b/src/main.rs index e38e4c8d..2de40a10 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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() { @@ -66,12 +61,12 @@ 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); }); @@ -79,7 +74,7 @@ fn main() { driver .start(endpoint, DevmodeEngine::new()) .unwrap_or_else(|err| { - error!("{}", err); + log::error!("{}", err); process::exit(1); }); }