@@ -18,7 +18,7 @@ use crate::sign::ecdsa::EcdsaChannelSigner;
1818#[ allow( unused_imports) ]
1919use crate :: prelude:: * ;
2020
21- use core:: { cmp, fmt } ;
21+ use core:: cmp;
2222use crate :: sync:: { Mutex , Arc } ;
2323#[ cfg( test) ] use crate :: sync:: MutexGuard ;
2424
@@ -71,9 +71,6 @@ pub struct TestChannelSigner {
7171 /// Channel state used for policy enforcement
7272 pub state : Arc < Mutex < EnforcementState > > ,
7373 pub disable_revocation_policy_check : bool ,
74- /// Set of signer operations that are disabled. If an operation is disabled,
75- /// the signer will return `Err` when the corresponding method is called.
76- pub disabled_signer_ops : Arc < Mutex < HashSet < SignerOp > > > ,
7774}
7875
7976#[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash ) ]
@@ -93,23 +90,23 @@ pub enum SignerOp {
9390 SignChannelAnnouncementWithFundingKey ,
9491}
9592
96- impl fmt :: Display for SignerOp {
97- fn fmt ( & self , f : & mut fmt :: Formatter ) -> fmt :: Result {
98- match self {
99- SignerOp :: GetPerCommitmentPoint => write ! ( f , "get_per_commitment_point" ) ,
100- SignerOp :: ReleaseCommitmentSecret => write ! ( f , "release_commitment_secret" ) ,
101- SignerOp :: ValidateHolderCommitment => write ! ( f , "validate_holder_commitment" ) ,
102- SignerOp :: SignCounterpartyCommitment => write ! ( f , "sign_counterparty_commitment" ) ,
103- SignerOp :: ValidateCounterpartyRevocation => write ! ( f , "validate_counterparty_revocation" ) ,
104- SignerOp :: SignHolderCommitment => write ! ( f , "sign_holder_commitment" ) ,
105- SignerOp :: SignJusticeRevokedOutput => write ! ( f , "sign_justice_revoked_output" ) ,
106- SignerOp :: SignJusticeRevokedHtlc => write ! ( f , "sign_justice_revoked_htlc" ) ,
107- SignerOp :: SignHolderHtlcTransaction => write ! ( f , "sign_holder_htlc_transaction" ) ,
108- SignerOp :: SignCounterpartyHtlcTransaction => write ! ( f , "sign_counterparty_htlc_transaction" ) ,
109- SignerOp :: SignClosingTransaction => write ! ( f , "sign_closing_transaction" ) ,
110- SignerOp :: SignHolderAnchorInput => write ! ( f , "sign_holder_anchor_input" ) ,
111- SignerOp :: SignChannelAnnouncementWithFundingKey => write ! ( f , "sign_channel_announcement_with_funding_key" ) ,
112- }
93+ impl SignerOp {
94+ pub fn all ( ) -> Vec < Self > {
95+ vec ! [
96+ SignerOp :: GetPerCommitmentPoint ,
97+ SignerOp :: ReleaseCommitmentSecret ,
98+ SignerOp :: ValidateHolderCommitment ,
99+ SignerOp :: SignCounterpartyCommitment ,
100+ SignerOp :: ValidateCounterpartyRevocation ,
101+ SignerOp :: SignHolderCommitment ,
102+ SignerOp :: SignJusticeRevokedOutput ,
103+ SignerOp :: SignJusticeRevokedHtlc ,
104+ SignerOp :: SignHolderHtlcTransaction ,
105+ SignerOp :: SignCounterpartyHtlcTransaction ,
106+ SignerOp :: SignClosingTransaction ,
107+ SignerOp :: SignHolderAnchorInput ,
108+ SignerOp :: SignChannelAnnouncementWithFundingKey ,
109+ ]
113110 }
114111}
115112
@@ -127,7 +124,6 @@ impl TestChannelSigner {
127124 inner,
128125 state,
129126 disable_revocation_policy_check : false ,
130- disabled_signer_ops : Arc :: new ( Mutex :: new ( new_hash_set ( ) ) ) ,
131127 }
132128 }
133129
@@ -141,7 +137,6 @@ impl TestChannelSigner {
141137 inner,
142138 state,
143139 disable_revocation_policy_check,
144- disabled_signer_ops : Arc :: new ( Mutex :: new ( new_hash_set ( ) ) ) ,
145140 }
146141 }
147142
@@ -152,16 +147,19 @@ impl TestChannelSigner {
152147 self . state . lock ( ) . unwrap ( )
153148 }
154149
155- pub fn enable_op ( & mut self , signer_op : SignerOp ) {
156- self . disabled_signer_ops . lock ( ) . unwrap ( ) . remove ( & signer_op) ;
150+ #[ cfg( test) ]
151+ pub fn enable_op ( & self , signer_op : SignerOp ) {
152+ self . get_enforcement_state ( ) . disabled_signer_ops . remove ( & signer_op) ;
157153 }
158154
159- pub fn disable_op ( & mut self , signer_op : SignerOp ) {
160- self . disabled_signer_ops . lock ( ) . unwrap ( ) . insert ( signer_op) ;
155+ #[ cfg( test) ]
156+ pub fn disable_op ( & self , signer_op : SignerOp ) {
157+ self . get_enforcement_state ( ) . disabled_signer_ops . insert ( signer_op) ;
161158 }
162159
160+ #[ cfg( test) ]
163161 fn is_signer_available ( & self , signer_op : SignerOp ) -> bool {
164- !self . disabled_signer_ops . lock ( ) . unwrap ( ) . contains ( & signer_op)
162+ !self . get_enforcement_state ( ) . disabled_signer_ops . contains ( & signer_op)
165163 }
166164}
167165
@@ -189,6 +187,7 @@ impl ChannelSigner for TestChannelSigner {
189187 }
190188
191189 fn validate_counterparty_revocation ( & self , idx : u64 , _secret : & SecretKey ) -> Result < ( ) , ( ) > {
190+ #[ cfg( test) ]
192191 if !self . is_signer_available ( SignerOp :: ValidateCounterpartyRevocation ) {
193192 return Err ( ( ) ) ;
194193 }
@@ -212,6 +211,7 @@ impl EcdsaChannelSigner for TestChannelSigner {
212211 self . verify_counterparty_commitment_tx ( commitment_tx, secp_ctx) ;
213212
214213 {
214+ #[ cfg( test) ]
215215 if !self . is_signer_available ( SignerOp :: SignCounterpartyCommitment ) {
216216 return Err ( ( ) ) ;
217217 }
@@ -231,6 +231,7 @@ impl EcdsaChannelSigner for TestChannelSigner {
231231 }
232232
233233 fn sign_holder_commitment ( & self , commitment_tx : & HolderCommitmentTransaction , secp_ctx : & Secp256k1 < secp256k1:: All > ) -> Result < Signature , ( ) > {
234+ #[ cfg( test) ]
234235 if !self . is_signer_available ( SignerOp :: SignHolderCommitment ) {
235236 return Err ( ( ) ) ;
236237 }
@@ -252,13 +253,15 @@ impl EcdsaChannelSigner for TestChannelSigner {
252253 }
253254
254255 fn sign_justice_revoked_output ( & self , justice_tx : & Transaction , input : usize , amount : u64 , per_commitment_key : & SecretKey , secp_ctx : & Secp256k1 < secp256k1:: All > ) -> Result < Signature , ( ) > {
256+ #[ cfg( test) ]
255257 if !self . is_signer_available ( SignerOp :: SignJusticeRevokedOutput ) {
256258 return Err ( ( ) ) ;
257259 }
258260 Ok ( EcdsaChannelSigner :: sign_justice_revoked_output ( & self . inner , justice_tx, input, amount, per_commitment_key, secp_ctx) . unwrap ( ) )
259261 }
260262
261263 fn sign_justice_revoked_htlc ( & self , justice_tx : & Transaction , input : usize , amount : u64 , per_commitment_key : & SecretKey , htlc : & HTLCOutputInCommitment , secp_ctx : & Secp256k1 < secp256k1:: All > ) -> Result < Signature , ( ) > {
264+ #[ cfg( test) ]
262265 if !self . is_signer_available ( SignerOp :: SignJusticeRevokedHtlc ) {
263266 return Err ( ( ) ) ;
264267 }
@@ -269,6 +272,7 @@ impl EcdsaChannelSigner for TestChannelSigner {
269272 & self , htlc_tx : & Transaction , input : usize , htlc_descriptor : & HTLCDescriptor ,
270273 secp_ctx : & Secp256k1 < secp256k1:: All >
271274 ) -> Result < Signature , ( ) > {
275+ #[ cfg( test) ]
272276 if !self . is_signer_available ( SignerOp :: SignHolderHtlcTransaction ) {
273277 return Err ( ( ) ) ;
274278 }
@@ -305,6 +309,7 @@ impl EcdsaChannelSigner for TestChannelSigner {
305309 }
306310
307311 fn sign_counterparty_htlc_transaction ( & self , htlc_tx : & Transaction , input : usize , amount : u64 , per_commitment_point : & PublicKey , htlc : & HTLCOutputInCommitment , secp_ctx : & Secp256k1 < secp256k1:: All > ) -> Result < Signature , ( ) > {
312+ #[ cfg( test) ]
308313 if !self . is_signer_available ( SignerOp :: SignCounterpartyHtlcTransaction ) {
309314 return Err ( ( ) ) ;
310315 }
@@ -324,6 +329,7 @@ impl EcdsaChannelSigner for TestChannelSigner {
324329 // As long as our minimum dust limit is enforced and is greater than our anchor output
325330 // value, an anchor output can only have an index within [0, 1].
326331 assert ! ( anchor_tx. input[ input] . previous_output. vout == 0 || anchor_tx. input[ input] . previous_output. vout == 1 ) ;
332+ #[ cfg( test) ]
327333 if !self . is_signer_available ( SignerOp :: SignHolderAnchorInput ) {
328334 return Err ( ( ) ) ;
329335 }
@@ -417,6 +423,9 @@ pub struct EnforcementState {
417423 pub last_holder_revoked_commitment : u64 ,
418424 /// The last validated holder commitment number, backwards counting
419425 pub last_holder_commitment : u64 ,
426+ /// Set of signer operations that are disabled. If an operation is disabled,
427+ /// the signer will return `Err` when the corresponding method is called.
428+ pub disabled_signer_ops : HashSet < SignerOp > ,
420429}
421430
422431impl EnforcementState {
@@ -427,6 +436,7 @@ impl EnforcementState {
427436 last_counterparty_revoked_commitment : INITIAL_REVOKED_COMMITMENT_NUMBER ,
428437 last_holder_revoked_commitment : INITIAL_REVOKED_COMMITMENT_NUMBER ,
429438 last_holder_commitment : INITIAL_REVOKED_COMMITMENT_NUMBER ,
439+ disabled_signer_ops : new_hash_set ( ) ,
430440 }
431441 }
432442}
0 commit comments