@@ -43,37 +43,35 @@ pub const INITIAL_REVOKED_COMMITMENT_NUMBER: u64 = 1 << 48;
4343///
4444/// Note that before we do so we should ensure its serialization format has backwards- and
4545/// forwards-compatibility prefix/suffixes!
46+
4647#[ derive( Clone ) ]
4748pub struct EnforcingSigner {
4849 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 > > ,
50+ /// Channel state used for policy enforcement
51+ pub state : Arc < Mutex < EnforcementState > > ,
5352 pub disable_revocation_policy_check : bool ,
5453}
5554
5655impl EnforcingSigner {
5756 /// Construct an EnforcingSigner
5857 pub fn new ( inner : InMemorySigner ) -> Self {
58+ let state = Arc :: new ( Mutex :: new ( EnforcementState :: new ( ) ) ) ;
5959 Self {
6060 inner,
61- last_commitment_number : Arc :: new ( Mutex :: new ( None ) ) ,
62- revoked_commitment : Arc :: new ( Mutex :: new ( INITIAL_REVOKED_COMMITMENT_NUMBER ) ) ,
61+ state,
6362 disable_revocation_policy_check : false
6463 }
6564 }
6665
6766 /// Construct an EnforcingSigner with externally managed storage
6867 ///
6968 /// 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
69+ /// so that all copies are aware of enforcement state . A pointer to this state is provided here, usually
7170 /// by an implementation of KeysInterface.
72- pub fn new_with_revoked ( inner : InMemorySigner , revoked_commitment : Arc < Mutex < u64 > > , disable_revocation_policy_check : bool ) -> Self {
71+ pub fn new_with_revoked ( inner : InMemorySigner , state : Arc < Mutex < EnforcementState > > , disable_revocation_policy_check : bool ) -> Self {
7372 Self {
7473 inner,
75- last_commitment_number : Arc :: new ( Mutex :: new ( None ) ) ,
76- revoked_commitment,
74+ state,
7775 disable_revocation_policy_check
7876 }
7977 }
@@ -86,9 +84,9 @@ impl BaseSign for EnforcingSigner {
8684
8785 fn release_commitment_secret ( & self , idx : u64 ) -> [ u8 ; 32 ] {
8886 {
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;
87+ let mut state = self . state . lock ( ) . unwrap ( ) ;
88+ 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 ) ;
89+ state . revoked_commitment = idx;
9290 }
9391 self . inner . release_commitment_secret ( idx)
9492 }
@@ -100,13 +98,13 @@ impl BaseSign for EnforcingSigner {
10098 self . verify_counterparty_commitment_tx ( commitment_tx, secp_ctx) ;
10199
102100 {
103- let mut last_commitment_number_guard = self . last_commitment_number . lock ( ) . unwrap ( ) ;
101+ let mut state = self . state . lock ( ) . unwrap ( ) ;
104102 let actual_commitment_number = commitment_tx. commitment_number ( ) ;
105- let last_commitment_number = last_commitment_number_guard . unwrap_or ( actual_commitment_number) ;
103+ let last_commitment_number = state . last_commitment_number . unwrap_or ( actual_commitment_number) ;
106104 // These commitment numbers are backwards counting. We expect either the same as the previously encountered,
107105 // or the next one.
108106 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) )
107+ state . last_commitment_number = Some ( cmp:: min ( last_commitment_number, actual_commitment_number) )
110108 }
111109
112110 Ok ( self . inner . sign_counterparty_commitment ( commitment_tx, secp_ctx) . unwrap ( ) )
@@ -117,12 +115,12 @@ impl BaseSign for EnforcingSigner {
117115 let commitment_txid = trusted_tx. txid ( ) ;
118116 let holder_csv = self . inner . counterparty_selected_contest_delay ( ) ;
119117
120- let revoked = self . revoked_commitment . lock ( ) . unwrap ( ) ;
118+ let state = self . state . lock ( ) . unwrap ( ) ;
121119 let commitment_number = trusted_tx. commitment_number ( ) ;
122- if * revoked - 1 != commitment_number && * revoked - 2 != commitment_number {
120+ if state . revoked_commitment - 1 != commitment_number && state . revoked_commitment - 2 != commitment_number {
123121 if !self . disable_revocation_policy_check {
124122 panic ! ( "can only sign the next two unrevoked commitment numbers, revoked={} vs requested={} for {}" ,
125- * revoked , commitment_number, self . inner. commitment_seed[ 0 ] )
123+ state . revoked_commitment , commitment_number, self . inner. commitment_seed[ 0 ] )
126124 }
127125 }
128126
@@ -175,20 +173,18 @@ impl Sign for EnforcingSigner {}
175173impl Writeable for EnforcingSigner {
176174 fn write < W : Writer > ( & self , writer : & mut W ) -> Result < ( ) , Error > {
177175 self . inner . write ( writer) ?;
178- let last = * self . last_commitment_number . lock ( ) . unwrap ( ) ;
179- last. write ( writer) ?;
176+ // NOTE - the commitment state is maintained by KeysInterface, so we don't persist it
180177 Ok ( ( ) )
181178 }
182179}
183180
184181impl Readable for EnforcingSigner {
185182 fn read < R : io:: Read > ( reader : & mut R ) -> Result < Self , DecodeError > {
186183 let inner = Readable :: read ( reader) ?;
187- let last_commitment_number = Readable :: read ( reader ) ? ;
184+ let state = Arc :: new ( Mutex :: new ( EnforcementState :: new ( ) ) ) ;
188185 Ok ( EnforcingSigner {
189186 inner,
190- last_commitment_number : Arc :: new ( Mutex :: new ( last_commitment_number) ) ,
191- revoked_commitment : Arc :: new ( Mutex :: new ( INITIAL_REVOKED_COMMITMENT_NUMBER ) ) ,
187+ state,
192188 disable_revocation_policy_check : false ,
193189 } )
194190 }
@@ -207,3 +203,26 @@ impl EnforcingSigner {
207203 . expect ( "derived different per-tx keys or built transaction" )
208204 }
209205}
206+
207+ /// The state required by [`EnforcingSigner`] in order to enforce policy checks
208+ ///
209+ /// This structure is maintained by [`crate::chain::keysinteface::KeysInterface`]
210+ /// since we may have multiple copies of the signer and they must coordinate their state.
211+ #[ derive( Clone ) ]
212+ pub struct EnforcementState {
213+ /// The last counterparty commitment number we signed, backwards counting
214+ pub last_commitment_number : Option < u64 > ,
215+ /// The last holder commitment number we revoked, backwards counting
216+ pub revoked_commitment : u64 ,
217+
218+ }
219+
220+ impl EnforcementState {
221+ /// Enforcement state for a new channel
222+ pub fn new ( ) -> Self {
223+ EnforcementState {
224+ last_commitment_number : None ,
225+ revoked_commitment : INITIAL_REVOKED_COMMITMENT_NUMBER
226+ }
227+ }
228+ }
0 commit comments