Skip to content

Commit

Permalink
chore: impl according to review
Browse files Browse the repository at this point in the history
  • Loading branch information
driftluo committed Jun 14, 2019
1 parent 60fea84 commit 1227071
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 32 deletions.
2 changes: 1 addition & 1 deletion core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub mod uncle;
pub use crate::error::Error;

pub use bytes::Bytes;
pub use occupied_capacity::{capacity_bytes, Capacity};
pub use occupied_capacity::{capacity_bytes, Capacity, OccupiedCapacity};
pub type PublicKey = numext_fixed_hash::H512;
pub type BlockNumber = u64;
pub type EpochNumber = u64;
Expand Down
70 changes: 45 additions & 25 deletions miner/src/block_assembler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ use ckb_core::transaction::{
CellInput, CellOutput, ProposalShortId, Transaction, TransactionBuilder,
};
use ckb_core::uncle::UncleBlock;
use ckb_core::BlockNumber;
use ckb_core::OccupiedCapacity;
use ckb_core::{BlockNumber, Bytes, Capacity};
use ckb_core::{Cycle, Version};
use ckb_logger::{error, info};
use ckb_notify::NotifyController;
use ckb_shared::{shared::Shared, tx_pool::ProposedEntry};
use ckb_store::ChainStore;
use ckb_traits::ChainProvider;
use ckb_verification::TransactionError;
use crossbeam_channel::{self, select, Receiver, Sender};
use failure::Error as FailureError;
use faketime::unix_time_as_millis;
Expand Down Expand Up @@ -403,19 +403,21 @@ impl<CS: ChainStore + 'static> BlockAssembler<CS> {
) -> Result<Transaction, FailureError> {
let witness = lock.into_witness();
let input = CellInput::new_cellbase_input(candidate_number);
let output = CellOutput::new(
self.shared
.consensus()
.genesis_epoch_ext()
.block_reward(candidate_number)?,
self.config.data.clone().into_bytes(),
self.shared.consensus().bootstrap_lock().clone(),
None,
);
// User-defined data has a risk of exceeding capacity
if output.is_lack_of_capacity()? {
return Err(TransactionError::InsufficientCellCapacity.into());
}

let block_reward = self
.shared
.consensus()
.genesis_epoch_ext()
.block_reward(candidate_number)?;

let lock = self.shared.consensus().bootstrap_lock().clone();

let r#type = None;

let data = self.custom_data(&block_reward, &lock, &r#type)?;

let output = CellOutput::new(block_reward, data, lock, r#type);

Ok(TransactionBuilder::default()
.input(input)
.output(output)
Expand All @@ -429,23 +431,41 @@ impl<CS: ChainStore + 'static> BlockAssembler<CS> {

let witness = lock.into_witness();
let input = CellInput::new_cellbase_input(candidate_number);
let output = CellOutput::new(
block_reward,
self.config.data.clone().into_bytes(),
target_lock,
None,
);
// User-defined data has a risk of exceeding capacity
if output.is_lack_of_capacity()? {
return Err(TransactionError::InsufficientCellCapacity.into());
}

let r#type = None;

let data = self.custom_data(&block_reward, &target_lock, &r#type)?;

let output = CellOutput::new(block_reward, data, target_lock, r#type);

Ok(TransactionBuilder::default()
.input(input)
.output(output)
.witness(witness)
.build())
}

fn custom_data(
&self,
reward: &Capacity,
lock: &Script,
r#type: &Option<Script>,
) -> Result<Bytes, FailureError> {
let mut data = self.config.data.clone().into_bytes();

let data_max_len = (reward.as_u64()
- lock.occupied_capacity()?.as_u64()
- reward.occupied_capacity()?.as_u64()
- r#type.occupied_capacity()?.as_u64()) as usize;

// User-defined data has a risk of exceeding capacity
// just truncate it
if data.len() > data_max_len {
data.truncate(data_max_len);
}
Ok(data)
}

fn prepare_uncles(
&self,
current_epoch_ext: &EpochExt,
Expand Down
9 changes: 5 additions & 4 deletions resource/ckb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,14 @@ cell_output_cache_size = 128
#
# ckb cli secp256k1-lock <pubkey>
#
# Also, ckb allows the miner to add any data to the cellbase that he has dug out.
# The data must be A 0x-prefixed hex string. **Please note** that the data cannot be
# larger than the capacity value of the current cellbase. If it is larger,
# the block template cannot be obtained normally. That is, it is impossible to mine normally.
# Also, ckb allows the miners to add any data to the cellbase that they have dug out.
# The data must be A 0x-prefixed hex string.
#
# note: The data field is optional.
#
# **WARNING**: if data is larger than the capacity value of the current cellbase,
# it will be truncated
#
# Below is an example block assembler configuration section:
#
# [block_assembler]
Expand Down
7 changes: 5 additions & 2 deletions test/src/specs/mining/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use ckb_app_config::{BlockAssemblerConfig, CKBAppConfig};
use ckb_chain_spec::ChainSpec;
use ckb_core::block::Block;
use ckb_core::script::Script;
use ckb_core::Bytes;
use jsonrpc_types::JsonBytes;
use log::info;
use numext_fixed_hash::{h256, H256};
Expand Down Expand Up @@ -44,7 +45,9 @@ impl Spec for BootstrapCellbase {

let blk: Block = node.rpc_client().get_block(hash).unwrap().into();
assert!(
blk.transactions()[0].is_cellbase() && blk.transactions()[0].outputs()[0].lock == miner
blk.transactions()[0].is_cellbase()
&& blk.transactions()[0].outputs()[0].lock == miner
&& blk.transactions()[0].outputs()[0].data == Bytes::from(vec![1; 30])
)
}

Expand All @@ -66,7 +69,7 @@ impl Spec for BootstrapCellbase {
config.block_assembler = Some(BlockAssemblerConfig {
code_hash: h256!("0xa2"),
args: vec![],
data: JsonBytes::default(),
data: JsonBytes::from_bytes(Bytes::from(vec![1; 30])),
});
})
}
Expand Down

0 comments on commit 1227071

Please sign in to comment.