Skip to content

Commit

Permalink
refactor: apply review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangsoledad committed Jun 11, 2019
1 parent d3fa274 commit 48eba6d
Show file tree
Hide file tree
Showing 16 changed files with 93 additions and 102 deletions.
5 changes: 1 addition & 4 deletions chain/src/tests/reward.rs
@@ -1,6 +1,5 @@
use crate::tests::util::{create_always_success_out_point, create_always_success_tx, start_chain};
use ckb_chain_spec::consensus::Consensus;
use ckb_chain_spec::Foundation;
use ckb_core::block::{Block, BlockBuilder};
use ckb_core::header::{Header, HeaderBuilder};
use ckb_core::script::Script;
Expand Down Expand Up @@ -99,9 +98,7 @@ fn finalize_reward() {

let consensus = Consensus::default()
.set_cellbase_maturity(0)
.set_foundation(Foundation {
lock: always_success_script.clone(),
})
.set_bootstrap_lock(always_success_script.clone())
.set_genesis_block(genesis_block);

let (chain_controller, shared, mut parent) = start_chain(Some(consensus));
Expand Down
6 changes: 2 additions & 4 deletions chain/src/tests/util.rs
@@ -1,5 +1,5 @@
use crate::chain::{ChainController, ChainService};
use ckb_chain_spec::{consensus::Consensus, Foundation};
use ckb_chain_spec::consensus::Consensus;
use ckb_core::block::Block;
use ckb_core::block::BlockBuilder;
use ckb_core::header::{Header, HeaderBuilder};
Expand Down Expand Up @@ -47,9 +47,7 @@ pub(crate) fn start_chain(
.build();
Consensus::default()
.set_cellbase_maturity(0)
.set_foundation(Foundation {
lock: always_success_script.clone(),
})
.set_bootstrap_lock(always_success_script.clone())
.set_genesis_block(genesis_block)
});
let shared = builder.consensus(consensus).build().unwrap();
Expand Down
13 changes: 9 additions & 4 deletions miner/src/block_assembler.rs
Expand Up @@ -373,24 +373,29 @@ impl<CS: ChainStore + 'static> BlockAssembler<CS> {
Ok(template)
}

/// Miner mined block H(c), the block reward will be finalized at H(c + w_far + 1).
/// Miner specify own lock in cellbase witness.
/// The cellbase have only one output,
/// if H(c) > reserve_number, miner should collect the block reward for finalize target H(c - w_far - 1)
/// otherwise miner create a output with bootstrap lock.
fn build_cellbase(
&self,
tip: &Header,
lock: Script,
) -> Result<(Transaction, usize), FailureError> {
let candidate_number = tip.number() + 1;

let tx = if candidate_number > (self.shared.consensus().foundation_reserve_number()) {
let tx = if candidate_number > (self.shared.consensus().reserve_number()) {
self.ordinary_cellbase(tip, lock)
} else {
self.foundation_cellbase(candidate_number, lock)
self.bootstrap_cellbase(candidate_number, lock)
}?;

let serialized_size = tx.serialized_size();
Ok((tx, serialized_size))
}

fn foundation_cellbase(
fn bootstrap_cellbase(
&self,
candidate_number: BlockNumber,
lock: Script,
Expand All @@ -403,7 +408,7 @@ impl<CS: ChainStore + 'static> BlockAssembler<CS> {
.genesis_epoch_ext()
.block_reward(candidate_number)?,
Bytes::default(),
self.shared.consensus().foundation().lock.clone(),
self.shared.consensus().bootstrap_lock().clone(),
None,
);
Ok(TransactionBuilder::default()
Expand Down
2 changes: 1 addition & 1 deletion resource/specs/dev.toml
Expand Up @@ -33,7 +33,7 @@ code_hash = "0xb35557e7e9854206f7bc13e3c3a7fa4cf8892c84a09237fb0aab40aab3771eee"
args = []


[genesis.foundation.lock]
[genesis.bootstrap_lock]
code_hash = "0x9e3b3557f11b2b3532ce352bfe8017e9fd11d154c4c7f9b7aaaa1e621b539a08"
args = ["0xb2e61ff569acf041b3c2c17724e2379c581eeac3"]

Expand Down
2 changes: 1 addition & 1 deletion resource/specs/testnet.toml
Expand Up @@ -32,7 +32,7 @@ files = [
code_hash = "0xb35557e7e9854206f7bc13e3c3a7fa4cf8892c84a09237fb0aab40aab3771eee"
args = []

[genesis.foundation.lock]
[genesis.bootstrap_lock]
code_hash = "0x9e3b3557f11b2b3532ce352bfe8017e9fd11d154c4c7f9b7aaaa1e621b539a08"
args = ["0xb2e61ff569acf041b3c2c17724e2379c581eeac3"]

Expand Down
24 changes: 11 additions & 13 deletions spec/src/consensus.rs
@@ -1,8 +1,8 @@
use crate::Foundation;
use ckb_core::block::{Block, BlockBuilder};
use ckb_core::extras::EpochExt;
use ckb_core::header::Header;
use ckb_core::header::HeaderBuilder;
use ckb_core::script::Script;
use ckb_core::{capacity_bytes, BlockNumber, Capacity, Cycle, Version};
use ckb_pow::{Pow, PowEngine};
use numext_fixed_hash::H256;
Expand Down Expand Up @@ -84,8 +84,8 @@ pub struct Consensus {
// block version number supported
pub max_block_proposals_limit: u64,
pub genesis_epoch_ext: EpochExt,
// foundation lock
pub foundation: Foundation,
// genesis lock
pub bootstrap_lock: Script,
}

// genesis difficulty should not be zero
Expand Down Expand Up @@ -128,9 +128,7 @@ impl Default for Consensus {
block_version: BLOCK_VERSION,
proposer_reward_ratio: PROPOSER_REWARD_RATIO,
max_block_proposals_limit: MAX_BLOCK_PROPOSALS_LIMIT,
foundation: Foundation {
lock: Default::default(),
},
bootstrap_lock: Default::default(),
}
}
}
Expand Down Expand Up @@ -179,11 +177,15 @@ impl Consensus {
}

#[must_use]
pub fn set_foundation(mut self, foundation: Foundation) -> Self {
self.foundation = foundation;
pub fn set_bootstrap_lock(mut self, lock: Script) -> Self {
self.bootstrap_lock = lock;
self
}

pub fn bootstrap_lock(&self) -> &Script {
&self.bootstrap_lock
}

pub fn set_tx_proposal_window(mut self, proposal_window: ProposalWindow) -> Self {
self.tx_proposal_window = proposal_window;
self
Expand All @@ -198,15 +200,11 @@ impl Consensus {
&self.genesis_block
}

pub fn foundation(&self) -> &Foundation {
&self.foundation
}

pub fn proposer_reward_ratio(&self) -> Ratio {
self.proposer_reward_ratio
}

pub fn foundation_reserve_number(&self) -> BlockNumber {
pub fn reserve_number(&self) -> BlockNumber {
self.finalization_delay_length()
}

Expand Down
25 changes: 9 additions & 16 deletions spec/src/lib.rs
Expand Up @@ -57,7 +57,7 @@ pub struct Genesis {
pub issued_cells: Vec<IssuedCell>,
pub genesis_cell: GenesisCell,
pub system_cells: SystemCells,
pub foundation: Foundation,
pub bootstrap_lock: Script,
pub seal: Seal,
}

Expand All @@ -81,11 +81,6 @@ pub struct GenesisCell {
pub lock: Script,
}

#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
pub struct Foundation {
pub lock: Script,
}

#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
pub struct IssuedCell {
pub capacity: Capacity,
Expand All @@ -96,7 +91,7 @@ pub struct IssuedCell {
pub enum SpecLoadError {
FileNotFound,
ChainNameNotAllowed(String),
GenesisMismatch { expect: H256, actual: H256 }
GenesisMismatch { expect: H256, actual: H256 },
}

impl SpecLoadError {
Expand Down Expand Up @@ -211,7 +206,7 @@ impl ChainSpec {
.set_epoch_reward(self.params.epoch_reward)
.set_secondary_epoch_reward(self.params.secondary_epoch_reward)
.set_max_block_cycles(self.params.max_block_cycles)
.set_foundation(self.genesis.foundation.clone())
.set_bootstrap_lock(self.genesis.bootstrap_lock.clone())
.set_pow(self.pow.clone());

Ok(consensus)
Expand Down Expand Up @@ -245,7 +240,7 @@ impl Genesis {
// - issued cells
outputs.push(self.genesis_cell.build_output()?);
self.system_cells.build_outputs_into(&mut outputs)?;
outputs.push(self.foundation.build_output()?);
outputs.push(build_bootstrap_output(&self.bootstrap_lock)?);
outputs.extend(self.issued_cells.iter().map(IssuedCell::build_output));

Ok(TransactionBuilder::default()
Expand All @@ -265,13 +260,11 @@ impl GenesisCell {
}
}

impl Foundation {
fn build_output(&self) -> Result<CellOutput, Box<dyn Error>> {
let mut cell = CellOutput::default();
cell.lock = self.lock.clone();
cell.capacity = cell.occupied_capacity()?;
Ok(cell)
}
fn build_bootstrap_output(lock: &Script) -> Result<CellOutput, Box<dyn Error>> {
let mut cell = CellOutput::default();
cell.lock.clone_from(lock);
cell.capacity = cell.occupied_capacity()?;
Ok(cell)
}

impl IssuedCell {
Expand Down
7 changes: 2 additions & 5 deletions sync/src/synchronizer/mod.rs
Expand Up @@ -597,7 +597,6 @@ mod tests {
use crate::{SyncSharedState, MAX_TIP_AGE};
use ckb_chain::chain::ChainService;
use ckb_chain_spec::consensus::Consensus;
use ckb_chain_spec::Foundation;
use ckb_core::block::BlockBuilder;
use ckb_core::extras::EpochExt;
use ckb_core::header::BlockNumber;
Expand Down Expand Up @@ -637,9 +636,7 @@ mod tests {
let mut builder = SharedBuilder::<MemoryKeyValueDB>::new();

let consensus = consensus.unwrap_or_else(Default::default);
builder = builder.consensus(consensus.set_foundation(Foundation {
lock: Script::default(),
}));
builder = builder.consensus(consensus.set_bootstrap_lock(Script::default()));

let shared = builder.build().unwrap();

Expand All @@ -655,7 +652,7 @@ mod tests {
parent_header: &Header,
number: BlockNumber,
) -> Transaction {
let reward = if number > shared.consensus().foundation_reserve_number() {
let reward = if number > shared.consensus().reserve_number() {
let (_, block_reward) = shared.finalize_block_reward(parent_header).unwrap();
block_reward
} else {
Expand Down
8 changes: 3 additions & 5 deletions sync/src/tests/synchronizer.rs
Expand Up @@ -2,7 +2,7 @@ use crate::synchronizer::{BLOCK_FETCH_TOKEN, SEND_GET_HEADERS_TOKEN, TIMEOUT_EVI
use crate::tests::TestNode;
use crate::{Config, NetworkProtocol, SyncSharedState, Synchronizer};
use ckb_chain::chain::ChainService;
use ckb_chain_spec::{consensus::Consensus, Foundation};
use ckb_chain_spec::consensus::Consensus;
use ckb_core::block::BlockBuilder;
use ckb_core::header::HeaderBuilder;
use ckb_core::transaction::{CellInput, CellOutput, OutPoint, TransactionBuilder};
Expand Down Expand Up @@ -88,9 +88,7 @@ fn setup_node(

let consensus = Consensus::default()
.set_genesis_block(block.clone())
.set_foundation(Foundation {
lock: always_success_script.clone(),
})
.set_bootstrap_lock(always_success_script.clone())
.set_cellbase_maturity(0);
let shared = SharedBuilder::<MemoryKeyValueDB>::new()
.consensus(consensus)
Expand All @@ -110,7 +108,7 @@ fn setup_node(
.next_epoch_ext(&last_epoch, block.header())
.unwrap_or(last_epoch);

let reward = if number > shared.consensus().foundation_reserve_number() {
let reward = if number > shared.consensus().reserve_number() {
let (_, block_reward) = shared.finalize_block_reward(block.header()).unwrap();
block_reward
} else {
Expand Down
2 changes: 1 addition & 1 deletion test/integration.toml
Expand Up @@ -39,7 +39,7 @@ files = [
code_hash = "0xb35557e7e9854206f7bc13e3c3a7fa4cf8892c84a09237fb0aab40aab3771eee"
args = []

[genesis.foundation.lock]
[genesis.bootstrap_lock]
code_hash = "0x28e83a1277d48add8e72fadaa9248559e1b632bab2bd60b27955ebc4c03800a5"
args = []

Expand Down
2 changes: 1 addition & 1 deletion test/src/main.rs
Expand Up @@ -31,7 +31,7 @@ fn main() {
specs.insert("chain_fork_6", Box::new(ChainFork6));
specs.insert("chain_fork_7", Box::new(ChainFork7));
specs.insert("mining_basic", Box::new(MiningBasic));
specs.insert("mining_foundation_cellbase", Box::new(FoundationCellbase));
specs.insert("mining_bootstrap_cellbase", Box::new(BootstrapCellbase));
specs.insert("mining_template_size_limit", Box::new(TemplateSizeLimit));
specs.insert("pool_reconcile", Box::new(PoolReconcile));
specs.insert("pool_resurrect", Box::new(PoolResurrect));
Expand Down
Expand Up @@ -6,18 +6,18 @@ use ckb_core::script::Script;
use log::info;
use numext_fixed_hash::{h256, H256};

pub struct FoundationCellbase;
pub struct BootstrapCellbase;

impl Spec for FoundationCellbase {
impl Spec for BootstrapCellbase {
fn run(&self, net: Net) {
info!("Running FoundationCellbase");
info!("Running BootstrapCellbase");
let node = &net.nodes[0];

let blk_hashes: Vec<_> = (0..=DEFAULT_TX_PROPOSAL_WINDOW.1)
.map(|_| node.generate_block())
.collect();

let foundation = Script {
let bootstrap_lock = Script {
args: vec![],
code_hash: h256!("0xa1"),
};
Expand All @@ -27,17 +27,17 @@ impl Spec for FoundationCellbase {
code_hash: h256!("0xa2"),
};

let is_foundation_cellbase = |blk_hash: &H256| {
let is_bootstrap_cellbase = |blk_hash: &H256| {
let blk: Block = node
.rpc_client()
.get_block(blk_hash.clone())
.unwrap()
.into();
blk.transactions()[0].is_cellbase()
&& blk.transactions()[0].outputs()[0].lock == foundation
&& blk.transactions()[0].outputs()[0].lock == bootstrap_lock
};

assert!(blk_hashes.iter().all(is_foundation_cellbase));
assert!(blk_hashes.iter().all(is_bootstrap_cellbase));

let hash = node.generate_block();

Expand All @@ -53,7 +53,7 @@ impl Spec for FoundationCellbase {

fn modify_chain_spec(&self) -> Box<dyn Fn(&mut ChainSpec) -> ()> {
Box::new(|spec_config| {
spec_config.genesis.foundation.lock = Script {
spec_config.genesis.bootstrap_lock = Script {
args: vec![],
code_hash: h256!("0xa1"),
};
Expand Down
4 changes: 2 additions & 2 deletions test/src/specs/mining/mod.rs
@@ -1,7 +1,7 @@
mod basic;
mod foundation;
mod bootstrap;
mod size_limit;

pub use basic::MiningBasic;
pub use foundation::FoundationCellbase;
pub use bootstrap::BootstrapCellbase;
pub use size_limit::TemplateSizeLimit;

0 comments on commit 48eba6d

Please sign in to comment.