Skip to content

Commit 324488c

Browse files
zhangsoledaddoitian
authored andcommitted
feat: add uncles_count to Header
1 parent 52441df commit 324488c

21 files changed

Lines changed: 218 additions & 69 deletions

File tree

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

chain/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ log = "0.4"
1212
ckb-core = { path = "../core" }
1313
ckb-shared = { path = "../shared" }
1414
ckb-chain-spec = { path = "../spec" }
15-
ckb-util = { path = "../util" }
1615
ckb-db = { path = "../db" }
1716
ckb-time = { path = "../util/time" }
1817
ckb-notify = { path = "../notify" }

chain/src/chain.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use std::cmp;
1616
use std::sync::Arc;
1717
use std::thread::{self, JoinHandle};
1818
use time::now_ms;
19-
use util::RwLockUpgradableReadGuard;
2019
use verification::{BlockVerifier, Verifier};
2120

2221
pub struct ChainService<CI> {
@@ -136,7 +135,7 @@ impl<CI: ChainIndex + 'static> ChainService<CI> {
136135
let mut old_cumulative_blks = Vec::new();
137136
let mut new_cumulative_blks = Vec::new();
138137

139-
let tip_header = self.shared.tip_header().upgradable_read();
138+
let mut tip_header = self.shared.tip_header().write();
140139
let tip_number = tip_header.number();
141140
self.shared.store().save_with_batch(|batch| {
142141
let root = self.check_transactions(batch, block)?;
@@ -180,7 +179,6 @@ impl<CI: ChainIndex + 'static> ChainService<CI> {
180179

181180
if new_best_block {
182181
debug!(target: "chain", "update index");
183-
let mut guard = RwLockUpgradableReadGuard::upgrade(tip_header);
184182
let new_tip_header =
185183
TipHeader::new(block.header().clone(), total_difficulty, output_root);
186184
self.shared.store().save_with_batch(|batch| {
@@ -197,7 +195,7 @@ impl<CI: ChainIndex + 'static> ChainService<CI> {
197195
self.shared.store().rebuild_tree(output_root);
198196
Ok(())
199197
})?;
200-
*guard = new_tip_header;
198+
*tip_header = new_tip_header;
201199
debug!(target: "chain", "update index release");
202200
}
203201

chain/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ extern crate ckb_verification as verification;
1919
extern crate log;
2020
#[macro_use]
2121
extern crate crossbeam_channel as channel;
22-
extern crate ckb_util as util;
2322

2423
#[cfg(test)]
2524
extern crate rand;

core/src/block.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ impl Block {
3232
&self.header
3333
}
3434

35+
pub fn mut_header(&mut self) -> &mut Header {
36+
&mut self.header
37+
}
38+
3539
pub fn is_genesis(&self) -> bool {
3640
self.header.is_genesis()
3741
}
@@ -98,11 +102,15 @@ impl BlockBuilder {
98102
}
99103

100104
pub fn uncle(mut self, uncle: UncleBlock) -> Self {
105+
*self.inner.mut_header().mut_raw().mut_uncles_count() =
106+
self.inner.header().raw().uncles_count() + 1;
101107
self.inner.uncles.push(uncle);
102108
self
103109
}
104110

105111
pub fn uncles(mut self, uncles: Vec<UncleBlock>) -> Self {
112+
*self.inner.mut_header().mut_raw().mut_uncles_count() =
113+
self.inner.header().raw().uncles_count() + uncles.len() as u32;
106114
self.inner.uncles.extend(uncles);
107115
self
108116
}
@@ -156,6 +164,7 @@ impl BlockBuilder {
156164
.txs_commit(&txs_commit)
157165
.txs_proposal(&txs_proposal)
158166
.uncles_hash(&uncles_hash)
167+
.uncles_count(self.inner.uncles.len() as u32)
159168
.build();
160169
self.inner
161170
}

core/src/header.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ pub struct RawHeader {
3535
cellbase_id: H256,
3636
/// Hash of the uncles
3737
uncles_hash: H256,
38+
/// Hash of the uncles
39+
uncles_count: u32,
3840
}
3941

4042
impl RawHeader {
@@ -60,6 +62,14 @@ impl RawHeader {
6062
pub fn difficulty(&self) -> U256 {
6163
self.difficulty
6264
}
65+
66+
pub fn uncles_count(&self) -> u32 {
67+
self.uncles_count
68+
}
69+
70+
pub fn mut_uncles_count(&mut self) -> &mut u32 {
71+
&mut self.uncles_count
72+
}
6373
}
6474

6575
#[derive(Clone, Serialize, Deserialize, Debug, Default, PartialEq, Eq)]
@@ -128,9 +138,21 @@ impl Header {
128138
self.raw.uncles_hash
129139
}
130140

131-
pub fn raw(self) -> RawHeader {
141+
pub fn raw(&self) -> &RawHeader {
142+
&self.raw
143+
}
144+
145+
pub fn into_raw(self) -> RawHeader {
132146
self.raw
133147
}
148+
149+
pub fn mut_raw(&mut self) -> &mut RawHeader {
150+
&mut self.raw
151+
}
152+
153+
pub fn uncles_count(&self) -> u32 {
154+
self.raw.uncles_count
155+
}
134156
}
135157

136158
#[derive(Default)]
@@ -205,6 +227,11 @@ impl HeaderBuilder {
205227
self
206228
}
207229

230+
pub fn uncles_count(mut self, uncles_count: u32) -> Self {
231+
self.inner.raw.uncles_count = uncles_count;
232+
self
233+
}
234+
208235
pub fn build(self) -> Header {
209236
let hash = H256::from_slice(&sha3_256(serialize(&self.inner).unwrap()));
210237
self.with_hash(&hash)

network/src/network.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -597,11 +597,11 @@ impl Network {
597597
swarm_controller.clone(),
598598
basic_transport.clone(),
599599
),
600-
discovery_service.start_protocol(
601-
Arc::clone(&network),
602-
swarm_controller.clone(),
603-
basic_transport.clone(),
604-
),
600+
// discovery_service.start_protocol(
601+
// Arc::clone(&network),
602+
// swarm_controller.clone(),
603+
// basic_transport.clone(),
604+
// ),
605605
identify_service.start_protocol(
606606
Arc::clone(&network),
607607
swarm_controller.clone(),

protocol/src/builder.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ impl<'a> FbsHeader<'a> {
5151
builder.add_proof(proof);
5252
builder.add_cellbase_id(cellbase_id);
5353
builder.add_uncles_hash(uncles_hash);
54+
builder.add_uncles_count(header.uncles_count());
5455
builder.finish()
5556
}
5657
}

protocol/src/convert.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ impl<'a> From<ckb_protocol::Header<'a>> for ckb_core::header::Header {
6565
header.uncles_hash().and_then(|b| b.seq()).unwrap(),
6666
)).nonce(header.nonce())
6767
.proof(header.proof().and_then(|b| b.seq()).unwrap())
68+
.uncles_count(header.uncles_count())
6869
.build()
6970
}
7071
}

protocol/src/protocol.fbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ table Header {
4141
proof: Bytes;
4242
cellbase_id: Bytes;
4343
uncles_hash: Bytes;
44+
uncles_count: uint32;
4445
}
4546

4647
table Block {

0 commit comments

Comments
 (0)