@@ -46,34 +46,31 @@ pub const INITIAL_REVOKED_COMMITMENT_NUMBER: u64 = 1 << 48;
4646#[ derive( Clone ) ]
4747pub struct EnforcingSigner {
4848 pub inner : InMemorySigner ,
49- /// The last counterparty commitment number we signed, backwards counting
50- pub last_commitment_number : Arc < Mutex < Option < u64 > > > ,
51- /// The last holder commitment number we revoked, backwards counting
52- pub revoked_commitment : Arc < Mutex < u64 > > ,
49+ /// Channel state used for policy enforcement
50+ pub state : Arc < Mutex < EnforcementState > > ,
5351 pub disable_revocation_policy_check : bool ,
5452}
5553
5654impl EnforcingSigner {
5755 /// Construct an EnforcingSigner
5856 pub fn new ( inner : InMemorySigner ) -> Self {
57+ let state = Arc :: new ( Mutex :: new ( EnforcementState :: new ( ) ) ) ;
5958 Self {
6059 inner,
61- last_commitment_number : Arc :: new ( Mutex :: new ( None ) ) ,
62- revoked_commitment : Arc :: new ( Mutex :: new ( INITIAL_REVOKED_COMMITMENT_NUMBER ) ) ,
60+ state,
6361 disable_revocation_policy_check : false
6462 }
6563 }
6664
6765 /// Construct an EnforcingSigner with externally managed storage
6866 ///
6967 /// Since there are multiple copies of this struct for each channel, some coordination is needed
70- /// so that all copies are aware of revocations . A pointer to this state is provided here, usually
68+ /// so that all copies are aware of enforcement state . A pointer to this state is provided here, usually
7169 /// by an implementation of KeysInterface.
72- pub fn new_with_revoked ( inner : InMemorySigner , revoked_commitment : Arc < Mutex < u64 > > , disable_revocation_policy_check : bool ) -> Self {
70+ pub fn new_with_revoked ( inner : InMemorySigner , state : Arc < Mutex < EnforcementState > > , disable_revocation_policy_check : bool ) -> Self {
7371 Self {
7472 inner,
75- last_commitment_number : Arc :: new ( Mutex :: new ( None ) ) ,
76- revoked_commitment,
73+ state,
7774 disable_revocation_policy_check
7875 }
7976 }
@@ -86,9 +83,9 @@ impl BaseSign for EnforcingSigner {
8683
8784 fn release_commitment_secret ( & self , idx : u64 ) -> [ u8 ; 32 ] {
8885 {
89- let mut revoked = self . revoked_commitment . lock ( ) . unwrap ( ) ;
90- assert ! ( idx == * revoked || idx == * revoked - 1 , "can only revoke the current or next unrevoked commitment - trying {}, revoked {}" , idx, * revoked ) ;
91- * revoked = idx;
86+ let mut state = self . state . lock ( ) . unwrap ( ) ;
87+ assert ! ( idx == state . revoked_commitment || idx == state . revoked_commitment - 1 , "can only revoke the current or next unrevoked commitment - trying {}, revoked {}" , idx, state . revoked_commitment ) ;
88+ state . revoked_commitment = idx;
9289 }
9390 self . inner . release_commitment_secret ( idx)
9491 }
@@ -100,13 +97,13 @@ impl BaseSign for EnforcingSigner {
10097 self . verify_counterparty_commitment_tx ( commitment_tx, secp_ctx) ;
10198
10299 {
103- let mut last_commitment_number_guard = self . last_commitment_number . lock ( ) . unwrap ( ) ;
100+ let mut state = self . state . lock ( ) . unwrap ( ) ;
104101 let actual_commitment_number = commitment_tx. commitment_number ( ) ;
105- let last_commitment_number = last_commitment_number_guard . unwrap_or ( actual_commitment_number) ;
102+ let last_commitment_number = state . last_commitment_number . unwrap_or ( actual_commitment_number) ;
106103 // These commitment numbers are backwards counting. We expect either the same as the previously encountered,
107104 // or the next one.
108105 assert ! ( last_commitment_number == actual_commitment_number || last_commitment_number - 1 == actual_commitment_number, "{} doesn't come after {}" , actual_commitment_number, last_commitment_number) ;
109- * last_commitment_number_guard = Some ( cmp:: min ( last_commitment_number, actual_commitment_number) )
106+ state . last_commitment_number = Some ( cmp:: min ( last_commitment_number, actual_commitment_number) )
110107 }
111108
112109 Ok ( self . inner . sign_counterparty_commitment ( commitment_tx, secp_ctx) . unwrap ( ) )
@@ -117,12 +114,12 @@ impl BaseSign for EnforcingSigner {
117114 let commitment_txid = trusted_tx. txid ( ) ;
118115 let holder_csv = self . inner . counterparty_selected_contest_delay ( ) ;
119116
120- let revoked = self . revoked_commitment . lock ( ) . unwrap ( ) ;
117+ let state = self . state . lock ( ) . unwrap ( ) ;
121118 let commitment_number = trusted_tx. commitment_number ( ) ;
122- if * revoked - 1 != commitment_number && * revoked - 2 != commitment_number {
119+ if state . revoked_commitment - 1 != commitment_number && state . revoked_commitment - 2 != commitment_number {
123120 if !self . disable_revocation_policy_check {
124121 panic ! ( "can only sign the next two unrevoked commitment numbers, revoked={} vs requested={} for {}" ,
125- * revoked , commitment_number, self . inner. commitment_seed[ 0 ] )
122+ state . revoked_commitment , commitment_number, self . inner. commitment_seed[ 0 ] )
126123 }
127124 }
128125
@@ -175,20 +172,18 @@ impl Sign for EnforcingSigner {}
175172impl Writeable for EnforcingSigner {
176173 fn write < W : Writer > ( & self , writer : & mut W ) -> Result < ( ) , Error > {
177174 self . inner . write ( writer) ?;
178- let last = * self . last_commitment_number . lock ( ) . unwrap ( ) ;
179- last. write ( writer) ?;
175+ // NOTE - the commitment state is maintained by KeysInterface, so we don't persist it
180176 Ok ( ( ) )
181177 }
182178}
183179
184180impl Readable for EnforcingSigner {
185181 fn read < R : io:: Read > ( reader : & mut R ) -> Result < Self , DecodeError > {
186182 let inner = Readable :: read ( reader) ?;
187- let last_commitment_number = Readable :: read ( reader ) ? ;
183+ let state = Arc :: new ( Mutex :: new ( EnforcementState :: new ( ) ) ) ;
188184 Ok ( EnforcingSigner {
189185 inner,
190- last_commitment_number : Arc :: new ( Mutex :: new ( last_commitment_number) ) ,
191- revoked_commitment : Arc :: new ( Mutex :: new ( INITIAL_REVOKED_COMMITMENT_NUMBER ) ) ,
186+ state,
192187 disable_revocation_policy_check : false ,
193188 } )
194189 }
@@ -207,3 +202,26 @@ impl EnforcingSigner {
207202 . expect ( "derived different per-tx keys or built transaction" )
208203 }
209204}
205+
206+ /// The state used by [`EnforcingSigner`] in order to enforce policy checks
207+ ///
208+ /// This structure is maintained by KeysInterface since we may have multiple copies of
209+ /// the signer and they must coordinate their state.
210+ #[ derive( Clone ) ]
211+ pub struct EnforcementState {
212+ /// The last counterparty commitment number we signed, backwards counting
213+ pub last_commitment_number : Option < u64 > ,
214+ /// The last holder commitment number we revoked, backwards counting
215+ pub revoked_commitment : u64 ,
216+
217+ }
218+
219+ impl EnforcementState {
220+ /// Enforcement state for a new channel
221+ pub fn new ( ) -> Self {
222+ EnforcementState {
223+ last_commitment_number : None ,
224+ revoked_commitment : INITIAL_REVOKED_COMMITMENT_NUMBER
225+ }
226+ }
227+ }
0 commit comments