1use crate::constants::*;
4use crate::economic::calculate_fee;
5use crate::error::{ConsensusError, Result};
6use crate::script::verify_script;
7use crate::segwit::Witness;
8use crate::transaction::{check_transaction, check_tx_inputs};
9use crate::types::*;
10use blvm_spec_lock::spec_locked;
11use std::collections::HashSet;
12
13#[spec_locked("9.1", "AcceptToMemoryPool")]
33pub fn accept_to_memory_pool(
34 tx: &Transaction,
35 witnesses: Option<&[Witness]>,
36 utxo_set: &UtxoSet,
37 mempool: &Mempool,
38 height: Natural,
39 time_context: Option<TimeContext>,
40) -> Result<MempoolResult> {
41 if tx.inputs.is_empty() && tx.outputs.is_empty() {
45 return Ok(MempoolResult::Rejected(
46 "Transaction must have at least one input or output".to_string(),
47 ));
48 }
49 if is_coinbase(tx) {
50 return Ok(MempoolResult::Rejected(
51 "Coinbase transactions cannot be added to mempool".to_string(),
52 ));
53 }
54 assert!(
55 height <= i64::MAX as u64,
56 "Block height {height} must fit in i64"
57 );
58 assert!(
59 utxo_set.len() <= u32::MAX as usize,
60 "UTXO set size {} exceeds maximum",
61 utxo_set.len()
62 );
63 if let Some(wits) = witnesses {
64 assert!(
65 wits.len() == tx.inputs.len(),
66 "Witness count {} must match input count {}",
67 wits.len(),
68 tx.inputs.len()
69 );
70 }
71
72 let tx_id = crate::block::calculate_tx_id(tx);
74 assert!(tx_id != [0u8; 32], "Transaction ID must be non-zero");
76 if mempool.contains(&tx_id) {
77 return Ok(MempoolResult::Rejected(
78 "Transaction already in mempool".to_string(),
79 ));
80 }
81
82 if !matches!(check_transaction(tx)?, ValidationResult::Valid) {
84 return Ok(MempoolResult::Rejected(
85 "Invalid transaction structure".to_string(),
86 ));
87 }
88
89 let block_time = time_context.map(|ctx| ctx.median_time_past).unwrap_or(0);
92 if !is_final_tx(tx, height, block_time) {
93 return Ok(MempoolResult::Rejected(
94 "Transaction not final (locktime not satisfied)".to_string(),
95 ));
96 }
97
98 let (input_valid, fee) = check_tx_inputs(tx, utxo_set, height)?;
100 assert!(fee >= 0, "Fee {fee} must be non-negative");
102 use crate::constants::MAX_MONEY;
103 assert!(fee <= MAX_MONEY, "Fee {fee} must not exceed MAX_MONEY");
104 if !matches!(input_valid, ValidationResult::Valid) {
105 return Ok(MempoolResult::Rejected(
106 "Invalid transaction inputs".to_string(),
107 ));
108 }
109
110 if !is_coinbase(tx) {
112 let flags = calculate_script_flags(tx, witnesses);
115
116 #[cfg(all(feature = "production", feature = "rayon"))]
117 {
118 use rayon::prelude::*;
119
120 let input_utxos: Vec<(usize, Option<&UTXO>)> = {
124 let mut result = Vec::with_capacity(tx.inputs.len());
125 for (i, input) in tx.inputs.iter().enumerate() {
126 result.push((i, utxo_set.get(&input.prevout).map(|a| a.as_ref())));
127 }
128 result
129 };
130
131 let script_results: Result<Vec<bool>> = input_utxos
133 .par_iter()
134 .map(|(i, opt_utxo)| {
135 if let Some(utxo) = opt_utxo {
136 let input = &tx.inputs[*i];
137 let witness: Option<&ByteString> = witnesses
138 .and_then(|wits| wits.get(*i))
139 .and_then(|wit| wit.first());
140
141 verify_script(&input.script_sig, &utxo.script_pubkey, witness, flags)
142 } else {
143 Ok(false)
144 }
145 })
146 .collect();
147
148 let script_results = script_results?;
150 assert!(
152 script_results.len() == tx.inputs.len(),
153 "Script results count {} must match input count {}",
154 script_results.len(),
155 tx.inputs.len()
156 );
157 for (i, &is_valid) in script_results.iter().enumerate() {
158 assert!(
160 i < tx.inputs.len(),
161 "Input index {i} out of bounds in script validation loop",
162 );
163 if !is_valid {
165 return Ok(MempoolResult::Rejected(format!(
166 "Invalid script at input {i}"
167 )));
168 }
169 }
170 }
171
172 #[cfg(not(all(feature = "production", feature = "rayon")))]
173 {
174 for (i, input) in tx.inputs.iter().enumerate() {
176 if let Some(utxo) = utxo_set.get(&input.prevout) {
177 let witness: Option<&ByteString> =
182 witnesses.and_then(|wits| wits.get(i)).and_then(|wit| {
183 wit.first()
186 });
187
188 if !verify_script(&input.script_sig, &utxo.script_pubkey, witness, flags)? {
189 return Ok(MempoolResult::Rejected(format!(
190 "Invalid script at input {i}"
191 )));
192 }
193 }
194 }
195 }
196 }
197
198 if !check_mempool_rules(tx, fee, mempool)? {
200 return Ok(MempoolResult::Rejected("Failed mempool rules".to_string()));
201 }
202
203 if has_conflicts(tx, mempool)? {
205 return Ok(MempoolResult::Rejected(
206 "Transaction conflicts with mempool".to_string(),
207 ));
208 }
209
210 Ok(MempoolResult::Accepted)
211}
212
213fn calculate_script_flags(tx: &Transaction, witnesses: Option<&[Witness]>) -> u32 {
220 let has_witness = witnesses.map(|w| !w.is_empty()).unwrap_or(false);
231 const MEMPOOL_POLICY_HEIGHT: u64 = 1_000_000; crate::block::calculate_script_flags_for_block_network(
233 tx,
234 has_witness,
235 MEMPOOL_POLICY_HEIGHT,
236 crate::types::Network::Mainnet,
237 )
238}
239
240#[spec_locked("9.2", "IsStandardTx")]
248pub fn is_standard_tx(tx: &Transaction) -> Result<bool> {
249 let tx_size = calculate_transaction_size(tx);
251 if tx_size > MAX_TX_SIZE {
252 return Ok(false);
253 }
254
255 for (i, input) in tx.inputs.iter().enumerate() {
257 assert!(i < tx.inputs.len(), "Input index {i} out of bounds");
259 assert!(
261 input.script_sig.len() <= MAX_SCRIPT_SIZE * 2,
262 "Script size {} must be reasonable for input {}",
263 input.script_sig.len(),
264 i
265 );
266 if input.script_sig.len() > MAX_SCRIPT_SIZE {
267 return Ok(false);
268 }
269 }
270
271 for (i, output) in tx.outputs.iter().enumerate() {
272 assert!(i < tx.outputs.len(), "Output index {i} out of bounds");
274 assert!(
276 output.script_pubkey.len() <= MAX_SCRIPT_SIZE * 2,
277 "Script size {} must be reasonable for output {}",
278 output.script_pubkey.len(),
279 i
280 );
281 if output.script_pubkey.len() > MAX_SCRIPT_SIZE {
282 return Ok(false);
283 }
284 }
285
286 for (i, output) in tx.outputs.iter().enumerate() {
288 assert!(
290 i < tx.outputs.len(),
291 "Output index {i} out of bounds in standard check"
292 );
293 if !is_standard_script(&output.script_pubkey)? {
294 return Ok(false);
295 }
296 }
297
298 let result = true;
300 Ok(result)
302}
303
304#[spec_locked("9.3", "ReplacementChecks")]
315pub fn replacement_checks(
316 new_tx: &Transaction,
317 existing_tx: &Transaction,
318 utxo_set: &UtxoSet,
319 mempool: &Mempool,
320) -> Result<bool> {
321 if new_tx.inputs.is_empty() && new_tx.outputs.is_empty() {
326 return Err(crate::error::ConsensusError::ConsensusRuleViolation(
327 "New transaction must have at least one input or output"
328 .to_string()
329 .into(),
330 ));
331 }
332 if existing_tx.inputs.is_empty() && existing_tx.outputs.is_empty() {
333 return Err(crate::error::ConsensusError::ConsensusRuleViolation(
334 "Existing transaction must have at least one input or output"
335 .to_string()
336 .into(),
337 ));
338 }
339 if is_coinbase(new_tx) {
340 return Err(crate::error::ConsensusError::ConsensusRuleViolation(
341 "New transaction cannot be coinbase".to_string().into(),
342 ));
343 }
344 if is_coinbase(existing_tx) {
345 return Err(crate::error::ConsensusError::ConsensusRuleViolation(
346 "Existing transaction cannot be coinbase".to_string().into(),
347 ));
348 }
349 assert!(
350 utxo_set.len() <= u32::MAX as usize,
351 "UTXO set size {} exceeds maximum",
352 utxo_set.len()
353 );
354
355 if !signals_rbf(existing_tx) {
358 return Ok(false);
359 }
360
361 let new_fee = calculate_fee(new_tx, utxo_set)?;
363 let existing_fee = calculate_fee(existing_tx, utxo_set)?;
364 assert!(new_fee >= 0, "New fee {new_fee} must be non-negative");
366 assert!(
367 existing_fee >= 0,
368 "Existing fee {existing_fee} must be non-negative"
369 );
370 use crate::constants::MAX_MONEY;
371 assert!(
372 new_fee <= MAX_MONEY,
373 "New fee {new_fee} must not exceed MAX_MONEY"
374 );
375 assert!(
376 existing_fee <= MAX_MONEY,
377 "Existing fee {existing_fee} must not exceed MAX_MONEY"
378 );
379
380 let new_tx_size = calculate_transaction_size_vbytes(new_tx);
381 let existing_tx_size = calculate_transaction_size_vbytes(existing_tx);
382 assert!(
384 new_tx_size > 0,
385 "New transaction size {new_tx_size} must be positive"
386 );
387 assert!(
388 existing_tx_size > 0,
389 "Existing transaction size {existing_tx_size} must be positive"
390 );
391 assert!(
392 new_tx_size <= MAX_TX_SIZE * 2,
393 "New transaction size {new_tx_size} must be reasonable"
394 );
395 assert!(
396 existing_tx_size <= MAX_TX_SIZE * 2,
397 "Existing transaction size {existing_tx_size} must be reasonable"
398 );
399
400 if new_tx_size == 0 || existing_tx_size == 0 {
401 return Ok(false);
402 }
403
404 debug_assert!(
411 new_tx_size > 0,
412 "New transaction size ({new_tx_size}) must be positive"
413 );
414 debug_assert!(
415 existing_tx_size > 0,
416 "Existing transaction size ({existing_tx_size}) must be positive"
417 );
418
419 let new_fee_scaled = (new_fee as u128)
422 .checked_mul(existing_tx_size as u128)
423 .ok_or_else(|| {
424 ConsensusError::TransactionValidation("Fee rate calculation overflow".into())
425 })?;
426 let existing_fee_scaled = (existing_fee as u128)
427 .checked_mul(new_tx_size as u128)
428 .ok_or_else(|| {
429 ConsensusError::TransactionValidation("Fee rate calculation overflow".into())
430 })?;
431
432 if new_fee_scaled <= existing_fee_scaled {
433 return Ok(false);
434 }
435
436 if new_fee <= existing_fee + MIN_RELAY_FEE {
438 return Ok(false);
439 }
440
441 if !has_conflict_with_tx(new_tx, existing_tx) {
443 return Ok(false);
444 }
445
446 if creates_new_dependencies(new_tx, existing_tx, utxo_set, mempool)? {
449 return Ok(false);
450 }
451
452 Ok(true)
453}
454
455pub type Mempool = HashSet<Hash>;
461
462#[derive(Debug, Clone, PartialEq, Eq)]
464pub enum MempoolResult {
465 Accepted,
466 Rejected(String),
467}
468
469pub fn update_mempool_after_block(
530 mempool: &mut Mempool,
531 block: &crate::types::Block,
532 _utxo_set: &crate::types::UtxoSet,
533) -> Result<Vec<Hash>> {
534 let mut removed = Vec::new();
535
536 for tx in &block.transactions {
538 let tx_id = crate::block::calculate_tx_id(tx);
539 if mempool.remove(&tx_id) {
540 removed.push(tx_id);
541 }
542 }
543
544 Ok(removed)
552}
553
554pub fn update_mempool_after_block_with_lookup<F>(
569 mempool: &mut Mempool,
570 block: &crate::types::Block,
571 get_tx_by_id: F,
572) -> Result<Vec<Hash>>
573where
574 F: Fn(&Hash) -> Option<crate::types::Transaction>,
575{
576 let mut removed = Vec::new();
577
578 for tx in &block.transactions {
580 let tx_id = crate::block::calculate_tx_id(tx);
581 if mempool.remove(&tx_id) {
582 removed.push(tx_id);
583 }
584 }
585
586 let mut spent_outpoints = std::collections::HashSet::new();
589 for tx in &block.transactions {
590 if !crate::transaction::is_coinbase(tx) {
591 for input in &tx.inputs {
592 spent_outpoints.insert(input.prevout);
593 }
594 }
595 }
596
597 let mut invalid_tx_ids = Vec::new();
599 for &tx_id in mempool.iter() {
600 if let Some(tx) = get_tx_by_id(&tx_id) {
601 for input in &tx.inputs {
603 if spent_outpoints.contains(&input.prevout) {
604 invalid_tx_ids.push(tx_id);
605 break;
606 }
607 }
608 }
609 }
610
611 for tx_id in invalid_tx_ids {
613 if mempool.remove(&tx_id) {
614 removed.push(tx_id);
615 }
616 }
617
618 Ok(removed)
619}
620
621fn check_mempool_rules(tx: &Transaction, fee: Integer, mempool: &Mempool) -> Result<bool> {
623 let tx_size = calculate_transaction_size(tx);
625 debug_assert!(
629 tx_size > 0,
630 "Transaction size ({tx_size}) must be positive for fee rate calculation"
631 );
632
633 let fee_rate = (fee as f64) / (tx_size as f64);
634
635 debug_assert!(
637 fee_rate >= 0.0,
638 "Fee rate ({fee_rate:.6}) must be non-negative (fee: {fee}, size: {tx_size})"
639 );
640
641 let config = crate::config::get_consensus_config_ref();
643 let min_fee_rate = config.mempool.min_relay_fee_rate as f64; let min_tx_fee = config.mempool.min_tx_fee; if fee < min_tx_fee {
648 return Ok(false);
649 }
650
651 if fee_rate < min_fee_rate {
653 return Ok(false);
654 }
655
656 if mempool.len() > config.mempool.max_mempool_txs {
659 return Ok(false);
660 }
661
662 Ok(true)
663}
664
665fn has_conflicts(tx: &Transaction, mempool: &Mempool) -> Result<bool> {
667 for input in &tx.inputs {
669 if mempool.contains(&input.prevout.hash) {
673 return Ok(true);
674 }
675 }
676
677 Ok(false)
678}
679
680#[spec_locked("9.1.1", "CheckFinalTxAtTip")]
721pub fn is_final_tx(tx: &Transaction, height: Natural, block_time: Natural) -> bool {
722 use crate::constants::SEQUENCE_FINAL;
723
724 if tx.lock_time == 0 {
726 return true;
727 }
728
729 let locktime_satisfied = if (tx.lock_time as u32) < LOCKTIME_THRESHOLD {
735 (tx.lock_time as Natural) < height
737 } else {
738 (tx.lock_time as Natural) < block_time
740 };
741
742 if locktime_satisfied {
743 return true;
744 }
745
746 for input in &tx.inputs {
750 if (input.sequence as u32) != SEQUENCE_FINAL {
751 return false;
752 }
753 }
754
755 true
757}
758
759pub fn signals_rbf(tx: &Transaction) -> bool {
763 for input in &tx.inputs {
764 if (input.sequence as u32) < SEQUENCE_FINAL {
765 return true;
766 }
767 }
768 false
769}
770
771fn calculate_transaction_size_vbytes(tx: &Transaction) -> usize {
777 calculate_transaction_size(tx)
780}
781
782pub fn has_conflict_with_tx(new_tx: &Transaction, existing_tx: &Transaction) -> bool {
787 for new_input in &new_tx.inputs {
788 for existing_input in &existing_tx.inputs {
789 if new_input.prevout == existing_input.prevout {
790 return true;
791 }
792 }
793 }
794 false
795}
796
797fn creates_new_dependencies(
803 new_tx: &Transaction,
804 existing_tx: &Transaction,
805 utxo_set: &UtxoSet,
806 mempool: &Mempool,
807) -> Result<bool> {
808 for input in &new_tx.inputs {
809 if utxo_set.contains_key(&input.prevout) {
811 continue;
812 }
813
814 let mut found_in_existing = false;
816 for existing_input in &existing_tx.inputs {
817 if existing_input.prevout == input.prevout {
818 found_in_existing = true;
819 break;
820 }
821 }
822
823 if found_in_existing {
824 continue;
825 }
826
827 if !mempool.contains(&input.prevout.hash) {
830 return Ok(true); }
832 }
833
834 Ok(false)
835}
836
837fn is_standard_script(script: &ByteString) -> Result<bool> {
839 if script.is_empty() {
842 return Ok(false);
843 }
844
845 if script.len() > MAX_SCRIPT_SIZE {
847 return Ok(false);
848 }
849
850 for &byte in script {
852 if byte > 0x60 && byte < 0x7f {
853 return Ok(false);
855 }
856 }
857
858 Ok(true)
859}
860
861#[deprecated(note = "Use crate::block::calculate_tx_id instead")]
866#[spec_locked("5.1", "CalculateTxId")]
867pub fn calculate_tx_id(tx: &Transaction) -> Hash {
868 crate::block::calculate_tx_id(tx)
869}
870
871fn calculate_transaction_size(tx: &Transaction) -> usize {
875 use crate::transaction::calculate_transaction_size as tx_size;
876 tx_size(tx)
877}
878
879fn is_coinbase(tx: &Transaction) -> bool {
881 #[cfg(feature = "production")]
883 {
884 use crate::optimizations::constant_folding::is_zero_hash;
885 tx.inputs.len() == 1
886 && is_zero_hash(&tx.inputs[0].prevout.hash)
887 && tx.inputs[0].prevout.index == 0xffffffff
888 }
889
890 #[cfg(not(feature = "production"))]
891 {
892 tx.inputs.len() == 1
893 && tx.inputs[0].prevout.hash == [0u8; 32]
894 && tx.inputs[0].prevout.index == 0xffffffff
895 }
896}
897
898#[cfg(test)]
918mod tests {
919 use super::*;
920 use crate::opcodes::*;
921
922 #[test]
923 fn test_accept_to_memory_pool_valid() {
924 let tx = create_valid_transaction();
926 let utxo_set = create_test_utxo_set();
927 let mempool = Mempool::new();
928
929 let time_context = Some(TimeContext {
931 network_time: 1234567890,
932 median_time_past: 1234567890,
933 });
934 let result =
935 accept_to_memory_pool(&tx, None, &utxo_set, &mempool, 100, time_context).unwrap();
936 assert!(matches!(result, MempoolResult::Rejected(_)));
937 }
938
939 #[test]
940 fn test_accept_to_memory_pool_duplicate() {
941 let tx = create_valid_transaction();
942 let utxo_set = create_test_utxo_set();
943 let mut mempool = Mempool::new();
944 mempool.insert(crate::block::calculate_tx_id(&tx));
945
946 let time_context = Some(TimeContext {
947 network_time: 1234567890,
948 median_time_past: 1234567890,
949 });
950 let result =
951 accept_to_memory_pool(&tx, None, &utxo_set, &mempool, 100, time_context).unwrap();
952 assert!(matches!(result, MempoolResult::Rejected(_)));
953 }
954
955 #[test]
956 fn test_is_standard_tx_valid() {
957 let tx = create_valid_transaction();
958 assert!(is_standard_tx(&tx).unwrap());
959 }
960
961 #[test]
962 fn test_is_standard_tx_too_large() {
963 let mut tx = create_valid_transaction();
964 for _ in 0..MAX_INPUTS {
967 tx.inputs.push(create_dummy_input());
968 }
969 assert!(!is_standard_tx(&tx).unwrap());
971 }
972
973 #[test]
974 fn test_replacement_checks_all_requirements() {
975 let utxo_set = create_test_utxo_set();
976 let mempool = Mempool::new();
977
978 let mut existing_tx = create_valid_transaction();
980 existing_tx.inputs[0].sequence = SEQUENCE_RBF as u64;
981 existing_tx.outputs[0].value = 9000; let mut new_tx = existing_tx.clone();
988 new_tx.outputs[0].value = 8000; new_tx.outputs[0].value = 7999; let result = replacement_checks(&new_tx, &existing_tx, &utxo_set, &mempool).unwrap();
994 assert!(result, "Valid RBF replacement should be accepted");
995 }
996
997 #[test]
998 fn test_replacement_checks_no_rbf_signal() {
999 let utxo_set = create_test_utxo_set();
1000 let mempool = Mempool::new();
1001
1002 let new_tx = create_valid_transaction();
1003 let existing_tx = create_valid_transaction(); assert!(!replacement_checks(&new_tx, &existing_tx, &utxo_set, &mempool).unwrap());
1007 }
1008
1009 #[test]
1010 fn test_replacement_checks_no_conflict() {
1011 let mut utxo_set = create_test_utxo_set();
1012 let new_outpoint = OutPoint {
1014 hash: [2; 32],
1015 index: 0,
1016 };
1017 let new_utxo = UTXO {
1018 value: 10000,
1019 script_pubkey: vec![OP_1].into(),
1020 height: 0,
1021 is_coinbase: false,
1022 };
1023 utxo_set.insert(new_outpoint, std::sync::Arc::new(new_utxo));
1024
1025 let mempool = Mempool::new();
1026
1027 let mut existing_tx = create_valid_transaction();
1028 existing_tx.inputs[0].sequence = SEQUENCE_RBF as u64;
1029
1030 let mut new_tx = create_valid_transaction();
1032 new_tx.inputs[0].prevout.hash = [2; 32]; new_tx.inputs[0].sequence = SEQUENCE_RBF as u64;
1034 new_tx.outputs[0].value = 5000; assert!(!replacement_checks(&new_tx, &existing_tx, &utxo_set, &mempool).unwrap());
1039 }
1040
1041 #[test]
1042 fn test_replacement_checks_fee_rate_too_low() {
1043 let utxo_set = create_test_utxo_set();
1044 let mempool = Mempool::new();
1045
1046 let mut existing_tx = create_valid_transaction();
1048 existing_tx.inputs[0].sequence = SEQUENCE_RBF as u64;
1049 existing_tx.outputs[0].value = 5000; let mut new_tx = existing_tx.clone();
1053 new_tx.outputs[0].value = 4999; assert!(!replacement_checks(&new_tx, &existing_tx, &utxo_set, &mempool).unwrap());
1057 }
1058
1059 #[test]
1060 fn test_replacement_checks_absolute_fee_insufficient() {
1061 let utxo_set = create_test_utxo_set();
1062 let mempool = Mempool::new();
1063
1064 let mut existing_tx = create_valid_transaction();
1066 existing_tx.inputs[0].sequence = SEQUENCE_RBF as u64;
1067 existing_tx.outputs[0].value = 9000; let mut new_tx = existing_tx.clone();
1072 new_tx.outputs[0].value = 8001; assert!(!replacement_checks(&new_tx, &existing_tx, &utxo_set, &mempool).unwrap());
1076
1077 new_tx.outputs[0].value = 7999; }
1082
1083 #[test]
1088 fn test_accept_to_memory_pool_coinbase() {
1089 let coinbase_tx = create_coinbase_transaction();
1090 let utxo_set = UtxoSet::default();
1091 let mempool = Mempool::new();
1092 let time_context = Some(TimeContext {
1094 network_time: 0,
1095 median_time_past: 0,
1096 });
1097 let result =
1098 accept_to_memory_pool(&coinbase_tx, None, &utxo_set, &mempool, 100, time_context)
1099 .unwrap();
1100 assert!(matches!(result, MempoolResult::Rejected(_)));
1101 }
1102
1103 #[test]
1104 fn test_is_standard_tx_large_script() {
1105 let mut tx = create_valid_transaction();
1106 tx.inputs[0].script_sig = vec![OP_1; MAX_SCRIPT_SIZE + 1];
1108
1109 let result = is_standard_tx(&tx).unwrap();
1110 assert!(!result);
1111 }
1112
1113 #[test]
1114 fn test_is_standard_tx_large_output_script() {
1115 let mut tx = create_valid_transaction();
1116 tx.outputs[0].script_pubkey = vec![OP_1; MAX_SCRIPT_SIZE + 1];
1118
1119 let result = is_standard_tx(&tx).unwrap();
1120 assert!(!result);
1121 }
1122
1123 #[test]
1124 fn test_replacement_checks_new_unconfirmed_dependency() {
1125 let utxo_set = create_test_utxo_set();
1126 let mempool = Mempool::new();
1127
1128 let mut existing_tx = create_valid_transaction();
1130 existing_tx.inputs[0].sequence = SEQUENCE_RBF as u64;
1131
1132 let mut new_tx = existing_tx.clone();
1134 new_tx.inputs.push(TransactionInput {
1135 prevout: OutPoint {
1136 hash: [99; 32],
1137 index: 0,
1138 }, script_sig: vec![],
1140 sequence: SEQUENCE_RBF as u64,
1141 });
1142 new_tx.outputs[0].value = 7000; assert!(!replacement_checks(&new_tx, &existing_tx, &utxo_set, &mempool).unwrap());
1146 }
1147
1148 #[test]
1149 fn test_has_conflict_with_tx_true() {
1150 let tx1 = create_valid_transaction();
1151 let mut tx2 = create_valid_transaction();
1152 tx2.inputs[0].prevout = tx1.inputs[0].prevout.clone(); assert!(has_conflict_with_tx(&tx2, &tx1));
1155 }
1156
1157 #[test]
1158 fn test_has_conflict_with_tx_false() {
1159 let tx1 = create_valid_transaction();
1160 let mut tx2 = create_valid_transaction();
1161 tx2.inputs[0].prevout.hash = [2; 32]; assert!(!has_conflict_with_tx(&tx2, &tx1));
1164 }
1165
1166 #[test]
1167 fn test_replacement_checks_minimum_relay_fee() {
1168 let utxo_set = create_test_utxo_set();
1169 let mempool = Mempool::new();
1170
1171 let mut existing_tx = create_valid_transaction();
1173 existing_tx.inputs[0].sequence = SEQUENCE_RBF as u64;
1174 existing_tx.outputs[0].value = 9500; let mut new_tx = existing_tx.clone();
1178 new_tx.outputs[0].value = 8500; assert!(!replacement_checks(&new_tx, &existing_tx, &utxo_set, &mempool).unwrap());
1180
1181 new_tx.outputs[0].value = 8499;
1185 }
1186
1187 #[test]
1188 fn test_check_mempool_rules_low_fee() {
1189 let tx = create_valid_transaction();
1190 let fee = 1; let mempool = Mempool::new();
1192
1193 let result = check_mempool_rules(&tx, fee, &mempool).unwrap();
1194 assert!(!result);
1195 }
1196
1197 #[test]
1198 fn test_check_mempool_rules_high_fee() {
1199 let tx = create_valid_transaction();
1200 let fee = 10000; let mempool = Mempool::new();
1202
1203 let result = check_mempool_rules(&tx, fee, &mempool).unwrap();
1204 assert!(result);
1205 }
1206
1207 #[test]
1208 fn test_check_mempool_rules_full_mempool() {
1209 let tx = create_valid_transaction();
1210 let fee = 10000;
1211 let mut mempool = Mempool::new();
1212
1213 for i in 0..100_001 {
1216 let mut hash = [0u8; 32];
1217 hash[0] = (i & 0xff) as u8;
1218 hash[1] = ((i >> 8) & 0xff) as u8;
1219 hash[2] = ((i >> 16) & 0xff) as u8;
1220 hash[3] = ((i >> 24) & 0xff) as u8;
1221 mempool.insert(hash);
1222 }
1223
1224 assert!(mempool.len() > 100_000);
1226
1227 let result = check_mempool_rules(&tx, fee, &mempool).unwrap();
1228 assert!(!result);
1229 }
1230
1231 #[test]
1232 fn test_has_conflicts_no_conflicts() {
1233 let tx = create_valid_transaction();
1234 let mempool = Mempool::new();
1235
1236 let result = has_conflicts(&tx, &mempool).unwrap();
1237 assert!(!result);
1238 }
1239
1240 #[test]
1241 fn test_has_conflicts_with_conflicts() {
1242 let tx = create_valid_transaction();
1243 let mut mempool = Mempool::new();
1244
1245 mempool.insert(tx.inputs[0].prevout.hash);
1247
1248 let result = has_conflicts(&tx, &mempool).unwrap();
1249 assert!(result);
1250 }
1251
1252 #[test]
1253 fn test_signals_rbf_true() {
1254 let mut tx = create_valid_transaction();
1255 tx.inputs[0].sequence = 0xfffffffe; assert!(signals_rbf(&tx));
1258 }
1259
1260 #[test]
1261 fn test_signals_rbf_false() {
1262 let tx = create_valid_transaction(); assert!(!signals_rbf(&tx));
1265 }
1266
1267 #[test]
1268 fn test_calculate_fee_rate() {
1269 let tx = create_valid_transaction();
1270 let utxo_set = create_test_utxo_set();
1271 let fee = calculate_fee(&tx, &utxo_set);
1272
1273 assert!(fee.is_ok());
1275 }
1276
1277 #[test]
1278 fn test_creates_new_dependencies_no_new() {
1279 let new_tx = create_valid_transaction();
1280 let existing_tx = create_valid_transaction();
1281 let mempool = Mempool::new();
1282
1283 let utxo_set = create_test_utxo_set();
1284 let result = creates_new_dependencies(&new_tx, &existing_tx, &utxo_set, &mempool).unwrap();
1285 assert!(!result);
1286 }
1287
1288 #[test]
1289 fn test_creates_new_dependencies_with_new() {
1290 let mut new_tx = create_valid_transaction();
1291 let existing_tx = create_valid_transaction();
1292 let mempool = Mempool::new();
1293
1294 new_tx.inputs[0].prevout.hash = [2; 32];
1296
1297 let utxo_set = create_test_utxo_set();
1298 let result = creates_new_dependencies(&new_tx, &existing_tx, &utxo_set, &mempool).unwrap();
1299 assert!(result);
1300 }
1301
1302 #[test]
1303 fn test_is_standard_script_empty() {
1304 let script = vec![];
1305 let result = is_standard_script(&script).unwrap();
1306 assert!(!result);
1307 }
1308
1309 #[test]
1310 fn test_is_standard_script_too_large() {
1311 let script = vec![OP_1; MAX_SCRIPT_SIZE + 1];
1312 let result = is_standard_script(&script).unwrap();
1313 assert!(!result);
1314 }
1315
1316 #[test]
1317 fn test_is_standard_script_non_standard_opcode() {
1318 let script = vec![OP_VERIF]; let result = is_standard_script(&script).unwrap();
1320 assert!(!result);
1321 }
1322
1323 #[test]
1324 fn test_is_standard_script_valid() {
1325 let script = vec![OP_1];
1326 let result = is_standard_script(&script).unwrap();
1327 assert!(result);
1328 }
1329
1330 #[test]
1331 fn test_calculate_tx_id() {
1332 let tx = create_valid_transaction();
1333 let tx_id = crate::block::calculate_tx_id(&tx);
1334
1335 assert_eq!(tx_id.len(), 32);
1337
1338 let tx_id2 = crate::block::calculate_tx_id(&tx);
1340 assert_eq!(tx_id, tx_id2);
1341 }
1342
1343 #[test]
1344 fn test_calculate_tx_id_different_txs() {
1345 let tx1 = create_valid_transaction();
1346 let mut tx2 = tx1.clone();
1347 tx2.version = 2; let id1 = crate::block::calculate_tx_id(&tx1);
1350 let id2 = crate::block::calculate_tx_id(&tx2);
1351
1352 assert_ne!(id1, id2);
1353 }
1354
1355 #[test]
1356 fn test_calculate_transaction_size() {
1357 let tx = create_valid_transaction();
1358 let size = calculate_transaction_size(&tx);
1359
1360 assert!(size > 0);
1361
1362 let size2 = calculate_transaction_size(&tx);
1364 assert_eq!(size, size2);
1365 }
1366
1367 #[test]
1368 fn test_calculate_transaction_size_multiple_inputs_outputs() {
1369 let mut tx = create_valid_transaction();
1370 tx.inputs.push(create_dummy_input());
1371 tx.outputs.push(create_dummy_output());
1372
1373 let size = calculate_transaction_size(&tx);
1374 assert!(size > 0);
1375 }
1376
1377 #[test]
1378 fn test_is_coinbase_true() {
1379 let coinbase_tx = create_coinbase_transaction();
1380 assert!(is_coinbase(&coinbase_tx));
1381 }
1382
1383 #[test]
1384 fn test_is_coinbase_false() {
1385 let regular_tx = create_valid_transaction();
1386 assert!(!is_coinbase(®ular_tx));
1387 }
1388
1389 fn create_valid_transaction() -> Transaction {
1391 Transaction {
1392 version: 1,
1393 inputs: vec![create_dummy_input()].into(),
1394 outputs: vec![create_dummy_output()].into(),
1395 lock_time: 0,
1396 }
1397 }
1398
1399 fn create_dummy_input() -> TransactionInput {
1400 TransactionInput {
1401 prevout: OutPoint {
1402 hash: [1; 32],
1403 index: 0,
1404 },
1405 script_sig: vec![OP_1],
1406 sequence: 0xffffffff,
1407 }
1408 }
1409
1410 fn create_dummy_output() -> TransactionOutput {
1411 TransactionOutput {
1412 value: 1000,
1413 script_pubkey: vec![OP_1].into(), }
1415 }
1416
1417 fn create_test_utxo_set() -> UtxoSet {
1418 let mut utxo_set = UtxoSet::default();
1419 let outpoint = OutPoint {
1420 hash: [1; 32],
1421 index: 0,
1422 };
1423 let utxo = UTXO {
1424 value: 10000,
1425 script_pubkey: vec![OP_1].into(), height: 0,
1427 is_coinbase: false,
1428 };
1429 utxo_set.insert(outpoint, std::sync::Arc::new(utxo));
1430 utxo_set
1431 }
1432
1433 fn create_coinbase_transaction() -> Transaction {
1434 Transaction {
1435 version: 1,
1436 inputs: vec![TransactionInput {
1437 prevout: OutPoint {
1438 hash: [0; 32].into(),
1439 index: 0xffffffff,
1440 },
1441 script_sig: vec![],
1442 sequence: 0xffffffff,
1443 }]
1444 .into(),
1445 outputs: vec![TransactionOutput {
1446 value: 5000000000,
1447 script_pubkey: vec![].into(),
1448 }]
1449 .into(),
1450 lock_time: 0,
1451 }
1452 }
1453}