Skip to content

Commit

Permalink
feat: add uncles_count to Header
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangsoledad authored and doitian committed Nov 19, 2018
1 parent 52441df commit 324488c
Show file tree
Hide file tree
Showing 21 changed files with 218 additions and 69 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

1 change: 0 additions & 1 deletion chain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ log = "0.4"
ckb-core = { path = "../core" }
ckb-shared = { path = "../shared" }
ckb-chain-spec = { path = "../spec" }
ckb-util = { path = "../util" }
ckb-db = { path = "../db" }
ckb-time = { path = "../util/time" }
ckb-notify = { path = "../notify" }
Expand Down
6 changes: 2 additions & 4 deletions chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use std::cmp;
use std::sync::Arc;
use std::thread::{self, JoinHandle};
use time::now_ms;
use util::RwLockUpgradableReadGuard;
use verification::{BlockVerifier, Verifier};

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

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

if new_best_block {
debug!(target: "chain", "update index");
let mut guard = RwLockUpgradableReadGuard::upgrade(tip_header);
let new_tip_header =
TipHeader::new(block.header().clone(), total_difficulty, output_root);
self.shared.store().save_with_batch(|batch| {
Expand All @@ -197,7 +195,7 @@ impl<CI: ChainIndex + 'static> ChainService<CI> {
self.shared.store().rebuild_tree(output_root);
Ok(())
})?;
*guard = new_tip_header;
*tip_header = new_tip_header;
debug!(target: "chain", "update index release");
}

Expand Down
1 change: 0 additions & 1 deletion chain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ extern crate ckb_verification as verification;
extern crate log;
#[macro_use]
extern crate crossbeam_channel as channel;
extern crate ckb_util as util;

#[cfg(test)]
extern crate rand;
Expand Down
9 changes: 9 additions & 0 deletions core/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ impl Block {
&self.header
}

pub fn mut_header(&mut self) -> &mut Header {
&mut self.header
}

pub fn is_genesis(&self) -> bool {
self.header.is_genesis()
}
Expand Down Expand Up @@ -98,11 +102,15 @@ impl BlockBuilder {
}

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

pub fn uncles(mut self, uncles: Vec<UncleBlock>) -> Self {
*self.inner.mut_header().mut_raw().mut_uncles_count() =
self.inner.header().raw().uncles_count() + uncles.len() as u32;
self.inner.uncles.extend(uncles);
self
}
Expand Down Expand Up @@ -156,6 +164,7 @@ impl BlockBuilder {
.txs_commit(&txs_commit)
.txs_proposal(&txs_proposal)
.uncles_hash(&uncles_hash)
.uncles_count(self.inner.uncles.len() as u32)
.build();
self.inner
}
Expand Down
29 changes: 28 additions & 1 deletion core/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ pub struct RawHeader {
cellbase_id: H256,
/// Hash of the uncles
uncles_hash: H256,
/// Hash of the uncles
uncles_count: u32,
}

impl RawHeader {
Expand All @@ -60,6 +62,14 @@ impl RawHeader {
pub fn difficulty(&self) -> U256 {
self.difficulty
}

pub fn uncles_count(&self) -> u32 {
self.uncles_count
}

pub fn mut_uncles_count(&mut self) -> &mut u32 {
&mut self.uncles_count
}
}

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

pub fn raw(self) -> RawHeader {
pub fn raw(&self) -> &RawHeader {
&self.raw
}

pub fn into_raw(self) -> RawHeader {
self.raw
}

pub fn mut_raw(&mut self) -> &mut RawHeader {
&mut self.raw
}

pub fn uncles_count(&self) -> u32 {
self.raw.uncles_count
}
}

#[derive(Default)]
Expand Down Expand Up @@ -205,6 +227,11 @@ impl HeaderBuilder {
self
}

pub fn uncles_count(mut self, uncles_count: u32) -> Self {
self.inner.raw.uncles_count = uncles_count;
self
}

pub fn build(self) -> Header {
let hash = H256::from_slice(&sha3_256(serialize(&self.inner).unwrap()));
self.with_hash(&hash)
Expand Down
10 changes: 5 additions & 5 deletions network/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,11 +597,11 @@ impl Network {
swarm_controller.clone(),
basic_transport.clone(),
),
discovery_service.start_protocol(
Arc::clone(&network),
swarm_controller.clone(),
basic_transport.clone(),
),
// discovery_service.start_protocol(
// Arc::clone(&network),
// swarm_controller.clone(),
// basic_transport.clone(),
// ),
identify_service.start_protocol(
Arc::clone(&network),
swarm_controller.clone(),
Expand Down
1 change: 1 addition & 0 deletions protocol/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ impl<'a> FbsHeader<'a> {
builder.add_proof(proof);
builder.add_cellbase_id(cellbase_id);
builder.add_uncles_hash(uncles_hash);
builder.add_uncles_count(header.uncles_count());
builder.finish()
}
}
Expand Down
1 change: 1 addition & 0 deletions protocol/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ impl<'a> From<ckb_protocol::Header<'a>> for ckb_core::header::Header {
header.uncles_hash().and_then(|b| b.seq()).unwrap(),
)).nonce(header.nonce())
.proof(header.proof().and_then(|b| b.seq()).unwrap())
.uncles_count(header.uncles_count())
.build()
}
}
Expand Down
1 change: 1 addition & 0 deletions protocol/src/protocol.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ table Header {
proof: Bytes;
cellbase_id: Bytes;
uncles_hash: Bytes;
uncles_count: uint32;
}

table Block {
Expand Down
15 changes: 14 additions & 1 deletion protocol/src/protocol_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
#![cfg_attr(rustfmt, rustfmt_skip)]
// automatically generated by the FlatBuffers compiler, do not modify


#![allow(dead_code)]
#![allow(unused_imports)]
extern crate flatbuffers;

pub mod ckb {
#![allow(dead_code)]
Expand Down Expand Up @@ -658,6 +660,7 @@ impl<'a> Header<'a> {
builder.add_nonce(args.nonce);
builder.add_number(args.number);
builder.add_timestamp(args.timestamp);
builder.add_uncles_count(args.uncles_count);
if let Some(x) = args.uncles_hash { builder.add_uncles_hash(x); }
if let Some(x) = args.cellbase_id { builder.add_cellbase_id(x); }
if let Some(x) = args.proof { builder.add_proof(x); }
Expand All @@ -680,6 +683,7 @@ impl<'a> Header<'a> {
pub const VT_PROOF: flatbuffers::VOffsetT = 20;
pub const VT_CELLBASE_ID: flatbuffers::VOffsetT = 22;
pub const VT_UNCLES_HASH: flatbuffers::VOffsetT = 24;
pub const VT_UNCLES_COUNT: flatbuffers::VOffsetT = 26;

#[inline]
pub fn version(&self) -> u32 {
Expand Down Expand Up @@ -725,6 +729,10 @@ impl<'a> Header<'a> {
pub fn uncles_hash(&self) -> Option<Bytes<'a>> {
self._tab.get::<flatbuffers::ForwardsUOffset<Bytes<'a>>>(Header::VT_UNCLES_HASH, None)
}
#[inline]
pub fn uncles_count(&self) -> u32 {
self._tab.get::<u32>(Header::VT_UNCLES_COUNT, Some(0)).unwrap()
}
}

pub struct HeaderArgs<'a> {
Expand All @@ -739,6 +747,7 @@ pub struct HeaderArgs<'a> {
pub proof: Option<flatbuffers::WIPOffset<Bytes<'a >>>,
pub cellbase_id: Option<flatbuffers::WIPOffset<Bytes<'a >>>,
pub uncles_hash: Option<flatbuffers::WIPOffset<Bytes<'a >>>,
pub uncles_count: u32,
}
impl<'a> Default for HeaderArgs<'a> {
#[inline]
Expand All @@ -755,6 +764,7 @@ impl<'a> Default for HeaderArgs<'a> {
proof: None,
cellbase_id: None,
uncles_hash: None,
uncles_count: 0,
}
}
}
Expand Down Expand Up @@ -808,6 +818,10 @@ impl<'a: 'b, 'b> HeaderBuilder<'a, 'b> {
self.fbb_.push_slot_always::<flatbuffers::WIPOffset<Bytes>>(Header::VT_UNCLES_HASH, uncles_hash);
}
#[inline]
pub fn add_uncles_count(&mut self, uncles_count: u32) {
self.fbb_.push_slot::<u32>(Header::VT_UNCLES_COUNT, uncles_count, 0);
}
#[inline]
pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> HeaderBuilder<'a, 'b> {
let start = _fbb.start_table();
HeaderBuilder {
Expand Down Expand Up @@ -2305,4 +2319,3 @@ pub fn finish_size_prefixed_sync_message_buffer<'a, 'b>(fbb: &'b mut flatbuffers
}
} // pub mod Protocol
} // pub mod Ckb

2 changes: 1 addition & 1 deletion rpc/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ impl<CI: ChainIndex + 'static> RpcService<CI> {
.with_header_builder(header_builder);

Ok(BlockTemplate {
raw_header: block.header().clone().raw(),
raw_header: block.header().clone().into_raw(),
uncles: block.uncles().to_vec(),
commit_transactions: block.commit_transactions().to_vec(),
proposal_transactions: block.proposal_transactions().to_vec(),
Expand Down
21 changes: 10 additions & 11 deletions shared/src/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,19 +280,18 @@ impl<CI: ChainIndex> ChainProvider for Shared<CI> {
}

fn get_ancestor(&self, base: &H256, number: BlockNumber) -> Option<Header> {
{
if let Some(n_number) = self.block_number(base) {
if number > n_number {
return None;
} else if number == n_number {
return Some(self.tip_header.read().inner().clone());
} else {
return self
.block_hash(number)
.and_then(|hash| self.block_header(&hash));
}
// if base in the main chain
if let Some(n_number) = self.block_number(base) {
if number > n_number {
return None;
} else {
return self
.block_hash(number)
.and_then(|hash| self.block_header(&hash));
}
}

// if base in the fork
if let Some(header) = self.block_header(base) {
let mut n_number = header.number();
let mut index_walk = header;
Expand Down
2 changes: 1 addition & 1 deletion sync/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ ckb-verification = { path = "../verification" }
serde = "1.0"
serde_derive = "1.0"
flatbuffers = "0.5.0"
ckb-chain-spec = { path = "../spec" }

[dev-dependencies]
ckb-notify = { path = "../notify" }
ckb-db = { path = "../db" }
ckb-time = { path = "../util/time", features = ["mock_timer"] }
env_logger = "0.5"
crossbeam-channel = "0.2"
ckb-chain-spec = { path = "../spec" }
3 changes: 1 addition & 2 deletions sync/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ extern crate ckb_verification;
extern crate bitflags;
#[macro_use]
extern crate serde_derive;

#[cfg(test)]
extern crate ckb_chain_spec as chain_spec;

#[cfg(test)]
extern crate ckb_db as db;
#[cfg(test)]
Expand Down
21 changes: 9 additions & 12 deletions sync/src/synchronizer/header_view.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
use bigint::{H256, U256};
use core::header::{BlockNumber, Header};
use std::cmp::Ordering;

#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct HeaderView {
inner: Header,
total_difficulty: U256,
total_uncles_count: u64,
}

impl HeaderView {
pub fn new(inner: Header, total_difficulty: U256) -> Self {
pub fn new(inner: Header, total_difficulty: U256, total_uncles_count: u64) -> Self {
HeaderView {
inner,
total_difficulty,
total_uncles_count,
}
}

Expand All @@ -24,23 +25,19 @@ impl HeaderView {
self.inner.hash()
}

pub fn total_uncles_count(&self) -> u64 {
self.total_uncles_count
}

pub fn total_difficulty(&self) -> U256 {
self.total_difficulty
}

pub fn inner(&self) -> &Header {
&self.inner
}
}

impl Ord for HeaderView {
fn cmp(&self, other: &HeaderView) -> Ordering {
self.total_difficulty.cmp(&other.total_difficulty)
}
}

impl PartialOrd for HeaderView {
fn partial_cmp(&self, other: &HeaderView) -> Option<Ordering> {
Some(self.cmp(other))
pub fn into_inner(self) -> Header {
self.inner
}
}
Loading

0 comments on commit 324488c

Please sign in to comment.