Просмотр исходного кода

script/research/blockchain-explorer: minor code beatifications

skoupidi 1 год назад
Родитель
Сommit
92f5b05cee

+ 8 - 15
script/research/blockchain-explorer/src/blocks.rs

@@ -135,21 +135,14 @@ impl ExplorerDb {
             .parse::<HeaderHash>()
             .map_err(|_| Error::ParseFailed("[get_block_by_hash] Invalid header hash"))?;
 
-        // Fetch all blocks by hash and handle encountered errors
-        let blocks = match self.blockchain.get_blocks_by_hash(&[header_hash]) {
-            Ok(blocks) => blocks,
-            Err(Error::BlockNotFound(_)) => return Ok(None),
-            Err(e) => {
-                return Err(Error::DatabaseError(format!(
-                    "[get_block_by_hash] Block retrieval failed: {e:?}"
-                )))
-            }
-        };
-
-        // Transform found block to a BlockRecord
-        let block = Some(BlockRecord::from(&blocks[0]));
-
-        Ok(block)
+        // Fetch block by hash and handle encountered errors
+        match self.blockchain.get_blocks_by_hash(&[header_hash]) {
+            Ok(blocks) => Ok(Some(BlockRecord::from(&blocks[0]))),
+            Err(Error::BlockNotFound(_)) => Ok(None),
+            Err(e) => Err(Error::DatabaseError(format!(
+                "[get_block_by_hash] Block retrieval failed: {e:?}"
+            ))),
+        }
     }
 
     /// Fetch the last block from the database.

+ 1 - 1
script/research/blockchain-explorer/src/main.rs

@@ -126,7 +126,7 @@ impl Explorerd {
     async fn new(db_path: String, endpoint: Url, ex: Arc<smol::Executor<'static>>) -> Result<Self> {
         // Initialize rpc client
         let rpc_client = RpcClient::new(endpoint.clone(), ex).await?;
-        info!(target: "explorerd", "Created rpc client: {:?}", endpoint.clone());
+        info!(target: "explorerd", "Created rpc client: {:?}", endpoint);
 
         // Initialize explorer database
         let explorer_db = ExplorerDb::new(db_path)?;

+ 30 - 42
script/research/blockchain-explorer/src/transactions.rs

@@ -90,12 +90,12 @@ impl ExplorerDb {
     /// Fetch all known transactions from the database.
     pub fn get_transactions(&self) -> Result<Vec<TransactionRecord>> {
         // Retrieve all transactions and handle any errors encountered
-        let transactions_result = self.blockchain.transactions.get_all().map_err(|e| {
+        let transactions = self.blockchain.transactions.get_all().map_err(|e| {
             Error::DatabaseError(format!("[get_transactions] Trxs retrieval: {e:?}"))
         })?;
 
         // Transform the found transactions into a vector of transaction records
-        let transaction_records: Vec<TransactionRecord> = transactions_result
+        let transaction_records: Vec<TransactionRecord> = transactions
             .iter()
             .map(|(tx_hash, tx)| TransactionRecord::from((&tx_hash.as_string(), tx)))
             .collect();
@@ -113,7 +113,7 @@ impl ExplorerDb {
             .parse::<HeaderHash>()
             .map_err(|_| Error::ParseFailed("[get_transactions_by_header_hash] Invalid hash"))?;
 
-        // Fetch all blocks by hash and handle encountered errors
+        // Fetch block by hash and handle encountered errors
         let blocks = match self.blockchain.get_blocks_by_hash(&[header_hash]) {
             Ok(blocks) => blocks,
             Err(Error::BlockNotFound(_)) => return Ok(vec![]),
@@ -125,16 +125,11 @@ impl ExplorerDb {
         };
 
         // Transform block transactions into transaction records
-        let tx_records = {
-            let block = &blocks[0];
-            block
-                .txs
-                .iter()
-                .map(|tx| TransactionRecord::from((&block.header.hash().as_string(), tx)))
-                .collect::<Vec<TransactionRecord>>()
-        };
-
-        Ok(tx_records)
+        Ok(blocks[0]
+            .txs
+            .iter()
+            .map(|tx| TransactionRecord::from((&blocks[0].header.hash().as_string(), tx)))
+            .collect::<Vec<TransactionRecord>>())
     }
 
     /// Fetch a transaction given its header hash.
@@ -151,36 +146,29 @@ impl ExplorerDb {
             ))
         })?;
 
-        // Match on the fetched transactions to process the result
-        let tx_record = match &txs[0] {
-            Some(tx) => {
-                // Retrieve the location of the transaction to obtain its header hash
-                let locations = tx_store.get_location(&[*tx_hash], false).map_err(|e| {
-                    Error::DatabaseError(format!(
-                        "[get_transaction_by_hash] Location retrieval failed: {e:?}"
-                    ))
-                })?;
-
-                // Unwrap the first location since we know it exists for a valid transaction
-                let (block_height, _) = locations[0].unwrap();
-
-                // Retrieve the block corresponding to the transaction's height
-                let block_data =
-                    &self.blockchain.blocks.get_order(&[block_height], false).map_err(|e| {
-                        Error::DatabaseError(format!(
-                            "[get_transaction_by_hash] Block retrieval failed: {e:?}"
-                        ))
-                    })?;
-
-                // Unwrap the block since we are assured it exists due to stored location
-                let header_hash = block_data[0].unwrap();
-
-                // Transform the transaction into a TransactionRecord
-                Some(TransactionRecord::from((&header_hash.as_string(), tx)))
-            }
-            None => None,
+        // Check if transaction was found
+        if txs[0].is_none() {
+            return Ok(None);
         };
 
-        Ok(tx_record)
+        // Retrieve the location of the transaction to obtain its header hash
+        let (block_height, _) = tx_store.get_location(&[*tx_hash], true).map_err(|e| {
+            Error::DatabaseError(format!(
+                "[get_transaction_by_hash] Location retrieval failed: {e:?}"
+            ))
+        })?[0]
+            .unwrap();
+
+        // Retrieve the block corresponding to the transaction's height
+        let header_hash =
+            &self.blockchain.blocks.get_order(&[block_height], true).map_err(|e| {
+                Error::DatabaseError(format!(
+                    "[get_transaction_by_hash] Block retrieval failed: {e:?}"
+                ))
+            })?[0]
+                .unwrap();
+
+        // Transform the transaction into a TransactionRecord
+        Ok(Some(TransactionRecord::from((&header_hash.as_string(), txs[0].as_ref().unwrap()))))
     }
 }