From e9eec06a8838ebbdb331188dd8551d9cb94c140c Mon Sep 17 00:00:00 2001 From: minh-bq <97180373+minh-bq@users.noreply.github.com> Date: Mon, 31 Jul 2023 17:06:56 +0700 Subject: [PATCH] consortium/v2: fix the incorrect snapshot lookup in GetFinalizedBlock (#330) Currently, to check if the justified block number - 1 is justfied, we look at the snapshot at justfied block number - 1. This is incorrect because at the snapshot N, the maximum justified block number is N - 1. So, we need to look at the snapshot at justified block number to check if the justified block number - 1 is justified. --- consensus/consortium/v2/consortium.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/consensus/consortium/v2/consortium.go b/consensus/consortium/v2/consortium.go index 06870b0e98..ec2b81775f 100644 --- a/consensus/consortium/v2/consortium.go +++ b/consensus/consortium/v2/consortium.go @@ -1206,8 +1206,14 @@ func (c *Consortium) GetFinalizedBlock( justifiedHash, descendantJustifiedHash common.Hash ) + justifiedNumber = headNumber + justifiedHash = headHash + for { - justifiedNumber, justifiedHash = c.GetJustifiedBlock(chain, headNumber, headHash) + // When getting the snapshot at block N, the maximum justified number is N - 1. + // Here, we want to check if the block at justifiedNumber - 1 is justified too. + // So, the snapshot we need to look up is at justifiedNumber. + justifiedNumber, justifiedHash = c.GetJustifiedBlock(chain, justifiedNumber, justifiedHash) if justifiedNumber == 0 { return 0, common.Hash{} } @@ -1244,9 +1250,6 @@ func (c *Consortium) GetFinalizedBlock( } } - header := chain.GetHeaderByHash(justifiedHash) - headNumber = header.Number.Uint64() - 1 - headHash = header.ParentHash descendantJustifiedNumber = justifiedNumber descendantJustifiedHash = justifiedHash }