Skip to content

Commit

Permalink
fix: remove naturally finality
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanBSC committed May 25, 2023
1 parent eaea77a commit dc987dc
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 25 deletions.
23 changes: 5 additions & 18 deletions consensus/parlia/parlia.go
Original file line number Diff line number Diff line change
Expand Up @@ -1719,19 +1719,13 @@ func (p *Parlia) GetJustifiedNumberAndHash(chain consensus.ChainHeaderReader, he
}

// GetFinalizedHeader returns highest finalized block header.
// It will find vote finalized block within NaturallyFinalizedDist blocks firstly,
// If the vote finalized block not found, return its naturally finalized block.
func (p *Parlia) GetFinalizedHeader(chain consensus.ChainHeaderReader, header *types.Header) *types.Header {
backward := uint64(types.NaturallyFinalizedDist)
if chain == nil || header == nil {
return nil
}
if !chain.Config().IsPlato(header.Number) {
return chain.GetHeaderByNumber(0)
}
if header.Number.Uint64() < backward {
backward = header.Number.Uint64()
}

snap, err := p.snapshot(chain, header.Number.Uint64(), header.Hash(), nil)
if err != nil {
Expand All @@ -1740,20 +1734,13 @@ func (p *Parlia) GetFinalizedHeader(chain consensus.ChainHeaderReader, header *t
return nil
}

for snap.Attestation != nil && snap.Attestation.SourceNumber >= header.Number.Uint64()-backward {
if snap.Attestation.TargetNumber == snap.Attestation.SourceNumber+1 {
return chain.GetHeaderByHash(snap.Attestation.SourceHash)
}

snap, err = p.snapshot(chain, snap.Attestation.SourceNumber, snap.Attestation.SourceHash, nil)
if err != nil {
log.Error("Unexpected error when getting snapshot",
"error", err, "blockNumber", snap.Attestation.SourceNumber, "blockHash", snap.Attestation.SourceHash)
return nil
if snap.Attestation == nil {
if p.chainConfig.IsLuban(header.Number) {
log.Debug("once one attestation generated, attestation of snap would not be nil forever basically")
}
return nil
}

return FindAncientHeader(header, backward, chain, nil)
return chain.GetHeader(snap.Attestation.SourceHash, snap.Attestation.SourceNumber)
}

// =========================== utility function ==========================
Expand Down
12 changes: 6 additions & 6 deletions consensus/parlia/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type Snapshot struct {
Validators map[common.Address]*ValidatorInfo `json:"validators"` // Set of authorized validators at this moment
Recents map[uint64]common.Address `json:"recents"` // Set of recent validators for spam protections
RecentForkHashes map[uint64]string `json:"recent_fork_hashes"` // Set of recent forkHash
Attestation *types.VoteData `json:"attestation:omitempty"` // Attestation for fast finality
Attestation *types.VoteData `json:"attestation:omitempty"` // Attestation for fast finality, but `Source` used as `Finalized`
}

type ValidatorInfo struct {
Expand Down Expand Up @@ -199,11 +199,11 @@ func (s *Snapshot) updateAttestation(header *types.Header, chainConfig *params.C
}

// Update attestation
s.Attestation = &types.VoteData{
SourceNumber: attestation.Data.SourceNumber,
SourceHash: attestation.Data.SourceHash,
TargetNumber: attestation.Data.TargetNumber,
TargetHash: attestation.Data.TargetHash,
s.Attestation.TargetNumber = attestation.Data.TargetNumber
s.Attestation.TargetHash = attestation.Data.TargetHash
if attestation.Data.SourceNumber+1 == attestation.Data.TargetNumber {
s.Attestation.SourceNumber = attestation.Data.SourceNumber
s.Attestation.SourceHash = attestation.Data.SourceHash
}
}

Expand Down
1 change: 0 additions & 1 deletion core/types/vote.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const (
BLSSignatureLength = 96

MaxAttestationExtraLength = 256
NaturallyFinalizedDist = 21 // The distance to naturally finalized a block
)

type BLSPublicKey [BLSPublicKeyLength]byte
Expand Down

0 comments on commit dc987dc

Please sign in to comment.