garbage_collect.rs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2024 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use std::sync::Arc;
  19. use darkfi::{
  20. error::TxVerifyFailed,
  21. validator::{consensus::TXS_CAP, verification::verify_transactions},
  22. Error, Result,
  23. };
  24. use darkfi_sdk::crypto::MerkleTree;
  25. use log::info;
  26. use crate::Darkfid;
  27. // TODO: handle all ? so the task don't stop on errors
  28. /// Async task used for purging erroneous pending transactions from the nodes mempool.
  29. pub async fn garbage_collect_task(node: Arc<Darkfid>) -> Result<()> {
  30. info!(target: "darkfid::task::garbage_collect_task", "Starting garbage collection task...");
  31. // Grab all current unproposed transactions. We verify them in batches,
  32. // to not load them all in memory.
  33. let (mut last_checked, mut txs) =
  34. node.validator.blockchain.transactions.get_after_pending(0, TXS_CAP)?;
  35. while !txs.is_empty() {
  36. // Verify each one against current forks
  37. for tx in txs {
  38. let tx_hash = tx.hash();
  39. let tx_vec = [tx.clone()];
  40. // Grab a lock over current consensus forks state
  41. let mut forks = node.validator.consensus.forks.write().await;
  42. // Iterate over them to verify transaction validity in their overlays
  43. for fork in forks.iter_mut() {
  44. // Clone forks' overlay
  45. let overlay = fork.overlay.lock().unwrap().full_clone()?;
  46. // Grab all current proposals transactions hashes
  47. let proposals_txs =
  48. overlay.lock().unwrap().get_blocks_txs_hashes(&fork.proposals)?;
  49. // If the hash is contained in the proposals transactions vec, skip it
  50. if proposals_txs.contains(&tx_hash) {
  51. continue
  52. }
  53. // Grab forks' next block height
  54. let next_block_height = fork.get_next_block_height()?;
  55. // Verify transaction
  56. match verify_transactions(
  57. &overlay,
  58. next_block_height,
  59. &tx_vec,
  60. &mut MerkleTree::new(1),
  61. false,
  62. )
  63. .await
  64. {
  65. Ok(_) => {}
  66. Err(Error::TxVerifyFailed(TxVerifyFailed::ErroneousTxs(_))) => {
  67. // Remove transaction from fork's mempool
  68. fork.mempool.retain(|tx| *tx != tx_hash);
  69. }
  70. Err(e) => return Err(e),
  71. }
  72. }
  73. // Drop forks lock
  74. drop(forks);
  75. }
  76. (last_checked, txs) =
  77. node.validator.blockchain.transactions.get_after_pending(last_checked, TXS_CAP)?;
  78. }
  79. info!(target: "darkfid::task::garbage_collect_task", "Garbage collection finished successfully!");
  80. Ok(())
  81. }