Skip to content
Snippets Groups Projects
Commit 3040f42c authored by Éloïs's avatar Éloïs
Browse files

[fix] gva:find_inputs: do not use inputs with amout less than 100

parent 6bf5eafa
No related branches found
No related tags found
1 merge request!1346Opti/gva txs history
......@@ -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(())
}
......
......@@ -244,6 +244,9 @@ where
let it = utxos_iter.filter_map(|entry_res| match entry_res {
Ok((gva_utxo_id, SourceAmountValV2(utxo_amount))) => {
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
......@@ -262,6 +265,7 @@ where
Err(e) => Some(Err(e)),
}
}
}
Err(e) => Some(Err(e)),
});
let utxos = if let Some(limit) = page_info.limit_opt {
......@@ -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);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment