Skip to content

Commit 0cef731

Browse files
Merge #3648
3648: chore(light-client): update according to the RFC changes r=zhangsoledad a=yangby-cryptape ### What problem does this PR solve? - chore(light-client): update according to the RFC changes - chore(light-client): rename the "hash" filed in HeaderDigest to "children_hash" Ref: nervosnetwork/rfcs#370 (comment) - docs(light-client): remove outdated documentation and link to the related RFC - chore(deps): bump ckb-merkle-mountain-range from 0.5.1 to 0.5.2 ### Check List Tests - Unit test - Integration test - Manual test (add detailed scripts or steps below) - No code ci-runs-only: [ quick_checks,linters ] Side effects: None ### Release note ```release-note None: Exclude this PR from the release note. ``` Co-authored-by: Boyu Yang <[email protected]>
2 parents 5288e8a + 39ff541 commit 0cef731

File tree

10 files changed

+21
-101
lines changed

10 files changed

+21
-101
lines changed

Cargo.lock

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

chain/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ ckb-app-config = { path = "../util/app-config", version = "= 0.105.0-pre" }
2727
ckb-rust-unstable-port = { path = "../util/rust-unstable-port", version = "= 0.105.0-pre" }
2828
ckb-channel = { path = "../util/channel", version = "= 0.105.0-pre" }
2929
faux = { version = "^0.1", optional = true }
30-
ckb-merkle-mountain-range = "0.5.1"
30+
ckb-merkle-mountain-range = "0.5.2"
3131

3232
[dev-dependencies]
3333
ckb-test-chain-utils = { path = "../util/test-chain-utils", version = "= 0.105.0-pre" }

store/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ ckb-error = { path = "../error", version = "= 0.105.0-pre" }
1919
ckb-app-config = { path = "../util/app-config", version = "= 0.105.0-pre" }
2020
ckb-db-schema = { path = "../db-schema", version = "= 0.105.0-pre" }
2121
ckb-freezer = { path = "../freezer", version = "= 0.105.0-pre" }
22-
ckb-merkle-mountain-range = "0.5.1"
22+
ckb-merkle-mountain-range = "0.5.2"
2323

2424
[dev-dependencies]
2525
tempfile = "3.0"

util/light-client-protocol-server/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ ckb-shared = { path = "../../shared", version = "= 0.105.0-pre" }
1515
ckb-logger = { path = "../logger", version = "= 0.105.0-pre" }
1616
ckb-types = { path = "../types", version = "= 0.105.0-pre" }
1717
ckb-store = { path = "../../store", version = "= 0.105.0-pre" }
18-
ckb-merkle-mountain-range = "0.5.1"
18+
ckb-merkle-mountain-range = "0.5.2"
1919

2020
[dev-dependencies]
2121
ckb-chain = { path = "../../chain", version = "= 0.105.0-pre" }

util/snapshot/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ ckb-proposal-table = { path = "../proposal-table", version = "= 0.105.0-pre" }
2020
arc-swap = "1.3"
2121
ckb-db-schema = { path = "../../db-schema", version = "= 0.105.0-pre" }
2222
ckb-freezer = { path = "../../freezer", version = "= 0.105.0-pre" }
23-
ckb-merkle-mountain-range = "0.5.1"
23+
ckb-merkle-mountain-range = "0.5.2"
2424

2525
[features]
2626
portable = ["ckb-db/portable", "ckb-store/portable"]

util/types/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ ckb-error = { path = "../../error", version = "= 0.105.0-pre" }
2222
ckb-rational = { path = "../rational", version = "= 0.105.0-pre" }
2323
once_cell = "1.8.0"
2424
derive_more = { version = "0.99.0", default-features=false, features = ["display"] }
25-
ckb-merkle-mountain-range = "0.5.1"
25+
ckb-merkle-mountain-range = "0.5.2"
2626

2727
[dev-dependencies]
2828
proptest = "1.0"

util/types/schemas/extensions.mol

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ vector OutPointVec <OutPoint>;
2626
/* Types for Light Client */
2727

2828
struct HeaderDigest {
29-
hash: Byte32,
29+
children_hash: Byte32,
3030

3131
total_difficulty: Uint256,
3232

util/types/src/generated/extensions.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -2490,7 +2490,7 @@ impl ::core::fmt::Debug for HeaderDigest {
24902490
impl ::core::fmt::Display for HeaderDigest {
24912491
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
24922492
write!(f, "{} {{ ", Self::NAME)?;
2493-
write!(f, "{}: {}", "hash", self.hash())?;
2493+
write!(f, "{}: {}", "children_hash", self.children_hash())?;
24942494
write!(f, ", {}: {}", "total_difficulty", self.total_difficulty())?;
24952495
write!(f, ", {}: {}", "start_number", self.start_number())?;
24962496
write!(f, ", {}: {}", "end_number", self.end_number())?;
@@ -2529,7 +2529,7 @@ impl HeaderDigest {
25292529
pub const TOTAL_SIZE: usize = 120;
25302530
pub const FIELD_SIZES: [usize; 10] = [32, 32, 8, 8, 8, 8, 8, 8, 4, 4];
25312531
pub const FIELD_COUNT: usize = 10;
2532-
pub fn hash(&self) -> Byte32 {
2532+
pub fn children_hash(&self) -> Byte32 {
25332533
Byte32::new_unchecked(self.0.slice(0..32))
25342534
}
25352535
pub fn total_difficulty(&self) -> Uint256 {
@@ -2586,7 +2586,7 @@ impl molecule::prelude::Entity for HeaderDigest {
25862586
}
25872587
fn as_builder(self) -> Self::Builder {
25882588
Self::new_builder()
2589-
.hash(self.hash())
2589+
.children_hash(self.children_hash())
25902590
.total_difficulty(self.total_difficulty())
25912591
.start_number(self.start_number())
25922592
.end_number(self.end_number())
@@ -2617,7 +2617,7 @@ impl<'r> ::core::fmt::Debug for HeaderDigestReader<'r> {
26172617
impl<'r> ::core::fmt::Display for HeaderDigestReader<'r> {
26182618
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
26192619
write!(f, "{} {{ ", Self::NAME)?;
2620-
write!(f, "{}: {}", "hash", self.hash())?;
2620+
write!(f, "{}: {}", "children_hash", self.children_hash())?;
26212621
write!(f, ", {}: {}", "total_difficulty", self.total_difficulty())?;
26222622
write!(f, ", {}: {}", "start_number", self.start_number())?;
26232623
write!(f, ", {}: {}", "end_number", self.end_number())?;
@@ -2644,7 +2644,7 @@ impl<'r> HeaderDigestReader<'r> {
26442644
pub const TOTAL_SIZE: usize = 120;
26452645
pub const FIELD_SIZES: [usize; 10] = [32, 32, 8, 8, 8, 8, 8, 8, 4, 4];
26462646
pub const FIELD_COUNT: usize = 10;
2647-
pub fn hash(&self) -> Byte32Reader<'r> {
2647+
pub fn children_hash(&self) -> Byte32Reader<'r> {
26482648
Byte32Reader::new_unchecked(&self.as_slice()[0..32])
26492649
}
26502650
pub fn total_difficulty(&self) -> Uint256Reader<'r> {
@@ -2698,7 +2698,7 @@ impl<'r> molecule::prelude::Reader<'r> for HeaderDigestReader<'r> {
26982698
}
26992699
#[derive(Debug, Default)]
27002700
pub struct HeaderDigestBuilder {
2701-
pub(crate) hash: Byte32,
2701+
pub(crate) children_hash: Byte32,
27022702
pub(crate) total_difficulty: Uint256,
27032703
pub(crate) start_number: Uint64,
27042704
pub(crate) end_number: Uint64,
@@ -2713,8 +2713,8 @@ impl HeaderDigestBuilder {
27132713
pub const TOTAL_SIZE: usize = 120;
27142714
pub const FIELD_SIZES: [usize; 10] = [32, 32, 8, 8, 8, 8, 8, 8, 4, 4];
27152715
pub const FIELD_COUNT: usize = 10;
2716-
pub fn hash(mut self, v: Byte32) -> Self {
2717-
self.hash = v;
2716+
pub fn children_hash(mut self, v: Byte32) -> Self {
2717+
self.children_hash = v;
27182718
self
27192719
}
27202720
pub fn total_difficulty(mut self, v: Uint256) -> Self {
@@ -2761,7 +2761,7 @@ impl molecule::prelude::Builder for HeaderDigestBuilder {
27612761
Self::TOTAL_SIZE
27622762
}
27632763
fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
2764-
writer.write_all(self.hash.as_slice())?;
2764+
writer.write_all(self.children_hash.as_slice())?;
27652765
writer.write_all(self.total_difficulty.as_slice())?;
27662766
writer.write_all(self.start_number.as_slice())?;
27672767
writer.write_all(self.end_number.as_slice())?;

util/types/src/utilities/merkle_mountain_range.rs

+3-83
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,8 @@
11
//! Types for variable difficulty Merkle Mountain Range (MMR) in CKB.
22
//!
3-
//! Since CKB doesn't record MMR data in headers since the genesis block, we use an activation
4-
//! block number to enable MMR and create all MMR nodes before that block to make sure that the
5-
//! index of MMR leaf is EQUAL to the block number.
6-
//!
7-
//! ```text
8-
//! height position
9-
//!
10-
//! 3 14
11-
//! / \
12-
//! / \
13-
//! / \
14-
//! / \
15-
//! / \
16-
//! / \
17-
//! 2 6 13
18-
//! / \ / \
19-
//! / \ / \
20-
//! / \ / \
21-
//! 1 2 5 9 12 17
22-
//! / \ / \ / \ / \ / \
23-
//! 0 0 1 3 4 7 8 10 11 15 16 18
24-
//! --------------------------------------------------------
25-
//! index 0 1 2 3 4 5 6 7 8 9 10 ... N
26-
//! --------------------------------------------------------
27-
//! number 0 1 2 3 4 5 6 7 8 9 10 ... N
28-
//! --------------------------------------------------------
29-
//! ```
30-
//!
31-
//! - `height`: the MMR node height.
32-
//! - `position`: the MMR node position.
33-
//! - `index`: the MMR leaf index; same as the block height.
34-
//! - `number`: the block height.
35-
//! - `N`: the activation block number; the block number of the last block which doesn't records MMR root hash.
36-
//!
37-
//! There are three kind of blocks base on its MMR data:
38-
//!
39-
//! - The genesis block
40-
//!
41-
//! First node, also first leaf node in MMR; no chain root;
42-
//!
43-
//! - The blocks which height is less than `N`
44-
//!
45-
//! No chain root in blocks but store them in database.
46-
//!
47-
//! - The blocks which height is equal to or greater than `N`
48-
//!
49-
//! Has chain root in blocks.
50-
//!
51-
//! There are two kinds of MMR nodes: leaf node and non-leaf node.
52-
//!
53-
//! Each MMR node is defined as follows:
54-
//!
55-
//! - `hash`
56-
//!
57-
//! - For leaf node, it's an empty hash (`0x0000...0000`).
58-
//!
59-
//! - For non-leaf node, it's the hash of it's child nodes' hashes (concatenate serialized data).
60-
//!
61-
//! - `total_difficulty`
62-
//!
63-
//! - For leaf node, it's the difficulty it took to mine the current block.
64-
//!
65-
//! - For non-leaf node, it's the sum of `total_difficulty` in it's child nodes.
66-
//!
67-
//! - `start_*`
68-
//!
69-
//! Such as `start_number`, `start_epoch`, `start_timestamp`, `start_compact_target`.
70-
//!
71-
//! - For leaf node, it's the data of current block.
72-
//!
73-
//! - For non-leaf node, it's the `start_*` of left node.
74-
//!
75-
//! - `end_*`
76-
//!
77-
//! Such as `end_number`, `end_epoch`, `end_timestamp`, `end_compact_target`.
78-
//!
79-
//! - For leaf node, it's the data of current block.
80-
//!
81-
//! - For non-leaf node, it's the `end_*` of right node.
82-
//!
833
//! ## References
844
//!
85-
//! - [Peter Todd, Merkle mountain range.](https://github.com/opentimestamps/opentimestamps-server/blob/master/doc/merkle-mountain-range.md).
5+
//! - [CKB RFC 0044](https://github.com/nervosnetwork/rfcs/blob/master/rfcs/0044-ckb-light-client/0044-ckb-light-client.md)
866
877
use ckb_hash::new_blake2b;
888
use ckb_merkle_mountain_range::{Error as MMRError, Merge, MerkleProof, Result as MMRResult, MMR};
@@ -211,7 +131,7 @@ impl Merge for MergeHeaderDigest {
211131
type Item = packed::HeaderDigest;
212132

213133
fn merge(lhs: &Self::Item, rhs: &Self::Item) -> MMRResult<Self::Item> {
214-
let hash = {
134+
let children_hash = {
215135
let mut hasher = new_blake2b();
216136
let mut hash = [0u8; 32];
217137
hasher.update(&lhs.calc_mmr_hash().raw_data());
@@ -249,7 +169,7 @@ impl Merge for MergeHeaderDigest {
249169
}
250170

251171
Ok(Self::Item::new_builder()
252-
.hash(hash.pack())
172+
.children_hash(children_hash.pack())
253173
.total_difficulty(total_difficulty.pack())
254174
.start_number(lhs.start_number())
255175
.start_epoch(lhs.start_epoch())

verification/contextual/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ tokio = { version = "1", features = ["sync", "rt-multi-thread"] }
2424
ckb-async-runtime = { path = "../../util/runtime", version = "= 0.105.0-pre" }
2525
ckb-verification-traits = { path = "../traits", version = "= 0.105.0-pre" }
2626
ckb-verification = { path = "..", version = "= 0.105.0-pre" }
27-
ckb-merkle-mountain-range = "0.5.1"
27+
ckb-merkle-mountain-range = "0.5.2"
2828

2929
[dev-dependencies]
3030
ckb-chain = { path = "../../chain", version = "= 0.105.0-pre" }

0 commit comments

Comments
 (0)