@@ -18,7 +18,7 @@ use crate::sign::ecdsa::EcdsaChannelSigner;
18
18
#[ allow( unused_imports) ]
19
19
use crate :: prelude:: * ;
20
20
21
- use core:: { cmp, fmt } ;
21
+ use core:: cmp;
22
22
use crate :: sync:: { Mutex , Arc } ;
23
23
#[ cfg( test) ] use crate :: sync:: MutexGuard ;
24
24
@@ -71,9 +71,6 @@ pub struct TestChannelSigner {
71
71
/// Channel state used for policy enforcement
72
72
pub state : Arc < Mutex < EnforcementState > > ,
73
73
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 > > > ,
77
74
}
78
75
79
76
#[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash ) ]
@@ -93,23 +90,23 @@ pub enum SignerOp {
93
90
SignChannelAnnouncementWithFundingKey ,
94
91
}
95
92
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
+ ]
113
110
}
114
111
}
115
112
@@ -127,7 +124,6 @@ impl TestChannelSigner {
127
124
inner,
128
125
state,
129
126
disable_revocation_policy_check : false ,
130
- disabled_signer_ops : Arc :: new ( Mutex :: new ( new_hash_set ( ) ) ) ,
131
127
}
132
128
}
133
129
@@ -141,7 +137,6 @@ impl TestChannelSigner {
141
137
inner,
142
138
state,
143
139
disable_revocation_policy_check,
144
- disabled_signer_ops : Arc :: new ( Mutex :: new ( new_hash_set ( ) ) ) ,
145
140
}
146
141
}
147
142
@@ -152,16 +147,19 @@ impl TestChannelSigner {
152
147
self . state . lock ( ) . unwrap ( )
153
148
}
154
149
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) ;
157
153
}
158
154
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) ;
161
158
}
162
159
160
+ #[ cfg( test) ]
163
161
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)
165
163
}
166
164
}
167
165
@@ -189,6 +187,7 @@ impl ChannelSigner for TestChannelSigner {
189
187
}
190
188
191
189
fn validate_counterparty_revocation ( & self , idx : u64 , _secret : & SecretKey ) -> Result < ( ) , ( ) > {
190
+ #[ cfg( test) ]
192
191
if !self . is_signer_available ( SignerOp :: ValidateCounterpartyRevocation ) {
193
192
return Err ( ( ) ) ;
194
193
}
@@ -212,6 +211,7 @@ impl EcdsaChannelSigner for TestChannelSigner {
212
211
self . verify_counterparty_commitment_tx ( commitment_tx, secp_ctx) ;
213
212
214
213
{
214
+ #[ cfg( test) ]
215
215
if !self . is_signer_available ( SignerOp :: SignCounterpartyCommitment ) {
216
216
return Err ( ( ) ) ;
217
217
}
@@ -231,6 +231,7 @@ impl EcdsaChannelSigner for TestChannelSigner {
231
231
}
232
232
233
233
fn sign_holder_commitment ( & self , commitment_tx : & HolderCommitmentTransaction , secp_ctx : & Secp256k1 < secp256k1:: All > ) -> Result < Signature , ( ) > {
234
+ #[ cfg( test) ]
234
235
if !self . is_signer_available ( SignerOp :: SignHolderCommitment ) {
235
236
return Err ( ( ) ) ;
236
237
}
@@ -252,13 +253,15 @@ impl EcdsaChannelSigner for TestChannelSigner {
252
253
}
253
254
254
255
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) ]
255
257
if !self . is_signer_available ( SignerOp :: SignJusticeRevokedOutput ) {
256
258
return Err ( ( ) ) ;
257
259
}
258
260
Ok ( EcdsaChannelSigner :: sign_justice_revoked_output ( & self . inner , justice_tx, input, amount, per_commitment_key, secp_ctx) . unwrap ( ) )
259
261
}
260
262
261
263
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) ]
262
265
if !self . is_signer_available ( SignerOp :: SignJusticeRevokedHtlc ) {
263
266
return Err ( ( ) ) ;
264
267
}
@@ -269,6 +272,7 @@ impl EcdsaChannelSigner for TestChannelSigner {
269
272
& self , htlc_tx : & Transaction , input : usize , htlc_descriptor : & HTLCDescriptor ,
270
273
secp_ctx : & Secp256k1 < secp256k1:: All >
271
274
) -> Result < Signature , ( ) > {
275
+ #[ cfg( test) ]
272
276
if !self . is_signer_available ( SignerOp :: SignHolderHtlcTransaction ) {
273
277
return Err ( ( ) ) ;
274
278
}
@@ -305,6 +309,7 @@ impl EcdsaChannelSigner for TestChannelSigner {
305
309
}
306
310
307
311
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) ]
308
313
if !self . is_signer_available ( SignerOp :: SignCounterpartyHtlcTransaction ) {
309
314
return Err ( ( ) ) ;
310
315
}
@@ -324,6 +329,7 @@ impl EcdsaChannelSigner for TestChannelSigner {
324
329
// As long as our minimum dust limit is enforced and is greater than our anchor output
325
330
// value, an anchor output can only have an index within [0, 1].
326
331
assert ! ( anchor_tx. input[ input] . previous_output. vout == 0 || anchor_tx. input[ input] . previous_output. vout == 1 ) ;
332
+ #[ cfg( test) ]
327
333
if !self . is_signer_available ( SignerOp :: SignHolderAnchorInput ) {
328
334
return Err ( ( ) ) ;
329
335
}
@@ -417,6 +423,9 @@ pub struct EnforcementState {
417
423
pub last_holder_revoked_commitment : u64 ,
418
424
/// The last validated holder commitment number, backwards counting
419
425
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 > ,
420
429
}
421
430
422
431
impl EnforcementState {
@@ -427,6 +436,7 @@ impl EnforcementState {
427
436
last_counterparty_revoked_commitment : INITIAL_REVOKED_COMMITMENT_NUMBER ,
428
437
last_holder_revoked_commitment : INITIAL_REVOKED_COMMITMENT_NUMBER ,
429
438
last_holder_commitment : INITIAL_REVOKED_COMMITMENT_NUMBER ,
439
+ disabled_signer_ops : new_hash_set ( ) ,
430
440
}
431
441
}
432
442
}
0 commit comments