Skip to content

Commit 8a75c5e

Browse files
committed
Introduce Params struct
Params limits the lifetime of the inner lock.
1 parent 81d6218 commit 8a75c5e

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

core/src/miner/miner.rs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ pub struct Miner {
117117
next_mandatory_reseal: NextMandatoryReseal,
118118
sealing_block_last_request: Mutex<u64>,
119119
sealing_work: Mutex<SealingWork>,
120-
params: RwLock<AuthoringParams>,
120+
params: Params,
121121
engine: Arc<dyn CodeChainEngine>,
122122
options: MinerOptions,
123123

@@ -151,6 +151,29 @@ impl NextMandatoryReseal {
151151
}
152152
}
153153

154+
struct Params {
155+
params: RwLock<AuthoringParams>,
156+
}
157+
158+
impl Params {
159+
pub fn new(params: AuthoringParams) -> Self {
160+
Self {
161+
params: RwLock::new(params),
162+
}
163+
}
164+
165+
pub fn get(&self) -> AuthoringParams {
166+
self.params.read().clone()
167+
}
168+
169+
pub fn apply<F>(&self, f: F)
170+
where
171+
F: FnOnce(&mut AuthoringParams) -> (), {
172+
let mut params = self.params.write();
173+
f(&mut params);
174+
}
175+
}
176+
154177
impl Miner {
155178
/// Push listener that will handle new jobs
156179
pub fn add_work_listener(&self, notifier: Box<dyn NotifyWork>) {
@@ -194,7 +217,7 @@ impl Miner {
194217
mem_pool,
195218
next_allowed_reseal: NextAllowedReseal::new(Instant::now()),
196219
next_mandatory_reseal: NextMandatoryReseal::new(Instant::now() + options.reseal_max_period),
197-
params: RwLock::new(AuthoringParams::default()),
220+
params: Params::new(AuthoringParams::default()),
198221
sealing_block_last_request: Mutex::new(0),
199222
sealing_work: Mutex::new(SealingWork {
200223
queue: SealingQueue::new(options.work_queue_size),
@@ -492,7 +515,7 @@ impl Miner {
492515

493516
let last_work_hash = sealing_work.queue.peek_last_ref().map(|pb| *pb.block().header().hash());
494517
ctrace!(MINER, "prepare_block: No existing work - making new block");
495-
let params = self.params.read().clone();
518+
let params = self.params.get();
496519
let open_block = chain.prepare_open_block(parent_block_id, params.author, params.extra_data);
497520
let (block_number, parent_hash) = {
498521
let header = open_block.block().header();
@@ -731,11 +754,11 @@ impl MinerService for Miner {
731754
}
732755

733756
fn authoring_params(&self) -> AuthoringParams {
734-
self.params.read().clone()
757+
self.params.get()
735758
}
736759

737760
fn set_author(&self, address: Address) -> Result<(), AccountProviderError> {
738-
self.params.write().author = address;
761+
self.params.apply(|params| params.author = address);
739762

740763
if self.engine_type().need_signer_key() && self.engine.seals_internally().is_some() {
741764
if let Some(ref ap) = self.accounts {
@@ -759,7 +782,7 @@ impl MinerService for Miner {
759782
}
760783

761784
fn set_extra_data(&self, extra_data: Bytes) {
762-
self.params.write().extra_data = extra_data;
785+
self.params.apply(|params| params.extra_data = extra_data);
763786
}
764787

765788
fn minimal_fee(&self) -> u64 {

0 commit comments

Comments
 (0)