dpp/address_funds/
witness_verification_operations.rs1#[derive(Debug, Clone, Default, PartialEq, Eq)]
6pub struct AddressWitnessVerificationOperations {
7 pub ecdsa_signature_verifications: u16,
10
11 pub message_hash_count: u16,
18
19 pub pubkey_hash_verifications: u16,
22
23 pub script_hash_verifications: u16,
26
27 pub signable_bytes_len: usize,
31}
32
33impl AddressWitnessVerificationOperations {
34 pub fn new() -> Self {
36 Self::default()
37 }
38
39 pub fn for_p2pkh(signable_bytes_len: usize) -> Self {
44 Self {
45 ecdsa_signature_verifications: 1,
46 message_hash_count: 1,
47 pubkey_hash_verifications: 1,
48 script_hash_verifications: 0,
49 signable_bytes_len,
50 }
51 }
52
53 pub fn for_p2sh_multisig(signatures_verified: u16, signable_bytes_len: usize) -> Self {
60 Self {
61 ecdsa_signature_verifications: signatures_verified,
62 message_hash_count: 1,
64 pubkey_hash_verifications: 0,
65 script_hash_verifications: 1,
66 signable_bytes_len,
67 }
68 }
69
70 pub fn combine(&mut self, other: &Self) {
72 self.ecdsa_signature_verifications = self
73 .ecdsa_signature_verifications
74 .saturating_add(other.ecdsa_signature_verifications);
75 self.message_hash_count = self
76 .message_hash_count
77 .saturating_add(other.message_hash_count);
78 self.pubkey_hash_verifications = self
79 .pubkey_hash_verifications
80 .saturating_add(other.pubkey_hash_verifications);
81 self.script_hash_verifications = self
82 .script_hash_verifications
83 .saturating_add(other.script_hash_verifications);
84 self.signable_bytes_len = self.signable_bytes_len.max(other.signable_bytes_len);
86 }
87
88 pub fn total_signature_verifications(&self) -> u16 {
90 self.ecdsa_signature_verifications
91 }
92
93 pub fn total_hash_operations(&self) -> u16 {
95 self.pubkey_hash_verifications
96 .saturating_add(self.script_hash_verifications)
97 }
98}
99
100#[cfg(test)]
101mod tests {
102 use super::*;
103
104 #[test]
105 fn test_p2pkh_operations() {
106 let ops = AddressWitnessVerificationOperations::for_p2pkh(100);
107 assert_eq!(ops.ecdsa_signature_verifications, 1);
108 assert_eq!(ops.pubkey_hash_verifications, 1);
109 assert_eq!(ops.script_hash_verifications, 0);
110 assert_eq!(ops.signable_bytes_len, 100);
111 }
112
113 #[test]
114 fn test_p2sh_multisig_operations() {
115 let ops = AddressWitnessVerificationOperations::for_p2sh_multisig(2, 150);
117 assert_eq!(ops.ecdsa_signature_verifications, 2);
118 assert_eq!(ops.pubkey_hash_verifications, 0);
119 assert_eq!(ops.script_hash_verifications, 1);
120 assert_eq!(ops.signable_bytes_len, 150);
121 }
122
123 #[test]
124 fn test_combine_operations() {
125 let mut ops1 = AddressWitnessVerificationOperations::for_p2pkh(100);
126 let ops2 = AddressWitnessVerificationOperations::for_p2sh_multisig(3, 150);
127
128 ops1.combine(&ops2);
129
130 assert_eq!(ops1.ecdsa_signature_verifications, 4); assert_eq!(ops1.pubkey_hash_verifications, 1); assert_eq!(ops1.script_hash_verifications, 1); assert_eq!(ops1.signable_bytes_len, 150); }
135}