Skip to content

Commit 45ef59f

Browse files
committed
refactor: add comments for persist_epoch and should_prune in historical_epochs_state
1 parent 3fd30a2 commit 45ef59f

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

modules/historical_epochs_state/src/historical_epochs_state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ impl HistoricalEpochsState {
143143
state.immutable.clone()
144144
};
145145

146-
if let Err(e) = persist_tx.send((current_block.epoch - 1, immutable)).await {
146+
if let Err(e) = persist_tx.send((current_block.epoch, immutable)).await {
147147
error!("persistence worker crashed: {e}");
148148
}
149149
}

modules/historical_epochs_state/src/immutable_historical_epochs_state.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,13 @@ impl ImmutableHistoricalEpochsState {
4949
pending.push_back(ea);
5050
}
5151

52-
/// Persists pending EpochActivityMessages for epoch N
52+
/// Persists pending EpochActivityMessages for Epoch N - 1
53+
/// at the first block of Epoch N
5354
/// There should be only one EpochActivityMessage for each epoch
5455
/// Returns the number of persisted EpochActivityMessages
5556
/// Errors if the batch commit or persist fails
5657
pub async fn persist_epoch(&self, epoch: u64) -> Result<u32> {
58+
let saving_epoch = epoch - 1;
5759
let drained_epochs = {
5860
let mut pending = self.pending.lock().await;
5961
std::mem::take(&mut *pending)
@@ -69,16 +71,16 @@ impl ImmutableHistoricalEpochsState {
6971
}
7072

7173
if let Err(e) = batch.commit() {
72-
error!("batch commit failed for epoch {epoch}: {e}");
74+
error!("batch commit failed for epoch {saving_epoch}: {e}");
7375
return Err(e.into());
7476
}
7577

7678
if let Err(e) = self.keyspace.persist(PersistMode::Buffer) {
77-
error!("persist failed for epoch {epoch}: {e}");
79+
error!("persist failed for epoch {saving_epoch}: {e}");
7880
return Err(e.into());
7981
}
8082

81-
info!("persisted {persisted_epochs} epochs for epoch {epoch}");
83+
info!("persisted {persisted_epochs} epochs for epoch {saving_epoch}");
8284
Ok(persisted_epochs)
8385
}
8486

modules/historical_epochs_state/src/state.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ impl State {
4040
}
4141
}
4242

43+
/// block_info is the first block of Epoch N (epoch param)
44+
/// And at that point, we are saving EpochActivityMessage for Epoch N - 1
45+
/// That is why we check Some(block_info.epoch - 1) != self.volatile.last_persisted_epoch
4346
pub fn ready_to_prune(&self, block_info: &BlockInfo) -> bool {
4447
block_info.epoch > 0
4548
&& Some(block_info.epoch - 1) != self.volatile.last_persisted_epoch
@@ -179,7 +182,7 @@ mod tests {
179182
assert!(state.ready_to_prune(&block_info));
180183

181184
state.prune_volatile().await;
182-
state.immutable.persist_epoch(0).await.unwrap();
185+
state.immutable.persist_epoch(block_info.epoch).await.unwrap();
183186

184187
let block_info = make_block_info(2, true);
185188
let ea_1 = make_ea(1);
@@ -192,7 +195,7 @@ mod tests {
192195
assert!(state.ready_to_prune(&block_info));
193196

194197
state.prune_volatile().await;
195-
state.immutable.persist_epoch(1).await.unwrap();
198+
state.immutable.persist_epoch(block_info.epoch).await.unwrap();
196199

197200
let historical_epoch = state.immutable.get_historical_epoch(0).unwrap().unwrap();
198201
assert_eq!(historical_epoch, ea_0);

0 commit comments

Comments
 (0)