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

drk/rpc: rescan genesis block if reorg height is 1

skoupidi 1 год назад
Родитель
Сommit
6cbd478fcc
1 измененных файлов с 19 добавлено и 4 удалено
  1. 19 4
      bin/drk/src/rpc.rs

+ 19 - 4
bin/drk/src/rpc.rs

@@ -156,16 +156,31 @@ impl Drk {
                         let block: BlockInfo = deserialize_async(&bytes).await?;
                         let block: BlockInfo = deserialize_async(&bytes).await?;
                         println!("Deserialized successfully. Scanning block...");
                         println!("Deserialized successfully. Scanning block...");
 
 
-                        // TODO: Fully test this once darkfid broadcasts reorg sequences
                         // Check if a reorg block was received, to reset to its previous
                         // Check if a reorg block was received, to reset to its previous
                         if block.header.height <= last_scanned_height {
                         if block.header.height <= last_scanned_height {
-                            if let Err(e) =
-                                self.reset_to_height(block.header.height.saturating_sub(1)).await
-                            {
+                            let reset_height = block.header.height.saturating_sub(1);
+                            if let Err(e) = self.reset_to_height(reset_height).await {
                                 return Err(Error::DatabaseError(format!(
                                 return Err(Error::DatabaseError(format!(
                                     "[subscribe_blocks] Wallet state reset failed: {e:?}"
                                     "[subscribe_blocks] Wallet state reset failed: {e:?}"
                                 )))
                                 )))
                             }
                             }
+
+                            // Scan genesis again if needed
+                            if reset_height == 0 {
+                                let genesis = match self.get_block_by_height(reset_height).await {
+                                    Ok(b) => b,
+                                    Err(e) => {
+                                        return Err(Error::Custom(format!(
+                                            "[subscribe_blocks] RPC client request failed: {e:?}"
+                                        )))
+                                    }
+                                };
+                                if let Err(e) = self.scan_block(&genesis).await {
+                                    return Err(Error::DatabaseError(format!(
+                                        "[subscribe_blocks] Scanning block failed: {e:?}"
+                                    )))
+                                };
+                            }
                         }
                         }
 
 
                         if let Err(e) = self.scan_block(&block).await {
                         if let Err(e) = self.scan_block(&block).await {