Skip to content

Commit

Permalink
Re-organize util/constant assume_valid_target code, put hardcoded exp…
Browse files Browse the repository at this point in the history
…onential growth target to lastest_assume_valid_target
  • Loading branch information
eval-exec committed Dec 8, 2024
1 parent b01408b commit 3574ca2
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 52 deletions.
4 changes: 2 additions & 2 deletions ckb-bin/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ default(TestNet): {}\n\n
You can explicitly set the value to 0x0000000000000000000000000000000000000000000000000000000000000000 \
to disable the default behavior and execute full verification for all blocks, \
",
ckb_constant::default_assume_valid_target::mainnet::DEFAULT_ASSUME_VALID_TARGET,
ckb_constant::default_assume_valid_target::testnet::DEFAULT_ASSUME_VALID_TARGET))
ckb_constant::latest_assume_valid_target::mainnet::DEFAULT_ASSUME_VALID_TARGET,
ckb_constant::latest_assume_valid_target::testnet::DEFAULT_ASSUME_VALID_TARGET))
).arg(
Arg::new(ARG_INDEXER)
.long(ARG_INDEXER)
Expand Down
60 changes: 37 additions & 23 deletions ckb-bin/src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use ckb_app_config::{
};
use ckb_chain_spec::{consensus::Consensus, ChainSpec};
use ckb_jsonrpc_types::ScriptHashType;
use ckb_logger::info;
use ckb_logger::{error, info};
use ckb_types::{u256, H256, U256};
use clap::ArgMatches;
use std::{path::PathBuf, str::FromStr};
Expand Down Expand Up @@ -71,33 +71,47 @@ impl Setup {
} else {
u256!("0x0")
};

let arg_assume_valid_target = matches.get_one::<String>(cli::ARG_ASSUME_VALID_TARGET);

config.network.sync.assume_valid_target =
arg_assume_valid_target.and_then(|s| H256::from_str(&s[2..]).ok());
if config.network.sync.assume_valid_target.is_none() {
config.network.sync.assume_valid_target = match consensus.id.as_str() {
config.network.sync.assume_valid_targets = matches
.get_one::<String>(cli::ARG_ASSUME_VALID_TARGET)
.map(|concacate_targets| {
concacate_targets
.split(',')
.map(|s| H256::from_str(&s[2..]))
.collect::<Result<Vec<H256>, _>>()
.map_err(|err| {
error!("Invalid assume valid target: {}", err);
ExitCode::Cli
})
})
.transpose()?; // Converts Result<Option<T>, E> to Option<Result<T, E>>

if config.network.sync.assume_valid_targets.is_none() {
config.network.sync.assume_valid_targets = match consensus.id.as_str() {
ckb_constant::hardfork::mainnet::CHAIN_SPEC_NAME => Some(
H256::from_str(&ckb_constant::default_assume_valid_target::mainnet::DEFAULT_ASSUME_VALID_TARGET[2..])
.expect("default assume_valid_target for mainnet must be valid"),
),
ckb_constant::default_assume_valid_target::mainnet::default_assume_valid_targets().iter().map(|target|
H256::from_str(&target[2..]).expect("default assume_valid_target for mainnet must be valid")).collect::<Vec<H256>>()),
ckb_constant::hardfork::testnet::CHAIN_SPEC_NAME => Some(
H256::from_str(&ckb_constant::default_assume_valid_target::testnet::DEFAULT_ASSUME_VALID_TARGET[2..])
.expect("default assume_valid_target for testnet must be valid"),
),
_ => None,
ckb_constant::default_assume_valid_target::testnet::default_assume_valid_targets().iter().map(|target|
H256::from_str(&target[2..]).expect("default assume_valid_target for testnet must be valid")).collect::<Vec<H256>>()),
_ => None,
};
}

if let Some(ref assume_valid_target) = config.network.sync.assume_valid_target {
if assume_valid_target
== &H256::from_slice(&[0; 32]).expect("must parse Zero h256 successful")
{
info!("Disable assume valid target since assume_valid_target is zero");
config.network.sync.assume_valid_target = None
} else {
info!("assume_valid_target set to 0x{}", assume_valid_target);
if let Some(ref assume_valid_targets) = config.network.sync.assume_valid_targets {
if let Some(first_target) = assume_valid_targets.first() {
if assume_valid_targets.len() == 1 {
if first_target
== &H256::from_slice(&[0; 32]).expect("must parse Zero h256 successful")
{
info!("Disable assume valid targets since assume_valid_targets is zero");
config.network.sync.assume_valid_targets = None;
} else {
info!(
"assume_valid_targets set to {:?}",
config.network.sync.assume_valid_targets
);
}
}
}
}

Expand Down
76 changes: 50 additions & 26 deletions util/constant/src/default_assume_valid_target.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,55 @@
/// The mod mainnet and mod testnet's codes are generated
/// by script: ./devtools/release/update_default_valid_target.sh
/// Please don't modify them manually.
///
/// Get default assume valid targets
/// sync config related to mainnet
/// mainnet
pub mod mainnet {
/// Default assume valid target for mainnet, expect to be a block 60 days ago.
///
/// Need to update when CKB's new release
/// in mainnet: the 60 days ago block is:
/// height: 13735790
/// hash: 0x1dc6ebf09bf066b6d4c6b9bf1ded8e4c692c55b14f98bff231a4cb26720412cd
/// date: Sun Aug 11 07:55:39 AM CST 2024
/// you can view this block in https://explorer.nervos.org/block/0x1dc6ebf09bf066b6d4c6b9bf1ded8e4c692c55b14f98bff231a4cb26720412cd
pub const DEFAULT_ASSUME_VALID_TARGET: &str =
"0x1dc6ebf09bf066b6d4c6b9bf1ded8e4c692c55b14f98bff231a4cb26720412cd";
use crate::latest_assume_valid_target;

/// get mainnet related default assume valid targets
pub fn default_assume_valid_targets() -> Vec<&'static str> {
vec![
//
// height: 500,000; https://explorer.nervos.org/block/0xb72f4d9758a36a2f9d4b8aea5a11d232e3e48332b76ec350f0a375fac10317a4
"0xb72f4d9758a36a2f9d4b8aea5a11d232e3e48332b76ec350f0a375fac10317a4",
//
// height: 1,000,000; https://explorer.nervos.org/block/0x7544e2a9db2054fbe42215ece2e5d31f175972cfeccaa7597c8ff3ec5c8b7d67
"0x7544e2a9db2054fbe42215ece2e5d31f175972cfeccaa7597c8ff3ec5c8b7d67",
//
// height: 2,000,000; https://explorer.nervos.org/block/0xc0c1ca7dcfa5862b9d2afeb5ea94db14744b8146c9005982879030f01e1f47cb
"0xc0c1ca7dcfa5862b9d2afeb5ea94db14744b8146c9005982879030f01e1f47cb",
//
// height: 4,000,000; https://explorer.nervos.org/block/0xcd925c9baa8c3110980546c916dad122dc69111780e49b50c3bb407ab7b6aa1c
"0xcd925c9baa8c3110980546c916dad122dc69111780e49b50c3bb407ab7b6aa1c",
//
// height: 8,000,000; https://explorer.nervos.org/block/0x063ccfcdbad01922792914f0bd61e47930bbb4a531f711013a24210638c0174a
"0x063ccfcdbad01922792914f0bd61e47930bbb4a531f711013a24210638c0174a",
latest_assume_valid_target::mainnet::DEFAULT_ASSUME_VALID_TARGET,
]
}
}
/// sync config related to testnet

/// testnet
pub mod testnet {
/// Default assume valid target for testnet, expect to be a block 60 days ago.
///
/// Need to update when CKB's new release
/// in testnet: the 60 days ago block is:
/// height: 14203467
/// hash: 0xa13450d53528d80fb5886f35386cf0119eea74cc63092c1138c38971416fe445
/// date: Sun Aug 11 07:56:19 AM CST 2024
/// you can view this block in https://pudge.explorer.nervos.org/block/0xa13450d53528d80fb5886f35386cf0119eea74cc63092c1138c38971416fe445
pub const DEFAULT_ASSUME_VALID_TARGET: &str =
"0xa13450d53528d80fb5886f35386cf0119eea74cc63092c1138c38971416fe445";
use crate::latest_assume_valid_target;

/// get testnet related default assume valid targets
pub fn default_assume_valid_targets() -> Vec<&'static str> {
vec![
//
// height: 500,000; https://pudge.explorer.nervos.org/block/0xf9c73f3db9a7c6707c3c6800a9a0dbd5a2edf69e3921832f65275dcd71f7871c
"0xf9c73f3db9a7c6707c3c6800a9a0dbd5a2edf69e3921832f65275dcd71f7871c",
//
// height: 1,000,000; https://pudge.explorer.nervos.org/block/0x935a48f2660fd141121114786edcf17ef5789c6c2fe7aca04ea27813b30e1fa3
"0x935a48f2660fd141121114786edcf17ef5789c6c2fe7aca04ea27813b30e1fa3",
//
// height: 2,000,000; https://pudge.explorer.nervos.org/block/0xf4d1648131b7bc4a0c9dbc442d240395c89a0c77b0cc197dce8794cd93669b32
"0xf4d1648131b7bc4a0c9dbc442d240395c89a0c77b0cc197dce8794cd93669b32",
//
// height: 4,000,000; https://pudge.explorer.nervos.org/block/0xb33c0e0a649003ab65062e93a3126a2235f6e7c3ca1b16fe9938816d846bb14f
"0xb33c0e0a649003ab65062e93a3126a2235f6e7c3ca1b16fe9938816d846bb14f",
//
// height: 8,000,000; https://pudge.explorer.nervos.org/block/0x2be0f327e78032f495f90da159883da84f2efd5025fde106a6a7590b8fca6647
"0x2be0f327e78032f495f90da159883da84f2efd5025fde106a6a7590b8fca6647",
latest_assume_valid_target::testnet::DEFAULT_ASSUME_VALID_TARGET,
]
}
}
31 changes: 31 additions & 0 deletions util/constant/src/latest_assume_valid_target.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/// The mod mainnet and mod testnet's codes are generated
/// by script: ./devtools/release/update_default_valid_target.sh
/// Please don't modify them manually.
///
/// sync config related to mainnet
pub mod mainnet {
/// Default assume valid target for mainnet, expect to be a block 60 days ago.
///
/// Need to update when CKB's new release
/// in mainnet: the 60 days ago block is:
/// height: 13735790
/// hash: 0x1dc6ebf09bf066b6d4c6b9bf1ded8e4c692c55b14f98bff231a4cb26720412cd
/// date: Sun Aug 11 07:55:39 AM CST 2024
/// you can view this block in https://explorer.nervos.org/block/0x1dc6ebf09bf066b6d4c6b9bf1ded8e4c692c55b14f98bff231a4cb26720412cd
pub const DEFAULT_ASSUME_VALID_TARGET: &str =
"0x1dc6ebf09bf066b6d4c6b9bf1ded8e4c692c55b14f98bff231a4cb26720412cd";
}
/// sync config related to testnet
pub mod testnet {
/// Default assume valid target for testnet, expect to be a block 60 days ago.
///
/// Need to update when CKB's new release
/// in testnet: the 60 days ago block is:
/// height: 14203467
/// hash: 0xa13450d53528d80fb5886f35386cf0119eea74cc63092c1138c38971416fe445
/// date: Sun Aug 11 07:56:19 AM CST 2024
/// you can view this block in https://pudge.explorer.nervos.org/block/0xa13450d53528d80fb5886f35386cf0119eea74cc63092c1138c38971416fe445
pub const DEFAULT_ASSUME_VALID_TARGET: &str =
"0xa13450d53528d80fb5886f35386cf0119eea74cc63092c1138c38971416fe445";
}
5 changes: 4 additions & 1 deletion util/constant/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@ pub mod store;
/// sync constant
pub mod sync;

/// hardcoded default assume valid target
/// default assume valid target
pub mod default_assume_valid_target;

/// hardcoded default assume valid target
pub mod latest_assume_valid_target;

0 comments on commit 3574ca2

Please sign in to comment.