Skip to content

Commit 8e02cf0

Browse files
jcnelsonobycode
authored andcommitted
chore: check both clarity tx and clarity db epoch versions at each epoch transition
1 parent d99d566 commit 8e02cf0

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

stackslib/src/chainstate/nakamoto/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3937,7 +3937,11 @@ impl NakamotoChainState {
39373937

39383938
// is this stacks block the first of a new epoch?
39393939
let (applied_epoch_transition, mut tx_receipts) =
3940-
StacksChainState::process_epoch_transition(&mut clarity_tx, burn_header_height)?;
3940+
StacksChainState::process_epoch_transition(
3941+
&mut clarity_tx,
3942+
sortition_dbconn.as_burn_state_db(),
3943+
burn_header_height,
3944+
)?;
39413945

39423946
debug!(
39433947
"Setup block: Processed epoch transition";

stackslib/src/chainstate/stacks/db/blocks.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4044,6 +4044,7 @@ impl StacksChainState {
40444044
/// Return (applied?, receipts)
40454045
pub fn process_epoch_transition(
40464046
clarity_tx: &mut ClarityTx,
4047+
burn_dbconn: &dyn BurnStateDB,
40474048
chain_tip_burn_header_height: u32,
40484049
) -> Result<(bool, Vec<StacksTransactionReceipt>), Error> {
40494050
// is this stacks block the first of a new epoch?
@@ -4111,6 +4112,22 @@ impl StacksChainState {
41114112
panic!("No defined transition from Epoch31 forward")
41124113
}
41134114
}
4115+
4116+
if current_epoch > StacksEpochId::Epoch2_05 {
4117+
// clarity tx should now have the current epoch
4118+
assert_eq!(
4119+
clarity_tx.block.get_epoch(),
4120+
current_epoch,
4121+
"FATAL: clarity_tx does not have the current epoch"
4122+
);
4123+
4124+
// clarity DB should now have the current epoch
4125+
assert_eq!(
4126+
clarity_tx.block.get_clarity_db_epoch_version(burn_dbconn)?,
4127+
current_epoch,
4128+
"FATAL: clarity DB does not report the current epoch"
4129+
);
4130+
}
41144131
}
41154132
}
41164133

@@ -5197,7 +5214,11 @@ impl StacksChainState {
51975214

51985215
// is this stacks block the first of a new epoch?
51995216
let (applied_epoch_transition, mut tx_receipts) =
5200-
StacksChainState::process_epoch_transition(&mut clarity_tx, burn_tip_height)?;
5217+
StacksChainState::process_epoch_transition(
5218+
&mut clarity_tx,
5219+
burn_dbconn,
5220+
burn_tip_height,
5221+
)?;
52015222

52025223
debug!(
52035224
"Setup block: Processed epoch transition at {}/{}",

stackslib/src/clarity_vm/clarity.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,21 @@ impl<'a, 'b> ClarityBlockConnection<'a, 'b> {
226226
None => None,
227227
}
228228
}
229+
230+
/// Load the epoch ID from the clarity DB.
231+
/// Used to sanity-check epoch transitions.
232+
pub fn get_clarity_db_epoch_version(
233+
&mut self,
234+
burn_state_db: &dyn BurnStateDB,
235+
) -> Result<StacksEpochId, Error> {
236+
let mut db = self.datastore.as_clarity_db(self.header_db, burn_state_db);
237+
// NOTE: the begin/roll_back shouldn't be necessary with how this gets used in practice,
238+
// but is put here defensively.
239+
db.begin();
240+
let result = db.get_clarity_epoch_version();
241+
db.roll_back()?;
242+
Ok(result?)
243+
}
229244
}
230245

231246
impl ClarityInstance {

0 commit comments

Comments
 (0)