You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
137 lines
4.6 KiB
137 lines
4.6 KiB
package node
|
|
|
|
import (
|
|
common2 "github.com/ethereum/go-ethereum/common"
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
"github.com/harmony-one/harmony/core/types"
|
|
"github.com/harmony-one/harmony/internal/utils"
|
|
"github.com/harmony-one/harmony/shard"
|
|
"github.com/pkg/errors"
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
)
|
|
|
|
const (
|
|
maxPendingCrossLinkSize = 1000
|
|
crossLinkBatchSize = 3
|
|
)
|
|
|
|
var (
|
|
errAlreadyExist = errors.New("crosslink already exist")
|
|
)
|
|
|
|
// VerifyBlockCrossLinks verifies the cross links of the block
|
|
func (node *Node) VerifyBlockCrossLinks(block *types.Block) error {
|
|
cxLinksData := block.Header().CrossLinks()
|
|
if len(cxLinksData) == 0 {
|
|
utils.Logger().Debug().Msgf("[CrossLinkVerification] Zero CrossLinks in the header")
|
|
return nil
|
|
}
|
|
|
|
crossLinks := types.CrossLinks{}
|
|
err := rlp.DecodeBytes(cxLinksData, &crossLinks)
|
|
if err != nil {
|
|
return errors.Wrapf(
|
|
err, "[CrossLinkVerification] failed to decode cross links",
|
|
)
|
|
}
|
|
|
|
if !crossLinks.IsSorted() {
|
|
return errors.New("[CrossLinkVerification] cross links are not sorted")
|
|
}
|
|
|
|
for _, crossLink := range crossLinks {
|
|
cl, err := node.Blockchain().ReadCrossLink(crossLink.ShardID(), crossLink.BlockNum())
|
|
if err == nil && cl != nil {
|
|
// Add slash for exist same blocknum but different crosslink
|
|
return errAlreadyExist
|
|
}
|
|
if err := node.VerifyCrossLink(crossLink); err != nil {
|
|
return errors.Wrapf(err, "cannot VerifyBlockCrossLinks")
|
|
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ProcessCrossLinkMessage verify and process Node/CrossLink message into crosslink when it's valid
|
|
func (node *Node) ProcessCrossLinkMessage(msgPayload []byte) {
|
|
if node.IsRunningBeaconChain() {
|
|
pendingCLs, err := node.Blockchain().ReadPendingCrossLinks()
|
|
if err == nil && len(pendingCLs) >= maxPendingCrossLinkSize {
|
|
utils.Logger().Debug().
|
|
Msgf("[ProcessingCrossLink] Pending Crosslink reach maximum size: %d", len(pendingCLs))
|
|
return
|
|
}
|
|
|
|
existingCLs := map[common2.Hash]struct{}{}
|
|
for _, pending := range pendingCLs {
|
|
existingCLs[pending.Hash()] = struct{}{}
|
|
}
|
|
|
|
crosslinks := []types.CrossLink{}
|
|
if err := rlp.DecodeBytes(msgPayload, &crosslinks); err != nil {
|
|
utils.Logger().Error().
|
|
Err(err).
|
|
Msg("[ProcessingCrossLink] Crosslink Message Broadcast Unable to Decode")
|
|
return
|
|
}
|
|
|
|
candidates := []types.CrossLink{}
|
|
utils.Logger().Debug().
|
|
Msgf("[ProcessingCrossLink] Received crosslinks: %d", len(crosslinks))
|
|
|
|
for i, cl := range crosslinks {
|
|
if i > crossLinkBatchSize*2 { // A sanity check to prevent spamming
|
|
break
|
|
}
|
|
|
|
if _, ok := existingCLs[cl.Hash()]; ok {
|
|
nodeCrossLinkMessageCounterVec.With(prometheus.Labels{"type": "duplicate_crosslink"}).Inc()
|
|
utils.Logger().Debug().Err(err).
|
|
Msgf("[ProcessingCrossLink] Cross Link already exists in pending queue, pass. Beacon Epoch: %d, Block num: %d, Epoch: %d, shardID %d",
|
|
node.Blockchain().CurrentHeader().Epoch(), cl.Number(), cl.Epoch(), cl.ShardID())
|
|
continue
|
|
}
|
|
|
|
exist, err := node.Blockchain().ReadCrossLink(cl.ShardID(), cl.Number().Uint64())
|
|
if err == nil && exist != nil {
|
|
nodeCrossLinkMessageCounterVec.With(prometheus.Labels{"type": "duplicate_crosslink"}).Inc()
|
|
utils.Logger().Debug().Err(err).
|
|
Msgf("[ProcessingCrossLink] Cross Link already exists, pass. Beacon Epoch: %d, Block num: %d, Epoch: %d, shardID %d", node.Blockchain().CurrentHeader().Epoch(), cl.Number(), cl.Epoch(), cl.ShardID())
|
|
continue
|
|
}
|
|
|
|
if err = node.VerifyCrossLink(cl); err != nil {
|
|
nodeCrossLinkMessageCounterVec.With(prometheus.Labels{"type": "invalid_crosslink"}).Inc()
|
|
utils.Logger().Info().
|
|
Str("cross-link-issue", err.Error()).
|
|
Msgf("[ProcessingCrossLink] Failed to verify new cross link for blockNum %d epochNum %d shard %d skipped: %v", cl.BlockNum(), cl.Epoch().Uint64(), cl.ShardID(), cl)
|
|
continue
|
|
}
|
|
|
|
candidates = append(candidates, cl)
|
|
nodeCrossLinkMessageCounterVec.With(prometheus.Labels{"type": "new_crosslink"}).Inc()
|
|
|
|
utils.Logger().Debug().
|
|
Msgf("[ProcessingCrossLink] Committing for shardID %d, blockNum %d",
|
|
cl.ShardID(), cl.Number().Uint64(),
|
|
)
|
|
}
|
|
Len, _ := node.Blockchain().AddPendingCrossLinks(candidates)
|
|
utils.Logger().Debug().
|
|
Msgf("[ProcessingCrossLink] Add pending crosslinks, total pending: %d", Len)
|
|
}
|
|
}
|
|
|
|
// VerifyCrossLink verifies the header is valid
|
|
func (node *Node) VerifyCrossLink(cl types.CrossLink) error {
|
|
if node.Blockchain().ShardID() != shard.BeaconChainShardID {
|
|
return errors.New("[VerifyCrossLink] Shard chains should not verify cross links")
|
|
}
|
|
engine := node.Blockchain().Engine()
|
|
|
|
if err := engine.VerifyCrossLink(node.Blockchain(), cl); err != nil {
|
|
return errors.Wrap(err, "[VerifyCrossLink]")
|
|
}
|
|
return nil
|
|
}
|
|
|