Skip to content

Commit

Permalink
consortium/v2: fix the incorrect snapshot lookup in GetFinalizedBlock (
Browse files Browse the repository at this point in the history
…axieinfinity#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.
  • Loading branch information
minh-bq committed Sep 12, 2023
1 parent cd2cda3 commit e9eec06
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions consensus/consortium/v2/consortium.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
}
Expand Down Expand Up @@ -1244,9 +1250,6 @@ func (c *Consortium) GetFinalizedBlock(
}
}

header := chain.GetHeaderByHash(justifiedHash)
headNumber = header.Number.Uint64() - 1
headHash = header.ParentHash
descendantJustifiedNumber = justifiedNumber
descendantJustifiedHash = justifiedHash
}
Expand Down

0 comments on commit e9eec06

Please sign in to comment.