-
Notifications
You must be signed in to change notification settings - Fork 21
smite-scenarios: sync target chain view via RPC after mining #211
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
NishantBansal2003
wants to merge
1
commit into
lnfuzz:master
Choose a base branch
from
NishantBansal2003:target-cli
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -14,7 +14,7 @@ use smite::bitcoin::BitcoinCli; | |
| use smite::process::ManagedProcess; | ||
|
|
||
| use super::bitcoind; | ||
| use super::{Target, TargetError, check_crash_log}; | ||
| use super::{Target, TargetError, TargetRpc, check_crash_log}; | ||
|
|
||
| /// Configuration for the LDK target. | ||
| pub struct LdkConfig { | ||
|
|
@@ -41,14 +41,49 @@ impl LdkConfig { | |
| bitcoind::BitcoindConfig { | ||
| rpc_port: self.bitcoind_rpc_port, | ||
| p2p_port: self.bitcoind_p2p_port, | ||
| // signals the wrapper (SIGUSR1) to sync on each new block instead | ||
| // of waiting for the next poll. | ||
| extra_args: vec!["-blocknotify=pkill -USR1 -f ^ldk-node-wrapper".to_string()], | ||
| ..bitcoind::BitcoindConfig::default() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// RPC handle for interacting with LDK node target. | ||
| /// | ||
| /// LDK currently has no RPC socket, so commands are delivered through signals | ||
| /// that invoke the corresponding APIs directly. | ||
| #[derive(Debug, Clone)] | ||
| pub struct LdkRpc; | ||
|
|
||
| impl TargetRpc for LdkRpc { | ||
| /// Signals the wrapper (SIGUSR1) to sync on new blocks immediately instead | ||
| /// of waiting for its regular poll interval, allowing it to sync faster. | ||
| /// | ||
| /// # Panics | ||
| /// | ||
| /// - If `pkill -USR1 ldk-node-wrapper` fails to execute. | ||
| /// - If `pkill` fails for any reason other than the wrapper being gone, | ||
| /// which means the call itself is at fault rather than the target having | ||
| /// crashed. | ||
| fn chain_sync(&mut self) { | ||
| let out = Command::new("pkill") | ||
| .arg("-USR1") | ||
| .arg("-f") | ||
| .arg("^ldk-node-wrapper") | ||
| .output() | ||
| .expect("pkill -USR1 ldk-node-wrapper should not fail"); | ||
|
|
||
| match out.status.code() { | ||
| Some(0) => {} | ||
| // pkill exits 1 when nothing matches, so LDK is gone, check_alive | ||
| // will report the crash at the end. | ||
| Some(1) => log::warn!("ldk-node-wrapper is not running, skipping chain sync"), | ||
| _ => panic!( | ||
| "pkill -USR1 ldk-node-wrapper failed: {}", | ||
| String::from_utf8_lossy(&out.stderr) | ||
| ), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// LDK Lightning node target. | ||
| /// | ||
| /// Field order matters: `ldk` is declared before `bitcoind` so it drops first, | ||
|
|
@@ -90,28 +125,6 @@ impl LdkTarget { | |
| cmd.env("LD_PRELOAD", handler); | ||
| } | ||
|
|
||
| // Ignore SIGUSR1 for the window between exec and the wrapper blocking | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some comments at |
||
| // it. Initial-block generation triggers a burst of asynchronous | ||
| // `-blocknotify` (`pkill -USR1`), and a stray one landing in that window | ||
| // would kill the wrapper, since SIGUSR1 is fatal by default. SIG_IGN | ||
| // survives exec; a caught handler would not. | ||
| // | ||
| // Signals arriving while SIG_IGN is in effect are dropped, but that ends | ||
| // once the wrapper calls `pthread_sigmask(SIG_BLOCK)`: blocking wins over | ||
| // the disposition, so SIGUSR1 then stays pending for `sigwait()` instead | ||
| // of being discarded. SIG_IGN therefore stays in effect for the whole run | ||
| // and the wrapper never needs to replace it. | ||
| // | ||
| // SAFETY: runs in the child after fork, before exec; calls only the | ||
| // async-signal-safe `signal`. | ||
| unsafe { | ||
| use std::os::unix::process::CommandExt; | ||
| cmd.pre_exec(|| { | ||
| libc::signal(libc::SIGUSR1, libc::SIG_IGN); | ||
| Ok(()) | ||
| }); | ||
| } | ||
|
|
||
| let mut ldk = ManagedProcess::spawn(&mut cmd, "ldk-node-wrapper")?; | ||
|
|
||
| // Parse pubkey from stdout. The wrapper prints: | ||
|
|
@@ -150,6 +163,7 @@ impl LdkTarget { | |
|
|
||
| impl Target for LdkTarget { | ||
| type Config = LdkConfig; | ||
| type Rpc = LdkRpc; | ||
|
|
||
| fn start(config: Self::Config) -> Result<Self, TargetError> { | ||
| let (data_path, temp_dir) = bitcoind::resolve_data_dir()?; | ||
|
|
@@ -178,6 +192,10 @@ impl Target for LdkTarget { | |
| self.addr | ||
| } | ||
|
|
||
| fn rpc(&self) -> Self::Rpc { | ||
| LdkRpc | ||
| } | ||
|
|
||
| fn bitcoin_cli(&self) -> &BitcoinCli { | ||
| &self.bitcoin_cli | ||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I realized this should now be renamed to
ClnRpc. I'll update it