Skip to content

Commit e1df2af

Browse files
committed
Introduce SealingBlockLastRequest
SealingBlockLastRequest limits the lifetime of the inner lock.
1 parent 5eec816 commit e1df2af

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

core/src/miner/miner.rs

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ pub struct Miner {
117117
mem_pool: Arc<RwLock<MemPool>>,
118118
next_allowed_reseal: NextAllowedReseal,
119119
next_mandatory_reseal: NextMandatoryReseal,
120-
sealing_block_last_request: Mutex<u64>,
120+
sealing_block_last_request: SealingBlockLastRequest,
121121
sealing_work: Mutex<SealingWork>,
122122
params: Params,
123123
engine: Arc<dyn CodeChainEngine>,
@@ -131,6 +131,30 @@ pub struct Miner {
131131
immune_users: RwLock<HashSet<Address>>,
132132
}
133133

134+
struct SealingBlockLastRequest {
135+
block_number: Mutex<u64>,
136+
}
137+
138+
impl SealingBlockLastRequest {
139+
pub fn new() -> Self {
140+
Self {
141+
block_number: Mutex::new(0),
142+
}
143+
}
144+
145+
pub fn get(&self) -> u64 {
146+
*self.block_number.lock()
147+
}
148+
149+
/// Returns previous value
150+
pub fn set(&self, block_number: u64) -> u64 {
151+
let mut guard = self.block_number.lock();
152+
let prev = *guard;
153+
*guard = block_number;
154+
prev
155+
}
156+
}
157+
134158
type NextAllowedReseal = NextMandatoryReseal;
135159

136160
struct NextMandatoryReseal {
@@ -221,7 +245,7 @@ impl Miner {
221245
next_allowed_reseal: NextAllowedReseal::new(Instant::now()),
222246
next_mandatory_reseal: NextMandatoryReseal::new(Instant::now() + options.reseal_max_period),
223247
params: Params::new(AuthoringParams::default()),
224-
sealing_block_last_request: Mutex::new(0),
248+
sealing_block_last_request: SealingBlockLastRequest::new(),
225249
sealing_work: Mutex::new(SealingWork {
226250
queue: SealingQueue::new(options.work_queue_size),
227251
enabled: options.force_sealing || scheme.engine.seals_internally().is_some(),
@@ -265,7 +289,7 @@ impl Miner {
265289
let mut sealing_work = self.sealing_work.lock();
266290
if sealing_work.enabled {
267291
ctrace!(MINER, "requires_reseal: sealing enabled");
268-
let last_request = *self.sealing_block_last_request.lock();
292+
let last_request = self.sealing_block_last_request.get();
269293
let should_disable_sealing = !self.options.force_sealing
270294
&& !has_local_transactions
271295
&& self.engine.seals_internally().is_none()
@@ -864,16 +888,16 @@ impl MinerService for Miner {
864888
}
865889
}
866890
}
867-
let mut sealing_block_last_request = self.sealing_block_last_request.lock();
891+
868892
let best_number = client.chain_info().best_block_number;
869-
if *sealing_block_last_request != best_number {
893+
let prev_request = self.sealing_block_last_request.set(best_number);
894+
if prev_request != best_number {
870895
ctrace!(
871896
MINER,
872897
"prepare_work_sealing: Miner received request (was {}, now {}) - waking up.",
873-
*sealing_block_last_request,
898+
prev_request,
874899
best_number
875900
);
876-
*sealing_block_last_request = best_number;
877901
}
878902

879903
// Return if we restarted

0 commit comments

Comments
 (0)