Skip to content

Commit

Permalink
cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusz-reichert authored and m-reichert committed Nov 4, 2024
1 parent 522e567 commit db104ff
Show file tree
Hide file tree
Showing 15 changed files with 217 additions and 197 deletions.
4 changes: 3 additions & 1 deletion src/bin/electrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,9 @@ fn main_() {
}

#[cfg(not(feature = "otlp-tracing"))]
fn main() { main_() }
fn main() {
main_()
}

#[cfg(feature = "otlp-tracing")]
#[tokio::main]
Expand Down
16 changes: 9 additions & 7 deletions src/bin/tx-fingerprint-stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,15 @@ fn main() {

//info!("{:?},{:?}", txid, blockid);

let prevouts = chain.lookup_txos(
tx.input
.iter()
.filter(|txin| has_prevout(txin))
.map(|txin| txin.previous_output)
.collect(),
).unwrap();
let prevouts = chain
.lookup_txos(
tx.input
.iter()
.filter(|txin| has_prevout(txin))
.map(|txin| txin.previous_output)
.collect(),
)
.unwrap();

let total_out: u64 = tx.output.iter().map(|out| out.value.to_sat()).sum();
let small_out = tx
Expand Down
60 changes: 30 additions & 30 deletions src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ lazy_static! {
const MAX_ATTEMPTS: u32 = 5;
const RETRY_WAIT_DURATION: Duration = Duration::from_secs(1);

#[instrument(skip_all, name="Daemon::parse_hash<T>")]
#[instrument(skip_all, name = "Daemon::parse_hash<T>")]
fn parse_hash<T>(value: &Value) -> Result<T>
where
T: FromStr,
Expand All @@ -57,7 +57,7 @@ where
.chain_err(|| format!("non-hex value: {}", value))?)
}

#[instrument(skip_all, name="Daemon::header_from_value")]
#[instrument(skip_all, name = "Daemon::header_from_value")]
fn header_from_value(value: Value) -> Result<BlockHeader> {
let header_hex = value
.as_str()
Expand Down Expand Up @@ -149,7 +149,7 @@ struct Connection {
signal: Waiter,
}

#[instrument(skip_all, name="Daemon::tcp_connect")]
#[instrument(skip_all, name = "Daemon::tcp_connect")]
fn tcp_connect(addr: SocketAddr, signal: &Waiter) -> Result<TcpStream> {
loop {
match TcpStream::connect_timeout(&addr, *DAEMON_CONNECTION_TIMEOUT) {
Expand All @@ -172,7 +172,7 @@ fn tcp_connect(addr: SocketAddr, signal: &Waiter) -> Result<TcpStream> {
}

impl Connection {
#[instrument(skip_all, name="Daemon::Connection::new")]
#[instrument(skip_all, name = "Daemon::Connection::new")]
fn new(
addr: SocketAddr,
cookie_getter: Arc<dyn CookieGetter>,
Expand All @@ -197,7 +197,7 @@ impl Connection {
Connection::new(self.addr, self.cookie_getter.clone(), self.signal.clone())
}

#[instrument(skip_all, name="Daemon::Connection::send")]
#[instrument(skip_all, name = "Daemon::Connection::send")]
fn send(&mut self, request: &str) -> Result<()> {
let cookie = &self.cookie_getter.get()?;
let msg = format!(
Expand All @@ -211,7 +211,7 @@ impl Connection {
})
}

#[instrument(skip_all, name="Daemon::Connection::recv")]
#[instrument(skip_all, name = "Daemon::Connection::recv")]
fn recv(&mut self) -> Result<String> {
// TODO: use proper HTTP parser.
let mut in_header = true;
Expand Down Expand Up @@ -392,7 +392,7 @@ impl Daemon {
})
}

#[instrument(skip_all, name="Daemon::list_blk_files")]
#[instrument(skip_all, name = "Daemon::list_blk_files")]
pub fn list_blk_files(&self) -> Result<Vec<PathBuf>> {
let path = self.blocks_dir.join("blk*.dat");
debug!("listing block files at {:?}", path);
Expand All @@ -408,7 +408,7 @@ impl Daemon {
self.network.magic()
}

#[instrument(skip_all, name="Daemon::call_jsonrpc")]
#[instrument(skip_all, name = "Daemon::call_jsonrpc")]
fn call_jsonrpc(&self, method: &str, request: &Value) -> Result<Value> {
let mut conn = self.conn.lock().unwrap();
let timer = self.latency.with_label_values(&[method]).start_timer();
Expand All @@ -426,15 +426,15 @@ impl Daemon {
Ok(result)
}

#[instrument(skip_all, name="Daemon::handle_request")]
#[instrument(skip_all, name = "Daemon::handle_request")]
fn handle_request(&self, method: &str, params: &Value) -> Result<Value> {
let id = self.message_id.next();
let req = json!({"method": method, "params": params, "id": id});
let reply = self.call_jsonrpc(method, &req)?;
parse_jsonrpc_reply(reply, method, id)
}

#[instrument(skip_all, name="Daemon::retry_request")]
#[instrument(skip_all, name = "Daemon::retry_request")]
fn retry_request(&self, method: &str, params: &Value) -> Result<Value> {
loop {
match self.handle_request(method, &params) {
Expand All @@ -450,7 +450,7 @@ impl Daemon {
}
}

#[instrument(skip_all, name="Daemon::request")]
#[instrument(skip_all, name = "Daemon::request")]
fn request(&self, method: &str, params: Value) -> Result<Value> {
self.retry_request(method, &params)
}
Expand All @@ -467,7 +467,7 @@ impl Daemon {
}
}

#[instrument(skip_all, name="Daemon::requests")]
#[instrument(skip_all, name = "Daemon::requests")]
// Send requests in parallel over multiple RPC connections as individual JSON-RPC requests (with no JSON-RPC batching),
// buffering the replies into a vector. If any of the requests fail, processing is terminated and an Err is returned.
fn requests(&self, method: &str, params_list: Vec<Value>) -> Result<Vec<Value>> {
Expand Down Expand Up @@ -498,29 +498,29 @@ impl Daemon {

// bitcoind JSONRPC API:

#[instrument(skip_all, name="Daemon::getblockchaininfo")]
#[instrument(skip_all, name = "Daemon::getblockchaininfo")]
pub fn getblockchaininfo(&self) -> Result<BlockchainInfo> {
let info: Value = self.request("getblockchaininfo", json!([]))?;
Ok(from_value(info).chain_err(|| "invalid blockchain info")?)
}

#[instrument(skip_all, name="Daemon::getnetworkinfo")]
#[instrument(skip_all, name = "Daemon::getnetworkinfo")]
fn getnetworkinfo(&self) -> Result<NetworkInfo> {
let info: Value = self.request("getnetworkinfo", json!([]))?;
Ok(from_value(info).chain_err(|| "invalid network info")?)
}

#[instrument(skip_all, name="Daemon::getbestblockhash")]
#[instrument(skip_all, name = "Daemon::getbestblockhash")]
pub fn getbestblockhash(&self) -> Result<BlockHash> {
parse_hash(&self.request("getbestblockhash", json!([]))?)
}

#[instrument(skip_all, name="Daemon::getblockheader")]
#[instrument(skip_all, name = "Daemon::getblockheader")]
pub fn getblockheader(&self, blockhash: &BlockHash) -> Result<BlockHeader> {
header_from_value(self.request("getblockheader", json!([blockhash, /*verbose=*/ false]))?)
}

#[instrument(skip_all, name="Daemon::getblockheaders")]
#[instrument(skip_all, name = "Daemon::getblockheaders")]
pub fn getblockheaders(&self, heights: &[usize]) -> Result<Vec<BlockHeader>> {
let heights: Vec<Value> = heights.iter().map(|height| json!([height])).collect();
let params_list: Vec<Value> = self
Expand All @@ -535,20 +535,20 @@ impl Daemon {
Ok(result)
}

#[instrument(skip_all, name="Daemon::getblock")]
#[instrument(skip_all, name = "Daemon::getblock")]
pub fn getblock(&self, blockhash: &BlockHash) -> Result<Block> {
let block =
block_from_value(self.request("getblock", json!([blockhash, /*verbose=*/ false]))?)?;
assert_eq!(block.block_hash(), *blockhash);
Ok(block)
}

#[instrument(skip_all, name="Daemon::getblock_raw")]
#[instrument(skip_all, name = "Daemon::getblock_raw")]
pub fn getblock_raw(&self, blockhash: &BlockHash, verbose: u32) -> Result<Value> {
self.request("getblock", json!([blockhash, verbose]))
}

#[instrument(skip_all, name="Daemon::getblocks")]
#[instrument(skip_all, name = "Daemon::getblocks")]
pub fn getblocks(&self, blockhashes: &[BlockHash]) -> Result<Vec<Block>> {
let params_list: Vec<Value> = blockhashes
.iter()
Expand Down Expand Up @@ -585,7 +585,7 @@ impl Daemon {

/// Fetch the given transactions in parallel over multiple threads and RPC connections,
/// ignoring any missing ones and returning whatever is available.
#[instrument(skip_all, name="Daemon::gettransactions_available")]
#[instrument(skip_all, name = "Daemon::gettransactions_available")]
pub fn gettransactions_available(&self, txids: &[&Txid]) -> Result<Vec<(Txid, Transaction)>> {
const RPC_INVALID_ADDRESS_OR_KEY: i64 = -5;

Expand All @@ -610,7 +610,7 @@ impl Daemon {
.collect()
}

#[instrument(skip_all, name="Daemon::gettransaction_raw")]
#[instrument(skip_all, name = "Daemon::gettransaction_raw")]
pub fn gettransaction_raw(
&self,
txid: &Txid,
Expand All @@ -620,24 +620,24 @@ impl Daemon {
self.request("getrawtransaction", json!([txid, verbose, blockhash]))
}

#[instrument(skip_all, name="getmempooltx")]
#[instrument(skip_all, name = "getmempooltx")]
pub fn getmempooltx(&self, txhash: &Txid) -> Result<Transaction> {
let value = self.request("getrawtransaction", json!([txhash, /*verbose=*/ false]))?;
tx_from_value(value)
}

#[instrument(skip_all, name="getmempooltxids")]
#[instrument(skip_all, name = "getmempooltxids")]
pub fn getmempooltxids(&self) -> Result<HashSet<Txid>> {
let res = self.request("getrawmempool", json!([/*verbose=*/ false]))?;
Ok(serde_json::from_value(res).chain_err(|| "invalid getrawmempool reply")?)
}

#[instrument(skip_all, name="broadcast")]
#[instrument(skip_all, name = "broadcast")]
pub fn broadcast(&self, tx: &Transaction) -> Result<Txid> {
self.broadcast_raw(&serialize_hex(tx))
}

#[instrument(skip_all, name="broadcast_raw")]
#[instrument(skip_all, name = "broadcast_raw")]
pub fn broadcast_raw(&self, txhex: &str) -> Result<Txid> {
let txid = self.request("sendrawtransaction", json!([txhex]))?;
Ok(
Expand All @@ -649,7 +649,7 @@ impl Daemon {
// Get estimated feerates for the provided confirmation targets using a batch RPC request
// Missing estimates are logged but do not cause a failure, whatever is available is returned
#[allow(clippy::float_cmp)]
#[instrument(skip_all, name="Daemon::estimatesmartfee_batch")]
#[instrument(skip_all, name = "Daemon::estimatesmartfee_batch")]
pub fn estimatesmartfee_batch(&self, conf_targets: &[u16]) -> Result<HashMap<u16, f64>> {
let params_list: Vec<Value> = conf_targets
.iter()
Expand Down Expand Up @@ -684,7 +684,7 @@ impl Daemon {
.collect())
}

#[instrument(skip_all, name="Daemon::get_all_headers")]
#[instrument(skip_all, name = "Daemon::get_all_headers")]
fn get_all_headers(&self, tip: &BlockHash) -> Result<Vec<BlockHeader>> {
let info: Value = self.request("getblockheader", json!([tip]))?;
let tip_height = info
Expand Down Expand Up @@ -712,7 +712,7 @@ impl Daemon {
}

// Returns a list of BlockHeaders in ascending height (i.e. the tip is last).
#[instrument(skip_all, name="Daemon::get_new_headers")]
#[instrument(skip_all, name = "Daemon::get_new_headers")]
pub fn get_new_headers(
&self,
indexed_headers: &HeaderList,
Expand Down Expand Up @@ -745,7 +745,7 @@ impl Daemon {
Ok(new_headers)
}

#[instrument(skip_all, name="Daemon::get_relayfee")]
#[instrument(skip_all, name = "Daemon::get_relayfee")]
pub fn get_relayfee(&self) -> Result<f64> {
let relayfee = self.getnetworkinfo()?.relayfee;

Expand Down
44 changes: 27 additions & 17 deletions src/electrum/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -547,23 +547,33 @@ mod tests {
false,
None,
));
discovery.add_default_server(
"electrum.blockstream.info".into(),
vec![Service::Tcp(60001)],
).unwrap();
discovery.add_default_server("testnet.hsmiths.com".into(), vec![Service::Ssl(53012)]).unwrap();
discovery.add_default_server(
"tn.not.fyi".into(),
vec![Service::Tcp(55001), Service::Ssl(55002)],
).unwrap();
discovery.add_default_server(
"electrum.blockstream.info".into(),
vec![Service::Tcp(60001), Service::Ssl(60002)],
).unwrap();
discovery.add_default_server(
"explorerzydxu5ecjrkwceayqybizmpjjznk5izmitf2modhcusuqlid.onion".into(),
vec![Service::Tcp(143)],
).unwrap();
discovery
.add_default_server(
"electrum.blockstream.info".into(),
vec![Service::Tcp(60001)],
)
.unwrap();
discovery
.add_default_server("testnet.hsmiths.com".into(), vec![Service::Ssl(53012)])
.unwrap();
discovery
.add_default_server(
"tn.not.fyi".into(),
vec![Service::Tcp(55001), Service::Ssl(55002)],
)
.unwrap();
discovery
.add_default_server(
"electrum.blockstream.info".into(),
vec![Service::Tcp(60001), Service::Ssl(60002)],
)
.unwrap();
discovery
.add_default_server(
"explorerzydxu5ecjrkwceayqybizmpjjznk5izmitf2modhcusuqlid.onion".into(),
vec![Service::Tcp(143)],
)
.unwrap();

debug!("{:#?}", discovery);

Expand Down
Loading

0 comments on commit db104ff

Please sign in to comment.