Skip to content

Commit bfbfce1

Browse files
committed
Introduce SealingBlockLastRequest
It will limit the lifetime of lock guard.
1 parent 0016bed commit bfbfce1

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
@@ -115,7 +115,7 @@ pub struct Miner {
115115
mem_pool: Arc<RwLock<MemPool>>,
116116
next_allowed_reseal: NextAllowedReseal,
117117
next_mandatory_reseal: NextMandatoryReseal,
118-
sealing_block_last_request: Mutex<u64>,
118+
sealing_block_last_request: SealingBlockLastRequest,
119119
sealing_work: Mutex<SealingWork>,
120120
params: Params,
121121
engine: Arc<dyn CodeChainEngine>,
@@ -129,6 +129,30 @@ pub struct Miner {
129129
immune_users: RwLock<HashSet<Address>>,
130130
}
131131

132+
struct SealingBlockLastRequest {
133+
block_number: Mutex<u64>,
134+
}
135+
136+
impl SealingBlockLastRequest {
137+
pub fn new() -> Self {
138+
Self {
139+
block_number: Mutex::new(0),
140+
}
141+
}
142+
143+
pub fn get(&self) -> u64 {
144+
*self.block_number.lock()
145+
}
146+
147+
/// Returns previous value
148+
pub fn set(&self, block_number: u64) -> u64 {
149+
let mut guard = self.block_number.lock();
150+
let prev = *guard;
151+
*guard = block_number;
152+
prev
153+
}
154+
}
155+
132156
struct NextAllowedReseal {
133157
instant: Mutex<Instant>,
134158
}
@@ -236,7 +260,7 @@ impl Miner {
236260
next_allowed_reseal: NextAllowedReseal::new(Instant::now()),
237261
next_mandatory_reseal: NextMandatoryReseal::new(Instant::now() + options.reseal_max_period),
238262
params: Params::new(AuthoringParams::default()),
239-
sealing_block_last_request: Mutex::new(0),
263+
sealing_block_last_request: SealingBlockLastRequest::new(),
240264
sealing_work: Mutex::new(SealingWork {
241265
queue: SealingQueue::new(options.work_queue_size),
242266
enabled: options.force_sealing || scheme.engine.seals_internally().is_some(),
@@ -280,7 +304,7 @@ impl Miner {
280304
let mut sealing_work = self.sealing_work.lock();
281305
if sealing_work.enabled {
282306
ctrace!(MINER, "requires_reseal: sealing enabled");
283-
let last_request = *self.sealing_block_last_request.lock();
307+
let last_request = self.sealing_block_last_request.get();
284308
let should_disable_sealing = !self.options.force_sealing
285309
&& !has_local_transactions
286310
&& self.engine.seals_internally().is_none()
@@ -908,16 +932,16 @@ impl MinerService for Miner {
908932
}
909933
}
910934
}
911-
let mut sealing_block_last_request = self.sealing_block_last_request.lock();
935+
912936
let best_number = client.chain_info().best_block_number;
913-
if *sealing_block_last_request != best_number {
937+
let prev_request = self.sealing_block_last_request.set(best_number);
938+
if prev_request != best_number {
914939
ctrace!(
915940
MINER,
916941
"prepare_work_sealing: Miner received request (was {}, now {}) - waking up.",
917-
*sealing_block_last_request,
942+
prev_request,
918943
best_number
919944
);
920-
*sealing_block_last_request = best_number;
921945
}
922946

923947
// Return if we restarted

0 commit comments

Comments
 (0)