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

Commit 75b11f3

Browse files
committed
fix: fix dependency imports
Signed-off-by: Joseph Livesey <joseph.livesey@btp.works>
1 parent 84959ef commit 75b11f3

3 files changed

Lines changed: 65 additions & 63 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ name = "sawtooth-devmode-engine-rust"
33
version = "1.2.5"
44
authors = ["Intel Corporation"]
55
description = "Hyperledger Sawtooth DevMode Rust consensus engine"
6+
edition = "2018"
67

78
[[bin]]
89
name = "devmode-engine-rust"

src/engine.rs

Lines changed: 51 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,19 @@
1515
* ------------------------------------------------------------------------------
1616
*/
1717

18-
use std::fmt::{self, Write};
19-
use std::str::FromStr;
20-
use std::sync::mpsc::{Receiver, RecvTimeoutError};
21-
use std::thread::sleep;
22-
use std::time;
23-
24-
use rand;
25-
use rand::Rng;
26-
27-
use sawtooth_sdk::consensus::{engine::*, service::Service};
18+
use std::{
19+
fmt::{self, Write},
20+
str::FromStr,
21+
sync::mpsc::{Receiver, RecvTimeoutError},
22+
thread::sleep,
23+
time,
24+
};
25+
26+
use rand::{thread_rng, Rng};
27+
use sawtooth_sdk::consensus::{
28+
engine::{Block, BlockId, Engine, Error, PeerId, StartupState, Update},
29+
service::Service,
30+
};
2831

2932
const DEFAULT_WAIT_TIME: u64 = 0;
3033
const NULL_BLOCK_IDENTIFIER: [u8; 8] = [0, 0, 0, 0, 0, 0, 0, 0];
@@ -49,15 +52,15 @@ impl DevmodeService {
4952
}
5053

5154
fn get_chain_head(&mut self) -> Block {
52-
debug!("Getting chain head");
55+
log::debug!("Getting chain head");
5356
self.service
5457
.get_chain_head()
5558
.expect("Failed to get chain head")
5659
}
5760

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

6871
fn initialize_block(&mut self) {
69-
debug!("Initializing block");
72+
log::debug!("Initializing block");
7073
self.service
7174
.initialize_block(None)
7275
.expect("Failed to initialize");
7376
}
7477

7578
fn finalize_block(&mut self) -> BlockId {
76-
debug!("Finalizing block");
79+
log::debug!("Finalizing block");
7780
let mut summary = self.service.summarize_block();
7881
while let Err(Error::BlockNotReady) = summary {
7982
if !self.log_guard.not_ready_to_summarize {
8083
self.log_guard.not_ready_to_summarize = true;
81-
debug!("Block not ready to summarize");
84+
log::debug!("Block not ready to summarize");
8285
}
8386
sleep(time::Duration::from_secs(1));
8487
summary = self.service.summarize_block();
8588
}
8689
self.log_guard.not_ready_to_summarize = false;
8790
let summary = summary.expect("Failed to summarize block");
88-
debug!("Block has been summarized successfully");
91+
log::debug!("Block has been summarized successfully");
8992

9093
let consensus: Vec<u8> = create_consensus(&summary);
9194
let mut block_id = self.service.finalize_block(consensus.clone());
9295
while let Err(Error::BlockNotReady) = block_id {
9396
if !self.log_guard.not_ready_to_finalize {
9497
self.log_guard.not_ready_to_finalize = true;
95-
debug!("Block not ready to finalize");
98+
log::debug!("Block not ready to finalize");
9699
}
97100
sleep(time::Duration::from_secs(1));
98101
block_id = self.service.finalize_block(consensus.clone());
99102
}
100103
self.log_guard.not_ready_to_finalize = false;
101104
let block_id = block_id.expect("Failed to finalize block");
102-
debug!(
105+
log::debug!(
103106
"Block has been finalized successfully: {}",
104107
to_hex(&block_id)
105108
);
@@ -108,35 +111,35 @@ impl DevmodeService {
108111
}
109112

110113
fn check_block(&mut self, block_id: BlockId) {
111-
debug!("Checking block {}", to_hex(&block_id));
114+
log::debug!("Checking block {}", to_hex(&block_id));
112115
self.service
113116
.check_blocks(vec![block_id])
114117
.expect("Failed to check block");
115118
}
116119

117120
fn fail_block(&mut self, block_id: BlockId) {
118-
debug!("Failing block {}", to_hex(&block_id));
121+
log::debug!("Failing block {}", to_hex(&block_id));
119122
self.service
120123
.fail_block(block_id)
121124
.expect("Failed to fail block");
122125
}
123126

124127
fn ignore_block(&mut self, block_id: BlockId) {
125-
debug!("Ignoring block {}", to_hex(&block_id));
128+
log::debug!("Ignoring block {}", to_hex(&block_id));
126129
self.service
127130
.ignore_block(block_id)
128131
.expect("Failed to ignore block")
129132
}
130133

131134
fn commit_block(&mut self, block_id: BlockId) {
132-
debug!("Committing block {}", to_hex(&block_id));
135+
log::debug!("Committing block {}", to_hex(&block_id));
133136
self.service
134137
.commit_block(block_id)
135138
.expect("Failed to commit block");
136139
}
137140

138141
fn cancel_block(&mut self) {
139-
debug!("Canceling block");
142+
log::debug!("Canceling block");
140143
match self.service.cancel_block() {
141144
Ok(_) => {}
142145
Err(Error::InvalidState(_)) => {}
@@ -147,7 +150,7 @@ impl DevmodeService {
147150
}
148151

149152
fn broadcast_published_block(&mut self, block_id: BlockId) {
150-
debug!("Broadcasting published block: {}", to_hex(&block_id));
153+
log::debug!("Broadcasting published block: {}", to_hex(&block_id));
151154
self.service
152155
.broadcast("published", block_id)
153156
.expect("Failed to broadcast published block");
@@ -195,18 +198,18 @@ impl DevmodeService {
195198
let min_wait_time: u64 = ints[0];
196199
let max_wait_time: u64 = ints[1];
197200

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

200203
if min_wait_time >= max_wait_time {
201204
DEFAULT_WAIT_TIME
202205
} else {
203-
rand::thread_rng().gen_range(min_wait_time, max_wait_time)
206+
thread_rng().gen_range(min_wait_time, max_wait_time)
204207
}
205208
} else {
206209
DEFAULT_WAIT_TIME
207210
};
208211

209-
info!("Wait time: {:?}", wait_time);
212+
log::info!("Wait time: {:?}", wait_time);
210213

211214
time::Duration::from_secs(wait_time)
212215
}
@@ -246,25 +249,25 @@ impl Engine for DevmodeEngine {
246249

247250
match incoming_message {
248251
Ok(update) => {
249-
debug!("Received message: {}", message_type(&update));
252+
log::debug!("Received message: {}", message_type(&update));
250253

251254
match update {
252255
Update::Shutdown => {
253256
break;
254257
}
255258
Update::BlockNew(block) => {
256-
info!("Checking consensus data: {}", DisplayBlock(&block));
259+
log::info!("Checking consensus data: {}", DisplayBlock(&block));
257260

258261
if block.previous_id == NULL_BLOCK_IDENTIFIER {
259-
warn!("Received genesis block; ignoring");
262+
log::warn!("Received genesis block; ignoring");
260263
continue;
261264
}
262265

263266
if check_consensus(&block) {
264-
info!("Passed consensus check: {}", DisplayBlock(&block));
267+
log::info!("Passed consensus check: {}", DisplayBlock(&block));
265268
service.check_block(block.block_id);
266269
} else {
267-
info!("Failed consensus check: {}", DisplayBlock(&block));
270+
log::info!("Failed consensus check: {}", DisplayBlock(&block));
268271
service.fail_block(block.block_id);
269272
}
270273
}
@@ -276,7 +279,7 @@ impl Engine for DevmodeEngine {
276279

277280
chain_head = service.get_chain_head();
278281

279-
info!(
282+
log::info!(
280283
"Choosing between chain heads -- current: {} -- new: {}",
281284
DisplayBlock(&chain_head),
282285
DisplayBlock(&block)
@@ -286,10 +289,10 @@ impl Engine for DevmodeEngine {
286289
if block.block_num > chain_head.block_num
287290
&& block.block_num == chain_head.block_num + 1
288291
{
289-
info!("Committing {}", DisplayBlock(&block));
292+
log::info!("Committing {}", DisplayBlock(&block));
290293
service.commit_block(block_id);
291294
} else {
292-
info!("Ignoring {}", DisplayBlock(&block));
295+
log::info!("Ignoring {}", DisplayBlock(&block));
293296
service.ignore_block(block_id);
294297
}
295298
} else {
@@ -298,7 +301,7 @@ impl Engine for DevmodeEngine {
298301
|| (block.block_num == chain_head.block_num
299302
&& block.block_id > chain_head.block_id)
300303
{
301-
info!("Committing {}", DisplayBlock(&block));
304+
log::info!("Committing {}", DisplayBlock(&block));
302305
service.commit_block(block_id);
303306
} else if block.block_num < chain_head.block_num {
304307
let mut chain_block = chain_head;
@@ -309,14 +312,17 @@ impl Engine for DevmodeEngine {
309312
}
310313
}
311314
if block.block_id > chain_block.block_id {
312-
info!("Switching to new fork {}", DisplayBlock(&block));
315+
log::info!(
316+
"Switching to new fork {}",
317+
DisplayBlock(&block)
318+
);
313319
service.commit_block(block_id);
314320
} else {
315-
info!("Ignoring fork {}", DisplayBlock(&block));
321+
log::info!("Ignoring fork {}", DisplayBlock(&block));
316322
service.ignore_block(block_id);
317323
}
318324
} else {
319-
info!("Ignoring {}", DisplayBlock(&block));
325+
log::info!("Ignoring {}", DisplayBlock(&block));
320326
service.ignore_block(block_id);
321327
}
322328
}
@@ -325,7 +331,7 @@ impl Engine for DevmodeEngine {
325331
// The chain head was updated, so abandon the
326332
// block in progress and start a new one.
327333
Update::BlockCommit(new_chain_head) => {
328-
info!(
334+
log::info!(
329335
"Chain head updated to {}, abandoning block in progress",
330336
to_hex(&new_chain_head)
331337
);
@@ -344,15 +350,15 @@ impl Engine for DevmodeEngine {
344350
.unwrap()
345351
{
346352
DevmodeMessage::Published => {
347-
info!(
353+
log::info!(
348354
"Received block published message from {}: {}",
349355
to_hex(&sender_id),
350356
to_hex(&message.content)
351357
);
352358
}
353359

354360
DevmodeMessage::Received => {
355-
info!(
361+
log::info!(
356362
"Received block received message from {}: {}",
357363
to_hex(&sender_id),
358364
to_hex(&message.content)
@@ -361,7 +367,7 @@ impl Engine for DevmodeEngine {
361367
}
362368

363369
DevmodeMessage::Ack => {
364-
info!(
370+
log::info!(
365371
"Received ack message from {}: {}",
366372
to_hex(&sender_id),
367373
to_hex(&message.content)
@@ -377,15 +383,15 @@ impl Engine for DevmodeEngine {
377383
}
378384

379385
Err(RecvTimeoutError::Disconnected) => {
380-
error!("Disconnected from validator");
386+
log::error!("Disconnected from validator");
381387
break;
382388
}
383389

384390
Err(RecvTimeoutError::Timeout) => {}
385391
}
386392

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

src/main.rs

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,19 @@
1515
* ------------------------------------------------------------------------------
1616
*/
1717

18-
#[macro_use]
19-
extern crate clap;
20-
#[macro_use]
21-
extern crate log;
22-
extern crate log4rs;
23-
extern crate rand;
24-
extern crate sawtooth_sdk;
25-
2618
mod engine;
2719

2820
use std::process;
2921

30-
use log::LevelFilter;
31-
use log4rs::append::console::ConsoleAppender;
32-
use log4rs::config::{Appender, Config, Root};
33-
use log4rs::encode::pattern::PatternEncoder;
34-
22+
use clap::{clap_app, crate_version};
3523
use engine::DevmodeEngine;
24+
use log::LevelFilter;
25+
use log4rs::{
26+
append::console::ConsoleAppender,
27+
config::{Appender, Root},
28+
encode::pattern::PatternEncoder,
29+
init_config, Config,
30+
};
3631
use sawtooth_sdk::consensus::zmq_driver::ZmqDriver;
3732

3833
fn main() {
@@ -66,20 +61,20 @@ fn main() {
6661
.appender(Appender::builder().build("stdout", Box::new(stdout)))
6762
.build(Root::builder().appender("stdout").build(console_log_level))
6863
.unwrap_or_else(|err| {
69-
error!("{}", err);
70-
process::exit(1);
64+
log::error!("{}", err);
65+
process::exit(1)
7166
});
7267

73-
log4rs::init_config(config).unwrap_or_else(|err| {
74-
error!("{}", err);
68+
init_config(config).unwrap_or_else(|err| {
69+
log::error!("{}", err);
7570
process::exit(1);
7671
});
7772

7873
let (driver, _stop) = ZmqDriver::new();
7974
driver
8075
.start(endpoint, DevmodeEngine::new())
8176
.unwrap_or_else(|err| {
82-
error!("{}", err);
77+
log::error!("{}", err);
8378
process::exit(1);
8479
});
8580
}

0 commit comments

Comments
 (0)