Skip to content

Commit

Permalink
f FMT
Browse files Browse the repository at this point in the history
  • Loading branch information
tnull committed Sep 21, 2022
1 parent bdc1b8e commit 0475505
Showing 1 changed file with 45 additions and 37 deletions.
82 changes: 45 additions & 37 deletions src/access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,22 @@ where

let mut locked_last_sync_height = self.last_sync_height.lock().unwrap();
if cur_height >= locked_last_sync_height.unwrap_or(0) {
self.sync_best_block_updated(confirmables.clone(), cur_height, &mut locked_last_sync_height)?;
self.sync_best_block_updated(
confirmables.clone(),
cur_height,
&mut locked_last_sync_height,
)?;
self.sync_transactions_confirmed(confirmables.clone())?;
self.sync_transaction_unconfirmed(confirmables.clone())?;
}
// TODO: check whether new outputs have been registered by now and process them
Ok(())
}

fn sync_best_block_updated(&self, confirmables: Vec<&(dyn Confirm + Sync)>, cur_height: u32, locked_last_sync_height: &mut MutexGuard<Option<u32>>) -> Result<(), Error> {
fn sync_best_block_updated(
&self, confirmables: Vec<&(dyn Confirm + Sync)>, cur_height: u32,
locked_last_sync_height: &mut MutexGuard<Option<u32>>,
) -> Result<(), Error> {
let client = &*self.blockchain;

// Inform the interface of the new block.
Expand All @@ -101,7 +108,9 @@ where
Ok(())
}

fn sync_transactions_confirmed(&self, confirmables: Vec<&(dyn Confirm + Sync)>) -> Result<(), Error> {
fn sync_transactions_confirmed(
&self, confirmables: Vec<&(dyn Confirm + Sync)>,
) -> Result<(), Error> {
let client = &*self.blockchain;

// First, check the confirmation status of registered transactions as well as the
Expand Down Expand Up @@ -132,11 +141,11 @@ where
let block_header = client.get_header(block_height)?;
if let Some(merkle_proof) = client.get_merkle_proof(&txid)? {
confirmed_txs.push((
tx,
block_height,
block_header,
merkle_proof.pos,
));
tx,
block_height,
block_header,
merkle_proof.pos,
));
continue;
}
}
Expand All @@ -147,42 +156,39 @@ where
}

// Check all registered outputs for dependent spending transactions.
let registered_outputs: Vec<WatchedOutput> = locked_watched_outputs
.iter()
.chain(locked_queued_outputs.iter())
.cloned()
.collect();
let registered_outputs: Vec<WatchedOutput> =
locked_watched_outputs.iter().chain(locked_queued_outputs.iter()).cloned().collect();

// Remember all registered outputs that haven't been spent for future processing.
let mut unspent_registered_outputs = Vec::new();

for output in registered_outputs {
if let Some(output_status) = client
.get_output_status(&output.outpoint.txid, output.outpoint.index as u64)?
{
if output_status.spent {
if let Some(spending_tx_status) = output_status.status {
if spending_tx_status.confirmed {
let spending_txid = output_status.txid.unwrap();
if let Some(spending_tx) = client.get_tx(&spending_txid)? {
let block_height = spending_tx_status.block_height.unwrap();
let block_header = client.get_header(block_height)?;
if let Some(merkle_proof) =
client.get_merkle_proof(&spending_txid)?
{
confirmed_txs.push((
spending_tx,
block_height,
block_header,
merkle_proof.pos,
));
continue;
}
if let Some(output_status) =
client.get_output_status(&output.outpoint.txid, output.outpoint.index as u64)?
{
if output_status.spent {
if let Some(spending_tx_status) = output_status.status {
if spending_tx_status.confirmed {
let spending_txid = output_status.txid.unwrap();
if let Some(spending_tx) = client.get_tx(&spending_txid)? {
let block_height = spending_tx_status.block_height.unwrap();
let block_header = client.get_header(block_height)?;
if let Some(merkle_proof) =
client.get_merkle_proof(&spending_txid)?
{
confirmed_txs.push((
spending_tx,
block_height,
block_header,
merkle_proof.pos,
));
continue;
}
}
}
}
}
}
unspent_registered_outputs.push(output);
}

Expand All @@ -192,7 +198,7 @@ where
|(_, block_height1, _, pos1), (_, block_height2, _, pos2)| {
block_height1.cmp(&block_height2).then_with(|| pos1.cmp(&pos2))
},
);
);
for (tx, block_height, block_header, pos) in confirmed_txs {
for c in &confirmables {
c.transactions_confirmed(&block_header, &[(pos, &tx)], block_height);
Expand All @@ -207,7 +213,9 @@ where
Ok(())
}

fn sync_transaction_unconfirmed(&self, confirmables: Vec<&(dyn Confirm + Sync)>) -> Result<(), Error> {
fn sync_transaction_unconfirmed(
&self, confirmables: Vec<&(dyn Confirm + Sync)>,
) -> Result<(), Error> {
let client = &*self.blockchain;
// Query the interface for relevant txids and check whether they have been
// reorged-out of the chain.
Expand All @@ -221,7 +229,7 @@ where
.unwrap_or(None)
.map_or(true, |status| !status.confirmed)
})
.collect::<Vec<Txid>>();
.collect::<Vec<Txid>>();

// Mark all relevant unconfirmed transactions as unconfirmed.
for txid in &unconfirmed_txids {
Expand Down

0 comments on commit 0475505

Please sign in to comment.