aboutsummaryrefslogtreecommitdiffstats
path: root/dex
diff options
context:
space:
mode:
authorJimmy Hu <jimmy.hu@dexon.org>2019-04-15 18:20:48 +0800
committerWei-Ning Huang <w@byzantine-lab.io>2019-06-15 22:09:55 +0800
commit7e8e8deb67ca50d7c19ee08287720bd722de8485 (patch)
tree7ff32c38227cb61cd472c06036c278b1560bf5a1 /dex
parent073064170c874bb19741108d0833b1bf019b439b (diff)
downloadgo-tangerine-7e8e8deb67ca50d7c19ee08287720bd722de8485.tar
go-tangerine-7e8e8deb67ca50d7c19ee08287720bd722de8485.tar.gz
go-tangerine-7e8e8deb67ca50d7c19ee08287720bd722de8485.tar.bz2
go-tangerine-7e8e8deb67ca50d7c19ee08287720bd722de8485.tar.lz
go-tangerine-7e8e8deb67ca50d7c19ee08287720bd722de8485.tar.xz
go-tangerine-7e8e8deb67ca50d7c19ee08287720bd722de8485.tar.zst
go-tangerine-7e8e8deb67ca50d7c19ee08287720bd722de8485.zip
dex: add pullblock rate limit (#363)
Diffstat (limited to 'dex')
-rw-r--r--dex/handler.go28
1 files changed, 20 insertions, 8 deletions
diff --git a/dex/handler.go b/dex/handler.go
index 8e5898817..fb2ce4e4d 100644
--- a/dex/handler.go
+++ b/dex/handler.go
@@ -83,7 +83,8 @@ const (
maxPullPeers = 3
maxPullVotePeers = 1
- pullVoteRateLimit = 10 * time.Second
+ pullVoteRateLimit = 3 * time.Second
+ pullBlockRateLimit = 3 * time.Second
maxAgreementResultBroadcast = 3
maxFinalizedBlockBroadcast = 3
@@ -103,13 +104,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
- gov governance
- blockchain *core.BlockChain
- chainconfig *params.ChainConfig
- cache *cache
- nextPullVote *sync.Map
- maxPeers int
+ txpool txPool
+ gov governance
+ blockchain *core.BlockChain
+ chainconfig *params.ChainConfig
+ cache *cache
+ nextPullVote *sync.Map
+ nextPullBlock *sync.Map
+ maxPeers int
downloader *downloader.Downloader
fetcher *fetcher.Fetcher
@@ -171,6 +173,7 @@ func NewProtocolManager(
blockchain: blockchain,
cache: newCache(5120, dexDB.NewDatabase(chaindb)),
nextPullVote: &sync.Map{},
+ nextPullBlock: &sync.Map{},
chainconfig: config,
whitelist: whitelist,
newPeerCh: make(chan *peer),
@@ -258,6 +261,7 @@ func (pm *ProtocolManager) removePeer(id string) {
log.Debug("Removing Ethereum peer", "peer", id)
pm.nextPullVote.Delete(peer.ID())
+ pm.nextPullBlock.Delete(peer.ID())
// Unregister the peer from the downloader and Ethereum peer set
pm.downloader.UnregisterPeer(id)
@@ -927,6 +931,14 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
if atomic.LoadInt32(&pm.receiveCoreMessage) == 0 {
break
}
+ next, ok := pm.nextPullBlock.Load(p.ID())
+ if ok {
+ nextTime := next.(time.Time)
+ if nextTime.After(time.Now()) {
+ break
+ }
+ }
+ pm.nextPullBlock.Store(p.ID(), time.Now().Add(pullBlockRateLimit))
var hashes coreCommon.Hashes
if err := msg.Decode(&hashes); err != nil {
return errResp(ErrDecode, "msg %v: %v", msg, err)