Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: block template cache #220

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 19 additions & 14 deletions miner/src/block_assembler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,16 @@ type BlockTemplateParams = (Option<Cycle>, Option<u64>, Option<Version>);
type BlockTemplateResult = Result<BlockTemplate, SharedError>;
const BLOCK_ASSEMBLER_SUBSCRIBER: &str = "block_assembler";
const BLOCK_TEMPLATE_TIMEOUT: u64 = 3000;
const TEMPLATE_CACHE_SIZE: usize = 10;

struct CurrentTemplate {
struct TemplateCache {
pub time: u64,
pub uncles_updated_at: u64,
pub txs_updated_at: u64,
pub template: BlockTemplate,
}

impl CurrentTemplate {
impl TemplateCache {
fn is_outdate(
&self,
last_uncles_updated_at: u64,
Expand All @@ -57,7 +58,7 @@ pub struct BlockAssembler<CI> {
type_hash: H256,
work_id: AtomicUsize,
last_uncles_updated_at: AtomicUsize,
current_template: Mutex<Option<CurrentTemplate>>,
template_caches: Mutex<LruCache<(Cycle, u64, Version), TemplateCache>>,
}

#[derive(Clone)]
Expand Down Expand Up @@ -106,7 +107,7 @@ impl<CI: ChainIndex + 'static> BlockAssembler<CI> {
candidate_uncles: LruCache::new(MAX_CANDIDATE_UNCLES),
work_id: AtomicUsize::new(0),
last_uncles_updated_at: AtomicUsize::new(0),
current_template: Mutex::new(None),
template_caches: Mutex::new(LruCache::new(TEMPLATE_CACHE_SIZE)),
}
}

Expand Down Expand Up @@ -228,15 +229,16 @@ impl<CI: ChainIndex + 'static> BlockAssembler<CI> {
let number = tip_header.number() + 1;
let current_time = cmp::max(unix_time_as_millis(), header.timestamp() + 1);

let mut current_template = self.current_template.lock();
if let Some(ref current_template) = *current_template {
if !current_template.is_outdate(
let mut template_caches = self.template_caches.lock();

if let Some(template_cache) = template_caches.get(&(cycles_limit, bytes_limit, version)) {
if !template_cache.is_outdate(
last_uncles_updated_at,
last_txs_updated_at,
current_time,
number,
) {
return Ok(current_template.template.clone());
return Ok(template_cache.template.clone());
}
}

Expand Down Expand Up @@ -278,12 +280,15 @@ impl<CI: ChainIndex + 'static> BlockAssembler<CI> {
work_id: format!("{}", self.work_id.fetch_add(1, Ordering::SeqCst)),
};

*current_template = Some(CurrentTemplate {
time: current_time,
uncles_updated_at: last_uncles_updated_at,
txs_updated_at: last_txs_updated_at,
template: template.clone(),
});
template_caches.insert(
(cycles_limit, bytes_limit, version),
TemplateCache {
time: current_time,
uncles_updated_at: last_uncles_updated_at,
txs_updated_at: last_txs_updated_at,
template: template.clone(),
},
);

Ok(template)
}
Expand Down