瀏覽代碼

add drk subcommand to fetch blockchain txs by hash

x 3 年之前
父節點
當前提交
c35365619d
共有 2 個文件被更改,包括 34 次插入0 次删除
  1. 19 0
      bin/drk/src/main.rs
  2. 15 0
      bin/drk/src/rpc_blockchain.rs

+ 19 - 0
bin/drk/src/main.rs

@@ -206,6 +206,12 @@ enum Subcmd {
         /// Reset Merkle tree to checkpoint index and start scanning
         /// Reset Merkle tree to checkpoint index and start scanning
         checkpoint: Option<u64>,
         checkpoint: Option<u64>,
     },
     },
+
+    /// Fetch a blockchain transaction by hash
+    FetchTx {
+        /// Transaction hash
+        tx_hash: String,
+    },
 }
 }
 
 
 #[derive(Subcommand)]
 #[derive(Subcommand)]
@@ -887,5 +893,18 @@ async fn main() -> Result<()> {
                 Ok(())
                 Ok(())
             }
             }
         },
         },
+
+        Subcmd::FetchTx { tx_hash } => {
+            let tx_hash = blake3::Hash::from_hex(&tx_hash)?;
+
+            let drk = Drk::new(args.endpoint).await?;
+
+            let _tx = drk.get_tx(&tx_hash).await.with_context(|| "Failed to fetch transaction")?;
+
+            println!("Transaction ID: {}", tx_hash);
+            // TODO: display the actual tx
+
+            Ok(())
+        }
     }
     }
 }
 }

+ 15 - 0
bin/drk/src/rpc_blockchain.rs

@@ -204,6 +204,21 @@ impl Drk {
         }
         }
     }
     }
 
 
+    /// Queries darkfid for a tx with given hash
+    pub async fn get_tx(&self, tx_hash: &blake3::Hash) -> Result<Option<Transaction>> {
+        let req = JsonRequest::new("blockchain.get_tx", json!([tx_hash.as_bytes()]));
+
+        match self.rpc_client.request(req).await {
+            Ok(v) => {
+                let tx_bytes: Vec<u8> = serde_json::from_value(v)?;
+                let tx = deserialize(&tx_bytes)?;
+                Ok(Some(tx))
+            }
+
+            Err(_) => Ok(None),
+        }
+    }
+
     /// Scans the blockchain starting from the last scanned slot, for relevant
     /// Scans the blockchain starting from the last scanned slot, for relevant
     /// money transfer transactions. If reset flag is provided, Merkle tree state
     /// money transfer transactions. If reset flag is provided, Merkle tree state
     /// and coins are reset, and start scanning from beginning. Alternatively,
     /// and coins are reset, and start scanning from beginning. Alternatively,