mirror of
https://github.com/darkrenaissance/darkfi.git
synced 2026-01-10 07:08:05 -05:00
minerd: Allow stopping the miner at given blockchain height
This commit is contained in:
@@ -11,11 +11,17 @@ RUST_TARGET = $(shell rustc -Vv | grep '^host: ' | cut -d' ' -f2)
|
||||
# Uncomment when doing musl static builds
|
||||
#RUSTFLAGS = -C target-feature=+crt-static -C link-self-contained=yes
|
||||
|
||||
SRC = \
|
||||
Cargo.toml \
|
||||
../../Cargo.toml \
|
||||
$(shell find src -type f -name '*.rs') \
|
||||
$(shell find ../../src -type f -name '*.rs')
|
||||
|
||||
BIN = $(shell grep '^name = ' Cargo.toml | cut -d' ' -f3 | tr -d '"')
|
||||
|
||||
all: $(BIN)
|
||||
|
||||
$(BIN):
|
||||
$(BIN): $(SRC)
|
||||
RUSTFLAGS="$(RUSTFLAGS)" $(CARGO) build --target=$(RUST_TARGET) --release --package $@
|
||||
cp -f ../../target/$(RUST_TARGET)/release/$@ $@
|
||||
cp -f ../../target/$(RUST_TARGET)/release/$@ ../../$@
|
||||
|
||||
@@ -46,6 +46,8 @@ pub type MinerNodePtr = Arc<MinerNode>;
|
||||
pub struct MinerNode {
|
||||
/// PoW miner number of threads to use
|
||||
threads: usize,
|
||||
/// Stop mining at this height
|
||||
stop_at_height: u32,
|
||||
/// Sender to stop miner threads
|
||||
sender: Sender<()>,
|
||||
/// Receiver to stop miner threads
|
||||
@@ -55,8 +57,19 @@ pub struct MinerNode {
|
||||
}
|
||||
|
||||
impl MinerNode {
|
||||
pub fn new(threads: usize, sender: Sender<()>, stop_signal: Receiver<()>) -> MinerNodePtr {
|
||||
Arc::new(Self { threads, sender, stop_signal, rpc_connections: Mutex::new(HashSet::new()) })
|
||||
pub fn new(
|
||||
threads: usize,
|
||||
stop_at_height: u32,
|
||||
sender: Sender<()>,
|
||||
stop_signal: Receiver<()>,
|
||||
) -> MinerNodePtr {
|
||||
Arc::new(Self {
|
||||
threads,
|
||||
stop_at_height,
|
||||
sender,
|
||||
stop_signal,
|
||||
rpc_connections: Mutex::new(HashSet::new()),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,14 +89,14 @@ impl Minerd {
|
||||
///
|
||||
/// Corresponding communication channels are setup to generate a new `MinerNode`,
|
||||
/// and a new task is generated to handle the JSON-RPC API.
|
||||
pub fn init(threads: usize) -> MinerdPtr {
|
||||
pub fn init(threads: usize, stop_at_height: u32) -> MinerdPtr {
|
||||
info!(target: "minerd::Minerd::init", "Initializing a new mining daemon...");
|
||||
|
||||
// Initialize the smol channels to send signal between the threads
|
||||
let (sender, stop_signal) = smol::channel::bounded(1);
|
||||
|
||||
// Generate the node
|
||||
let node = MinerNode::new(threads, sender, stop_signal);
|
||||
let node = MinerNode::new(threads, stop_at_height, sender, stop_signal);
|
||||
|
||||
// Generate the JSON-RPC task
|
||||
let rpc_task = StoppableTask::new();
|
||||
@@ -189,7 +202,7 @@ fn minerd_programmatic_control() -> Result<()> {
|
||||
.finish(|| {
|
||||
smol::block_on(async {
|
||||
// Initialize a daemon
|
||||
let daemon = Minerd::init(threads);
|
||||
let daemon = Minerd::init(threads, 0);
|
||||
|
||||
// Start it
|
||||
daemon.start(&ex, &rpc_settings);
|
||||
|
||||
@@ -45,6 +45,10 @@ struct Args {
|
||||
/// PoW miner number of threads to use
|
||||
threads: usize,
|
||||
|
||||
#[structopt(long, default_value = "0")]
|
||||
/// Refuse mining at given height (0 mines forever)
|
||||
stop_at_height: u32,
|
||||
|
||||
#[structopt(short, long)]
|
||||
/// Set log file to ouput into
|
||||
log: Option<String>,
|
||||
@@ -57,7 +61,7 @@ struct Args {
|
||||
async_daemonize!(realmain);
|
||||
async fn realmain(args: Args, ex: Arc<Executor<'static>>) -> Result<()> {
|
||||
info!(target: "minerd", "Starting DarkFi Mining Daemon...");
|
||||
let daemon = Minerd::init(args.threads);
|
||||
let daemon = Minerd::init(args.threads, args.stop_at_height);
|
||||
daemon.start(&ex, &args.rpc.into());
|
||||
|
||||
// Signal handling for graceful termination.
|
||||
|
||||
@@ -104,6 +104,12 @@ impl MinerNode {
|
||||
let block_hash = block.hash();
|
||||
info!(target: "minerd::rpc", "Received request to mine block {} for target: {}", block_hash, target);
|
||||
|
||||
// If we have a requested mining height, we'll keep dropping here.
|
||||
if self.stop_at_height > 0 && block.header.height >= self.stop_at_height {
|
||||
info!(target: "minerd::rpc", "Reached requested mining height {}", self.stop_at_height);
|
||||
return server_error(RpcError::MiningFailed, id, None)
|
||||
}
|
||||
|
||||
// Check if another request is being processed
|
||||
if let Some(e) = self.abort_pending(id).await {
|
||||
return e
|
||||
|
||||
Reference in New Issue
Block a user