From 3040f42c47240f998ddacb6ac55a2dd483aac54e Mon Sep 17 00:00:00 2001
From: librelois <c@elo.tf>
Date: Mon, 14 Dec 2020 19:59:30 +0100
Subject: [PATCH] [fix] gva:find_inputs: do not use inputs with amout less than
 100

---
 .../modules/gva/dbs-reader/src/find_inputs.rs | 26 ++++----
 rust-libs/modules/gva/dbs-reader/src/utxos.rs | 62 ++++++++++---------
 2 files changed, 47 insertions(+), 41 deletions(-)

diff --git a/rust-libs/modules/gva/dbs-reader/src/find_inputs.rs b/rust-libs/modules/gva/dbs-reader/src/find_inputs.rs
index 084dbb648..ffb3b9c8a 100644
--- a/rust-libs/modules/gva/dbs-reader/src/find_inputs.rs
+++ b/rust-libs/modules/gva/dbs-reader/src/find_inputs.rs
@@ -20,6 +20,8 @@ use crate::{
 };
 use dubp::{documents::transaction::TransactionInputV10, wallet::prelude::*};
 
+pub(super) const MIN_AMOUNT: i64 = 100;
+
 impl DbsReader {
     pub fn find_inputs<BcDb: BcV2DbReadable, TxsMpDb: TxsMpV2DbReadable>(
         &self,
@@ -59,9 +61,9 @@ impl DbsReader {
 
                     Ok((inputs, sum))
                 })?
-                .unwrap_or((Vec::with_capacity(50), SourceAmount::ZERO))
+                .unwrap_or((Vec::with_capacity(500), SourceAmount::ZERO))
         } else {
-            (Vec::with_capacity(50), SourceAmount::ZERO)
+            (Vec::with_capacity(500), SourceAmount::ZERO)
         };
         // UDs
         if script.nodes.is_empty() {
@@ -142,7 +144,7 @@ mod tests {
         WalletConditionsV2,
     };
 
-    const UD0: i64 = 10;
+    const UD0: i64 = 100;
 
     #[test]
     fn test_find_inputs() -> anyhow::Result<()> {
@@ -160,7 +162,7 @@ mod tests {
         let script = WalletScriptV10::single(WalletConditionV10::Sig(pk));
         let mut pending_utxos = BTreeSet::new();
         pending_utxos.insert(UtxoValV2::new(
-            SourceAmount::with_base0(90),
+            SourceAmount::with_base0(900),
             Hash::default(),
             10,
         ));
@@ -177,11 +179,11 @@ mod tests {
             .upsert(U32BE(0), b0.median_time)?;
         gva_db.gva_utxos_write().upsert(
             GvaUtxoIdDbV1::new(script.clone(), 0, Hash::default(), 0),
-            SourceAmountValV2(SourceAmount::with_base0(50)),
+            SourceAmountValV2(SourceAmount::with_base0(500)),
         )?;
         gva_db.gva_utxos_write().upsert(
             GvaUtxoIdDbV1::new(script.clone(), 0, Hash::default(), 1),
-            SourceAmountValV2(SourceAmount::with_base0(80)),
+            SourceAmountValV2(SourceAmount::with_base0(800)),
         )?;
         txs_mp_db
             .outputs_by_script_write()
@@ -191,12 +193,12 @@ mod tests {
         let (inputs, inputs_sum) = db_reader.find_inputs(
             &bc_db,
             &txs_mp_db,
-            SourceAmount::with_base0(55),
+            SourceAmount::with_base0(550),
             &script,
             false,
         )?;
         assert_eq!(inputs.len(), 2);
-        assert_eq!(inputs_sum, SourceAmount::with_base0(60));
+        assert_eq!(inputs_sum, SourceAmount::with_base0(600));
 
         // Insert tx1 inputs in mempool
         txs_mp_db
@@ -210,12 +212,12 @@ mod tests {
         let (inputs, inputs_sum) = db_reader.find_inputs(
             &bc_db,
             &txs_mp_db,
-            SourceAmount::with_base0(55),
+            SourceAmount::with_base0(550),
             &script,
             false,
         )?;
         assert_eq!(inputs.len(), 1);
-        assert_eq!(inputs_sum, SourceAmount::with_base0(80));
+        assert_eq!(inputs_sum, SourceAmount::with_base0(800));
 
         // Insert tx2 inputs in mempool
         txs_mp_db
@@ -226,12 +228,12 @@ mod tests {
         let (inputs, inputs_sum) = db_reader.find_inputs(
             &bc_db,
             &txs_mp_db,
-            SourceAmount::with_base0(75),
+            SourceAmount::with_base0(750),
             &script,
             true,
         )?;
         assert_eq!(inputs.len(), 1);
-        assert_eq!(inputs_sum, SourceAmount::with_base0(90));
+        assert_eq!(inputs_sum, SourceAmount::with_base0(900));
 
         Ok(())
     }
diff --git a/rust-libs/modules/gva/dbs-reader/src/utxos.rs b/rust-libs/modules/gva/dbs-reader/src/utxos.rs
index e4339143e..c6263b3b2 100644
--- a/rust-libs/modules/gva/dbs-reader/src/utxos.rs
+++ b/rust-libs/modules/gva/dbs-reader/src/utxos.rs
@@ -244,22 +244,26 @@ where
 
     let it = utxos_iter.filter_map(|entry_res| match entry_res {
         Ok((gva_utxo_id, SourceAmountValV2(utxo_amount))) => {
-            let tx_hash = gva_utxo_id.get_tx_hash();
-            let output_index = gva_utxo_id.get_output_index();
-            match txs_mp_db_ro
-                .utxos_ids()
-                .contains_key(&UtxoIdDbV2(tx_hash, output_index as u32))
-            {
-                Ok(false) => Some(Ok((
-                    UtxoCursor {
-                        tx_hash,
-                        output_index,
-                        block_number: BlockNumber(gva_utxo_id.get_block_number()),
-                    },
-                    utxo_amount,
-                ))),
-                Ok(true) => None,
-                Err(e) => Some(Err(e)),
+            if utxo_amount.amount() < super::find_inputs::MIN_AMOUNT {
+                None
+            } else {
+                let tx_hash = gva_utxo_id.get_tx_hash();
+                let output_index = gva_utxo_id.get_output_index();
+                match txs_mp_db_ro
+                    .utxos_ids()
+                    .contains_key(&UtxoIdDbV2(tx_hash, output_index as u32))
+                {
+                    Ok(false) => Some(Ok((
+                        UtxoCursor {
+                            tx_hash,
+                            output_index,
+                            block_number: BlockNumber(gva_utxo_id.get_block_number()),
+                        },
+                        utxo_amount,
+                    ))),
+                    Ok(true) => None,
+                    Err(e) => Some(Err(e)),
+                }
             }
         }
         Err(e) => Some(Err(e)),
@@ -320,15 +324,15 @@ mod tests {
 
         gva_db.gva_utxos_write().upsert(
             GvaUtxoIdDbV1::new(script.clone(), 0, Hash::default(), 0),
-            SourceAmountValV2(SourceAmount::with_base0(50)),
+            SourceAmountValV2(SourceAmount::with_base0(500)),
         )?;
         gva_db.gva_utxos_write().upsert(
             GvaUtxoIdDbV1::new(script.clone(), 0, Hash::default(), 1),
-            SourceAmountValV2(SourceAmount::with_base0(80)),
+            SourceAmountValV2(SourceAmount::with_base0(800)),
         )?;
         gva_db.gva_utxos_write().upsert(
             GvaUtxoIdDbV1::new(script.clone(), 0, Hash::default(), 2),
-            SourceAmountValV2(SourceAmount::with_base0(120)),
+            SourceAmountValV2(SourceAmount::with_base0(1200)),
         )?;
 
         // Find utxos with amount target
@@ -338,7 +342,7 @@ mod tests {
             has_previous_page,
         } = db_reader.find_script_utxos(
             &txs_mp_db,
-            Some(SourceAmount::with_base0(55)),
+            Some(SourceAmount::with_base0(550)),
             PageInfo::default(),
             &script,
         )?;
@@ -352,7 +356,7 @@ mod tests {
                         tx_hash: Hash::default(),
                         output_index: 0,
                     },
-                    SourceAmount::with_base0(50)
+                    SourceAmount::with_base0(500)
                 ),
                 (
                     UtxoCursor {
@@ -360,11 +364,11 @@ mod tests {
                         tx_hash: Hash::default(),
                         output_index: 1,
                     },
-                    SourceAmount::with_base0(80)
+                    SourceAmount::with_base0(800)
                 ),
             ]
         );
-        assert_eq!(sum, SourceAmount::with_base0(130));
+        assert_eq!(sum, SourceAmount::with_base0(1300));
         assert!(!has_next_page);
         assert!(!has_previous_page);
 
@@ -374,7 +378,7 @@ mod tests {
             ..
         } = db_reader.find_script_utxos(
             &txs_mp_db,
-            Some(SourceAmount::with_base0(55)),
+            Some(SourceAmount::with_base0(550)),
             PageInfo {
                 order: false,
                 ..Default::default()
@@ -390,10 +394,10 @@ mod tests {
                     tx_hash: Hash::default(),
                     output_index: 2,
                 },
-                SourceAmount::with_base0(120)
+                SourceAmount::with_base0(1200)
             ),]
         );
-        assert_eq!(sum, SourceAmount::with_base0(120));
+        assert_eq!(sum, SourceAmount::with_base0(1200));
         assert!(!has_next_page);
         assert!(!has_previous_page);
 
@@ -422,7 +426,7 @@ mod tests {
                         tx_hash: Hash::default(),
                         output_index: 2,
                     },
-                    SourceAmount::with_base0(120)
+                    SourceAmount::with_base0(1200)
                 ),
                 (
                     UtxoCursor {
@@ -430,11 +434,11 @@ mod tests {
                         tx_hash: Hash::default(),
                         output_index: 1,
                     },
-                    SourceAmount::with_base0(80)
+                    SourceAmount::with_base0(800)
                 )
             ]
         );
-        assert_eq!(sum, SourceAmount::with_base0(200));
+        assert_eq!(sum, SourceAmount::with_base0(2000));
         assert!(!has_next_page);
         assert!(has_previous_page);
 
-- 
GitLab