Skip to content

Commit e9bc48e

Browse files
committed
Enable all grandchild of the best block to become a best proposal block
Before this commit, only the child of the best block and the child of the best proposal block could be a new best proposal block. A block that is a grandchild of the best block but not the child of the best proposal block should be able to become a best proposal block. After this commit, CodeChain checks whether a new block is the best block's child or grandchild.
1 parent 3d2afd7 commit e9bc48e

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

core/src/blockchain/blockchain.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,16 @@ impl BlockChain {
184184
let new_header = new_block.header_view();
185185
let parent_hash_of_new_block = new_header.parent_hash();
186186
let parent_details_of_new_block = self.block_details(&parent_hash_of_new_block).expect("Invalid parent hash");
187-
let prev_best_proposal_hash = self.best_proposal_block_hash();
187+
let grandparent_hash_of_new_block = parent_details_of_new_block.parent;
188188
let prev_best_hash = self.best_block_hash();
189189

190190
if parent_details_of_new_block.total_score + new_header.score() > self.best_proposal_block_detail().total_score
191-
&& engine.can_change_canon_chain(&new_header, prev_best_hash, prev_best_proposal_hash)
191+
&& engine.can_change_canon_chain(
192+
new_header.hash(),
193+
parent_hash_of_new_block,
194+
grandparent_hash_of_new_block,
195+
prev_best_hash,
196+
)
192197
{
193198
cinfo!(
194199
BLOCKCHAIN,

core/src/blockchain/headerchain.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,16 @@ impl HeaderChain {
242242
fn best_header_changed(&self, new_header: &HeaderView, engine: &dyn CodeChainEngine) -> BestHeaderChanged {
243243
let parent_hash_of_new_header = new_header.parent_hash();
244244
let parent_details_of_new_header = self.block_details(&parent_hash_of_new_header).expect("Invalid parent hash");
245-
let prev_best_proposal_hash = self.best_proposal_header_hash();
245+
let grandparent_hash_of_new_header = parent_details_of_new_header.parent;
246246
let prev_best_hash = self.best_header_hash();
247247
let is_new_best = parent_details_of_new_header.total_score + new_header.score()
248248
> self.best_proposal_header_detail().total_score
249-
&& engine.can_change_canon_chain(&new_header, prev_best_hash, prev_best_proposal_hash);
249+
&& engine.can_change_canon_chain(
250+
new_header.hash(),
251+
parent_hash_of_new_header,
252+
grandparent_hash_of_new_header,
253+
prev_best_hash,
254+
);
250255

251256
if is_new_best {
252257
ctrace!(

core/src/consensus/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,9 +271,10 @@ pub trait ConsensusEngine: Sync + Send {
271271
/// Only the descendant of the current best block could be the next best block in Tendermint consensus.
272272
fn can_change_canon_chain(
273273
&self,
274-
_new_header: &HeaderView,
274+
_new_block_hash: H256,
275+
_parent_hash_of_new_header: H256,
276+
_grandparent_hash_of_new_header: H256,
275277
_previous_best_hash: H256,
276-
_previous_best_proposal_hash: H256,
277278
) -> bool {
278279
true
279280
}

core/src/consensus/tendermint/engine.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,11 +311,12 @@ impl ConsensusEngine for Tendermint {
311311

312312
fn can_change_canon_chain(
313313
&self,
314-
new_header: &HeaderView,
314+
_new_header_hash: H256,
315+
parent_hash_of_new_header: H256,
316+
grandparent_hash_of_new_header: H256,
315317
prev_best_hash: H256,
316-
prev_best_proposal_hash: H256,
317318
) -> bool {
318-
new_header.parent_hash() == prev_best_hash || new_header.parent_hash() == prev_best_proposal_hash
319+
parent_hash_of_new_header == prev_best_hash || grandparent_hash_of_new_header == prev_best_hash
319320
}
320321

321322
fn action_handlers(&self) -> &[Arc<dyn ActionHandler>] {

0 commit comments

Comments
 (0)