Skip to content

Commit ef475cb

Browse files
committed
refactor: merge common code in ClarityTestSim
1 parent cbacdf2 commit ef475cb

File tree

1 file changed

+61
-84
lines changed

1 file changed

+61
-84
lines changed

stackslib/src/chainstate/stacks/boot/contract_tests.rs

Lines changed: 61 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -137,56 +137,69 @@ impl ClarityTestSim {
137137
self.tenure_height + 100
138138
}
139139

140-
pub fn execute_next_block_as_conn_with_tenure<F, R>(&mut self, new_tenure: bool, f: F) -> R
141-
where
142-
F: FnOnce(&mut ClarityBlockConnection) -> R,
143-
{
144-
let r = {
145-
let mut store = self.marf.begin(
146-
&StacksBlockId(test_sim_height_to_hash(self.block_height, self.fork)),
147-
&StacksBlockId(test_sim_height_to_hash(self.block_height + 1, self.fork)),
148-
);
140+
/// Common setup logic for executing blocks in tests
141+
/// Returns (store, headers_db, burn_db, current_epoch)
142+
fn setup_block_environment(
143+
&mut self,
144+
new_tenure: bool,
145+
) -> (
146+
WritableMarfStore,
147+
TestSimHeadersDB,
148+
TestSimBurnStateDB,
149+
StacksEpochId,
150+
) {
151+
let mut store = self.marf.begin(
152+
&StacksBlockId(test_sim_height_to_hash(self.block_height, self.fork)),
153+
&StacksBlockId(test_sim_height_to_hash(self.block_height + 1, self.fork)),
154+
);
149155

150-
self.block_height += 1;
151-
if new_tenure {
152-
self.tenure_height += 1;
153-
}
156+
self.block_height += 1;
157+
if new_tenure {
158+
self.tenure_height += 1;
159+
}
154160

155-
let headers_db = TestSimHeadersDB {
156-
height: self.block_height,
157-
};
158-
let burn_db = TestSimBurnStateDB {
159-
epoch_bounds: self.epoch_bounds.clone(),
160-
pox_constants: PoxConstants::test_default(),
161-
height: (self.tenure_height + 100).try_into().unwrap(),
162-
};
161+
let headers_db = TestSimHeadersDB {
162+
height: self.block_height,
163+
};
164+
let burn_db = TestSimBurnStateDB {
165+
epoch_bounds: self.epoch_bounds.clone(),
166+
pox_constants: PoxConstants::test_default(),
167+
height: (self.tenure_height + 100).try_into().unwrap(),
168+
};
163169

164-
let cur_epoch = Self::check_and_bump_epoch(&mut store, &headers_db, &burn_db);
170+
let cur_epoch = Self::check_and_bump_epoch(&mut store, &headers_db, &burn_db);
165171

166-
let mut db = store.as_clarity_db(&headers_db, &burn_db);
167-
if cur_epoch.clarity_uses_tip_burn_block() {
168-
db.begin();
169-
db.set_tenure_height(self.tenure_height as u32)
170-
.expect("FAIL: unable to set tenure height in Clarity database");
171-
db.commit()
172-
.expect("FAIL: unable to commit tenure height in Clarity database");
173-
}
172+
// Setup common block metadata
173+
let mut db = store.as_clarity_db(&headers_db, &burn_db);
174+
if cur_epoch.clarity_uses_tip_burn_block() {
175+
db.begin();
176+
db.set_tenure_height(self.tenure_height as u32)
177+
.expect("FAIL: unable to set tenure height in Clarity database");
178+
db.commit()
179+
.expect("FAIL: unable to commit tenure height in Clarity database");
180+
}
174181

175-
if cur_epoch.uses_marfed_block_time() {
176-
db.begin();
177-
db.setup_block_metadata(Some(get_epoch_time_secs()))
178-
.expect("FAIL: unable to set block time in Clarity database");
179-
db.commit()
180-
.expect("FAIL: unable to commit block time in Clarity database");
181-
}
182+
if cur_epoch.uses_marfed_block_time() {
183+
db.begin();
184+
db.setup_block_metadata(Some(get_epoch_time_secs()))
185+
.expect("FAIL: unable to set block time in Clarity database");
186+
db.commit()
187+
.expect("FAIL: unable to commit block time in Clarity database");
188+
}
182189

183-
let mut block_conn =
184-
ClarityBlockConnection::new_test_conn(store, &headers_db, &burn_db, cur_epoch);
185-
let r = f(&mut block_conn);
186-
block_conn.commit_block();
190+
(store, headers_db, burn_db, cur_epoch)
191+
}
187192

188-
r
189-
};
193+
pub fn execute_next_block_as_conn_with_tenure<F, R>(&mut self, new_tenure: bool, f: F) -> R
194+
where
195+
F: FnOnce(&mut ClarityBlockConnection) -> R,
196+
{
197+
let (store, headers_db, burn_db, cur_epoch) = self.setup_block_environment(new_tenure);
198+
199+
let mut block_conn =
200+
ClarityBlockConnection::new_test_conn(store, &headers_db, &burn_db, cur_epoch);
201+
let r = f(&mut block_conn);
202+
block_conn.commit_block();
190203

191204
r
192205
}
@@ -202,49 +215,13 @@ impl ClarityTestSim {
202215
where
203216
F: FnOnce(&mut OwnedEnvironment) -> R,
204217
{
205-
let mut store = self.marf.begin(
206-
&StacksBlockId(test_sim_height_to_hash(self.block_height, self.fork)),
207-
&StacksBlockId(test_sim_height_to_hash(self.block_height + 1, self.fork)),
208-
);
209-
210-
self.block_height += 1;
211-
if new_tenure {
212-
self.tenure_height += 1;
213-
}
218+
let (mut store, headers_db, burn_db, cur_epoch) = self.setup_block_environment(new_tenure);
214219

215-
let r = {
216-
let headers_db = TestSimHeadersDB {
217-
height: self.block_height,
218-
};
219-
let burn_db = TestSimBurnStateDB {
220-
epoch_bounds: self.epoch_bounds.clone(),
221-
pox_constants: PoxConstants::test_default(),
222-
height: (self.tenure_height + 100).try_into().unwrap(),
223-
};
224-
225-
let cur_epoch = Self::check_and_bump_epoch(&mut store, &headers_db, &burn_db);
226-
debug!("Execute block in epoch {}", &cur_epoch);
220+
debug!("Execute block in epoch {}", &cur_epoch);
227221

228-
let mut db = store.as_clarity_db(&headers_db, &burn_db);
229-
if cur_epoch.clarity_uses_tip_burn_block() {
230-
db.begin();
231-
db.set_tenure_height(self.tenure_height as u32)
232-
.expect("FAIL: unable to set tenure height in Clarity database");
233-
db.commit()
234-
.expect("FAIL: unable to commit tenure height in Clarity database");
235-
}
236-
237-
if cur_epoch.uses_marfed_block_time() {
238-
db.begin();
239-
db.setup_block_metadata(Some(get_epoch_time_secs()))
240-
.expect("FAIL: unable to set block time in Clarity database");
241-
db.commit()
242-
.expect("FAIL: unable to commit block time in Clarity database");
243-
}
244-
245-
let mut owned_env = OwnedEnvironment::new_toplevel(db);
246-
f(&mut owned_env)
247-
};
222+
let db = store.as_clarity_db(&headers_db, &burn_db);
223+
let mut owned_env = OwnedEnvironment::new_toplevel(db);
224+
let r = f(&mut owned_env);
248225

249226
store.test_commit();
250227

0 commit comments

Comments
 (0)