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

backport 4126 4127 #4131

Merged
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion docs/hashes.toml
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ tx_hash = "0xd5780747735fd22c9ba7363bde8afe59061658caa836962867253b03cbda264e"
index = 1

[ckb_dev]
spec_hash = "0x6cb679a15a7ff16596cad85f4680ab4335c87bcbf43956c591c23d11841719e4"
spec_hash = "0xab85c78cf9641a7709f9d48ba06415c283050888c9fb927d743bc521aa1ddfec"
genesis = "0x823b2ff5785b12da8b1363cac9a5cbe566d8b715a4311441b119c39a0367488c"
cellbase = "0xa563884b3686078ec7e7677a5f86449b15cf2693f3c1241766c6996f206cc541"

Expand Down
29 changes: 24 additions & 5 deletions miner/src/worker/dummy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use indicatif::ProgressBar;
use rand::thread_rng;
use rand_distr::{self as dist, Distribution as _};
use std::thread;
use std::time::Duration;
use std::time::{Duration, Instant};

pub struct Dummy {
delay: Delay,
Expand Down Expand Up @@ -94,10 +94,29 @@ impl Dummy {
}
}

fn solve(&self, pow_hash: Byte32, work: Work, nonce: u128) {
thread::sleep(self.delay.duration());
if let Err(err) = self.nonce_tx.send((pow_hash, work, nonce)) {
error!("nonce_tx send error {:?}", err);
fn solve(&mut self, mut pow_hash: Byte32, mut work: Work, nonce: u128) {
let instant = Instant::now();
let delay = self.delay.duration();
loop {
thread::sleep(Duration::from_millis(10));
if instant.elapsed() > delay {
if let Err(err) = self.nonce_tx.send((pow_hash, work, nonce)) {
error!("nonce_tx send error {:?}", err);
}
return;
}
// if there is new work and pow_hash changed, start working on the new one
if let Ok(WorkerMessage::NewWork {
pow_hash: new_pow_hash,
work: new_work,
..
}) = self.worker_rx.try_recv()
{
if new_pow_hash != pow_hash {
pow_hash = new_pow_hash;
work = new_work;
}
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions resource/specs/dev.toml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ genesis_epoch_length = 1000
# For development and testing purposes only.
# Keep difficulty be permanent if the pow is Dummy. (default: false)
permanent_difficulty_in_dummy = true
starting_block_limiting_dao_withdrawing_lock = 0

[params.hardfork]
ckb2023 = 0
Expand Down
2 changes: 2 additions & 0 deletions script/src/syscalls/spawn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ where
.add_cycles_no_checking(transferred_byte_cycles(size))?;
}
Err(_) => {
// If loading binary fails, we still need to consume extra_cycles.
machine.add_cycles_no_checking(extra_cycles)?;
machine.set_register(A0, Mac::REG::from_u8(WRONG_FORMAT));
return Ok(true);
}
Expand Down
25 changes: 25 additions & 0 deletions spec/src/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ pub(crate) const SATOSHI_CELL_OCCUPIED_RATIO: Ratio = Ratio::new(6, 10);
pub(crate) const LC_MAINNET_ACTIVATION_THRESHOLD: Ratio = Ratio::new(8, 10);
pub(crate) const TESTNET_ACTIVATION_THRESHOLD: Ratio = Ratio::new(3, 4);

/// The starting block number from which the lock script size of a DAO withdrawing
/// cell shall be limited
pub(crate) const STARTING_BLOCK_LIMITING_DAO_WITHDRAWING_LOCK: u64 = 10_000_000;

/// The struct represent CKB two-step-transaction-confirmation params
///
/// [two-step-transaction-confirmation params](https://github.com/nervosnetwork/rfcs/blob/master/rfcs/0020-ckb-consensus-protocol/0020-ckb-consensus-protocol.md#two-step-transaction-confirmation)
Expand Down Expand Up @@ -289,6 +293,8 @@ impl ConsensusBuilder {
hardfork_switch: HardForks::new_mirana(),
deployments: HashMap::new(),
versionbits_caches: VersionbitsCache::default(),
starting_block_limiting_dao_withdrawing_lock:
STARTING_BLOCK_LIMITING_DAO_WITHDRAWING_LOCK,
},
}
}
Expand Down Expand Up @@ -483,6 +489,17 @@ impl ConsensusBuilder {
self.inner.deployments = deployments;
self
}

///The starting block number where Nervos DAO withdrawing cell's lock is
/// size limited.
pub fn starting_block_limiting_dao_withdrawing_lock(
mut self,
starting_block_limiting_dao_withdrawing_lock: u64,
) -> Self {
self.inner.starting_block_limiting_dao_withdrawing_lock =
starting_block_limiting_dao_withdrawing_lock;
self
}
}

/// Struct Consensus defines various parameters that influence chain consensus
Expand Down Expand Up @@ -563,6 +580,8 @@ pub struct Consensus {
pub deployments: HashMap<DeploymentPos, Deployment>,
/// Soft fork state cache
pub versionbits_caches: VersionbitsCache,
/// Starting block where DAO withdrawing lock is limited in size
pub starting_block_limiting_dao_withdrawing_lock: u64,
}

// genesis difficulty should not be zero
Expand Down Expand Up @@ -741,6 +760,12 @@ impl Consensus {
self.tx_proposal_window
}

/// The starting block number where Nervos DAO withdrawing cell's lock is
/// size limited.
pub fn starting_block_limiting_dao_withdrawing_lock(&self) -> u64 {
self.starting_block_limiting_dao_withdrawing_lock
}

// Apply the dampening filter on hash_rate estimation calculate
fn bounding_hash_rate(
&self,
Expand Down
23 changes: 22 additions & 1 deletion spec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub mod default_params {
CELLBASE_MATURITY, DEFAULT_EPOCH_DURATION_TARGET, DEFAULT_ORPHAN_RATE_TARGET,
DEFAULT_PRIMARY_EPOCH_REWARD_HALVING_INTERVAL, DEFAULT_SECONDARY_EPOCH_REWARD,
GENESIS_EPOCH_LENGTH, INITIAL_PRIMARY_EPOCH_REWARD, MAX_BLOCK_BYTES, MAX_BLOCK_CYCLES,
MAX_BLOCK_PROPOSALS_LIMIT,
MAX_BLOCK_PROPOSALS_LIMIT, STARTING_BLOCK_LIMITING_DAO_WITHDRAWING_LOCK,
};
use ckb_types::core::{Capacity, Cycle, EpochNumber};

Expand Down Expand Up @@ -170,6 +170,13 @@ pub mod default_params {
pub fn orphan_rate_target() -> (u32, u32) {
DEFAULT_ORPHAN_RATE_TARGET
}

/// The default starting_block_limiting_dao_withdrawing_lock
///
/// Apply to [`starting_block_limiting_dao_withdrawing_lock`](../consensus/struct.Consensus.html#structfield.starting_block_limiting_dao_withdrawing_lock)
pub fn starting_block_limiting_dao_withdrawing_lock() -> u64 {
STARTING_BLOCK_LIMITING_DAO_WITHDRAWING_LOCK
}
}

/// Parameters for CKB block chain
Expand Down Expand Up @@ -231,6 +238,11 @@ pub struct Params {
/// See [`orphan_rate_target`](consensus/struct.Consensus.html#structfield.orphan_rate_target)
#[serde(skip_serializing_if = "Option::is_none")]
pub orphan_rate_target: Option<(u32, u32)>,
/// The starting_block_limiting_dao_withdrawing_lock.
///
/// See [`starting_block_limiting_dao_withdrawing_lock`](consensus/struct.Consensus.html#structfield.starting_block_limiting_dao_withdrawing_lock)
#[serde(skip_serializing_if = "Option::is_none")]
pub starting_block_limiting_dao_withdrawing_lock: Option<u64>,
/// The parameters for hard fork features.
///
/// See [`hardfork_switch`](consensus/struct.Consensus.html#structfield.hardfork_switch)
Expand Down Expand Up @@ -304,6 +316,12 @@ impl Params {
self.orphan_rate_target
.unwrap_or_else(default_params::orphan_rate_target)
}

/// Return the `starting_block_limiting_dao_withdrawing_lock`, otherwise if None, returns the default value
pub fn starting_block_limiting_dao_withdrawing_lock(&self) -> u64 {
self.starting_block_limiting_dao_withdrawing_lock
.unwrap_or_else(default_params::starting_block_limiting_dao_withdrawing_lock)
}
}

/// The genesis information
Expand Down Expand Up @@ -585,6 +603,9 @@ impl ChainSpec {
.permanent_difficulty_in_dummy(self.params.permanent_difficulty_in_dummy())
.max_block_proposals_limit(self.params.max_block_proposals_limit())
.orphan_rate_target(self.params.orphan_rate_target())
.starting_block_limiting_dao_withdrawing_lock(
self.params.starting_block_limiting_dao_withdrawing_lock(),
)
.hardfork_switch(hardfork_switch);

if let Some(deployments) = self.softfork_deployments() {
Expand Down
31 changes: 31 additions & 0 deletions spec/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,18 @@ fn test_default_params() {
};

assert_eq!(params, expected);

let test_params: &str = r#"
starting_block_limiting_dao_withdrawing_lock = 77
"#;

let params: Params = toml::from_str(test_params).unwrap();
let expected = Params {
starting_block_limiting_dao_withdrawing_lock: Some(77),
..Default::default()
};

assert_eq!(params, expected);
}

#[test]
Expand Down Expand Up @@ -256,3 +268,22 @@ fn test_default_genesis_epoch_ext() {

assert_eq!(genesis_epoch_ext, expected);
}

#[test]
fn test_devnet_limits_dao_withdrawing_lock_from_genesis() {
let chain_spec = load_spec_by_name("ckb_dev");
let consensus = chain_spec.build_consensus().unwrap();

assert_eq!(consensus.starting_block_limiting_dao_withdrawing_lock(), 0);
}

#[test]
fn test_mainnet_limits_dao_withdrawing_lock_from_10000000() {
let chain_spec = load_spec_by_name("ckb");
let consensus = chain_spec.build_consensus().unwrap();

assert_eq!(
consensus.starting_block_limiting_dao_withdrawing_lock(),
10000000
);
}
2 changes: 1 addition & 1 deletion tx-pool/src/chunk_process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ impl ChunkProcess {
.and_then(|result| {
DaoScriptSizeVerifier::new(
Arc::clone(&rtx),
consensus.dao_type_hash(),
Arc::clone(&consensus),
data_loader.clone(),
)
.verify()?;
Expand Down
2 changes: 1 addition & 1 deletion tx-pool/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ impl TxPoolService {
ScriptVerifyResult::Completed(cycles) => {
if let Err(e) = DaoScriptSizeVerifier::new(
Arc::clone(&rtx),
self.consensus.dao_type_hash(),
Arc::clone(&self.consensus),
snapshot.as_data_loader(),
)
.verify()
Expand Down
4 changes: 2 additions & 2 deletions tx-pool/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ pub(crate) fn verify_rtx(
.and_then(|result| {
DaoScriptSizeVerifier::new(
rtx,
snapshot.cloned_consensus().dao_type_hash(),
snapshot.cloned_consensus(),
snapshot.as_data_loader(),
)
.verify()?;
Expand All @@ -122,7 +122,7 @@ pub(crate) fn verify_rtx(
.and_then(|result| {
DaoScriptSizeVerifier::new(
rtx,
snapshot.cloned_consensus().dao_type_hash(),
snapshot.cloned_consensus(),
snapshot.as_data_loader(),
)
.verify()?;
Expand Down
2 changes: 1 addition & 1 deletion verification/contextual/src/contextual_block_verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ impl<'a, 'b, CS: ChainStore + VersionbitsIndexer + 'static> BlockTxsVerifier<'a,
if self.context.versionbits_active(DeploymentPos::LightClient, self.parent) {
DaoScriptSizeVerifier::new(
Arc::clone(tx),
self.context.consensus.dao_type_hash(),
Arc::clone(&self.context.consensus),
self.context.store.as_data_loader(),
).verify()?;
}
Expand Down
Loading