Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix block relay #2854

Merged
merged 3 commits into from
Sep 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions block-relayer/src/block_relayer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,14 @@ impl BlockRelayer {
compact_block: CompactBlock,
peer_id: PeerId,
) -> Result<Block> {
let txns = {
let mut txns: Vec<Option<SignedUserTransaction>> =
vec![None; compact_block.short_ids.len()];
let expect_txn_len = compact_block.txn_len();
let txns = if expect_txn_len == 0 {
vec![]
} else {
let mut txns: Vec<Option<SignedUserTransaction>> = vec![None; expect_txn_len];
BLOCK_RELAYER_METRICS
.block_txns_count
.set(compact_block.short_ids.len() as u64);

let expect_txn_len = compact_block.txn_len();

.set(expect_txn_len as u64);
let mut missing_txn_short_ids = HashSet::new();
// Fill the block txns by tx pool
for (index, short_id) in compact_block.short_ids.iter().enumerate() {
Expand Down Expand Up @@ -154,7 +153,7 @@ impl BlockRelayer {
}
let collect_txns = txns.into_iter().filter_map(|txn| txn).collect::<Vec<_>>();
ensure!(
collect_txns.len() != expect_txn_len,
collect_txns.len() == expect_txn_len,
"Fill compact block error, expect txn len: {}, but collect txn len: {}",
collect_txns.len(),
expect_txn_len
Expand Down
17 changes: 4 additions & 13 deletions network/tests/network_service_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,7 @@ async fn test_event_notify_receive() {
let msg_send = PeerMessage::new_compact_block(
network2.peer_id(),
CompactBlockMessage::new(
CompactBlock::new(
Block::new(BlockHeader::random(), BlockBody::new_empty()),
vec![],
),
CompactBlock::new(Block::new(BlockHeader::random(), BlockBody::new_empty())),
mock_block_info(1.into()),
),
);
Expand All @@ -171,18 +168,12 @@ async fn test_event_notify_receive_repeat_block() {

let msg_send1 = PeerMessage::new_compact_block(
network2.peer_id(),
CompactBlockMessage::new(
CompactBlock::new(block.clone(), vec![]),
mock_block_info(1.into()),
),
CompactBlockMessage::new(CompactBlock::new(block.clone()), mock_block_info(1.into())),
);

let msg_send2 = PeerMessage::new_compact_block(
network2.peer_id(),
CompactBlockMessage::new(
CompactBlock::new(block.clone(), vec![]),
mock_block_info(1.into()),
),
CompactBlockMessage::new(CompactBlock::new(block.clone()), mock_block_info(1.into())),
);

let mut receiver = network2.message_handler.channel();
Expand Down Expand Up @@ -265,7 +256,7 @@ async fn test_event_broadcast() {

let block = Block::new(BlockHeader::random(), BlockBody::new_empty());
let notification = NotificationMessage::CompactBlock(Box::new(CompactBlockMessage::new(
CompactBlock::new(block.clone(), vec![]),
CompactBlock::new(block.clone()),
//difficulty should > genesis block difficulty.
mock_block_info(10.into()),
)));
Expand Down
13 changes: 6 additions & 7 deletions types/src/cmpact_block.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::block::{Block, BlockHeader};
use crate::transaction::{SignedUserTransaction, Transaction};
use crate::transaction::SignedUserTransaction;
use bcs_ext::Sample;
use serde::{Deserialize, Serialize};
use starcoin_crypto::HashValue;
Expand All @@ -23,13 +23,14 @@ pub struct PrefilledTxn {
pub struct ShortId(pub HashValue);

impl CompactBlock {
pub fn new(block: Block, prefilled_txn: Vec<PrefilledTxn>) -> Self {
pub fn new(block: Block) -> Self {
let prefilled_txn: Vec<PrefilledTxn> = vec![];
let header = block.header;
let short_ids: Vec<ShortId> = block
.body
.transactions
.into_iter()
.map(|tx| Transaction::UserTransaction(tx).id())
.map(|tx| tx.id())
.map(ShortId)
.collect();
CompactBlock {
Expand All @@ -41,15 +42,13 @@ impl CompactBlock {
}

pub fn txn_len(&self) -> usize {
self.short_ids
.len()
.saturating_add(self.prefilled_txn.len())
self.short_ids.len()
}
}

impl From<Block> for CompactBlock {
fn from(block: Block) -> Self {
CompactBlock::new(block, vec![])
CompactBlock::new(block)
}
}

Expand Down