Skip to content

Commit

Permalink
Refactor type of assume_valid_target from H256 to Vec<H256>
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 8, 2024
1 parent 3574ca2 commit 18b8c40
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 15 deletions.
2 changes: 1 addition & 1 deletion rpc/src/module/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ impl NetRpc for NetRpcImpl {
let unverified_tip = shared.get_unverified_tip();
let sync_state = SyncState {
ibd: chain.is_initial_block_download(),
assume_valid_target_reached: shared.assume_valid_target().is_none(),
assume_valid_target_reached: shared.assume_valid_targets().is_none(),
assume_valid_target: shared
.assume_valid_target_specified()
.as_ref()
Expand Down
10 changes: 5 additions & 5 deletions shared/src/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub struct Shared {
pub(crate) async_handle: Handle,
pub(crate) ibd_finished: Arc<AtomicBool>,

pub(crate) assume_valid_target: Arc<Mutex<Option<H256>>>,
pub(crate) assume_valid_targets: Arc<Mutex<Option<Vec<H256>>>>,
pub(crate) assume_valid_target_specified: Arc<Option<H256>>,

pub header_map: Arc<HeaderMap>,
Expand All @@ -83,7 +83,7 @@ impl Shared {
async_handle: Handle,
ibd_finished: Arc<AtomicBool>,

assume_valid_target: Arc<Mutex<Option<H256>>>,
assume_valid_targets: Arc<Mutex<Option<Vec<H256>>>>,
assume_valid_target_specified: Arc<Option<H256>>,
header_map: Arc<HeaderMap>,
block_status_map: Arc<DashMap<Byte32, BlockStatus>>,
Expand All @@ -106,7 +106,7 @@ impl Shared {
snapshot_mgr,
async_handle,
ibd_finished,
assume_valid_target,
assume_valid_targets,
assume_valid_target_specified,
header_map,
block_status_map,
Expand Down Expand Up @@ -467,8 +467,8 @@ impl Shared {
);
}

pub fn assume_valid_target(&self) -> MutexGuard<Option<H256>> {
self.assume_valid_target.lock()
pub fn assume_valid_targets(&self) -> MutexGuard<Option<Vec<H256>>> {
self.assume_valid_targets.lock()
}

pub fn assume_valid_target_specified(&self) -> Arc<Option<H256>> {
Expand Down
24 changes: 16 additions & 8 deletions shared/src/shared_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use ckb_tx_pool::{
};
use ckb_types::core::hardfork::HardForks;
use ckb_types::prelude::Pack;
use ckb_types::H256;
use ckb_types::{
core::service::PoolTransactionEntry, core::tx_pool::Reject, core::EpochExt, core::HeaderView,
};
Expand Down Expand Up @@ -352,10 +353,12 @@ impl SharedBuilder {
sync_config: &SyncConfig,
snapshot: &Snapshot,
) -> bool {
if let Some(ref target) = sync_config.assume_valid_target {
if snapshot.block_exists(&target.pack()) {
info!("assume valid target is already in db, CKB will do full verification from now on");
return true;
if let Some(ref target) = sync_config.assume_valid_targets {
if let Some(last_target) = target.last() {
if snapshot.block_exists(&last_target.pack()) {
info!("assume valid target is already in db, CKB will do full verification from now on");
return true;
}
}
}
false
Expand Down Expand Up @@ -442,14 +445,19 @@ impl SharedBuilder {

let block_status_map = Arc::new(DashMap::new());

let assume_valid_target = Arc::new(Mutex::new({
let assume_valid_targets = Arc::new(Mutex::new({
if Self::check_assume_valid_target_already_exists(&sync_config, &snapshot) {
None
} else {
sync_config.assume_valid_target.clone()
sync_config.assume_valid_targets.clone()
}
}));
let assume_valid_target_specified = Arc::new(sync_config.assume_valid_target);

let assume_valid_target_specified: Arc<Option<H256>> = Arc::new(
sync_config
.assume_valid_targets
.and_then(|targets| targets.last().cloned()),
);

let shared = Shared::new(
store,
Expand All @@ -460,7 +468,7 @@ impl SharedBuilder {
snapshot_mgr,
async_handle,
ibd_finished,
assume_valid_target,
assume_valid_targets,
assume_valid_target_specified,
header_map,
block_status_map,
Expand Down
2 changes: 1 addition & 1 deletion util/app-config/src/configs/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ pub struct SyncConfig {
pub header_map: HeaderMapConfig,
/// Block hash of assume valid target
#[serde(skip, default)]
pub assume_valid_target: Option<H256>,
pub assume_valid_targets: Option<Vec<H256>>,
/// Proof of minimum work during synchronization
#[serde(skip, default)]
pub min_chain_work: U256,
Expand Down

0 comments on commit 18b8c40

Please sign in to comment.