Skip to content

Commit 409c145

Browse files
committed
Remove retracted from ImportRoute
ImportRoute no longer allows retracted blocks or headers
1 parent 72a8961 commit 409c145

File tree

8 files changed

+34
-102
lines changed

8 files changed

+34
-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(&[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(&[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: 16 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,7 @@ 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;
3737
use std::sync::Arc;
3838

3939
pub struct Importer {
@@ -134,40 +134,24 @@ impl Importer {
134134
if !is_empty {
135135
ctrace!(CLIENT, "Call new_blocks even though block verification queue is not empty");
136136
}
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, &[]);
137+
let enacted = self.extract_enacted(&import_results);
138+
self.miner.chain_new_blocks(client, &imported_blocks, &invalid_blocks, &enacted);
139+
client.new_blocks(&imported_blocks, &invalid_blocks, &enacted, &[]);
140140
}
141141
}
142142

143143
client.db().flush().expect("DB flush failed.");
144144
imported
145145
}
146146

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
147+
pub fn extract_enacted(&self, import_results: &[ImportRoute]) -> Vec<BlockHash> {
148+
let set = import_results.iter().fold(HashSet::new(), |mut set, route| {
149+
route.enacted.iter().for_each(|hash| {
150+
set.insert(*hash);
151+
});
152+
set
165153
});
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))
154+
set.into_iter().collect()
171155
}
172156

173157
// NOTE: the header of the block passed here is not necessarily sealed, as
@@ -336,7 +320,7 @@ impl Importer {
336320
}
337321

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

341325
let new_best_proposal_header_hash = client.block_chain().best_proposal_header().hash();
342326
let best_proposal_header_changed = if prev_best_proposal_header_hash != new_best_proposal_header_hash {
@@ -349,7 +333,6 @@ impl Importer {
349333
&imported,
350334
&bad.iter().cloned().collect::<Vec<_>>(),
351335
&enacted,
352-
&retracted,
353336
&[],
354337
best_proposal_header_changed,
355338
);
@@ -370,7 +353,7 @@ impl Importer {
370353
client.db().write_buffered(batch);
371354
chain.commit();
372355
}
373-
client.new_headers(&[hash], &[], &[], &[], &[], None);
356+
client.new_headers(&[hash], &[], &[], &[], None);
374357

375358
client.db().flush().expect("DB flush failed.");
376359
}
@@ -388,8 +371,8 @@ impl Importer {
388371
client.db().write_buffered(batch);
389372
chain.commit();
390373
}
391-
self.miner.chain_new_blocks(client, &[hash], &[], &[], &[]);
392-
client.new_blocks(&[hash], &[], &[], &[], &[]);
374+
self.miner.chain_new_blocks(client, &[hash], &[], &[]);
375+
client.new_blocks(&[hash], &[], &[], &[]);
393376

394377
client.db().flush().expect("DB flush failed.");
395378
}

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 & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -598,29 +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-
624606
// ...and at the end remove the old ones
625607
{
626608
let fetch_account = |p: &Public| {

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: 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
@@ -669,9 +669,8 @@ impl NetworkExtension<Event> for Extension {
669669
Event::NewHeaders {
670670
imported,
671671
enacted,
672-
retracted,
673672
} => {
674-
self.new_headers(imported, enacted, retracted);
673+
self.new_headers(imported, enacted);
675674
}
676675
Event::NewBlocks {
677676
imported,
@@ -688,7 +687,6 @@ pub enum Event {
688687
NewHeaders {
689688
imported: Vec<BlockHash>,
690689
enacted: Vec<BlockHash>,
691-
retracted: Vec<BlockHash>,
692690
},
693691
NewBlocks {
694692
imported: Vec<BlockHash>,
@@ -697,7 +695,7 @@ pub enum Event {
697695
}
698696

699697
impl Extension {
700-
fn new_headers(&mut self, imported: Vec<BlockHash>, enacted: Vec<BlockHash>, retracted: Vec<BlockHash>) {
698+
fn new_headers(&mut self, imported: Vec<BlockHash>, enacted: Vec<BlockHash>) {
701699
if let State::Full = self.state {
702700
for peer in self.header_downloaders.values_mut() {
703701
peer.mark_as_imported(imported.clone());
@@ -723,7 +721,6 @@ impl Extension {
723721
let is_empty = header.transactions_root() == parent.transactions_root();
724722
self.body_downloader.add_target(&header.decode(), is_empty);
725723
}
726-
self.body_downloader.remove_target(&retracted);
727724
}
728725
}
729726

@@ -1192,15 +1189,13 @@ impl ChainNotify for BlockSyncSender {
11921189
imported: Vec<BlockHash>,
11931190
_invalid: Vec<BlockHash>,
11941191
enacted: Vec<BlockHash>,
1195-
retracted: Vec<BlockHash>,
11961192
_sealed: Vec<BlockHash>,
11971193
_new_best_proposal: Option<BlockHash>,
11981194
) {
11991195
self.0
12001196
.send(Event::NewHeaders {
12011197
imported,
12021198
enacted,
1203-
retracted,
12041199
})
12051200
.unwrap();
12061201
}
@@ -1210,7 +1205,6 @@ impl ChainNotify for BlockSyncSender {
12101205
imported: Vec<BlockHash>,
12111206
invalid: Vec<BlockHash>,
12121207
_enacted: Vec<BlockHash>,
1213-
_retracted: Vec<BlockHash>,
12141208
_sealed: Vec<BlockHash>,
12151209
) {
12161210
self.0

0 commit comments

Comments
 (0)