diff --git a/consensus/view_change_construct.go b/consensus/view_change_construct.go index fcf025e74..5d2553175 100644 --- a/consensus/view_change_construct.go +++ b/consensus/view_change_construct.go @@ -465,7 +465,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/core/state_processor.go b/core/state_processor.go index 9ccb256a7..38be4184a 100644 --- a/core/state_processor.go +++ b/core/state_processor.go @@ -22,8 +22,6 @@ import ( "math/big" "time" - lru "github.com/hashicorp/golang-lru" - "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/rlp" @@ -40,6 +38,7 @@ import ( "github.com/harmony-one/harmony/staking/effective" "github.com/harmony-one/harmony/staking/slash" staking "github.com/harmony-one/harmony/staking/types" + lru "github.com/hashicorp/golang-lru" "github.com/pkg/errors" ) @@ -319,6 +318,7 @@ func ApplyTransaction(bc ChainContext, author *common.Address, gp *GasPool, stat balance = a.String() } return nil, nil, nil, 0, errors.Wrapf(err, "apply failed from='%s' to='%s' balance='%s'", msg.From().Hex(), to, balance) + } // Update the state with pending changes var root []byte diff --git a/node/node.go b/node/node.go index 841d6330d..f80c502a0 100644 --- a/node/node.go +++ b/node/node.go @@ -1178,6 +1178,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")