Skip to content

Commit cfa0da3

Browse files
committed
connect a dag block
1. update MinedBlock and NewBranch message 2. add parent_hash function to block 3. update verifier
1 parent ff353e1 commit cfa0da3

File tree

12 files changed

+76
-176
lines changed

12 files changed

+76
-176
lines changed

block-relayer/src/block_relayer.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,14 @@ impl BlockRelayer {
7878
&self,
7979
network: NetworkServiceRef,
8080
executed_block: Arc<ExecutedBlock>,
81-
tips_hash: Option<Vec<HashValue>>,
8281
) {
8382
if !self.is_nearly_synced() {
8483
debug!("[block-relay] Ignore NewHeadBlock event because the node has not been synchronized yet.");
8584
return;
8685
}
8786
let compact_block = executed_block.block().clone().into();
8887
let compact_block_msg =
89-
CompactBlockMessage::new(compact_block, executed_block.block_info.clone(), tips_hash);
88+
CompactBlockMessage::new(compact_block, executed_block.block_info.clone());
9089
network.broadcast(NotificationMessage::CompactBlock(Box::new(
9190
compact_block_msg,
9291
)));
@@ -310,7 +309,7 @@ impl EventHandler<Self, NewBranch> for BlockRelayer {
310309
return;
311310
}
312311
};
313-
self.broadcast_compact_block(network, event.0, event.1);
312+
self.broadcast_compact_block(network, event.0);
314313
}
315314
}
316315

chain/src/chain.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -465,13 +465,15 @@ impl BlockChain {
465465
.storage
466466
.get_block_by_hash(blue.id())?
467467
.expect("block blue need exist");
468-
transactions.extend(
469-
blue_block
470-
.transactions()
471-
.iter()
472-
.cloned()
473-
.map(Transaction::UserTransaction),
474-
);
468+
// Todo: we already added txns of blue_blocks to target block when mining it.
469+
// see create_block_template in MinerService
470+
//transactions.extend(
471+
// blue_block
472+
// .transactions()
473+
// .iter()
474+
// .cloned()
475+
// .map(Transaction::UserTransaction),
476+
//);
475477
total_difficulty += blue_block.header.difficulty();
476478
}
477479
transactions.extend(

chain/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
#![deny(clippy::integer_arithmetic)]
44
mod chain;
55
pub mod verifier;
6-
pub use chain::{BlockChain, ChainStatusWithBlock};
6+
pub use chain::BlockChain;
77
pub use starcoin_chain_api::{ChainReader, ChainWriter};

chain/src/verifier/mod.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ pub trait BlockVerifier {
9393
R: ChainReader,
9494
{
9595
let epoch = current_chain.epoch();
96+
let is_dag = header.is_dag();
9697

9798
let switch_epoch = header.number() == epoch.end_block_number();
9899
// epoch first block's uncles should empty.
@@ -128,10 +129,18 @@ pub trait BlockVerifier {
128129

129130
verify_block!(
130131
VerifyBlockField::Uncle,
131-
uncle.number() < header.number() ,
132+
uncle.number() < header.number(),
132133
"uncle block number bigger than or equal to current block ,uncle block number is {} , current block number is {}", uncle.number(), header.number()
133134
);
134135

136+
if is_dag {
137+
verify_block!(
138+
VerifyBlockField::Uncle,
139+
uncle.number() <= header.number(),
140+
"For a Dag block, uncle block number bigger than current block ,uncle block number is {} , current block number is {}", uncle.number(), header.number()
141+
);
142+
}
143+
135144
verify_block!(
136145
VerifyBlockField::Uncle,
137146
Self::can_be_uncle(current_chain, uncle)?,
@@ -182,15 +191,18 @@ impl BlockVerifier for BasicVerifier {
182191
let expect_number = current.number().saturating_add(1);
183192

184193
// dag
194+
// todo: For a dag block
195+
// 1. It could be the first dag block, the chain is just a normal single chain
196+
// 2. Or, both block and dag are created for flexidag
185197
if chain_status.tips_hash.is_some() {
198+
// todo: make sure current block is a dag block
186199
let mut tips_hash = chain_status.tips_hash.clone().unwrap();
187200
tips_hash.sort();
188201

189202
// if it is a dag block
190203
if HashValue::sha3_256_of(&tips_hash.encode().expect("hash encode must be successful"))
191204
!= new_block_parent
192205
{
193-
// or a block of a single chain
194206
verify_block!(
195207
VerifyBlockField::Header,
196208
expect_number == new_block_header.number(),
@@ -209,6 +221,7 @@ impl BlockVerifier for BasicVerifier {
209221
);
210222
}
211223
} else {
224+
// todo: handle the first dag block
212225
// or a block of a single chain
213226
verify_block!(
214227
VerifyBlockField::Header,

miner/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,10 @@ impl MinerService {
251251
}
252252

253253
if let Some(task) = self.current_task.take() {
254-
let tips_header = task.block_template.parents_hash.clone();
255254
let block = task.finish(nonce, extra);
256255
let block_hash: HashValue = block.id();
257256
info!(target: "miner", "Mint new block: {}", block);
258-
ctx.broadcast(MinedBlock(Arc::new(block), tips_header));
257+
ctx.broadcast(MinedBlock(Arc::new(block)));
259258
if let Some(metrics) = self.metrics.as_ref() {
260259
metrics.block_mint_count.inc();
261260
}

network/api/src/messages.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,26 +48,20 @@ impl Sample for TransactionsMessage {
4848
pub struct CompactBlockMessage {
4949
pub compact_block: CompactBlock,
5050
pub block_info: BlockInfo,
51-
pub tips_hash: Option<Vec<HashValue>>,
5251
}
5352

5453
impl CompactBlockMessage {
55-
pub fn new(
56-
compact_block: CompactBlock,
57-
block_info: BlockInfo,
58-
tips_hash: Option<Vec<HashValue>>,
59-
) -> Self {
54+
pub fn new(compact_block: CompactBlock, block_info: BlockInfo) -> Self {
6055
Self {
6156
compact_block,
6257
block_info,
63-
tips_hash,
6458
}
6559
}
6660
}
6761

6862
impl Sample for CompactBlockMessage {
6963
fn sample() -> Self {
70-
Self::new(CompactBlock::sample(), BlockInfo::sample(), None)
64+
Self::new(CompactBlock::sample(), BlockInfo::sample())
7165
}
7266
}
7367

network/src/service.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,10 @@ impl Inner {
559559
peer_info.peer_info.update_chain_status(ChainStatus::new(
560560
block_header.clone(),
561561
compact_block_message.block_info.clone(),
562-
compact_block_message.tips_hash.clone(),
562+
compact_block_message
563+
.compact_block
564+
.dag_parent_and_tips()
565+
.map(|s| s.1.iter().map(|h| h.id()).collect::<Vec<_>>()),
563566
));
564567

565568
if self.self_peer.known_blocks.contains(&block_id) {
@@ -721,7 +724,9 @@ impl Inner {
721724
ChainStatus::new(
722725
msg.compact_block.header.clone(),
723726
msg.block_info.clone(),
724-
msg.tips_hash.clone(),
727+
msg.compact_block
728+
.dag_parent_and_tips()
729+
.map(|s| s.1.iter().map(|h| h.id()).collect::<Vec<_>>()),
725730
)
726731
.encode()
727732
.expect("Encoding the compact_block.header and block_info must be successful"),

sync/src/block_connector/block_connector_service.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ where
261261
TransactionPoolServiceT: TxPoolSyncService + 'static,
262262
{
263263
fn handle_event(&mut self, msg: MinedBlock, _ctx: &mut ServiceContext<Self>) {
264-
let MinedBlock(new_block, _tips_headers) = msg;
264+
let MinedBlock(new_block) = msg;
265265
let id = new_block.header().id();
266266
debug!("try connect mined block: {}", id);
267267

0 commit comments

Comments
 (0)