Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
1680: fix: get_block_transactions_process r=u2,quake a=zhangsoledad



1689: chore: issuance comment r=u2,quake a=zhangsoledad

comments were lost on rebase

1690: ci: Make sure cargo-audit up-to-date r=quake,u2 a=zhangsoledad

rustsec/rustsec#143
https://internals.rust-lang.org/t/idea-cargo-install-update/11072
rust-lang/cargo#6797



1691: chore: speed up maturity test r=u2,quake a=zhangsoledad



1693: test: simple header field json format check r=quake,u2 a=zhangsoledad



1696: fix: set `next_epoch_diff` to one instead of panic when it is zero r=zhangsoledad,u2 a=doitian



Co-authored-by: zhangsoledad <787953403@qq.com>
Co-authored-by: ian <ian@nervos.org>
  • Loading branch information
3 people committed Oct 10, 2019
7 parents b1c0173 + ecc0d7d + e346371 + 014fe92 + 6759803 + e5e9f7d + 103247c commit 7784bc7
Show file tree
Hide file tree
Showing 12 changed files with 125 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .travis.yml
Expand Up @@ -80,6 +80,11 @@ matrix:
- make check-cargotoml
- make check-whitespaces
- make check-dirty-rpc-doc
- name: Security Audit
if: 'tag IS NOT present AND (type = pull_request OR branch in (master, staging, trying) OR repo != nervosnetwork/ckb)'
os: linux
rust: nightly
script: make security-audit

- name: Test benchmarks on Linux
env: CACHE_NAME=bench
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Makefile
Expand Up @@ -107,8 +107,8 @@ clippy: setup-ckb-test ## Run linter to examine Rust source codes.

.PHONY: security-audit
security-audit: ## Use cargo-audit to audit Cargo.lock for crates with security vulnerabilities.
@cargo audit --version || cargo install cargo-audit
@cargo audit
@cargo +nightly install cargo-audit -Z install-upgrade
cargo audit
# expecting to see "Success No vulnerable packages found"

.PHONY: bench-test
Expand Down
28 changes: 14 additions & 14 deletions spec/src/consensus.rs
Expand Up @@ -24,8 +24,9 @@ use ckb_types::{
use std::cmp;
use std::sync::Arc;

// TODO: add secondary reward for miner
// 1.344 billion per year
pub(crate) const DEFAULT_SECONDARY_EPOCH_REWARD: Capacity = Capacity::shannons(613_698_63013698);
// 4.2 billion per year
pub(crate) const INITIAL_PRIMARY_EPOCH_REWARD: Capacity = Capacity::shannons(1_917_808_21917808);
const MAX_UNCLE_NUM: usize = 2;
pub(crate) const TX_PROPOSAL_WINDOW: ProposalWindow = ProposalWindow(2, 10);
Expand All @@ -34,7 +35,7 @@ pub(crate) const TX_PROPOSAL_WINDOW: ProposalWindow = ProposalWindow(2, 10);
// This is to reduce the risk of later txs being reversed if a chain reorganization occurs.
pub(crate) const CELLBASE_MATURITY: EpochNumberWithFraction =
EpochNumberWithFraction::new_unchecked(4, 0, 1);
// TODO: should adjust this value based on CKB average block time

const MEDIAN_TIME_BLOCK_COUNT: usize = 37;

// dampening factor
Expand Down Expand Up @@ -622,35 +623,34 @@ impl Consensus {
&adjusted_last_epoch_hash_rate * epoch_duration_target,
U256::one(),
);
let next_epoch_diff = if bound {
let diff_denominator = if bound {
if last_orphan_rate.is_zero() {
let denominator = U256::from(next_epoch_length);
(diff_numerator / denominator).into_u256()
RationalU256::new(next_epoch_length_u256, U256::one())
} else {
let orphan_rate_estimation_recip = ((&last_orphan_rate + U256::one())
* &epoch_duration_target_u256
* &last_epoch_length_u256
/ (&last_orphan_rate * &last_epoch_duration * &next_epoch_length_u256))
.saturating_sub_u256(U256::one());

let denominator = if orphan_rate_estimation_recip.is_zero() {
if orphan_rate_estimation_recip.is_zero() {
// small probability event, use o_ideal for now
(orphan_rate_target + U256::one()) * next_epoch_length_u256
} else {
let orphan_rate_estimation = RationalU256::one() / orphan_rate_estimation_recip;
(orphan_rate_estimation + U256::one()) * next_epoch_length_u256
};
(diff_numerator / denominator).into_u256()
}
}
} else {
let denominator = (orphan_rate_target + U256::one()) * next_epoch_length_u256;
(diff_numerator / denominator).into_u256()
(orphan_rate_target + U256::one()) * next_epoch_length_u256
};

debug_assert!(
next_epoch_diff > U256::zero(),
"next_epoch_diff should greater than one"
);
let next_epoch_diff = if diff_numerator > diff_denominator {
(diff_numerator / diff_denominator).into_u256()
} else {
// next_epoch_diff cannot be zero
U256::one()
};

let primary_epoch_reward = self.primary_epoch_reward_of_next_epoch(last_epoch).as_u64();
let block_reward = Capacity::shannons(primary_epoch_reward / next_epoch_length);
Expand Down
2 changes: 0 additions & 2 deletions sync/src/relayer/compact_block_process.rs
Expand Up @@ -226,8 +226,6 @@ impl<'a> CompactBlockProcess<'a> {
}
}

assert!(!missing_transactions.is_empty() || !missing_uncles.is_empty());

pending_compact_blocks
.entry(block_hash.clone())
.or_insert_with(|| (compact_block, HashMap::default()))
Expand Down
8 changes: 8 additions & 0 deletions sync/src/relayer/get_block_transactions_process.rs
Expand Up @@ -49,9 +49,17 @@ impl<'a> GetBlockTransactionsProcess<'a> {
})
.collect::<Vec<_>>();

let uncles = self
.message
.uncle_indexes()
.iter()
.filter_map(|i| block.uncles().get(Unpack::<u32>::unpack(&i) as usize))
.collect::<Vec<_>>();

let content = packed::BlockTransactions::new_builder()
.block_hash(block_hash)
.transactions(transactions.into_iter().map(|tx| tx.data()).pack())
.uncles(uncles.into_iter().map(|uncle| uncle.data()).pack())
.build();
let message = packed::RelayMessage::new_builder().set(content).build();
let data = message.as_slice().into();
Expand Down
1 change: 1 addition & 0 deletions test/src/main.rs
Expand Up @@ -288,6 +288,7 @@ fn all_specs() -> SpecMap {
Box::new(ProposeButNotCommit),
Box::new(ProposeDuplicated),
Box::new(ForkedTransaction),
Box::new(MissingUncleRequest),
];
specs.into_iter().map(|spec| (spec.name(), spec)).collect()
}
Expand Down
77 changes: 77 additions & 0 deletions test/src/specs/relay/get_block_transactions_process.rs
@@ -0,0 +1,77 @@
use crate::{Net, Spec, TestProtocol};
use ckb_sync::NetworkProtocol;
use ckb_types::{
core::UncleBlockView,
packed::{self, RelayMessage},
prelude::*,
};

pub struct MissingUncleRequest;

impl Spec for MissingUncleRequest {
crate::name!("missing_uncle_request");

crate::setup!(protocols: vec![TestProtocol::sync(), TestProtocol::relay()]);

// Case: Send to node GetBlockTransactions with missing uncle index, node should response BlockTransactions with uncles
fn run(&self, net: &mut Net) {
net.exit_ibd_mode();
let node = &net.nodes[0];
net.connect(node);
let (peer_id, _, _) = net.receive();

node.generate_block();
let _ = net.receive();

let builder = node.new_block_builder(None, None, None);
let block1 = builder.clone().nonce(0.pack()).build();
let block2 = builder.clone().nonce(1.pack()).build();
node.submit_block(&block1.data());
node.submit_block(&block2.data());

let builder = node.new_block_builder(None, None, None);
let block = builder
.clone()
.set_uncles(vec![block2.as_uncle()])
.nonce(0.pack())
.build();
node.submit_block(&block.data());

let content = packed::GetBlockTransactions::new_builder()
.block_hash(block.hash())
.uncle_indexes(vec![0u32].pack())
.build();
let message = packed::RelayMessage::new_builder().set(content).build();

(0..3).for_each(|_| {
net.receive(); // ignore three new block announce
});

net.send(
NetworkProtocol::RELAY.into(),
peer_id,
message.as_slice().into(),
);

let (_, _, data) = net.receive();
let message = RelayMessage::from_slice(&data).unwrap();

assert_eq!(
message.to_enum().item_name(),
packed::BlockTransactions::NAME,
"Node should reponse BlockTransactions message",
);

if let packed::RelayMessageUnionReader::BlockTransactions(reader) =
message.to_enum().as_reader()
{
let block_transactions = reader.to_entity();
let received_uncles: Vec<UncleBlockView> = block_transactions
.uncles()
.into_iter()
.map(|uncle| uncle.into_view())
.collect();
assert_eq!(received_uncles[0], block2.as_uncle());
}
}
}
2 changes: 2 additions & 0 deletions test/src/specs/relay/mod.rs
@@ -1,9 +1,11 @@
mod block_relay;
mod compact_block;
mod get_block_transactions_process;
mod transaction_relay;
mod transaction_relay_low_fee_rate;

pub use block_relay::*;
pub use compact_block::*;
pub use get_block_transactions_process::*;
pub use transaction_relay::*;
pub use transaction_relay_low_fee_rate::TransactionRelayLowFeeRate;
2 changes: 2 additions & 0 deletions test/src/specs/tx_pool/reference_header_maturity.rs
Expand Up @@ -108,6 +108,8 @@ impl Spec for ReferenceHeaderMaturity {
fn modify_chain_spec(&self) -> Box<dyn Fn(&mut ChainSpec) -> ()> {
Box::new(|spec_config| {
spec_config.params.cellbase_maturity = CELLBASE_MATURITY_VALUE;
spec_config.params.epoch_duration_target = 30;
spec_config.params.genesis_epoch_length = 5;
})
}
}
1 change: 1 addition & 0 deletions util/jsonrpc-types/Cargo.toml
Expand Up @@ -16,3 +16,4 @@ jsonrpc-core = "10.1"
[dev-dependencies]
proptest = "0.9"
regex = "1.1"
lazy_static = "1.3"
12 changes: 12 additions & 0 deletions util/jsonrpc-types/src/blockchain.rs
Expand Up @@ -685,7 +685,9 @@ impl From<core::BlockReward> for BlockReward {
mod tests {
use super::*;
use ckb_types::{bytes::Bytes, core::TransactionBuilder, packed::Byte32};
use lazy_static::lazy_static;
use proptest::{collection::size_range, prelude::*};
use regex::Regex;

fn mock_script(arg: Bytes) -> packed::Script {
packed::ScriptBuilder::default()
Expand Down Expand Up @@ -737,11 +739,21 @@ mod tests {
let encoded = serde_json::to_string(&json_block).unwrap();
let decode: BlockView = serde_json::from_str(&encoded).unwrap();
let decode_block: core::BlockView = decode.into();
header_field_format_check(&encoded);
prop_assert_eq!(decode_block.data(), block.data());
prop_assert_eq!(decode_block, block);
Ok(())
}

fn header_field_format_check(json: &str) {
lazy_static! {
static ref RE: Regex = Regex::new("\"(version|compact_target|parent_hash|timestamp|number|epoch|transactions_root|proposals_hash|uncles_hash|dao|nonce)\":\"(?P<value>.*?\")").unwrap();
}
for caps in RE.captures_iter(json) {
assert!(&caps["value"].starts_with("0x"));
}
}

proptest! {
#[test]
fn test_block_convert(
Expand Down

0 comments on commit 7784bc7

Please sign in to comment.