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

Commit

Permalink
feat: add no-forking feature
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 22b9358 commit a80daed
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 34 deletions.
9 changes: 6 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ path = "src/main.rs"

[dependencies]
clap = "2"
log = "0.3.0"
log4rs = "0.7.0"
log = "0.4.17"
log4rs = "1.2.0"
rand = "0.4.2"
sawtooth-sdk = "0.4"
sawtooth-sdk = "0.5.2"

[package.metadata.deb]
maintainer = "sawtooth"
Expand All @@ -24,3 +24,6 @@ assets = [
["target/release/devmode-engine-rust", "/usr/bin/devmode-engine-rust", "755"]
]
maintainer-scripts = "packaging/ubuntu"

[features]
no-forking = []
58 changes: 35 additions & 23 deletions src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl DevmodeService {

#[allow(clippy::ptr_arg)]
fn get_block(&mut self, block_id: &BlockId) -> Block {
debug!("Getting block {}", to_hex(&block_id));
debug!("Getting block {}", to_hex(block_id));
self.service
.get_blocks(vec![block_id.clone()])
.expect("Failed to get block")
Expand Down Expand Up @@ -164,7 +164,7 @@ impl DevmodeService {
#[allow(clippy::ptr_arg)]
fn send_block_ack(&mut self, sender_id: &PeerId, block_id: BlockId) {
self.service
.send_to(&sender_id, "ack", block_id)
.send_to(sender_id, "ack", block_id)
.expect("Failed to send block ack");
}

Expand Down Expand Up @@ -282,31 +282,43 @@ impl Engine for DevmodeEngine {
DisplayBlock(&block)
);

// Advance the chain if possible.
if block.block_num > chain_head.block_num
|| (block.block_num == chain_head.block_num
&& block.block_id > chain_head.block_id)
{
info!("Committing {}", DisplayBlock(&block));
service.commit_block(block_id);
} else if block.block_num < chain_head.block_num {
let mut chain_block = chain_head;
loop {
chain_block = service.get_block(&chain_block.previous_id);
if chain_block.block_num == block.block_num {
break;
}
}
if block.block_id > chain_block.block_id {
info!("Switching to new fork {}", DisplayBlock(&block));
if cfg!(feature = "no-forking") {
if block.block_num > chain_head.block_num
&& block.block_num == chain_head.block_num + 1
{
info!("Committing {}", DisplayBlock(&block));
service.commit_block(block_id);
} else {
info!("Ignoring fork {}", DisplayBlock(&block));
info!("Ignoring {}", DisplayBlock(&block));
service.ignore_block(block_id);
}
} else {
info!("Ignoring {}", DisplayBlock(&block));
service.ignore_block(block_id);
// Advance the chain if possible.
if block.block_num > chain_head.block_num
|| (block.block_num == chain_head.block_num
&& block.block_id > chain_head.block_id)
{
info!("Committing {}", DisplayBlock(&block));
service.commit_block(block_id);
} else if block.block_num < chain_head.block_num {
let mut chain_block = chain_head;
loop {
chain_block = service.get_block(&chain_block.previous_id);
if chain_block.block_num == block.block_num {
break;
}
}
if block.block_id > chain_block.block_id {
info!("Switching to new fork {}", DisplayBlock(&block));
service.commit_block(block_id);
} else {
info!("Ignoring fork {}", DisplayBlock(&block));
service.ignore_block(block_id);
}
} else {
info!("Ignoring {}", DisplayBlock(&block));
service.ignore_block(block_id);
}
}
}

Expand Down Expand Up @@ -411,7 +423,7 @@ impl<'b> fmt::Display for DisplayBlock<'b> {
fn to_hex(bytes: &[u8]) -> String {
let mut buf = String::new();
for b in bytes {
write!(&mut buf, "{:0x}", b).expect("Unable to write to string");
write!(&mut buf, "{b:0x}").expect("Unable to write to string");
}

buf
Expand Down
15 changes: 7 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ mod engine;

use std::process;

use log::LogLevelFilter;
use log::LevelFilter;
use log4rs::append::console::ConsoleAppender;
use log4rs::config::{Appender, Config, Root};
use log4rs::encode::pattern::PatternEncoder;
Expand All @@ -49,13 +49,12 @@ fn main() {
.value_of("connect")
.unwrap_or("tcp://localhost:5050");

let console_log_level;
match matches.occurrences_of("verbose") {
0 => console_log_level = LogLevelFilter::Warn,
1 => console_log_level = LogLevelFilter::Info,
2 => console_log_level = LogLevelFilter::Debug,
_ => console_log_level = LogLevelFilter::Trace,
}
let console_log_level = match matches.occurrences_of("verbose") {
0 => LevelFilter::Warn,
1 => LevelFilter::Info,
2 => LevelFilter::Debug,
_ => LevelFilter::Trace,
};

let stdout = ConsoleAppender::builder()
.encoder(Box::new(PatternEncoder::new(
Expand Down

0 comments on commit a80daed

Please sign in to comment.