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

feature/no-fork-cli-flag #1

Draft
wants to merge 3 commits into
base: btp-releases/1-2
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ name = "sawtooth-devmode-engine-rust"
version = "1.2.5"
authors = ["Intel Corporation"]
description = "Hyperledger Sawtooth DevMode Rust consensus engine"
edition = "2021"

[[bin]]
name = "devmode-engine-rust"
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 +25,6 @@ assets = [
["target/release/devmode-engine-rust", "/usr/bin/devmode-engine-rust", "755"]
]
maintainer-scripts = "packaging/ubuntu"

[features]
no-forking = []
6 changes: 3 additions & 3 deletions bin/run_docker_test
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def main():
inspect = [
'docker', 'inspect',
'-f', "{{.State.ExitCode}}",
"{}_{}_1".format(
"{}-{}-1".format(
isolation_id,
test_service)
]
Expand All @@ -97,7 +97,7 @@ def main():

for service in compose_dict['services']:
scrape += [
'--filter', 'name={}_{}_1'.format(isolation_id, service),
'--filter', 'name={}-{}-1'.format(isolation_id, service),
]

timer = Timer(args.timeout)
Expand Down Expand Up @@ -310,7 +310,7 @@ def _validate_compose_dict(compose_dict, test_service, compose_file):
def _check_for_existing_containers(compose_file, compose_dict, isolation_id):
containers = _get_existing_containers()
for service in compose_dict['services'].keys():
container_name_to_create = "{}_{}_1".format(isolation_id, service)
container_name_to_create = "{}-{}-1".format(isolation_id, service)
for existing_container_name in containers:
if container_name_to_create == existing_container_name:
raise RunDockerTestError(
Expand Down
85 changes: 50 additions & 35 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 Down Expand Up @@ -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));
debug!("Getting block {}", to_hex(block_id));
self.service
.get_blocks(vec![block_id.clone()])
.expect("Failed to get block")
Expand Down Expand Up @@ -141,7 +144,7 @@ impl DevmodeService {
Ok(_) => {}
Err(Error::InvalidState(_)) => {}
Err(err) => {
panic!("Failed to cancel block: {:?}", err);
panic!("Failed to cancel block: {err:?}");
}
};
}
Expand All @@ -164,7 +167,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 @@ -200,7 +203,7 @@ impl DevmodeService {
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
Expand Down Expand Up @@ -282,31 +285,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 +426,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
40 changes: 19 additions & 21 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,23 @@
* ------------------------------------------------------------------------------
*/

#[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::LogLevelFilter;
use log4rs::append::console::ConsoleAppender;
use log4rs::config::{Appender, Config, Root};
use log4rs::encode::pattern::PatternEncoder;
#[macro_use]
extern crate clap;
#[macro_use]
extern crate log;

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 All @@ -49,13 +48,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 All @@ -68,10 +66,10 @@ fn main() {
.build(Root::builder().appender("stdout").build(console_log_level))
.unwrap_or_else(|err| {
error!("{}", err);
process::exit(1);
process::exit(1)
});

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