Skip to content

Commit

Permalink
Merge #409
Browse files Browse the repository at this point in the history
409: feat: remove uncle cellbase r=doitian a=doitian



Co-authored-by: zhangsoledad <787953403@qq.com>
Co-authored-by: ian <ian@nervos.org>
  • Loading branch information
3 people committed Apr 8, 2019
2 parents 0890b4d + 2fb9ac6 commit 314f45e
Show file tree
Hide file tree
Showing 23 changed files with 26 additions and 153 deletions.
3 changes: 1 addition & 2 deletions chain/src/tests/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ pub(crate) fn gen_block(
.parent_hash(parent_header.hash().clone())
.timestamp(unix_time_as_millis())
.number(number)
.difficulty(difficulty)
.cellbase_id(cellbase.hash());
.difficulty(difficulty);

BlockBuilder::default()
.commit_transaction(cellbase)
Expand Down
12 changes: 0 additions & 12 deletions core/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ pub struct RawHeader {
txs_proposal: H256,
/// Block difficulty.
difficulty: U256,
/// Hash of the cellbase
cellbase_id: H256,
/// Hash of the uncles
uncles_hash: H256,
/// Number of the uncles
Expand Down Expand Up @@ -113,7 +111,6 @@ impl fmt::Debug for Header {
&format_args!("{:#x}", self.raw.txs_proposal),
)
.field("difficulty", &format_args!("{:#x}", self.raw.difficulty))
.field("cellbase_id", &format_args!("{:#x}", self.raw.cellbase_id))
.field("uncles_hash", &format_args!("{:#x}", self.raw.uncles_hash))
.field("uncles_count", &self.raw.uncles_count)
.field("seal", &self.seal)
Expand Down Expand Up @@ -174,10 +171,6 @@ impl Header {
&self.raw.txs_proposal
}

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

pub fn uncles_hash(&self) -> &H256 {
&self.raw.uncles_hash
}
Expand Down Expand Up @@ -268,11 +261,6 @@ impl HeaderBuilder {
self
}

pub fn cellbase_id(mut self, hash: H256) -> Self {
self.inner.raw.cellbase_id = hash;
self
}

pub fn uncles_hash(mut self, hash: H256) -> Self {
self.inner.raw.uncles_hash = hash;
self
Expand Down
19 changes: 2 additions & 17 deletions core/src/uncle.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::block::Block;
use crate::header::Header;
use crate::transaction::{ProposalShortId, Transaction};
use crate::transaction::ProposalShortId;
use crate::BlockNumber;
use bincode::serialize;
use hash::blake2b_256;
Expand All @@ -10,33 +10,22 @@ use serde_derive::{Deserialize, Serialize};
#[derive(Clone, Serialize, Deserialize, PartialEq, Eq, Default, Debug)]
pub struct UncleBlock {
pub header: Header,
pub cellbase: Transaction,
pub proposal_transactions: Vec<ProposalShortId>,
}

impl From<Block> for UncleBlock {
fn from(block: Block) -> Self {
UncleBlock {
header: block.header().clone(),
cellbase: block
.commit_transactions()
.first()
.expect("transactions shouldn't be empty")
.clone(),
proposal_transactions: block.proposal_transactions().to_vec(),
}
}
}

impl UncleBlock {
pub fn new(
header: Header,
cellbase: Transaction,
proposal_transactions: Vec<ProposalShortId>,
) -> UncleBlock {
pub fn new(header: Header, proposal_transactions: Vec<ProposalShortId>) -> UncleBlock {
UncleBlock {
header,
cellbase,
proposal_transactions,
}
}
Expand All @@ -45,10 +34,6 @@ impl UncleBlock {
&self.header
}

pub fn cellbase(&self) -> &Transaction {
&self.cellbase
}

pub fn number(&self) -> BlockNumber {
self.header.number()
}
Expand Down
16 changes: 5 additions & 11 deletions miner/src/block_assembler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,12 @@ impl<CI: ChainIndex + 'static> BlockAssembler<CI> {
fn transform_uncle(uncle: UncleBlock) -> UncleTemplate {
let UncleBlock {
header,
cellbase,
proposal_transactions,
} = uncle;

UncleTemplate {
hash: header.hash(),
required: false, //
cellbase: Self::transform_cellbase(&cellbase, None),
proposal_transactions: proposal_transactions.into_iter().map(Into::into).collect(),
header: (&header).into(),
}
Expand Down Expand Up @@ -396,16 +394,13 @@ impl<CI: ChainIndex + 'static> BlockAssembler<CI> {
|| excluded.contains(hash)
{
bad_uncles.push(hash.clone());
} else if let Some(cellbase) = block.commit_transactions().first() {
} else {
let uncle = UncleBlock {
header: block.header().clone(),
cellbase: cellbase.clone(),
proposal_transactions: block.proposal_transactions().to_vec(),
};
uncles.push(uncle);
included.insert(hash.clone());
} else {
bad_uncles.push(hash.clone());
}
}
(uncles, bad_uncles)
Expand Down Expand Up @@ -498,18 +493,17 @@ mod tests {
// uncles_count_limit,
} = block_template;

let (cellbase_id, cellbase) = {
let CellbaseTemplate { hash, data, .. } = cellbase;
(hash, data)
let cellbase = {
let CellbaseTemplate { data, .. } = cellbase;
data
};

let header_builder = HeaderBuilder::default()
.version(version)
.number(number)
.difficulty(difficulty)
.timestamp(current_time)
.parent_hash(parent_hash)
.cellbase_id(cellbase_id);
.parent_hash(parent_hash);

let block = BlockBuilder::default()
.uncles(uncles.into_iter().map(Into::into).collect())
Expand Down
9 changes: 4 additions & 5 deletions miner/src/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,17 @@ impl Miner {
// uncles_count_limit,
} = template;

let (cellbase_id, cellbase) = {
let CellbaseTemplate { hash, data, .. } = cellbase;
(hash, data)
let cellbase = {
let CellbaseTemplate { data, .. } = cellbase;
data
};

let header_builder = HeaderBuilder::default()
.version(version)
.number(number)
.difficulty(difficulty)
.timestamp(current_time)
.parent_hash(parent_hash)
.cellbase_id(cellbase_id);
.parent_hash(parent_hash);

let block = BlockBuilder::default()
.uncles(uncles.into_iter().map(Into::into).collect())
Expand Down
1 change: 0 additions & 1 deletion nodes_template/spec/dev.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ timestamp = 0
txs_commit = "0x0000000000000000000000000000000000000000000000000000000000000000"
txs_proposal = "0x0000000000000000000000000000000000000000000000000000000000000000"
difficulty = "0x100"
cellbase_id = "0x0000000000000000000000000000000000000000000000000000000000000000"
uncles_hash = "0x0000000000000000000000000000000000000000000000000000000000000000"

[genesis.seal]
Expand Down
4 changes: 0 additions & 4 deletions protocol/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ impl<'a> FbsHeader<'a> {
let txs_proposal = header.txs_proposal().into();
let difficulty = FbsBytes::build(fbb, &uint_to_bytes(header.difficulty()));
let proof = FbsBytes::build(fbb, &header.proof());
let cellbase_id = header.cellbase_id().into();
let uncles_hash = header.uncles_hash().into();
let mut builder = HeaderBuilder::new(fbb);
builder.add_version(header.version());
Expand All @@ -61,7 +60,6 @@ impl<'a> FbsHeader<'a> {
builder.add_difficulty(difficulty);
builder.add_nonce(header.nonce());
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 Expand Up @@ -231,7 +229,6 @@ impl<'a> FbsUncleBlock<'a> {
) -> WIPOffset<FbsUncleBlock<'b>> {
// TODO how to avoid clone here?
let header = FbsHeader::build(fbb, &uncle_block.header().clone());
let cellbase = FbsTransaction::build(fbb, &uncle_block.cellbase);
let vec = uncle_block
.proposal_transactions
.iter()
Expand All @@ -241,7 +238,6 @@ impl<'a> FbsUncleBlock<'a> {

let mut builder = UncleBlockBuilder::new(fbb);
builder.add_header(header);
builder.add_cellbase(cellbase);
builder.add_proposal_transactions(proposal_transactions);
builder.finish()
}
Expand Down
4 changes: 0 additions & 4 deletions protocol/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,9 @@ impl<'a> TryFrom<ckb_protocol::UncleBlock<'a>> for ckb_core::uncle::UncleBlock {
.map(TryInto::try_into)
.collect();
let header = cast!(uncle_block.header())?;
let cellbase = cast!(uncle_block.cellbase())?;

Ok(ckb_core::uncle::UncleBlock {
header: TryInto::try_into(header)?,
cellbase: TryInto::try_into(cellbase)?,
proposal_transactions: proposal_transactions?,
})
}
Expand All @@ -153,7 +151,6 @@ impl<'a> TryFrom<ckb_protocol::Header<'a>> for ckb_core::header::Header {
let parent_hash = cast!(header.parent_hash())?;
let txs_commit = cast!(header.txs_commit())?;
let txs_proposal = cast!(header.txs_proposal())?;
let cellbase_id = cast!(header.cellbase_id())?;
let uncles_hash = cast!(header.uncles_hash())?;

Ok(ckb_core::header::HeaderBuilder::default()
Expand All @@ -166,7 +163,6 @@ impl<'a> TryFrom<ckb_protocol::Header<'a>> for ckb_core::header::Header {
.difficulty(U256::from_little_endian(cast!(header
.difficulty()
.and_then(|d| d.seq()))?)?)
.cellbase_id(TryInto::try_into(cellbase_id)?)
.uncles_hash(TryInto::try_into(uncles_hash)?)
.nonce(header.nonce())
.proof(cast!(header
Expand Down
3 changes: 0 additions & 3 deletions protocol/src/protocol.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ table Header {
difficulty: Bytes;
nonce: uint64;
proof: Bytes;
cellbase_id: H256;
uncles_hash: H256;
uncles_count: uint32;
}
Expand All @@ -57,7 +56,6 @@ table Block {

table UncleBlock {
header: Header;
cellbase: Transaction;
proposal_transactions: [ProposalShortId];
}

Expand Down Expand Up @@ -224,4 +222,3 @@ table TimeMessage {
table Time {
timestamp: uint64;
}

30 changes: 3 additions & 27 deletions protocol/src/protocol_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1020,7 +1020,6 @@ impl<'a> Header<'a> {
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); }
if let Some(x) = args.difficulty { builder.add_difficulty(x); }
if let Some(x) = args.txs_proposal { builder.add_txs_proposal(x); }
Expand All @@ -1039,9 +1038,8 @@ impl<'a> Header<'a> {
pub const VT_DIFFICULTY: flatbuffers::VOffsetT = 16;
pub const VT_NONCE: flatbuffers::VOffsetT = 18;
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;
pub const VT_UNCLES_HASH: flatbuffers::VOffsetT = 22;
pub const VT_UNCLES_COUNT: flatbuffers::VOffsetT = 24;

#[inline]
pub fn version(&self) -> u32 {
Expand Down Expand Up @@ -1080,10 +1078,6 @@ impl<'a> Header<'a> {
self._tab.get::<flatbuffers::ForwardsUOffset<Bytes<'a>>>(Header::VT_PROOF, None)
}
#[inline]
pub fn cellbase_id(&self) -> Option<&'a H256> {
self._tab.get::<H256>(Header::VT_CELLBASE_ID, None)
}
#[inline]
pub fn uncles_hash(&self) -> Option<&'a H256> {
self._tab.get::<H256>(Header::VT_UNCLES_HASH, None)
}
Expand All @@ -1103,7 +1097,6 @@ pub struct HeaderArgs<'a> {
pub difficulty: Option<flatbuffers::WIPOffset<Bytes<'a >>>,
pub nonce: u64,
pub proof: Option<flatbuffers::WIPOffset<Bytes<'a >>>,
pub cellbase_id: Option<&'a H256>,
pub uncles_hash: Option<&'a H256>,
pub uncles_count: u32,
}
Expand All @@ -1120,7 +1113,6 @@ impl<'a> Default for HeaderArgs<'a> {
difficulty: None,
nonce: 0,
proof: None,
cellbase_id: None,
uncles_hash: None,
uncles_count: 0,
}
Expand Down Expand Up @@ -1168,10 +1160,6 @@ impl<'a: 'b, 'b> HeaderBuilder<'a, 'b> {
self.fbb_.push_slot_always::<flatbuffers::WIPOffset<Bytes>>(Header::VT_PROOF, proof);
}
#[inline]
pub fn add_cellbase_id(&mut self, cellbase_id: &'b H256) {
self.fbb_.push_slot_always::<&H256>(Header::VT_CELLBASE_ID, cellbase_id);
}
#[inline]
pub fn add_uncles_hash(&mut self, uncles_hash: &'b H256) {
self.fbb_.push_slot_always::<&H256>(Header::VT_UNCLES_HASH, uncles_hash);
}
Expand Down Expand Up @@ -1336,40 +1324,32 @@ impl<'a> UncleBlock<'a> {
args: &'args UncleBlockArgs<'args>) -> flatbuffers::WIPOffset<UncleBlock<'bldr>> {
let mut builder = UncleBlockBuilder::new(_fbb);
if let Some(x) = args.proposal_transactions { builder.add_proposal_transactions(x); }
if let Some(x) = args.cellbase { builder.add_cellbase(x); }
if let Some(x) = args.header { builder.add_header(x); }
builder.finish()
}

pub const VT_HEADER: flatbuffers::VOffsetT = 4;
pub const VT_CELLBASE: flatbuffers::VOffsetT = 6;
pub const VT_PROPOSAL_TRANSACTIONS: flatbuffers::VOffsetT = 8;
pub const VT_PROPOSAL_TRANSACTIONS: flatbuffers::VOffsetT = 6;

#[inline]
pub fn header(&self) -> Option<Header<'a>> {
self._tab.get::<flatbuffers::ForwardsUOffset<Header<'a>>>(UncleBlock::VT_HEADER, None)
}
#[inline]
pub fn cellbase(&self) -> Option<Transaction<'a>> {
self._tab.get::<flatbuffers::ForwardsUOffset<Transaction<'a>>>(UncleBlock::VT_CELLBASE, None)
}
#[inline]
pub fn proposal_transactions(&self) -> Option<&'a [ProposalShortId]> {
self._tab.get::<flatbuffers::ForwardsUOffset<flatbuffers::Vector<ProposalShortId>>>(UncleBlock::VT_PROPOSAL_TRANSACTIONS, None).map(|v| v.safe_slice() )
}
}

pub struct UncleBlockArgs<'a> {
pub header: Option<flatbuffers::WIPOffset<Header<'a >>>,
pub cellbase: Option<flatbuffers::WIPOffset<Transaction<'a >>>,
pub proposal_transactions: Option<flatbuffers::WIPOffset<flatbuffers::Vector<'a , ProposalShortId>>>,
}
impl<'a> Default for UncleBlockArgs<'a> {
#[inline]
fn default() -> Self {
UncleBlockArgs {
header: None,
cellbase: None,
proposal_transactions: None,
}
}
Expand All @@ -1384,10 +1364,6 @@ impl<'a: 'b, 'b> UncleBlockBuilder<'a, 'b> {
self.fbb_.push_slot_always::<flatbuffers::WIPOffset<Header>>(UncleBlock::VT_HEADER, header);
}
#[inline]
pub fn add_cellbase(&mut self, cellbase: flatbuffers::WIPOffset<Transaction<'b >>) {
self.fbb_.push_slot_always::<flatbuffers::WIPOffset<Transaction>>(UncleBlock::VT_CELLBASE, cellbase);
}
#[inline]
pub fn add_proposal_transactions(&mut self, proposal_transactions: flatbuffers::WIPOffset<flatbuffers::Vector<'b , ProposalShortId>>) {
self.fbb_.push_slot_always::<flatbuffers::WIPOffset<_>>(UncleBlock::VT_PROPOSAL_TRANSACTIONS, proposal_transactions);
}
Expand Down

0 comments on commit 314f45e

Please sign in to comment.