From 313daea3c9a1de0be537a31cfd482d84cb8f4d88 Mon Sep 17 00:00:00 2001 From: Gheis Mohammadi Date: Tue, 13 Feb 2024 16:21:12 +0800 Subject: [PATCH] =?UTF-8?q?[HOTFIX]=20fix=20leader=20crosslink=20issue=20t?= =?UTF-8?q?o=20not=20include=20old=20cross=20link=20in=20the=20propo?= =?UTF-8?q?=E2=80=A6=20(#4629)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix leader crosslink issue to not include old cross link in the proposing block * set higher epoch threshold for pending crosslinks to be added to proposing block * delete old pending cross links * delete when proposing * delete when proposing * delete when proposing * delete when proposing * minor logic change for the log * minor logic change for the log * minor logic change for the log * minor logic change for the log --------- Co-authored-by: Diego Nava --- consensus/view_change_construct.go | 2 +- node/node.go | 39 ++++++++++++++++++++++++++++++ node/node_newblock.go | 19 ++++++++++++++- 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/consensus/view_change_construct.go b/consensus/view_change_construct.go index 061d2a795..de430694a 100644 --- a/consensus/view_change_construct.go +++ b/consensus/view_change_construct.go @@ -464,7 +464,7 @@ func (vc *viewChange) InitPayload( if !inited { viewIDBytes := make([]byte, 8) binary.LittleEndian.PutUint64(viewIDBytes, viewID) - vc.getLogger().Info().Uint64("viewID", viewID).Uint64("blockNum", blockNum).Msg("[InitPayload] add my M3 (ViewID) type messaage") + vc.getLogger().Info().Uint64("viewID", viewID).Uint64("blockNum", blockNum).Msg("[InitPayload] add my M3 (ViewID) type message") for _, key := range privKeys { if _, ok := vc.viewIDBitmap[viewID]; !ok { viewIDBitmap := bls_cosi.NewMask(members) diff --git a/node/node.go b/node/node.go index da42db0e4..c34b018d6 100644 --- a/node/node.go +++ b/node/node.go @@ -1183,6 +1183,45 @@ func New( node.serviceManager = service.NewManager() + // delete old pending crosslinks + if node.Blockchain().ShardID() == shard.BeaconChainShardID { + ten := big.NewInt(10) + crossLinkEpochThreshold := new(big.Int).Sub(node.Blockchain().CurrentHeader().Epoch(), ten) + + invalidToDelete := make([]types.CrossLink, 0, 1000) + allPending, err := node.Blockchain().ReadPendingCrossLinks() + if err == nil { + for _, pending := range allPending { + // if pending crosslink is older than 10 epochs, delete it + if pending.EpochF.Cmp(crossLinkEpochThreshold) <= 0 { + invalidToDelete = append(invalidToDelete, pending) + utils.Logger().Info(). + Uint32("shard", pending.ShardID()). + Int64("epoch", pending.Epoch().Int64()). + Uint64("blockNum", pending.BlockNum()). + Int64("viewID", pending.ViewID().Int64()). + Interface("hash", pending.Hash()). + Msg("[PendingCrossLinksOnInit] delete old pending cross links") + } + } + + if n, err := node.Blockchain().DeleteFromPendingCrossLinks(invalidToDelete); err != nil { + utils.Logger().Error(). + Err(err). + Msg("[PendingCrossLinksOnInit] deleting old pending cross links failed") + } else if len(invalidToDelete) > 0 { + utils.Logger().Info(). + Int("not-deleted", n). + Int("deleted", len(invalidToDelete)). + Msg("[PendingCrossLinksOnInit] deleted old pending cross links") + } + } else { + utils.Logger().Error(). + Err(err). + Msg("[PendingCrossLinksOnInit] read pending cross links failed") + } + } + return &node } diff --git a/node/node_newblock.go b/node/node_newblock.go index fdca8b741..bafb340a8 100644 --- a/node/node_newblock.go +++ b/node/node_newblock.go @@ -1,6 +1,7 @@ package node import ( + "math/big" "sort" "strings" "time" @@ -226,11 +227,18 @@ func (node *Node) ProposeNewBlock(commitSigs chan []byte) (*types.Block, error) utils.AnalysisStart("proposeNewBlockVerifyCrossLinks") // Prepare cross links and slashing messages var crossLinksToPropose types.CrossLinks + ten := big.NewInt(10) + crossLinkEpochThreshold := new(big.Int).Sub(currentHeader.Epoch(), ten) if isBeaconchainInCrossLinkEra { allPending, err := node.Blockchain().ReadPendingCrossLinks() invalidToDelete := []types.CrossLink{} if err == nil { for _, pending := range allPending { + // if pending crosslink is older than 10 epochs, delete it and continue. this logic is also applied when the node starts + if pending.EpochF.Cmp(crossLinkEpochThreshold) <= 0 { + invalidToDelete = append(invalidToDelete, pending) + continue + } // ReadCrossLink beacon chain usage. exist, err := node.Blockchain().ReadCrossLink(pending.ShardID(), pending.BlockNum()) if err == nil || exist != nil { @@ -263,7 +271,16 @@ func (node *Node) ProposeNewBlock(commitSigs chan []byte) (*types.Block, error) len(allPending), ) } - node.Blockchain().DeleteFromPendingCrossLinks(invalidToDelete) + if n, err := node.Blockchain().DeleteFromPendingCrossLinks(invalidToDelete); err != nil { + utils.Logger().Error(). + Err(err). + Msg("[ProposeNewBlock] invalid pending cross links failed") + } else if len(invalidToDelete) > 0 { + utils.Logger().Info(). + Int("not-deleted", n). + Int("deleted", len(invalidToDelete)). + Msg("[ProposeNewBlock] deleted invalid pending cross links") + } } utils.AnalysisEnd("proposeNewBlockVerifyCrossLinks")