Skip to content

Commit d7fef3d

Browse files
Seulgi Kimmajecty
authored andcommitted
Make the client hold the miner directly
Currently, the client access the miner through the importer. It's better to give a method to access the miner directly.
1 parent b8d5a21 commit d7fef3d

File tree

3 files changed

+27
-29
lines changed

3 files changed

+27
-29
lines changed

codechain/test_lock.rs

Whitespace-only changes.

core/src/client/client.rs

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ pub struct Client {
7575

7676
importer: Importer,
7777

78+
/// Handles block sealing
79+
miner: Arc<Miner>,
80+
7881
/// Timer for reseal_min_period/reseal_max_period on miner client
7982
reseal_timer: TimerApi,
8083
}
@@ -106,7 +109,7 @@ impl Client {
106109

107110
let engine = scheme.engine.clone();
108111

109-
let importer = Importer::try_new(config, engine.clone(), message_channel.clone(), miner)?;
112+
let importer = Importer::try_new(config, engine.clone(), message_channel.clone(), Arc::clone(&miner))?;
110113
let genesis_accounts = scheme.genesis_accounts();
111114

112115
let client = Arc::new(Client {
@@ -119,6 +122,7 @@ impl Client {
119122
queue_transactions: AtomicUsize::new(0),
120123
genesis_accounts,
121124
importer,
125+
miner,
122126
reseal_timer,
123127
});
124128

@@ -199,7 +203,7 @@ impl Client {
199203

200204
/// This is triggered by a message coming from a engine when a new block should be created
201205
pub fn update_sealing(&self, parent_block: BlockId, allow_empty_block: bool) {
202-
self.importer.miner.update_sealing(self, parent_block, allow_empty_block);
206+
self.miner.update_sealing(self, parent_block, allow_empty_block);
203207
}
204208

205209
fn block_hash(chain: &BlockChain, id: &BlockId) -> Option<BlockHash> {
@@ -234,7 +238,7 @@ impl Client {
234238
self.queue_transactions.fetch_sub(transactions.len(), AtomicOrdering::SeqCst);
235239
let transactions: Vec<UnverifiedTransaction> =
236240
transactions.iter().filter_map(|bytes| Rlp::new(bytes).as_val().ok()).collect();
237-
let results = self.importer.miner.import_external_transactions(self, transactions);
241+
let results = self.miner.import_external_transactions(self, transactions);
238242
results.len()
239243
}
240244

@@ -264,7 +268,7 @@ impl Client {
264268
}
265269

266270
let (enacted, retracted) = self.importer.calculate_enacted_retracted(&[route]);
267-
self.importer.miner.chain_new_blocks(self, &[], &[], &enacted, &retracted);
271+
self.miner.chain_new_blocks(self, &[], &[], &enacted, &retracted);
268272
self.new_blocks(&[], &[], &enacted, &retracted, &[]);
269273
}
270274

@@ -315,7 +319,7 @@ impl TimeoutHandler for Client {
315319
match token {
316320
RESEAL_MAX_TIMER_TOKEN => {
317321
// Working in PoW only
318-
if self.engine().seals_internally().is_none() && !self.importer.miner.prepare_work_sealing(self) {
322+
if self.engine().seals_internally().is_none() && !self.miner.prepare_work_sealing(self) {
319323
self.update_sealing(BlockId::Latest, true);
320324
}
321325
}
@@ -547,7 +551,7 @@ impl EngineClient for Client {
547551

548552
/// Submit a seal for a block in the mining queue.
549553
fn submit_seal(&self, block_hash: BlockHash, seal: Vec<Bytes>) {
550-
if self.importer.miner.submit_seal(self, block_hash, seal).is_err() {
554+
if self.miner.submit_seal(self, block_hash, seal).is_err() {
551555
cwarn!(CLIENT, "Wrong internal seal submission!")
552556
}
553557
}
@@ -660,18 +664,15 @@ impl ImportBlock for Client {
660664
route
661665
};
662666
let (enacted, retracted) = self.importer.calculate_enacted_retracted(&[route]);
663-
self.importer.miner.chain_new_blocks(self, &[h], &[], &enacted, &retracted);
667+
self.miner.chain_new_blocks(self, &[h], &[], &enacted, &retracted);
664668
self.new_blocks(&[h], &[], &enacted, &retracted, &[h]);
665669
self.db().flush().expect("DB flush failed.");
666670
Ok(h)
667671
}
668672

669673
fn set_min_timer(&self) {
670674
self.reseal_timer.cancel(RESEAL_MIN_TIMER_TOKEN).expect("Reseal min timer clear succeeds");
671-
match self
672-
.reseal_timer
673-
.schedule_once(self.importer.miner.get_options().reseal_min_period, RESEAL_MIN_TIMER_TOKEN)
674-
{
675+
match self.reseal_timer.schedule_once(self.miner.get_options().reseal_min_period, RESEAL_MIN_TIMER_TOKEN) {
675676
Ok(_) => {}
676677
Err(TimerScheduleError::TokenAlreadyScheduled) => {
677678
// Since set_min_timer could be called in multi thread, ignore the TokenAlreadyScheduled error
@@ -682,10 +683,7 @@ impl ImportBlock for Client {
682683

683684
fn set_max_timer(&self) {
684685
self.reseal_timer.cancel(RESEAL_MAX_TIMER_TOKEN).expect("Reseal max timer clear succeeds");
685-
match self
686-
.reseal_timer
687-
.schedule_once(self.importer.miner.get_options().reseal_max_period, RESEAL_MAX_TIMER_TOKEN)
688-
{
686+
match self.reseal_timer.schedule_once(self.miner.get_options().reseal_max_period, RESEAL_MAX_TIMER_TOKEN) {
689687
Ok(_) => {}
690688
Err(TimerScheduleError::TokenAlreadyScheduled) => {
691689
// Since set_max_timer could be called in multi thread, ignore the TokenAlreadyScheduled error
@@ -703,7 +701,7 @@ impl BlockChainClient for Client {
703701

704702
/// Import own transaction
705703
fn queue_own_transaction(&self, transaction: SignedTransaction) -> Result<(), Error> {
706-
self.importer.miner.import_own_transaction(self, transaction)?;
704+
self.miner.import_own_transaction(self, transaction)?;
707705
Ok(())
708706
}
709707

@@ -726,26 +724,26 @@ impl BlockChainClient for Client {
726724
}
727725

728726
fn delete_all_pending_transactions(&self) {
729-
self.importer.miner.delete_all_pending_transactions();
727+
self.miner.delete_all_pending_transactions();
730728
}
731729

732730
fn ready_transactions(&self, range: Range<u64>) -> PendingSignedTransactions {
733-
self.importer.miner.ready_transactions(range)
731+
self.miner.ready_transactions(range)
734732
}
735733

736734
fn count_pending_transactions(&self, range: Range<u64>) -> usize {
737-
self.importer.miner.count_pending_transactions(range)
735+
self.miner.count_pending_transactions(range)
738736
}
739737

740738
fn future_included_count_pending_transactions(&self, range: Range<u64>) -> usize {
741-
self.importer.miner.future_included_count_pending_transactions(range)
739+
self.miner.future_included_count_pending_transactions(range)
742740
}
743741

744742
fn future_ready_transactions(&self, range: Range<u64>) -> PendingSignedTransactions {
745-
self.importer.miner.future_ready_transactions(range)
743+
self.miner.future_ready_transactions(range)
746744
}
747745
fn is_pending_queue_empty(&self) -> bool {
748-
self.importer.miner.status().transactions_in_pending_queue == 0
746+
self.miner.status().transactions_in_pending_queue == 0
749747
}
750748

751749
fn block_number(&self, id: &BlockId) -> Option<BlockNumber> {
@@ -895,27 +893,27 @@ impl BlockProducer for Client {
895893

896894
impl MiningBlockChainClient for Client {
897895
fn get_malicious_users(&self) -> Vec<Address> {
898-
self.importer.miner.get_malicious_users()
896+
self.miner.get_malicious_users()
899897
}
900898

901899
fn release_malicious_users(&self, prisoner_vec: Vec<Address>) {
902-
self.importer.miner.release_malicious_users(prisoner_vec)
900+
self.miner.release_malicious_users(prisoner_vec)
903901
}
904902

905903
fn imprison_malicious_users(&self, prisoner_vec: Vec<Address>) {
906-
self.importer.miner.imprison_malicious_users(prisoner_vec)
904+
self.miner.imprison_malicious_users(prisoner_vec)
907905
}
908906

909907
fn get_immune_users(&self) -> Vec<Address> {
910-
self.importer.miner.get_immune_users()
908+
self.miner.get_immune_users()
911909
}
912910

913911
fn register_immune_users(&self, immune_user_vec: Vec<Address>) {
914-
self.importer.miner.register_immune_users(immune_user_vec)
912+
self.miner.register_immune_users(immune_user_vec)
915913
}
916914

917915
fn mem_pool_min_fees(&self) -> MemPoolMinFees {
918-
self.importer.miner.get_options().mem_pool_min_fees
916+
self.miner.get_options().mem_pool_min_fees
919917
}
920918
}
921919

core/src/client/importer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub struct Importer {
5050
pub header_queue: HeaderQueue,
5151

5252
/// Handles block sealing
53-
pub miner: Arc<Miner>,
53+
miner: Arc<Miner>,
5454

5555
/// CodeChain engine to be used during import
5656
pub engine: Arc<dyn CodeChainEngine>,

0 commit comments

Comments
 (0)