-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support stop at specific height (#1581)
* feat: support stop at specific height * fix: fix fmt and clippy warnings
- Loading branch information
Showing
6 changed files
with
104 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use std::sync::RwLock; | ||
|
||
use protocol::tokio::{self}; | ||
|
||
pub enum StopOpt { | ||
MineNBlocks(u64), | ||
MineToHeight(u64), | ||
} | ||
|
||
type SignalSender = tokio::sync::oneshot::Sender<()>; | ||
|
||
pub struct StopSignal { | ||
tx: RwLock<Option<SignalSender>>, | ||
stop_at_height: Option<u64>, | ||
} | ||
|
||
impl StopSignal { | ||
pub fn new(tx: SignalSender) -> Self { | ||
Self { | ||
tx: RwLock::new(Some(tx)), | ||
stop_at_height: None, | ||
} | ||
} | ||
|
||
pub fn with_stop_at(tx: SignalSender, height: u64) -> Self { | ||
Self { | ||
tx: RwLock::new(Some(tx)), | ||
stop_at_height: Some(height), | ||
} | ||
} | ||
|
||
pub fn check_height_and_send(&self, height: u64) { | ||
if Some(height) == self.stop_at_height { | ||
self.send(); | ||
} | ||
} | ||
|
||
pub fn send(&self) { | ||
if let Some(tx) = self.tx.write().unwrap().take() { | ||
let _ = tx.send(()); | ||
} | ||
} | ||
|
||
pub fn is_stopped(&self) -> bool { | ||
self.tx.read().unwrap().is_none() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters