Skip to content

Commit

Permalink
feat: mmr active through versionbits
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangsoledad committed Aug 8, 2022
1 parent 608b64b commit 5518873
Show file tree
Hide file tree
Showing 28 changed files with 850 additions and 479 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ members = [
"util/types",
"util/jsonrpc-types",
"freezer",
"block-filter",
"resource",
"pow",
"util/dao/utils",
Expand All @@ -69,6 +68,7 @@ members = [
"db-migration",
"util/network-alert",
"store",
"block-filter",
"util/chain-iter",
"util/test-chain-utils",
"util/dao",
Expand All @@ -84,6 +84,7 @@ members = [
"util/instrument",
"rpc",
"util/launcher/migration-template",
"util/light-client-protocol-server",
"util/launcher",
"ckb-bin"
]
Expand Down
277 changes: 141 additions & 136 deletions chain/src/chain.rs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions chain/src/tests/block_assembler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ fn test_block_template_message() {
.unwrap()
.unwrap();

let cellbase_witness = CellbaseWitness::from_slice(
let _cellbase_witness = CellbaseWitness::from_slice(
block_template
.cellbase
.data
Expand All @@ -184,7 +184,7 @@ fn test_block_template_message() {
)
.expect("should be valid CellbaseWitness slice");

assert_eq!("TEST".as_bytes(), cellbase_witness.message().raw_data());
// assert_eq!("TEST".as_bytes(), cellbase_witness.message().raw_data());
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions docs/hashes.toml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ index = 1

# Spec: ckb_staging
[ckb_staging]
spec_hash = "0x754a6a35c71e267e638697a7cfabd1338735dac0e164306d562aa813bc19833c"
spec_hash = "0x1e501d0048dbfee37707432fb5ec4c5e2b0404d887ebc882564c8761ef54e68a"
genesis = "0xbc081e6b2e31149c1dc39007f161ed0a0b63d5d30b3b771acc6a3b622133fcc0"
cellbase = "0x7295631c414e50a8d9bb73d9845231ac212d10404045c96de7f149ec874ac6b7"

Expand Down Expand Up @@ -134,7 +134,7 @@ index = 1

# Spec: ckb_dev
[ckb_dev]
spec_hash = "0x9b457fe9ba2600eed42bff4e15cbdde2d9a1175eb49fb46e4d793bb4dc4259f0"
spec_hash = "0x307bfa518f3dd6ffe6d82317c5ce7eb738db4623402edcc06b5adbee2e3a6b29"
genesis = "0x823b2ff5785b12da8b1363cac9a5cbe566d8b715a4311441b119c39a0367488c"
cellbase = "0xa563884b3686078ec7e7677a5f86449b15cf2693f3c1241766c6996f206cc541"

Expand Down
1 change: 0 additions & 1 deletion resource/specs/dev.toml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ rfc_0031 = 0
rfc_0032 = 0
rfc_0036 = 0
rfc_0038 = 0
rfc_tmp1 = 1

[pow]
func = "Dummy"
1 change: 0 additions & 1 deletion resource/specs/staging.toml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ rfc_0031 = 0
rfc_0032 = 0
rfc_0036 = 0
rfc_0038 = 0
rfc_tmp1 = 1

[pow]
func = "Eaglesong"
5 changes: 2 additions & 3 deletions rpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1398,8 +1398,7 @@ Response
{ "rfc": "0031", "epoch_number": "0x0" },
{ "rfc": "0032", "epoch_number": "0x0" },
{ "rfc": "0036", "epoch_number": "0x0" },
{ "rfc": "0038", "epoch_number": "0x0" },
{ "rfc": "tmp1", "epoch_number": null }
{ "rfc": "0038", "epoch_number": "0x0" }
],
"id": "main",
"initial_primary_epoch_reward": "0x71afd498d000",
Expand Down Expand Up @@ -2198,7 +2197,7 @@ Response
],
"version": "0x0",
"witnesses": [
"0x650000000c00000055000000490000001000000030000000310000001892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df20114000000b2e61ff569acf041b3c2c17724e2379c581eeac30c00000054455354206d657373616765"
"0x650000000c00000055000000490000001000000030000000310000001892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df20114000000b2e61ff569acf041b3c2c17724e2379c581eeac30c00000000000020206d657373616765"
]
},
"hash": "0xbaf7e4db2fd002f19a597ca1a31dfe8cfe26ed8cebc91f52b75b16a7a5ec8bab"
Expand Down
3 changes: 1 addition & 2 deletions rpc/src/module/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1133,8 +1133,7 @@ pub trait ChainRpc {
/// { "rfc": "0031", "epoch_number": "0x0" },
/// { "rfc": "0032", "epoch_number": "0x0" },
/// { "rfc": "0036", "epoch_number": "0x0" },
/// { "rfc": "0038", "epoch_number": "0x0" },
/// { "rfc": "tmp1", "epoch_number": null }
/// { "rfc": "0038", "epoch_number": "0x0" }
/// ],
/// "id": "main",
/// "initial_primary_epoch_reward": "0x71afd498d000",
Expand Down
2 changes: 1 addition & 1 deletion rpc/src/module/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub trait MinerRpc {
/// ],
/// "version": "0x0",
/// "witnesses": [
/// "0x650000000c00000055000000490000001000000030000000310000001892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df20114000000b2e61ff569acf041b3c2c17724e2379c581eeac30c00000054455354206d657373616765"
/// "0x650000000c00000055000000490000001000000030000000310000001892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df20114000000b2e61ff569acf041b3c2c17724e2379c581eeac30c00000000000020206d657373616765"
/// ]
/// },
/// "hash": "0xbaf7e4db2fd002f19a597ca1a31dfe8cfe26ed8cebc91f52b75b16a7a5ec8bab"
Expand Down
31 changes: 22 additions & 9 deletions spec/src/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
use crate::{
calculate_block_reward,
versionbits::{
self, Deployment, DeploymentPos, VersionBits, VersionBitsCache,
VersionBitsConditionChecker, VersionBitsIndexer,
self, Deployment, DeploymentPos, ThresholdState, Versionbits, VersionbitsCache,
VersionbitsConditionChecker, VersionbitsIndexer,
},
OUTPUT_INDEX_DAO, OUTPUT_INDEX_SECP256K1_BLAKE160_MULTISIG_ALL,
OUTPUT_INDEX_SECP256K1_BLAKE160_SIGHASH_ALL,
Expand Down Expand Up @@ -92,7 +92,7 @@ pub(crate) const SATOSHI_PUBKEY_HASH: H160 = h160!("0x62e907b15cbf27d5425399ebf6
// only affects genesis cellbase's satoshi lock cells.
pub(crate) const SATOSHI_CELL_OCCUPIED_RATIO: Ratio = Ratio::new(6, 10);

pub(crate) const MAINNET_ACTIVATION_THRESHOLD: Ratio = Ratio::new(9, 10);
// pub(crate) const MAINNET_ACTIVATION_THRESHOLD: Ratio = Ratio::new(9, 10);
pub(crate) const TESTNET_ACTIVATION_THRESHOLD: Ratio = Ratio::new(3, 4);

/// The struct represent CKB two-step-transaction-confirmation params
Expand Down Expand Up @@ -284,7 +284,7 @@ impl ConsensusBuilder {
permanent_difficulty_in_dummy: false,
hardfork_switch: HardForkSwitch::new_mirana(),
deployments: HashMap::new(),
versionbits_caches: VersionBitsCache::default(),
versionbits_caches: VersionbitsCache::default(),
},
}
}
Expand Down Expand Up @@ -475,7 +475,7 @@ impl ConsensusBuilder {

/// Sets a soft fork deployments for the new Consensus.
pub fn softfork_deployments(mut self, deployments: HashMap<DeploymentPos, Deployment>) -> Self {
self.inner.versionbits_caches = VersionBitsCache::new(deployments.keys());
self.inner.versionbits_caches = VersionbitsCache::new(deployments.keys());
self.inner.deployments = deployments;
self
}
Expand Down Expand Up @@ -558,7 +558,7 @@ pub struct Consensus {
/// Soft fork deployments
pub deployments: HashMap<DeploymentPos, Deployment>,
/// Soft fork state cache
pub versionbits_caches: VersionBitsCache,
pub versionbits_caches: VersionbitsCache,
}

// genesis difficulty should not be zero
Expand Down Expand Up @@ -955,15 +955,16 @@ impl Consensus {
&self.hardfork_switch
}

pub fn compute_versionbits<I: VersionBitsIndexer>(
/// Returns what version a new block should use.
pub fn compute_versionbits<I: VersionbitsIndexer>(
&self,
parent: &HeaderView,
indexer: &I,
) -> Option<Version> {
let mut version = versionbits::VERSIONBITS_TOP_BITS;
for pos in self.deployments.keys() {
let versionbits = VersionBits::new(*pos, self);
let cache = self.versionbits_caches.cache(pos);
let versionbits = Versionbits::new(*pos, self);
let cache = self.versionbits_caches.cache(pos)?;
let state = versionbits.get_state(parent, cache, indexer)?;
if state == versionbits::ThresholdState::LockedIn
|| state == versionbits::ThresholdState::Started
Expand All @@ -974,6 +975,18 @@ impl Consensus {
Some(version)
}

/// Returns specified softfork deployment state
pub fn versionbits_state<I: VersionbitsIndexer>(
&self,
pos: DeploymentPos,
parent: &HeaderView,
indexer: &I,
) -> Option<ThresholdState> {
let cache = self.versionbits_caches.cache(&pos)?;
let versionbits = Versionbits::new(pos, self);
versionbits.get_state(parent, cache, indexer)
}

/// If the CKB block chain specification is for an public chain.
pub fn is_public_chain(&self) -> bool {
matches!(
Expand Down
15 changes: 1 addition & 14 deletions spec/src/hardfork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ pub struct HardForkConfig {
/// Ref: CKB RFC 0038
#[serde(skip_serializing_if = "Option::is_none")]
pub rfc_0038: Option<EpochNumber>,
/// TODO(light-client) update the description
pub rfc_tmp1: Option<EpochNumber>,
}

macro_rules! check_default {
Expand All @@ -85,7 +83,6 @@ impl HardForkConfig {
b,
mainnet::CKB2021_START_EPOCH,
mainnet::RFC0028_START_EPOCH,
mainnet::RFCTMP1_START_EPOCH,
)?;
b.build()
}
Expand All @@ -98,7 +95,6 @@ impl HardForkConfig {
b,
testnet::CKB2021_START_EPOCH,
testnet::RFC0028_START_EPOCH,
testnet::RFCTMP1_START_EPOCH,
)?;
b.build()
}
Expand All @@ -108,7 +104,6 @@ impl HardForkConfig {
builder: HardForkSwitchBuilder,
ckb2021: EpochNumber,
rfc_0028_start: EpochNumber,
rfc_tmp1_start: EpochNumber,
) -> Result<HardForkSwitchBuilder, String> {
let builder = builder
.rfc_0028(check_default!(self, rfc_0028, rfc_0028_start))
Expand All @@ -117,21 +112,14 @@ impl HardForkConfig {
.rfc_0031(check_default!(self, rfc_0031, ckb2021))
.rfc_0032(check_default!(self, rfc_0032, ckb2021))
.rfc_0036(check_default!(self, rfc_0036, ckb2021))
.rfc_0038(check_default!(self, rfc_0038, ckb2021))
.rfc_tmp1(check_default!(self, rfc_tmp1, rfc_tmp1_start));
.rfc_0038(check_default!(self, rfc_0038, ckb2021));
Ok(builder)
}

/// Converts to a hard fork switch.
///
/// Enable features which are set to `None` at the user provided epoch.
pub fn complete_with_default(&self, default: EpochNumber) -> Result<HardForkSwitch, String> {
if self.rfc_tmp1.map(|v| v == 0).unwrap_or(false) {
let errmsg = "Found the hard fork feature parameter \"rfc_tmp1\" is \
in the chain specification file, and its value is 0.
But it should NOT be 0 since genesis block doesn't has chain root.";
return Err(errmsg.to_string());
}
HardForkSwitch::new_builder()
.rfc_0028(self.rfc_0028.unwrap_or(default))
.rfc_0029(self.rfc_0029.unwrap_or(default))
Expand All @@ -140,7 +128,6 @@ impl HardForkConfig {
.rfc_0032(self.rfc_0032.unwrap_or(default))
.rfc_0036(self.rfc_0036.unwrap_or(default))
.rfc_0038(self.rfc_0038.unwrap_or(default))
.rfc_tmp1(self.rfc_tmp1.unwrap_or(default))
.build()
}
}
15 changes: 14 additions & 1 deletion spec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,20 @@ impl ChainSpec {
deployments.insert(DeploymentPos::LightClient, light_client);
Some(deployments)
}
_ => None,
_ => {
let mut deployments = HashMap::new();
let light_client = Deployment {
bit: 1,
start: 0,
timeout: 0,
min_activation_epoch: 0,
period: 10,
active_mode: ActiveMode::Always,
threshold: TESTNET_ACTIVATION_THRESHOLD,
};
deployments.insert(DeploymentPos::LightClient, light_client);
Some(deployments)
}
}
}

Expand Down
1 change: 1 addition & 0 deletions spec/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::collections::HashMap;
use crate::{build_genesis_epoch_ext, ChainSpec, Params};

mod consensus;
mod versionbits;

#[derive(Clone, Debug, Serialize, Deserialize)]
struct SystemCell {
Expand Down
Loading

0 comments on commit 5518873

Please sign in to comment.