@@ -131,20 +131,25 @@ impl CardanoTransactionsSigningConfig {
131131 ///
132132 /// The formula is as follows:
133133 ///
134- /// `block_number = ⌊(tip.block_number - security_parameter) / step⌋ × step`
134+ /// `block_number = ⌊(tip.block_number - security_parameter) / step⌋ × step - 1 `
135135 ///
136136 /// where `⌊x⌋` is the floor function which rounds to the greatest integer less than or equal to `x`.
137137 ///
138- /// *Note: The step is adjusted to be a multiple of the block range length in order
138+ /// *Notes:*
139+ /// * *The step is adjusted to be a multiple of the block range length in order
139140 /// to guarantee that the block number signed in a certificate is effectively signed.*
141+ /// * *1 is subtracted to the result because block range end is exclusive (ie: a BlockRange over
142+ /// `30..45` finish at 44 included, 45 is included in the next block range).*
140143 pub fn compute_block_number_to_be_signed ( & self , block_number : BlockNumber ) -> BlockNumber {
141144 // TODO: See if we can remove this adjustment by including a "partial" block range in
142145 // the signed data.
143146 let adjusted_step = BlockRange :: from_block_number ( self . step ) . start ;
144147 // We can't have a step lower than the block range length.
145148 let adjusted_step = std:: cmp:: max ( adjusted_step, BlockRange :: LENGTH ) ;
146149
147- ( block_number. saturating_sub ( self . security_parameter ) ) / adjusted_step * adjusted_step
150+ let block_number_to_be_signed =
151+ ( block_number. saturating_sub ( self . security_parameter ) ) / adjusted_step * adjusted_step;
152+ block_number_to_be_signed. saturating_sub ( 1 )
148153 }
149154}
150155
@@ -199,11 +204,11 @@ mod tests {
199204 )
200205 ) ;
201206
202- // The block number to be signed is 0 because the step is 15, the block number is 20, and
207+ // The block number to be signed is 14 because the step is 15, the block number is 20, and
203208 // the security parameter is 0.
204209 // This is further tested in the "computing_block_number_to_be_signed" tests below.
205210 assert_eq ! (
206- SignedEntityType :: CardanoTransactions ( Epoch ( 1 ) , 15 ) ,
211+ SignedEntityType :: CardanoTransactions ( Epoch ( 1 ) , 14 ) ,
207212 config. time_point_to_signed_entity(
208213 SignedEntityTypeDiscriminants :: CardanoTransactions ,
209214 & time_point
@@ -220,7 +225,7 @@ mod tests {
220225 step: 15 ,
221226 }
222227 . compute_block_number_to_be_signed( 105 ) ,
223- 105
228+ 104
224229 ) ;
225230
226231 assert_eq ! (
@@ -229,7 +234,7 @@ mod tests {
229234 step: 15 ,
230235 }
231236 . compute_block_number_to_be_signed( 100 ) ,
232- 90
237+ 89
233238 ) ;
234239
235240 assert_eq ! (
@@ -238,7 +243,7 @@ mod tests {
238243 step: 15 ,
239244 }
240245 . compute_block_number_to_be_signed( 100 ) ,
241- 15
246+ 14
242247 ) ;
243248
244249 assert_eq ! (
@@ -271,7 +276,7 @@ mod tests {
271276 step: BlockRange :: LENGTH * 2 - 1 ,
272277 }
273278 . compute_block_number_to_be_signed( BlockRange :: LENGTH * 5 + 1 ) ,
274- BlockRange :: LENGTH * 5
279+ BlockRange :: LENGTH * 5 - 1
275280 ) ;
276281
277282 assert_eq ! (
@@ -280,7 +285,7 @@ mod tests {
280285 step: BlockRange :: LENGTH * 2 + 1 ,
281286 }
282287 . compute_block_number_to_be_signed( BlockRange :: LENGTH * 5 + 1 ) ,
283- BlockRange :: LENGTH * 4
288+ BlockRange :: LENGTH * 4 - 1
284289 ) ;
285290
286291 // Adjusted step is always at least BLOCK_RANGE_LENGTH.
@@ -290,7 +295,7 @@ mod tests {
290295 step: BlockRange :: LENGTH - 1 ,
291296 }
292297 . compute_block_number_to_be_signed( BlockRange :: LENGTH * 10 - 1 ) ,
293- BlockRange :: LENGTH * 9
298+ BlockRange :: LENGTH * 9 - 1
294299 ) ;
295300
296301 assert_eq ! (
@@ -424,7 +429,7 @@ mod tests {
424429 SignedEntityType :: MithrilStakeDistribution ( beacon. epoch) ,
425430 SignedEntityType :: CardanoStakeDistribution ( beacon. epoch) ,
426431 SignedEntityType :: CardanoImmutableFilesFull ( beacon. clone( ) ) ,
427- SignedEntityType :: CardanoTransactions ( beacon. epoch, chain_point. block_number) ,
432+ SignedEntityType :: CardanoTransactions ( beacon. epoch, chain_point. block_number - 1 ) ,
428433 ] ,
429434 signed_entity_types
430435 ) ;
0 commit comments