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

add subcommand to simulate/validate txs

x 3 лет назад
Родитель
Сommit
5751c08cd8
2 измененных файлов с 50 добавлено и 14 удалено
  1. 49 13
      bin/drk/src/main.rs
  2. 1 1
      bin/drk/src/rpc_blockchain.rs

+ 49 - 13
bin/drk/src/main.rs

@@ -207,11 +207,9 @@ enum Subcmd {
         checkpoint: Option<u64>,
     },
 
-    /// Fetch a blockchain transaction by hash
-    FetchTx {
-        /// Transaction hash
-        tx_hash: String,
-    },
+    /// Explorer related subcommands
+    #[command(subcommand)]
+    Explorer(ExplorerSubcmd),
 }
 
 #[derive(Subcommand)]
@@ -333,6 +331,18 @@ enum DaoSubcmd {
     },
 }
 
+#[derive(Subcommand)]
+enum ExplorerSubcmd {
+    /// Fetch a blockchain transaction by hash
+    FetchTx {
+        /// Transaction hash
+        tx_hash: String,
+    },
+
+    /// Read a transaction from stdin and simulate it
+    SimulateTx,
+}
+
 pub struct Drk {
     pub rpc_client: RpcClient,
 }
@@ -894,17 +904,43 @@ async fn main() -> Result<()> {
             }
         },
 
-        Subcmd::FetchTx { tx_hash } => {
-            let tx_hash = blake3::Hash::from_hex(&tx_hash)?;
+        Subcmd::Explorer(cmd) => match cmd {
+            ExplorerSubcmd::FetchTx { tx_hash } => {
+                let tx_hash = blake3::Hash::from_hex(&tx_hash)?;
 
-            let drk = Drk::new(args.endpoint).await?;
+                let drk = Drk::new(args.endpoint).await?;
+
+                let tx =
+                    drk.get_tx(&tx_hash).await.with_context(|| "Failed to fetch transaction")?;
+                let _ = if let Some(tx) = tx {
+                    tx
+                } else {
+                    eprintln!("Tx not found");
+                    exit(1);
+                };
 
-            let _tx = drk.get_tx(&tx_hash).await.with_context(|| "Failed to fetch transaction")?;
+                println!("Transaction ID: {}", tx_hash);
+                // TODO: display the actual tx
 
-            println!("Transaction ID: {}", tx_hash);
-            // TODO: display the actual tx
+                Ok(())
+            }
 
-            Ok(())
-        }
+            ExplorerSubcmd::SimulateTx => {
+                eprintln!("Reading transaction from stdin...");
+                let mut buf = String::new();
+                stdin().read_to_string(&mut buf)?;
+                let bytes = bs58::decode(&buf.trim()).into_vec()?;
+                let tx = deserialize(&bytes)?;
+
+                let drk = Drk::new(args.endpoint).await?;
+
+                let is_err =
+                    drk.is_erroneous_tx(&tx).await.with_context(|| "Failed to check tx state")?;
+
+                println!("State: {}", if is_err { "valid" } else { "invalid" });
+
+                Ok(())
+            }
+        },
     }
 }

+ 1 - 1
bin/drk/src/rpc_blockchain.rs

@@ -297,7 +297,7 @@ impl Drk {
     }
 
     /// Queries darkfid to check if transaction is in the erroneous set
-    async fn is_erroneous_tx(&self, tx: &Transaction) -> Result<bool> {
+    pub async fn is_erroneous_tx(&self, tx: &Transaction) -> Result<bool> {
         let serialized = serialize(tx);
         let tx_hash = blake3::hash(&serialized);
         let req = JsonRequest::new("blockchain.is_erroneous_tx", json!([tx_hash.as_bytes()]));