aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJimmy Hu <jimmy.hu@dexon.org>2019-01-24 10:39:36 +0800
committerWei-Ning Huang <w@dexon.org>2019-04-09 21:32:56 +0800
commitf44ee70c0b861c67d5efc2b92300e1c5cf90f51c (patch)
treebe56f18af3a22b9d3254cdcd76cf74af18fed96d
parentbbb1ebede10cd691de4ef2fe4bf276d2fa357a31 (diff)
downloaddexon-f44ee70c0b861c67d5efc2b92300e1c5cf90f51c.tar.gz
dexon-f44ee70c0b861c67d5efc2b92300e1c5cf90f51c.tar.zst
dexon-f44ee70c0b861c67d5efc2b92300e1c5cf90f51c.zip
dex: Add rate limit for pullVote (#169)
-rw-r--r--dex/handler.go28
1 files changed, 21 insertions, 7 deletions
diff --git a/dex/handler.go b/dex/handler.go
index a745bbfc7..b8b39c6f4 100644
--- a/dex/handler.go
+++ b/dex/handler.go
@@ -80,6 +80,8 @@ const (
recordChanSize = 10240
maxPullPeers = 3
+
+ pullVoteRateLimit = 10 * time.Second
)
// errIncompatibleConfig is returned if the requested protocols and configs are
@@ -97,13 +99,14 @@ type ProtocolManager struct {
fastSync uint32 // Flag whether fast sync is enabled (gets disabled if we already have blocks)
acceptTxs uint32 // Flag whether we're considered synchronised (enables transaction processing)
- txpool txPool
- nodeTable *nodeTable
- gov governance
- blockchain *core.BlockChain
- chainconfig *params.ChainConfig
- cache *cache
- maxPeers int
+ txpool txPool
+ nodeTable *nodeTable
+ gov governance
+ blockchain *core.BlockChain
+ chainconfig *params.ChainConfig
+ cache *cache
+ nextPullVote *sync.Map
+ maxPeers int
downloader *downloader.Downloader
fetcher *fetcher.Fetcher
@@ -166,6 +169,7 @@ func NewProtocolManager(
gov: gov,
blockchain: blockchain,
cache: newCache(5120, dexDB.NewDatabase(chaindb)),
+ nextPullVote: &sync.Map{},
chainconfig: config,
newPeerCh: make(chan *peer),
noMorePeers: make(chan struct{}),
@@ -250,6 +254,8 @@ func (pm *ProtocolManager) removePeer(id string) {
}
log.Debug("Removing Ethereum peer", "peer", id)
+ pm.nextPullVote.Delete(peer.ID())
+
// Unregister the peer from the downloader and Ethereum peer set
pm.downloader.UnregisterPeer(id)
if err := pm.peers.Unregister(id); err != nil {
@@ -863,6 +869,14 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
if !pm.isBlockProposer {
break
}
+ next, ok := pm.nextPullVote.Load(p.ID())
+ if ok {
+ nextTime := next.(time.Time)
+ if nextTime.After(time.Now()) {
+ break
+ }
+ }
+ pm.nextPullVote.Store(p.ID(), time.Now().Add(pullVoteRateLimit))
var pos coreTypes.Position
if err := msg.Decode(&pos); err != nil {
return errResp(ErrDecode, "msg %v: %v", msg, err)