Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Signed-off-by: Eval EXEC <[email protected]>
  • Loading branch information
eval-exec committed Dec 14, 2024
1 parent a12cd96 commit 320a8fc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
16 changes: 10 additions & 6 deletions sync/src/synchronizer/headers_process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl<'a> HeadersProcess<'a> {

fn is_parent_exists(&self, first_header: &core:HeaderView) -> bool {
let shared: &SyncShared = self.synchronizer.shared();
shared.get_header_fields(first_header.parent_hash).is_some()
shared.get_header_fields(first_header.parent_hash()).is_some()
}


Expand Down Expand Up @@ -98,15 +98,19 @@ impl<'a> HeadersProcess<'a> {

pub fn execute(self) -> Status {
debug!("HeadersProcess begins");
let shared: &SyncShared = self.synchronizer.shared();
let consensus = shared.consensus();
let headers = self
.message
.headers()
.to_entity()
.into_iter()
.map(packed::Header::into_view)
.collect::<Vec<_>>();
self.execute_inner(headers)
}

fn execute_inner(self, headers: Vec<core::HeaderView>) -> Status {
let shared: &SyncShared = self.synchronizer.shared();
let consensus = shared.consensus();

if headers.len() > MAX_HEADERS_LEN {
warn!("HeadersProcess is oversized");
Expand Down Expand Up @@ -136,7 +140,7 @@ impl<'a> HeadersProcess<'a> {

if !self.is_parent_exists(&headers[0]) {
// put the headers into a memory cache
self.synchronizer.header_cache.insert(headers[0].parent_hash, headers);
self.synchronizer.header_cache.insert(headers[0].parent_hash(), headers);
// verify them later
return Status::ok();
}
Expand Down Expand Up @@ -226,8 +230,8 @@ impl<'a> HeadersProcess<'a> {
{
// these headers verify success
// may the headers's tail header_hash exist in headers_cahce?
if let Some(headers) = self.synchronizer.headers_cache.get(headers.last().expect("last header must exist").hash){
HeadersProcess::new().execute();
if let Some(headers) = self.synchronizer.header_cache.get(headers.last().expect("last header must exist").hash){
return self.execute_inner(headers);
}
}

Expand Down
4 changes: 4 additions & 0 deletions sync/src/synchronizer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ use ckb_systemtime::unix_time_as_millis;

#[cfg(test)]
use ckb_types::core;
use ckb_types::packed::Header;
use ckb_types::{
core::BlockNumber,
packed::{self, Byte32},
prelude::*,
};
use std::collections::HashMap;
use std::{
collections::HashSet,
sync::{atomic::Ordering, Arc},
Expand Down Expand Up @@ -312,10 +314,12 @@ impl Synchronizer {
///
/// This is a runtime sync protocol shared state, and any Sync protocol messages will be processed and forwarded by it
pub fn new(chain: ChainController, shared: Arc<SyncShared>) -> Synchronizer {
let header_cache = HashMap::new();
Synchronizer {
chain,
shared,
fetch_channel: None,
header_cache,
}
}

Expand Down

0 comments on commit 320a8fc

Please sign in to comment.