From a9e60ba8524563b3a1560354b86d008d27ad6eea Mon Sep 17 00:00:00 2001 From: rjl493456442 Date: Wed, 12 Jun 2019 09:56:24 +0800 Subject: [PATCH] les: address comments --- les/handler.go | 6 +++--- les/peer.go | 9 ++++++--- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/les/handler.go b/les/handler.go index 1cc7b61852..1102a0f0cb 100644 --- a/les/handler.go +++ b/les/handler.go @@ -46,7 +46,7 @@ import ( "github.com/ethereum/go-ethereum/trie" ) -var errTooMuchInvalidRequest = errors.New("too much invalid requests made") +var errTooManyInvalidRequest = errors.New("too many invalid requests made") const ( softResponseLimit = 2 * 1024 * 1024 // Target maximum size of returned blocks, headers or node data. @@ -1152,8 +1152,8 @@ func (pm *ProtocolManager) handleMsg(p *peer) error { } // If the client has made too much invalid request(e.g. request a non-exist data), // reject them to prevent SPAM attack. - if atomic.LoadUint64(&p.invalidReq) > maxRequesetErrors { - return errTooMuchInvalidRequest + if atomic.LoadUint64(&p.invalidReq) > maxRequestErrors { + return errTooManyInvalidRequest } return nil } diff --git a/les/peer.go b/les/peer.go index e07cb1d4ec..5c678cb7e9 100644 --- a/les/peer.go +++ b/les/peer.go @@ -43,7 +43,7 @@ var ( ) const ( - maxRequesetErrors = 20 // number of invalid requests tolerated (makes the protocol less brittle but still avoids spam) + maxRequestErrors = 20 // number of invalid requests tolerated (makes the protocol less brittle but still avoids spam) maxResponseErrors = 50 // number of invalid responses tolerated (makes the protocol less brittle but still avoids spam) ) @@ -71,8 +71,12 @@ const ( ) type peer struct { - *p2p.Peer + // WARNING: The `invalidReq` field is accessed atomically. On 32 bit platforms, only + // 64-bit aligned fields can be atomic. The struct is guaranteed to be so aligned, + // so take advantage of that (https://golang.org/pkg/sync/atomic/#pkg-note-BUG). + invalidReq uint64 + *p2p.Peer rw p2p.MsgReadWriter version int // Protocol version negotiated @@ -92,7 +96,6 @@ type peer struct { // RequestProcessed is called responseLock sync.Mutex responseCount uint64 - invalidReq uint64 poolEntry *poolEntry hasBlock func(common.Hash, uint64, bool) bool