Quellcode durchsuchen

consensus/proto/sync: Implement state transition validation for incoming blocks.

parazyd vor 4 Jahren
Ursprung
Commit
ffcaae67b5
1 geänderte Dateien mit 27 neuen und 2 gelöschten Zeilen
  1. 27 2
      src/consensus/proto/protocol_sync.rs

+ 27 - 2
src/consensus/proto/protocol_sync.rs

@@ -1,17 +1,18 @@
 use async_executor::Executor;
 use async_executor::Executor;
 use async_std::sync::Arc;
 use async_std::sync::Arc;
 use async_trait::async_trait;
 use async_trait::async_trait;
-use log::debug;
+use log::{debug, error, warn};
 
 
 use crate::{
 use crate::{
     consensus::{
     consensus::{
         block::{BlockInfo, BlockOrder, BlockResponse},
         block::{BlockInfo, BlockOrder, BlockResponse},
-        ValidatorStatePtr,
+        ValidatorState, ValidatorStatePtr,
     },
     },
     net::{
     net::{
         ChannelPtr, MessageSubscription, P2pPtr, ProtocolBase, ProtocolBasePtr,
         ChannelPtr, MessageSubscription, P2pPtr, ProtocolBase, ProtocolBasePtr,
         ProtocolJobsManager, ProtocolJobsManagerPtr,
         ProtocolJobsManager, ProtocolJobsManagerPtr,
     },
     },
+    node::MemoryState,
     Result,
     Result,
 };
 };
 
 
@@ -85,8 +86,32 @@ impl ProtocolSync {
             if !self.consensus_mode {
             if !self.consensus_mode {
                 let info_copy = (*info).clone();
                 let info_copy = (*info).clone();
                 if !self.state.read().await.blockchain.has_block(&info_copy)? {
                 if !self.state.read().await.blockchain.has_block(&info_copy)? {
+                    debug!("handle_receive_block(): Starting state transition validation");
+                    let canon_state_clone =
+                        self.state.read().await.state_machine.lock().await.clone();
+                    let mem_state = MemoryState::new(canon_state_clone);
+                    let state_updates =
+                        match ValidatorState::validate_state_transitions(mem_state, &info.txs) {
+                            Ok(v) => v,
+                            Err(e) => {
+                                warn!("handle_receive_block(): State transition fail: {}", e);
+                                continue
+                            }
+                        };
+                    debug!("ProtocolSync::handle_receive_block(): All state transitions passed");
+
+                    debug!("ProtocolSync::handle_receive_block(): Updating canon state machine");
+                    match self.state.write().await.update_canon_state(state_updates, None).await {
+                        Ok(()) => {}
+                        Err(e) => {
+                            error!("Failed updating canon state machine: {}", e);
+                            continue
+                        }
+                    }
+
                     debug!("ProtocolSync::handle_receive_block(): Appending block to ledger");
                     debug!("ProtocolSync::handle_receive_block(): Appending block to ledger");
                     self.state.write().await.blockchain.add(&[info_copy.clone()])?;
                     self.state.write().await.blockchain.add(&[info_copy.clone()])?;
+
                     self.state.write().await.remove_txs(info_copy.txs.clone())?;
                     self.state.write().await.remove_txs(info_copy.txs.clone())?;
                     self.p2p.broadcast(info_copy).await?;
                     self.p2p.broadcast(info_copy).await?;
                 }
                 }