Skip to content

Commit e57497a

Browse files
HoOngEemajecty
authored andcommitted
Remove the enum variant BranchBecomingCanonChain
In tendermint consensus algorithm branch is not allowed so that Foundry don't have to handle such cases anymore.
1 parent 9274e92 commit e57497a

File tree

5 files changed

+23
-122
lines changed

5 files changed

+23
-122
lines changed

core/src/blockchain/block_info.rs

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2018 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
@@ -14,7 +14,6 @@
1414
// You should have received a copy of the GNU Affero General Public License
1515
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1616

17-
use super::route::TreeRoute;
1817
use crate::views::{BlockView, HeaderView};
1918
use ctypes::BlockHash;
2019
use primitives::Bytes;
@@ -28,13 +27,6 @@ pub enum BestBlockChanged {
2827
},
2928
/// Nothing changed.
3029
None,
31-
/// It's part of the fork which should become canon chain,
32-
/// because its total score is higher than current
33-
/// canon chain score.
34-
BranchBecomingCanonChain {
35-
best_block: Bytes,
36-
tree_route: TreeRoute,
37-
},
3830
}
3931

4032
impl BestBlockChanged {
@@ -47,10 +39,6 @@ impl BestBlockChanged {
4739
BestBlockChanged::CanonChainAppended {
4840
best_block,
4941
} => best_block,
50-
BestBlockChanged::BranchBecomingCanonChain {
51-
best_block,
52-
..
53-
} => best_block,
5442
BestBlockChanged::None => return None,
5543
};
5644

@@ -67,13 +55,6 @@ pub enum BestHeaderChanged {
6755
},
6856
/// Nothing changed.
6957
None,
70-
/// It's part of the fork which should become canon chain,
71-
/// because its total score is higher than current
72-
/// canon chain score.
73-
BranchBecomingCanonChain {
74-
best_header: Vec<u8>,
75-
tree_route: TreeRoute,
76-
},
7758
}
7859

7960
impl BestHeaderChanged {
@@ -86,10 +67,6 @@ impl BestHeaderChanged {
8667
BestHeaderChanged::CanonChainAppended {
8768
best_header,
8869
} => best_header,
89-
BestHeaderChanged::BranchBecomingCanonChain {
90-
best_header,
91-
..
92-
} => best_header,
9370
BestHeaderChanged::None => return None,
9471
};
9572

core/src/blockchain/blockchain.rs

Lines changed: 10 additions & 5 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
@@ -256,10 +256,15 @@ impl BlockChain {
256256
0 => BestBlockChanged::CanonChainAppended {
257257
best_block: new_best_block,
258258
},
259-
_ => BestBlockChanged::BranchBecomingCanonChain {
260-
tree_route: route,
261-
best_block: new_best_block,
262-
},
259+
_ => {
260+
cerror!(
261+
BLOCKCHAIN,
262+
"Older/Forked block header #{}({}) is inserted as a new block",
263+
new_header.number(),
264+
new_header.hash()
265+
);
266+
BestBlockChanged::None
267+
}
263268
}
264269
} else {
265270
BestBlockChanged::None

core/src/blockchain/body_db.rs

Lines changed: 1 addition & 47 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
@@ -140,11 +140,6 @@ impl BodyDB {
140140
&self,
141141
best_block_changed: &BestBlockChanged,
142142
) -> HashMap<TxHash, Option<TransactionAddress>> {
143-
let block_hash = if let Some(best_block_hash) = best_block_changed.new_best_hash() {
144-
best_block_hash
145-
} else {
146-
return HashMap::new()
147-
};
148143
let block = match best_block_changed.best_block() {
149144
Some(block) => block,
150145
None => return HashMap::new(),
@@ -155,27 +150,6 @@ impl BodyDB {
155150
BestBlockChanged::CanonChainAppended {
156151
..
157152
} => tx_hash_and_address_entries(best_block_changed.new_best_hash().unwrap(), tx_hashes).collect(),
158-
BestBlockChanged::BranchBecomingCanonChain {
159-
tree_route,
160-
..
161-
} => {
162-
let enacted = tree_route.enacted.iter().flat_map(|hash| {
163-
let body = self.block_body(hash).expect("Enacted block must be in database.");
164-
let enacted_tx_hashes = body.transaction_hashes();
165-
tx_hash_and_address_entries(*hash, enacted_tx_hashes)
166-
});
167-
168-
let current_addresses = { tx_hash_and_address_entries(block_hash, tx_hashes) };
169-
170-
let retracted = tree_route.retracted.iter().flat_map(|hash| {
171-
let body = self.block_body(&hash).expect("Retracted block must be in database.");
172-
let retracted_tx_hashes = body.transaction_hashes().into_iter();
173-
retracted_tx_hashes.map(|hash| (hash, None))
174-
});
175-
176-
// The order here is important! Don't remove transactions if it was part of enacted blocks as well.
177-
retracted.chain(enacted).chain(current_addresses).collect()
178-
}
179153
BestBlockChanged::None => HashMap::new(),
180154
}
181155
}
@@ -204,26 +178,6 @@ impl BodyDB {
204178
Box::new(::std::iter::empty()),
205179
Box::new(tracker_and_addresses_entries(block_hash, block.transactions())),
206180
),
207-
BestBlockChanged::BranchBecomingCanonChain {
208-
ref tree_route,
209-
..
210-
} => {
211-
let enacted = tree_route
212-
.enacted
213-
.iter()
214-
.flat_map(|hash| {
215-
let body = self.block_body(hash).expect("Enacted block must be in database.");
216-
tracker_and_addresses_entries(*hash, body.transactions())
217-
})
218-
.chain(tracker_and_addresses_entries(block_hash, block.transactions()));
219-
220-
let retracted = tree_route.retracted.iter().flat_map(|hash| {
221-
let body = self.block_body(hash).expect("Retracted block must be in database.");
222-
tracker_and_addresses_entries(*hash, body.transactions())
223-
});
224-
225-
(Box::new(retracted), Box::new(enacted))
226-
}
227181
BestBlockChanged::None => return Default::default(),
228182
};
229183

core/src/blockchain/headerchain.rs

Lines changed: 10 additions & 19 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
@@ -243,20 +243,6 @@ impl HeaderChain {
243243
let best_header_view = HeaderView::new(best_header);
244244
hashes.insert(best_header_view.number(), best_header_view.hash());
245245
}
246-
BestHeaderChanged::BranchBecomingCanonChain {
247-
tree_route,
248-
best_header,
249-
} => {
250-
let ancestor_number = self.block_number(&tree_route.ancestor).expect("Ancestor always exist in DB");
251-
let start_number = ancestor_number + 1;
252-
253-
for (index, hash) in tree_route.enacted.iter().enumerate() {
254-
hashes.insert(start_number + index as BlockNumber, *hash);
255-
}
256-
257-
let best_header_view = HeaderView::new(best_header);
258-
hashes.insert(best_header_view.number(), best_header_view.hash());
259-
}
260246
}
261247

262248
hashes
@@ -321,10 +307,15 @@ impl HeaderChain {
321307
0 => BestHeaderChanged::CanonChainAppended {
322308
best_header: new_best_header,
323309
},
324-
_ => BestHeaderChanged::BranchBecomingCanonChain {
325-
tree_route: route,
326-
best_header: new_best_header,
327-
},
310+
_ => {
311+
cerror!(
312+
HEADERCHAIN,
313+
"Older/Forked block header #{}({}) is inserted as a new block",
314+
new_header.number(),
315+
new_header.hash()
316+
);
317+
BestHeaderChanged::None
318+
}
328319
}
329320
} else {
330321
BestHeaderChanged::None

core/src/blockchain/route.rs

Lines changed: 1 addition & 27 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
@@ -142,19 +142,6 @@ impl ImportRoute {
142142
enacted: vec![],
143143
omitted,
144144
},
145-
BestBlockChanged::BranchBecomingCanonChain {
146-
tree_route,
147-
..
148-
} => {
149-
let mut enacted = tree_route.enacted.clone();
150-
enacted.push(best_block_changed.new_best_hash().unwrap());
151-
let retracted = tree_route.retracted.clone();
152-
ImportRoute {
153-
retracted,
154-
enacted,
155-
omitted,
156-
}
157-
}
158145
}
159146
}
160147

@@ -180,19 +167,6 @@ impl ImportRoute {
180167
enacted: vec![],
181168
omitted,
182169
},
183-
BestHeaderChanged::BranchBecomingCanonChain {
184-
tree_route,
185-
..
186-
} => {
187-
let mut enacted = tree_route.enacted.clone();
188-
enacted.push(best_header_changed.new_best_hash().unwrap());
189-
let retracted = tree_route.retracted.clone();
190-
ImportRoute {
191-
retracted,
192-
enacted,
193-
omitted,
194-
}
195-
}
196170
}
197171
}
198172

0 commit comments

Comments
 (0)