Преглед изворни кода

darkfid: tx.broadcast: Append tx to mempool if participating in consensus.

parazyd пре 3 година
родитељ
комит
d1db46fbf8
2 измењених фајлова са 18 додато и 18 уклоњено
  1. 2 8
      bin/darkfid/src/main.rs
  2. 16 10
      bin/darkfid/src/rpc_tx.rs

+ 2 - 8
bin/darkfid/src/main.rs

@@ -173,7 +173,7 @@ struct Args {
 
 pub struct Darkfid {
     synced: Mutex<bool>, // AtomicBool is weird in Arc
-    _consensus_p2p: Option<P2pPtr>,
+    consensus_p2p: Option<P2pPtr>,
     sync_p2p: Option<P2pPtr>,
     wallet: WalletPtr,
     validator_state: ValidatorStatePtr,
@@ -249,13 +249,7 @@ impl Darkfid {
         sync_p2p: Option<P2pPtr>,
         wallet: WalletPtr,
     ) -> Self {
-        Self {
-            synced: Mutex::new(false),
-            _consensus_p2p: consensus_p2p,
-            sync_p2p,
-            wallet,
-            validator_state,
-        }
+        Self { synced: Mutex::new(false), consensus_p2p, sync_p2p, wallet, validator_state }
     }
 }
 

+ 16 - 10
bin/darkfid/src/rpc_tx.rs

@@ -107,22 +107,28 @@ impl Darkfid {
             }
         };
 
-        // Simulate state transition
-        if let Err(e) =
-            self.validator_state.read().await.verify_transactions(&[tx.clone()], false).await
-        {
-            error!("[RPC] tx.broadcast: Failed to validate state transition: {}", e);
-            return server_error(RpcError::TxSimulationFail, id, None)
-        };
+        if self.consensus_p2p.is_some() {
+            // Consider we're participating in consensus here?
+            // The append_tx function performs a state transition check.
+            if !self.validator_state.write().await.append_tx(tx.clone()).await {
+                error!("[RPC] tx.broadcast: Failed to append transaction to mempool");
+                return server_error(RpcError::TxBroadcastFail, id, None)
+            }
+        } else {
+            // We'll perform the state transition check here.
+            if let Err(e) =
+                self.validator_state.read().await.verify_transactions(&[tx.clone()], false).await
+            {
+                error!("[RPC] tx.broadcast: Failed to validate state transition: {}", e);
+                return server_error(RpcError::TxSimulationFail, id, None)
+            };
+        }
 
-        // TODO: Should we apply the state transition locally before broadcasting it?
         if let Some(sync_p2p) = &self.sync_p2p {
             if let Err(e) = sync_p2p.broadcast(tx.clone()).await {
                 error!("[RPC] tx.broadcast: Failed broadcasting transaction: {}", e);
                 return server_error(RpcError::TxBroadcastFail, id, None)
             }
-
-            // TODO: Mark coin as spent in the wallet
         } else {
             warn!("[RPC] tx.broadcast: No sync P2P network, not broadcasting transaction.");
             return server_error(RpcError::TxBroadcastFail, id, None)