Skip to content

Commit 41e0654

Browse files
remagpiemergify[bot]
authored andcommitted
Add a helper function for obtaining client
1 parent f1fb75f commit 41e0654

File tree

2 files changed

+7
-15
lines changed

2 files changed

+7
-15
lines changed

core/src/consensus/tendermint/engine.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,7 @@ impl ConsensusEngine for Tendermint {
187187
_ => {
188188
let rewards = stake::drain_previous_rewards(block.state_mut())?;
189189
let start_of_the_current_term = metadata.last_term_finished_block_num() + 1;
190-
let client = self
191-
.client
192-
.read()
193-
.as_ref()
194-
.ok_or(EngineError::CannotOpenBlock)?
195-
.upgrade()
196-
.ok_or(EngineError::CannotOpenBlock)?;
190+
let client = self.client().ok_or(EngineError::CannotOpenBlock)?;
197191

198192
if term > 1 {
199193
let start_of_the_previous_term = {
@@ -273,7 +267,7 @@ impl ConsensusEngine for Tendermint {
273267

274268
let inner = self.inner.clone();
275269
let extension = service.register_extension(move |api| TendermintExtension::new(inner, timeouts, api));
276-
let client = Weak::clone(self.client.read().as_ref().unwrap());
270+
let client = Arc::downgrade(&self.client().unwrap());
277271
self.extension_initializer.send((extension, client)).unwrap();
278272

279273
let (result, receiver) = crossbeam::bounded(1);
@@ -317,13 +311,7 @@ impl ConsensusEngine for Tendermint {
317311
}
318312

319313
fn possible_authors(&self, block_number: Option<u64>) -> Result<Option<Vec<Address>>, EngineError> {
320-
let client = self
321-
.client
322-
.read()
323-
.as_ref()
324-
.ok_or(EngineError::CannotOpenBlock)?
325-
.upgrade()
326-
.ok_or(EngineError::CannotOpenBlock)?;
314+
let client = self.client().ok_or(EngineError::CannotOpenBlock)?;
327315
let block_hash = match block_number {
328316
None => {
329317
client.block_header(&BlockId::Latest).expect("latest block must exist").hash() // the latest block

core/src/consensus/tendermint/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ impl Tendermint {
115115
has_signer: false.into(),
116116
})
117117
}
118+
119+
fn client(&self) -> Option<Arc<dyn ConsensusClient>> {
120+
self.client.read().as_ref()?.upgrade()
121+
}
118122
}
119123

120124
const SEAL_FIELDS: usize = 4;

0 commit comments

Comments
 (0)