- let block_hash = source.get_block_hash_by_height(block_height).await
- .map_err(|_| UtxoLookupError::UnknownTx)?;
- let block_data = source.get_block(&block_hash).await
- .map_err(|_| UtxoLookupError::UnknownTx)?;
- let mut block = match block_data {
- BlockData::HeaderOnly(_) => return Err(UtxoLookupError::UnknownTx),
- BlockData::FullBlock(block) => block,
+ let (outpoint, output);
+
+ 'tx_found: loop { // Used as a simple goto
+ macro_rules! process_block {
+ ($block: expr) => { {
+ if transaction_index as usize >= $block.txdata.len() {
+ return Err(UtxoLookupError::UnknownTx);
+ }
+ let transaction = &$block.txdata[transaction_index as usize];
+ if output_index as usize >= transaction.output.len() {
+ return Err(UtxoLookupError::UnknownTx);
+ }
+
+ outpoint = OutPoint::new(transaction.txid(), output_index.into());
+ output = transaction.output[output_index as usize].clone();
+ } }
+ }
+ {
+ let recent_blocks = block_cache.lock().unwrap();
+ for (height, block) in recent_blocks.iter() {
+ if *height == block_height {
+ process_block!(block);
+ break 'tx_found;
+ }
+ }
+ }
+
+ let ((_, tip_height_opt), block_hash) =
+ Joiner::new(source.get_best_block(), source.get_block_hash_by_height(block_height))
+ .await
+ .map_err(|_| UtxoLookupError::UnknownTx)?;
+ if let Some(tip_height) = tip_height_opt {
+ // If the block doesn't yet have five confirmations, error out.
+ //
+ // The BOLT spec requires nodes wait for six confirmations before announcing a
+ // channel, and we give them one block of headroom in case we're delayed seeing a
+ // block.
+ if block_height + 5 > tip_height {
+ return Err(UtxoLookupError::UnknownTx);
+ }
+ }
+ let block_data = source.get_block(&block_hash).await
+ .map_err(|_| UtxoLookupError::UnknownTx)?;
+ let block = match block_data {
+ BlockData::HeaderOnly(_) => return Err(UtxoLookupError::UnknownTx),
+ BlockData::FullBlock(block) => block,
+ };
+ process_block!(block);
+ {
+ let mut recent_blocks = block_cache.lock().unwrap();
+ let mut insert = true;
+ for (height, _) in recent_blocks.iter() {
+ if *height == block_height {
+ insert = false;
+ }
+ }
+ if insert {
+ if recent_blocks.len() >= BLOCK_CACHE_SIZE {
+ recent_blocks.pop_front();
+ }
+ recent_blocks.push_back((block_height, block));
+ }
+ }
+ break 'tx_found;