Skip to content

Commit b14af9c

Browse files
HoOngEemajecty
authored andcommitted
Remove retracted from ImportRoute
ImportRoute no longer allows retracted blocks or headers
1 parent e57497a commit b14af9c

File tree

8 files changed

+32
-102
lines changed

8 files changed

+32
-102
lines changed

core/src/blockchain/route.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,9 @@ pub fn tree_route(db: &dyn HeaderProvider, from: BlockHash, to: BlockHash) -> Op
110110
/// Import route for newly inserted block.
111111
#[derive(Debug, PartialEq)]
112112
pub struct ImportRoute {
113-
/// Blocks that were invalidated by new block.
114-
pub retracted: Vec<BlockHash>,
115113
/// Blocks that were validated by new block.
116114
pub enacted: Vec<BlockHash>,
117-
/// Blocks which are neither retracted nor enacted.
115+
/// Blocks which are not enacted.
118116
pub omitted: Vec<BlockHash>,
119117
}
120118

@@ -132,13 +130,11 @@ impl ImportRoute {
132130
let mut enacted = Vec::new();
133131
enacted.push(best_block_changed.new_best_hash().unwrap());
134132
ImportRoute {
135-
retracted: vec![],
136133
enacted,
137134
omitted,
138135
}
139136
}
140137
BestBlockChanged::None => ImportRoute {
141-
retracted: vec![],
142138
enacted: vec![],
143139
omitted,
144140
},
@@ -157,13 +153,11 @@ impl ImportRoute {
157153
} => {
158154
let enacted = vec![best_header_changed.new_best_hash().unwrap()];
159155
ImportRoute {
160-
retracted: vec![],
161156
enacted,
162157
omitted,
163158
}
164159
}
165160
BestHeaderChanged::None => ImportRoute {
166-
retracted: vec![],
167161
enacted: vec![],
168162
omitted,
169163
},
@@ -172,13 +166,12 @@ impl ImportRoute {
172166

173167
pub fn none() -> Self {
174168
ImportRoute {
175-
retracted: vec![],
176169
enacted: vec![],
177170
omitted: vec![],
178171
}
179172
}
180173

181174
pub fn is_none(&self) -> bool {
182-
self.retracted.is_empty() && self.enacted.is_empty() && self.omitted.is_empty()
175+
self.enacted.is_empty() && self.omitted.is_empty()
183176
}
184177
}

core/src/client/chain_notify.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2018-2019 Kodebox, Inc.
1+
// Copyright 2018-2020 Kodebox, Inc.
22
// This file is part of CodeChain.
33
//
44
// This program is free software: you can redistribute it and/or modify
@@ -25,7 +25,6 @@ pub trait ChainNotify: Send + Sync {
2525
_imported: Vec<BlockHash>,
2626
_invalid: Vec<BlockHash>,
2727
_enacted: Vec<BlockHash>,
28-
_retracted: Vec<BlockHash>,
2928
_sealed: Vec<BlockHash>,
3029
_new_best_proposal: Option<BlockHash>,
3130
) {
@@ -38,7 +37,6 @@ pub trait ChainNotify: Send + Sync {
3837
_imported: Vec<BlockHash>,
3938
_invalid: Vec<BlockHash>,
4039
_enacted: Vec<BlockHash>,
41-
_retracted: Vec<BlockHash>,
4240
_sealed: Vec<BlockHash>,
4341
) {
4442
// does nothing by default

core/src/client/client.rs

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -150,26 +150,16 @@ impl Client {
150150
imported: &[BlockHash],
151151
invalid: &[BlockHash],
152152
enacted: &[BlockHash],
153-
retracted: &[BlockHash],
154153
sealed: &[BlockHash],
155154
) {
156-
self.notify(|notify| {
157-
notify.new_blocks(
158-
imported.to_vec(),
159-
invalid.to_vec(),
160-
enacted.to_vec(),
161-
retracted.to_vec(),
162-
sealed.to_vec(),
163-
)
164-
});
155+
self.notify(|notify| notify.new_blocks(imported.to_vec(), invalid.to_vec(), enacted.to_vec(), sealed.to_vec()));
165156
}
166157

167158
pub fn new_headers(
168159
&self,
169160
imported: &[BlockHash],
170161
invalid: &[BlockHash],
171162
enacted: &[BlockHash],
172-
retracted: &[BlockHash],
173163
sealed: &[BlockHash],
174164
new_best_proposal: Option<BlockHash>,
175165
) {
@@ -178,7 +168,6 @@ impl Client {
178168
imported.to_vec(),
179169
invalid.to_vec(),
180170
enacted.to_vec(),
181-
retracted.to_vec(),
182171
sealed.to_vec(),
183172
new_best_proposal,
184173
);
@@ -273,9 +262,9 @@ impl Client {
273262
return
274263
}
275264

276-
let (enacted, retracted) = self.importer.calculate_enacted_retracted(&[route]);
277-
self.importer.miner.chain_new_blocks(self, &[], &[], &enacted, &retracted);
278-
self.new_blocks(&[], &[], &enacted, &retracted, &[]);
265+
let enacted = self.importer.extract_enacted(vec![route]);
266+
self.importer.miner.chain_new_blocks(self, &[], &[], &enacted);
267+
self.new_blocks(&[], &[], &enacted, &[]);
279268
}
280269

281270
fn block_number_ref(&self, id: &BlockId) -> Option<BlockNumber> {
@@ -664,9 +653,9 @@ impl ImportBlock for Client {
664653
cinfo!(CLIENT, "Imported sealed block #{} ({})", number, h);
665654
route
666655
};
667-
let (enacted, retracted) = self.importer.calculate_enacted_retracted(&[route]);
668-
self.importer.miner.chain_new_blocks(self, &[h], &[], &enacted, &retracted);
669-
self.new_blocks(&[h], &[], &enacted, &retracted, &[h]);
656+
let enacted = self.importer.extract_enacted(vec![route]);
657+
self.importer.miner.chain_new_blocks(self, &[h], &[], &enacted);
658+
self.new_blocks(&[h], &[], &enacted, &[h]);
670659
self.db().flush().expect("DB flush failed.");
671660
Ok(h)
672661
}

core/src/client/importer.rs

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2018-2019 Kodebox, Inc.
1+
// Copyright 2018-2020 Kodebox, Inc.
22
// This file is part of CodeChain.
33
//
44
// This program is free software: you can redistribute it and/or modify
@@ -33,7 +33,8 @@ use kvdb::DBTransaction;
3333
use parking_lot::{Mutex, MutexGuard};
3434
use rlp::Encodable;
3535
use std::borrow::Borrow;
36-
use std::collections::{HashMap, HashSet};
36+
use std::collections::HashSet;
37+
use std::iter::FromIterator;
3738
use std::sync::Arc;
3839

3940
pub struct Importer {
@@ -134,40 +135,22 @@ impl Importer {
134135
if !is_empty {
135136
ctrace!(CLIENT, "Call new_blocks even though block verification queue is not empty");
136137
}
137-
let (enacted, retracted) = self.calculate_enacted_retracted(&import_results);
138-
self.miner.chain_new_blocks(client, &imported_blocks, &invalid_blocks, &enacted, &retracted);
139-
client.new_blocks(&imported_blocks, &invalid_blocks, &enacted, &retracted, &[]);
138+
let enacted = self.extract_enacted(import_results);
139+
self.miner.chain_new_blocks(client, &imported_blocks, &invalid_blocks, &enacted);
140+
client.new_blocks(&imported_blocks, &invalid_blocks, &enacted, &[]);
140141
}
141142
}
142143

143144
client.db().flush().expect("DB flush failed.");
144145
imported
145146
}
146147

147-
pub fn calculate_enacted_retracted(&self, import_results: &[ImportRoute]) -> (Vec<BlockHash>, Vec<BlockHash>) {
148-
fn map_to_vec(map: Vec<(BlockHash, bool)>) -> Vec<BlockHash> {
149-
map.into_iter().map(|(k, _v)| k).collect()
150-
}
151-
152-
// In ImportRoute we get all the blocks that have been enacted and retracted by single insert.
153-
// Because we are doing multiple inserts some of the blocks that were enacted in import `k`
154-
// could be retracted in import `k+1`. This is why to understand if after all inserts
155-
// the block is enacted or retracted we iterate over all routes and at the end final state
156-
// will be in the hashmap
157-
let map = import_results.iter().fold(HashMap::new(), |mut map, route| {
158-
for hash in &route.enacted {
159-
map.insert(*hash, true);
160-
}
161-
for hash in &route.retracted {
162-
map.insert(*hash, false);
163-
}
164-
map
148+
pub fn extract_enacted(&self, import_results: Vec<ImportRoute>) -> Vec<BlockHash> {
149+
let set = import_results.into_iter().fold(HashSet::new(), |mut set, route| {
150+
set.extend(route.enacted);
151+
set
165152
});
166-
167-
// Split to enacted retracted (using hashmap value)
168-
let (enacted, retracted) = map.into_iter().partition(|&(_k, v)| v);
169-
// And convert tuples to keys
170-
(map_to_vec(enacted), map_to_vec(retracted))
153+
Vec::from_iter(set)
171154
}
172155

173156
// NOTE: the header of the block passed here is not necessarily sealed, as
@@ -336,7 +319,7 @@ impl Importer {
336319
}
337320

338321
self.header_queue.mark_as_bad(&bad.drain().collect::<Vec<_>>());
339-
let (enacted, retracted) = self.calculate_enacted_retracted(&routes);
322+
let enacted = self.extract_enacted(routes);
340323

341324
let new_best_proposal_header_hash = client.block_chain().best_proposal_header().hash();
342325
let best_proposal_header_changed = if prev_best_proposal_header_hash != new_best_proposal_header_hash {
@@ -349,7 +332,6 @@ impl Importer {
349332
&imported,
350333
&bad.iter().cloned().collect::<Vec<_>>(),
351334
&enacted,
352-
&retracted,
353335
&[],
354336
best_proposal_header_changed,
355337
);
@@ -370,7 +352,7 @@ impl Importer {
370352
client.db().write_buffered(batch);
371353
chain.commit();
372354
}
373-
client.new_headers(&[hash], &[], &[], &[], &[], None);
355+
client.new_headers(&[hash], &[], &[], &[], None);
374356

375357
client.db().flush().expect("DB flush failed.");
376358
}
@@ -388,8 +370,8 @@ impl Importer {
388370
client.db().write_buffered(batch);
389371
chain.commit();
390372
}
391-
self.miner.chain_new_blocks(client, &[hash], &[], &[], &[]);
392-
client.new_blocks(&[hash], &[], &[], &[], &[]);
373+
self.miner.chain_new_blocks(client, &[hash], &[], &[]);
374+
client.new_blocks(&[hash], &[], &[], &[]);
393375

394376
client.db().flush().expect("DB flush failed.");
395377
}

core/src/consensus/tendermint/chain_notify.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ impl ChainNotify for TendermintChainNotify {
3838
imported: Vec<BlockHash>,
3939
_invalid: Vec<BlockHash>,
4040
enacted: Vec<BlockHash>,
41-
_retracted: Vec<BlockHash>,
4241
_sealed: Vec<BlockHash>,
4342
) {
4443
self.inner

core/src/miner/miner.rs

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -598,30 +598,11 @@ impl MinerService for Miner {
598598
self.mem_pool.write().set_limit(limit)
599599
}
600600

601-
fn chain_new_blocks<C>(
602-
&self,
603-
chain: &C,
604-
_imported: &[BlockHash],
605-
_invalid: &[BlockHash],
606-
_enacted: &[BlockHash],
607-
retracted: &[BlockHash],
608-
) where
601+
fn chain_new_blocks<C>(&self, chain: &C, _imported: &[BlockHash], _invalid: &[BlockHash], _enacted: &[BlockHash])
602+
where
609603
C: AccountData + BlockChainTrait + BlockProducer + EngineInfo + ImportBlock, {
610604
ctrace!(MINER, "chain_new_blocks");
611605

612-
// Then import all transactions...
613-
{
614-
let mut mem_pool = self.mem_pool.write();
615-
for hash in retracted {
616-
let block = chain.block(&(*hash).into()).expect(
617-
"Client is sending message after commit to db and inserting to chain; the block is available; qed",
618-
);
619-
let transactions = block.transactions();
620-
let _ = self.add_transactions_to_pool(chain, transactions, TxOrigin::RetractedBlock, &mut mem_pool);
621-
}
622-
}
623-
624-
// ...and at the end remove the old ones
625606
{
626607
let fetch_account = |p: &Public| {
627608
let address = public_to_address(p);

core/src/miner/mod.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2018-2019 Kodebox, Inc.
1+
// Copyright 2018-2020 Kodebox, Inc.
22
// This file is part of CodeChain.
33
//
44
// This program is free software: you can redistribute it and/or modify
@@ -63,14 +63,8 @@ pub trait MinerService: Send + Sync {
6363
fn set_transactions_limit(&self, limit: usize);
6464

6565
/// Called when blocks are imported to chain, updates transactions queue.
66-
fn chain_new_blocks<C>(
67-
&self,
68-
chain: &C,
69-
imported: &[BlockHash],
70-
invalid: &[BlockHash],
71-
enacted: &[BlockHash],
72-
retracted: &[BlockHash],
73-
) where
66+
fn chain_new_blocks<C>(&self, chain: &C, imported: &[BlockHash], invalid: &[BlockHash], enacted: &[BlockHash])
67+
where
7468
C: AccountData + BlockChainTrait + BlockProducer + EngineInfo + ImportBlock;
7569

7670
/// Get the type of consensus engine.

sync/src/block/extension.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -666,9 +666,8 @@ impl NetworkExtension<Event> for Extension {
666666
Event::NewHeaders {
667667
imported,
668668
enacted,
669-
retracted,
670669
} => {
671-
self.new_headers(imported, enacted, retracted);
670+
self.new_headers(imported, enacted);
672671
}
673672
Event::NewBlocks {
674673
imported,
@@ -685,7 +684,6 @@ pub enum Event {
685684
NewHeaders {
686685
imported: Vec<BlockHash>,
687686
enacted: Vec<BlockHash>,
688-
retracted: Vec<BlockHash>,
689687
},
690688
NewBlocks {
691689
imported: Vec<BlockHash>,
@@ -694,7 +692,7 @@ pub enum Event {
694692
}
695693

696694
impl Extension {
697-
fn new_headers(&mut self, imported: Vec<BlockHash>, enacted: Vec<BlockHash>, retracted: Vec<BlockHash>) {
695+
fn new_headers(&mut self, imported: Vec<BlockHash>, enacted: Vec<BlockHash>) {
698696
if let State::Full = self.state {
699697
for peer in self.header_downloaders.values_mut() {
700698
peer.mark_as_imported(imported.clone());
@@ -715,7 +713,6 @@ impl Extension {
715713
for header in headers {
716714
self.body_downloader.add_target(&header.decode());
717715
}
718-
self.body_downloader.remove_target(&retracted);
719716
}
720717
}
721718

@@ -1184,15 +1181,13 @@ impl ChainNotify for BlockSyncSender {
11841181
imported: Vec<BlockHash>,
11851182
_invalid: Vec<BlockHash>,
11861183
enacted: Vec<BlockHash>,
1187-
retracted: Vec<BlockHash>,
11881184
_sealed: Vec<BlockHash>,
11891185
_new_best_proposal: Option<BlockHash>,
11901186
) {
11911187
self.0
11921188
.send(Event::NewHeaders {
11931189
imported,
11941190
enacted,
1195-
retracted,
11961191
})
11971192
.unwrap();
11981193
}
@@ -1202,7 +1197,6 @@ impl ChainNotify for BlockSyncSender {
12021197
imported: Vec<BlockHash>,
12031198
invalid: Vec<BlockHash>,
12041199
_enacted: Vec<BlockHash>,
1205-
_retracted: Vec<BlockHash>,
12061200
_sealed: Vec<BlockHash>,
12071201
) {
12081202
self.0

0 commit comments

Comments
 (0)