Skip to content

Commit 5be333d

Browse files
committed
Introduce Params struct in miner.rs
Params limits the lifetime of the inner lock.
1 parent 1684df5 commit 5be333d

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

core/src/miner/miner.rs

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ pub struct AuthoringParams {
8787
pub struct Miner {
8888
mem_pool: Arc<RwLock<MemPool>>,
8989
next_allowed_reseal: NextAllowedReseal,
90-
params: RwLock<AuthoringParams>,
90+
params: Params,
9191
engine: Arc<dyn CodeChainEngine>,
9292
options: MinerOptions,
9393

@@ -98,6 +98,29 @@ pub struct Miner {
9898
immune_users: RwLock<HashSet<Public>>,
9999
}
100100

101+
struct Params {
102+
params: RwLock<AuthoringParams>,
103+
}
104+
105+
impl Params {
106+
pub fn new(params: AuthoringParams) -> Self {
107+
Self {
108+
params: RwLock::new(params),
109+
}
110+
}
111+
112+
pub fn get(&self) -> AuthoringParams {
113+
self.params.read().clone()
114+
}
115+
116+
pub fn apply<F>(&self, f: F)
117+
where
118+
F: FnOnce(&mut AuthoringParams) -> (), {
119+
let mut params = self.params.write();
120+
f(&mut params);
121+
}
122+
}
123+
101124
struct NextAllowedReseal {
102125
instant: Mutex<Instant>,
103126
}
@@ -149,7 +172,7 @@ impl Miner {
149172
Self {
150173
mem_pool,
151174
next_allowed_reseal: NextAllowedReseal::new(Instant::now()),
152-
params: RwLock::new(AuthoringParams::default()),
175+
params: Params::new(AuthoringParams::default()),
153176
engine: scheme.engine.clone(),
154177
options,
155178
sealing_enabled: AtomicBool::new(true),
@@ -269,7 +292,7 @@ impl Miner {
269292
) -> Result<Option<ClosedBlock>, Error> {
270293
let (transactions, mut open_block, block_number, block_tx_signer, block_tx_seq) = {
271294
ctrace!(MINER, "prepare_block: No existing work - making new block");
272-
let params = self.params.read().clone();
295+
let params = self.params.get();
273296
let open_block = chain.prepare_open_block(parent_block_id, params.author, params.extra_data);
274297
let (block_number, parent_hash) = {
275298
let header = open_block.block().header();
@@ -455,11 +478,11 @@ impl MinerService for Miner {
455478
}
456479

457480
fn authoring_params(&self) -> AuthoringParams {
458-
self.params.read().clone()
481+
self.params.get()
459482
}
460483

461484
fn set_author(&self, pubkey: Public) -> Result<(), AccountProviderError> {
462-
self.params.write().author = pubkey;
485+
self.params.apply(|params| params.author = pubkey);
463486

464487
if self.engine_type().need_signer_key() {
465488
ctrace!(MINER, "Set author to {:?}", pubkey);
@@ -471,11 +494,11 @@ impl MinerService for Miner {
471494
}
472495

473496
fn get_author(&self) -> Public {
474-
self.params.read().author
497+
self.params.get().author
475498
}
476499

477500
fn set_extra_data(&self, extra_data: Bytes) {
478-
self.params.write().extra_data = extra_data;
501+
self.params.apply(|params| params.extra_data = extra_data);
479502
}
480503

481504
fn transactions_limit(&self) -> usize {

0 commit comments

Comments
 (0)