-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from ckb-cell/feature/watch-only
feat: add subcommand to provide JSON-RPC APIs without updating any CKB cells
- Loading branch information
Showing
3 changed files
with
94 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
//! The `watch` sub-command. | ||
use std::{net::SocketAddr, path::PathBuf, thread, time}; | ||
|
||
use clap::Parser; | ||
|
||
use crate::{ | ||
components::{ApiServiceConfig, SpvService, Storage}, | ||
prelude::*, | ||
result::{Error, Result}, | ||
}; | ||
|
||
#[derive(Parser)] | ||
pub struct Args { | ||
#[clap(flatten)] | ||
pub(crate) common: super::CommonArgs, | ||
|
||
/// The directory, which stores all cached data. | ||
#[arg(long)] | ||
pub(crate) data_dir: PathBuf, | ||
|
||
#[clap(flatten)] | ||
pub(crate) ckb: super::CkbRoArgs, | ||
|
||
#[clap(flatten)] | ||
pub(crate) bitcoin: super::BitcoinArgs, | ||
|
||
/// The JSON-RPC server's listen address. | ||
#[arg(long)] | ||
pub(crate) listen_address: SocketAddr, | ||
|
||
/// A interval in seconds. | ||
/// | ||
/// - When no better bitcoin blocks, waiting for several seconds. | ||
/// - After a CKB transaction is sent, waiting for several seconds. | ||
#[arg(long, default_value = "30")] | ||
pub(crate) interval: u64, | ||
|
||
/// The batch size that how many Bitcoin headers will be downloaded at once. | ||
#[arg(long, default_value = "30")] | ||
pub(crate) bitcoin_headers_download_batch_size: u32, | ||
} | ||
|
||
impl Args { | ||
pub fn execute(&self) -> Result<()> { | ||
log::info!("Starting the Bitcoin SPV service (readonly)"); | ||
|
||
let storage = Storage::new(&self.data_dir)?; | ||
if !storage.is_initialized()? { | ||
let msg = format!( | ||
"user-provided data directory \"{}\" is empty, please initialize it", | ||
self.data_dir.display() | ||
); | ||
return Err(Error::other(msg)); | ||
} | ||
let ckb_cli = self.ckb.client(); | ||
let btc_cli = self.bitcoin.client(); | ||
|
||
let spv_service = SpvService { | ||
ckb_cli: ckb_cli.clone(), | ||
btc_cli: btc_cli.clone(), | ||
storage: storage.clone(), | ||
}; | ||
|
||
let _api_service = ApiServiceConfig::new(self.listen_address).start(spv_service.clone()); | ||
|
||
loop { | ||
if !spv_service.sync_storage(self.bitcoin_headers_download_batch_size)? { | ||
continue; | ||
} | ||
self.take_a_break(); | ||
} | ||
|
||
// TODO Handle Ctrl-C and clean resources before exit. | ||
} | ||
|
||
fn take_a_break(&self) { | ||
thread::sleep(time::Duration::from_secs(self.interval)); | ||
} | ||
} |