P2p filterbeaconmsg (#3757)

* [p2p] ban beacon node sync message where block height is smaller than current height

* small param change

* small wording fix

* elaborate log message

* fix spelling
pull/3760/head v4.1.3
Jacky Wang 4 years ago committed by GitHub
parent 6ef6580ac0
commit bd78e06cc2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 24
      node/node.go

@ -9,6 +9,8 @@ import (
"sync"
"time"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/common"
protobuf "github.com/golang/protobuf/proto"
"github.com/harmony-one/abool"
@ -373,6 +375,8 @@ var (
errInvalidShard = errors.New("invalid shard")
)
const beaconBlockHeightTolerance = 2
// validateNodeMessage validate node message
func (node *Node) validateNodeMessage(ctx context.Context, payload []byte) (
[]byte, proto_node.MessageType, error) {
@ -403,6 +407,26 @@ func (node *Node) validateNodeMessage(ctx context.Context, payload []byte) (
if node.Blockchain().ShardID() == shard.BeaconChainShardID {
return nil, 0, errIgnoreBeaconMsg
}
// checks whether the beacon block is larger than current block number
blocksPayload := payload[p2pNodeMsgPrefixSize+1:]
var blocks []*types.Block
if err := rlp.DecodeBytes(blocksPayload, &blocks); err != nil {
return nil, 0, errors.Wrap(err, "block decode error")
}
curBeaconHeight := node.Beaconchain().CurrentBlock().NumberU64()
for _, block := range blocks {
// Ban blocks number that is smaller than tolerance
if block.NumberU64()+beaconBlockHeightTolerance <= curBeaconHeight {
utils.Logger().Warn().Uint64("receivedNum", block.NumberU64()).
Uint64("currentNum", curBeaconHeight).Msg("beacon block sync message rejected")
return nil, 0, errors.New("beacon block height smaller than current height beyond tolerance")
} else if block.NumberU64() <= curBeaconHeight {
utils.Logger().Info().Uint64("receivedNum", block.NumberU64()).
Uint64("currentNum", curBeaconHeight).Msg("beacon block sync message ignored")
return nil, 0, errIgnoreBeaconMsg
}
}
case proto_node.SlashCandidate:
nodeNodeMessageCounterVec.With(prometheus.Labels{"type": "slash"}).Inc()
// only beacon chain node process slash candidate messages

Loading…
Cancel
Save