From ec3ea9c3ff064723b06115e3a8284c6006dcedbe Mon Sep 17 00:00:00 2001 From: Antioch Peverell Date: Mon, 27 Jul 2020 11:05:12 +0100 Subject: [PATCH] pass ref to chain around (#3401) --- api/src/handlers/blocks_api.rs | 4 ++-- api/src/handlers/chain_api.rs | 12 +++--------- api/src/handlers/transactions_api.rs | 17 +++++++++-------- api/src/handlers/utils.rs | 2 +- api/src/types.rs | 24 ++++++++++-------------- 5 files changed, 25 insertions(+), 34 deletions(-) diff --git a/api/src/handlers/blocks_api.rs b/api/src/handlers/blocks_api.rs index aad4271d..7ddb2274 100644 --- a/api/src/handlers/blocks_api.rs +++ b/api/src/handlers/blocks_api.rs @@ -133,14 +133,14 @@ impl BlockHandler { ) -> Result { let chain = w(&self.chain)?; let block = chain.get_block(h).context(ErrorKind::NotFound)?; - BlockPrintable::from_block(&block, chain, include_proof, include_merkle_proof) + BlockPrintable::from_block(&block, &chain, include_proof, include_merkle_proof) .map_err(|_| ErrorKind::Internal("chain error".to_owned()).into()) } fn get_compact_block(&self, h: &Hash) -> Result { let chain = w(&self.chain)?; let block = chain.get_block(h).context(ErrorKind::NotFound)?; - CompactBlockPrintable::from_compact_block(&block.into(), chain) + CompactBlockPrintable::from_compact_block(&block.into(), &chain) .map_err(|_| ErrorKind::Internal("chain error".to_owned()).into()) } diff --git a/api/src/handlers/chain_api.rs b/api/src/handlers/chain_api.rs index 598bcf8c..5fe8f348 100644 --- a/api/src/handlers/chain_api.rs +++ b/api/src/handlers/chain_api.rs @@ -189,7 +189,7 @@ impl OutputHandler { .map(|x| { OutputPrintable::from_output( x, - chain.clone(), + &chain, None, include_proof.unwrap_or(false), false, @@ -248,13 +248,7 @@ impl OutputHandler { .iter() .filter(|output| commitments.is_empty() || commitments.contains(&output.commit)) .map(|output| { - OutputPrintable::from_output( - output, - chain.clone(), - Some(&header), - include_proof, - true, - ) + OutputPrintable::from_output(output, &chain, Some(&header), include_proof, true) }) .collect::, _>>() .context(ErrorKind::Internal("cain error".to_owned()))?; @@ -289,7 +283,7 @@ impl OutputHandler { .map(|output| { OutputPrintable::from_output( output, - chain.clone(), + &chain, Some(&header), include_rproof, include_merkle_proof, diff --git a/api/src/handlers/transactions_api.rs b/api/src/handlers/transactions_api.rs index 3df09f44..3b931c87 100644 --- a/api/src/handlers/transactions_api.rs +++ b/api/src/handlers/transactions_api.rs @@ -47,7 +47,8 @@ pub struct TxHashSetHandler { impl TxHashSetHandler { // gets roots fn get_roots(&self) -> Result { - let res = TxHashSet::from_head(w(&self.chain)?).context(ErrorKind::Internal( + let chain = w(&self.chain)?; + let res = TxHashSet::from_head(&chain).context(ErrorKind::Internal( "failed to read roots from txhashset".to_owned(), ))?; Ok(res) @@ -55,20 +56,20 @@ impl TxHashSetHandler { // gets last n outputs inserted in to the tree fn get_last_n_output(&self, distance: u64) -> Result, Error> { - Ok(TxHashSetNode::get_last_n_output(w(&self.chain)?, distance)) + let chain = w(&self.chain)?; + Ok(TxHashSetNode::get_last_n_output(&chain, distance)) } // gets last n rangeproofs inserted in to the tree fn get_last_n_rangeproof(&self, distance: u64) -> Result, Error> { - Ok(TxHashSetNode::get_last_n_rangeproof( - w(&self.chain)?, - distance, - )) + let chain = w(&self.chain)?; + Ok(TxHashSetNode::get_last_n_rangeproof(&chain, distance)) } // gets last n kernels inserted in to the tree fn get_last_n_kernel(&self, distance: u64) -> Result, Error> { - Ok(TxHashSetNode::get_last_n_kernel(w(&self.chain)?, distance)) + let chain = w(&self.chain)?; + Ok(TxHashSetNode::get_last_n_kernel(&chain, distance)) } // allows traversal of utxo set @@ -92,7 +93,7 @@ impl TxHashSetHandler { outputs: outputs .2 .iter() - .map(|x| OutputPrintable::from_output(x, chain.clone(), None, true, true)) + .map(|x| OutputPrintable::from_output(x, &chain, None, true, true)) .collect::, _>>() .context(ErrorKind::Internal("chain error".to_owned()))?, }; diff --git a/api/src/handlers/utils.rs b/api/src/handlers/utils.rs index 738cf1fc..96ed152f 100644 --- a/api/src/handlers/utils.rs +++ b/api/src/handlers/utils.rs @@ -94,7 +94,7 @@ pub fn get_output_v2( let output_printable = OutputPrintable::from_output( &output, - chain, + &chain, header.as_ref(), include_proof, include_merkle_proof, diff --git a/api/src/types.rs b/api/src/types.rs index 58ff7510..1ca36a27 100644 --- a/api/src/types.rs +++ b/api/src/types.rs @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::sync::Arc; - use crate::chain; use crate::core::core::hash::Hashed; use crate::core::core::merkle_proof::MerkleProof; @@ -119,7 +117,7 @@ impl TxHashSet { /// A TxHashSet in the context of the api is simply the collection of PMMR roots. /// We can obtain these in a lightweight way by reading them from the head of the chain. /// We will have validated the roots on this header against the roots of the txhashset. - pub fn from_head(chain: Arc) -> Result { + pub fn from_head(chain: &chain::Chain) -> Result { let header = chain.head_header()?; Ok(TxHashSet { output_root_hash: header.output_root.to_hex(), @@ -138,7 +136,7 @@ pub struct TxHashSetNode { } impl TxHashSetNode { - pub fn get_last_n_output(chain: Arc, distance: u64) -> Vec { + pub fn get_last_n_output(chain: &chain::Chain, distance: u64) -> Vec { let mut return_vec = Vec::new(); let last_n = chain.get_last_n_output(distance); for x in last_n { @@ -147,9 +145,9 @@ impl TxHashSetNode { return_vec } - pub fn get_last_n_rangeproof(head: Arc, distance: u64) -> Vec { + pub fn get_last_n_rangeproof(chain: &chain::Chain, distance: u64) -> Vec { let mut return_vec = Vec::new(); - let last_n = head.get_last_n_rangeproof(distance); + let last_n = chain.get_last_n_rangeproof(distance); for elem in last_n { return_vec.push(TxHashSetNode { hash: elem.0.to_hex(), @@ -158,9 +156,9 @@ impl TxHashSetNode { return_vec } - pub fn get_last_n_kernel(head: Arc, distance: u64) -> Vec { + pub fn get_last_n_kernel(chain: &chain::Chain, distance: u64) -> Vec { let mut return_vec = Vec::new(); - let last_n = head.get_last_n_kernel(distance); + let last_n = chain.get_last_n_kernel(distance); for elem in last_n { return_vec.push(TxHashSetNode { hash: elem.0.to_hex(), @@ -281,7 +279,7 @@ pub struct OutputPrintable { impl OutputPrintable { pub fn from_output( output: &core::Output, - chain: Arc, + chain: &chain::Chain, block_header: Option<&core::BlockHeader>, include_proof: bool, include_merkle_proof: bool, @@ -627,7 +625,7 @@ pub struct BlockPrintable { impl BlockPrintable { pub fn from_block( block: &core::Block, - chain: Arc, + chain: &chain::Chain, include_proof: bool, include_merkle_proof: bool, ) -> Result { @@ -681,15 +679,13 @@ impl CompactBlockPrintable { /// api response pub fn from_compact_block( cb: &core::CompactBlock, - chain: Arc, + chain: &chain::Chain, ) -> Result { let block = chain.get_block(&cb.hash())?; let out_full = cb .out_full() .iter() - .map(|x| { - OutputPrintable::from_output(x, chain.clone(), Some(&block.header), false, true) - }) + .map(|x| OutputPrintable::from_output(x, chain, Some(&block.header), false, true)) .collect::, _>>()?; let kern_full = cb .kern_full()