Skip to content

Commit

Permalink
Rename scripthash status
Browse files Browse the repository at this point in the history
  • Loading branch information
romanz committed Aug 13, 2021
1 parent 1c198de commit 27e7541
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 22 deletions.
8 changes: 4 additions & 4 deletions src/bin/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use bitcoin::{Address, Amount};
use std::collections::BTreeMap;
use std::str::FromStr;

use electrs::{Balance, Cache, Config, Daemon, ScriptHash, Status, Tracker};
use electrs::{Balance, Cache, Config, Daemon, ScriptHash, ScriptHashStatus, Tracker};

fn main() -> Result<()> {
let config = Config::from_args();
Expand All @@ -19,9 +19,9 @@ fn main() -> Result<()> {
let cache = Cache::default();
let daemon = Daemon::connect(&config)?;
let mut tracker = Tracker::new(&config)?;
let mut map: BTreeMap<Address, Status> = addresses
let mut map: BTreeMap<Address, ScriptHashStatus> = addresses
.map(|addr| {
let status = Status::new(ScriptHash::new(&addr.script_pubkey()));
let status = ScriptHashStatus::new(ScriptHash::new(&addr.script_pubkey()));
(addr, status)
})
.collect();
Expand All @@ -30,7 +30,7 @@ fn main() -> Result<()> {
tracker.sync(&daemon)?;
let mut total = Amount::ZERO;
for (addr, status) in map.iter_mut() {
tracker.update_status(status, &daemon, &cache)?;
tracker.update_scripthash_status(status, &daemon, &cache)?;
let balance = tracker.get_balance(status, &cache);
if balance != Balance::default() {
info!("{} has {}", addr, balance.confirmed());
Expand Down
20 changes: 10 additions & 10 deletions src/electrum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{
daemon::{self, extract_bitcoind_error, Daemon},
merkle::Proof,
metrics::Histogram,
status::Status,
status::ScriptHashStatus,
tracker::Tracker,
types::ScriptHash,
};
Expand All @@ -31,7 +31,7 @@ const UNKNOWN_FEE: isize = -1; // (allowed by Electrum protocol)
#[derive(Default)]
pub struct Client {
tip: Option<BlockHash>,
status: HashMap<ScriptHash, Status>,
scripthashes: HashMap<ScriptHash, ScriptHashStatus>,
}

#[derive(Deserialize)]
Expand Down Expand Up @@ -142,12 +142,12 @@ impl Rpc {
pub fn update_client(&self, client: &mut Client) -> Result<Vec<String>> {
let chain = self.tracker.chain();
let mut notifications = client
.status
.scripthashes
.par_iter_mut()
.filter_map(|(scripthash, status)| -> Option<Result<Value>> {
match self
.tracker
.update_status(status, &self.daemon, &self.cache)
.update_scripthash_status(status, &self.daemon, &self.cache)
{
Ok(true) => Some(Ok(notification(
"blockchain.scripthash.subscribe",
Expand Down Expand Up @@ -225,7 +225,7 @@ impl Rpc {
client: &Client,
(scripthash,): (ScriptHash,),
) -> Result<Value> {
let balance = match client.status.get(&scripthash) {
let balance = match client.scripthashes.get(&scripthash) {
Some(status) => self.tracker.get_balance(status, &self.cache),
None => {
warn!(
Expand All @@ -246,7 +246,7 @@ impl Rpc {
client: &Client,
(scripthash,): (ScriptHash,),
) -> Result<Value> {
let history_entries = match client.status.get(&scripthash) {
let history_entries = match client.scripthashes.get(&scripthash) {
Some(status) => self.tracker.get_history(status),
None => {
warn!(
Expand All @@ -266,14 +266,14 @@ impl Rpc {
) -> Result<Value> {
let status = self.new_status(scripthash)?;
let statushash = status.statushash();
client.status.insert(scripthash, status); // skip if already exists
client.scripthashes.insert(scripthash, status); // skip if already exists
Ok(json!(statushash))
}

fn new_status(&self, scripthash: ScriptHash) -> Result<Status> {
let mut status = Status::new(scripthash);
fn new_status(&self, scripthash: ScriptHash) -> Result<ScriptHashStatus> {
let mut status = ScriptHashStatus::new(scripthash);
self.tracker
.update_status(&mut status, &self.daemon, &self.cache)?;
.update_scripthash_status(&mut status, &self.daemon, &self.cache)?;
Ok(status)
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub use {
config::Config,
daemon::Daemon,
electrum::{Client, Rpc},
status::{Balance, Status},
status::{Balance, ScriptHashStatus},
tracker::Tracker,
types::ScriptHash,
};
4 changes: 2 additions & 2 deletions src/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl HistoryEntry {
}

/// ScriptHash subscription status
pub struct Status {
pub struct ScriptHashStatus {
scripthash: ScriptHash,
tip: BlockHash,
statushash: Option<StatusHash>,
Expand Down Expand Up @@ -171,7 +171,7 @@ fn make_outpoints<'a>(txid: &'a Txid, outputs: &'a [u32]) -> impl Iterator<Item
outputs.iter().map(move |vout| OutPoint::new(*txid, *vout))
}

impl Status {
impl ScriptHashStatus {
pub fn new(scripthash: ScriptHash) -> Self {
Self {
scripthash,
Expand Down
10 changes: 5 additions & 5 deletions src/tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
index::Index,
mempool::{Histogram, Mempool},
metrics::Metrics,
status::{Balance, HistoryEntry, Status},
status::{Balance, HistoryEntry, ScriptHashStatus},
};

/// Electrum protocol subscriptions' tracker
Expand Down Expand Up @@ -51,7 +51,7 @@ impl Tracker {
&self.metrics
}

pub(crate) fn get_history(&self, status: &Status) -> Vec<HistoryEntry> {
pub(crate) fn get_history(&self, status: &ScriptHashStatus) -> Vec<HistoryEntry> {
status.get_history(self.index.chain(), &self.mempool)
}

Expand All @@ -64,9 +64,9 @@ impl Tracker {
Ok(())
}

pub fn update_status(
pub fn update_scripthash_status(
&self,
status: &mut Status,
status: &mut ScriptHashStatus,
daemon: &Daemon,
cache: &Cache,
) -> Result<bool> {
Expand All @@ -75,7 +75,7 @@ impl Tracker {
Ok(prev_statushash != status.statushash())
}

pub fn get_balance(&self, status: &Status, cache: &Cache) -> Balance {
pub fn get_balance(&self, status: &ScriptHashStatus, cache: &Cache) -> Balance {
let get_amount_fn = |outpoint: OutPoint| {
cache
.get_tx(&outpoint.txid, |tx| {
Expand Down

0 comments on commit 27e7541

Please sign in to comment.