Skip to content

Commit b629b31

Browse files
committed
Adjust computation of block number to sign
Substacting `1` to the result to account for the fact that a block range end is exclusive.
1 parent e2c42cf commit b629b31

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

mithril-aggregator/tests/create_certificate.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ async fn create_certificate() {
142142

143143
comment!(
144144
"Increase cardano chain block number to 185,
145-
the state machine should be signing CardanoTransactions for block 180"
145+
the state machine should be signing CardanoTransactions for block 179"
146146
);
147147
tester.increase_block_number(85, 185).await.unwrap();
148148
cycle!(tester, "signing");
@@ -166,7 +166,7 @@ async fn create_certificate() {
166166
.map(|s| s.signer_with_stake.clone().into())
167167
.collect::<Vec<_>>(),
168168
fixture.compute_and_encode_avk(),
169-
SignedEntityType::CardanoTransactions(Epoch(1), 180),
169+
SignedEntityType::CardanoTransactions(Epoch(1), 179),
170170
ExpectedCertificate::genesis_identifier(&CardanoDbBeacon::new(
171171
"devnet".to_string(),
172172
1,
@@ -201,7 +201,7 @@ async fn create_certificate() {
201201
.map(|s| s.signer_with_stake.clone().into())
202202
.collect::<Vec<_>>(),
203203
fixture.compute_and_encode_avk(),
204-
SignedEntityType::CardanoTransactions(Epoch(1), 120),
204+
SignedEntityType::CardanoTransactions(Epoch(1), 119),
205205
ExpectedCertificate::genesis_identifier(&CardanoDbBeacon::new(
206206
"devnet".to_string(),
207207
1,

mithril-common/src/entities/signed_entity_config.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)