Просмотр исходного кода

minerd: Allow stopping the miner at given blockchain height

parazyd 1 год назад
Родитель
Сommit
817db023f6
4 измененных файлов с 36 добавлено и 7 удалено
  1. 7 1
      bin/minerd/Makefile
  2. 18 5
      bin/minerd/src/lib.rs
  3. 5 1
      bin/minerd/src/main.rs
  4. 6 0
      bin/minerd/src/rpc.rs

+ 7 - 1
bin/minerd/Makefile

@@ -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/$@ ../../$@

+ 18 - 5
bin/minerd/src/lib.rs

@@ -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);

+ 5 - 1
bin/minerd/src/main.rs

@@ -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.

+ 6 - 0
bin/minerd/src/rpc.rs

@@ -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