2022-09-25 19:01:39 +00:00
|
|
|
/*
|
|
|
|
Copyright 2022 Erigon-Lightclient contributors
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-09-24 18:48:56 +00:00
|
|
|
package peers
|
|
|
|
|
|
|
|
import (
|
2022-09-25 00:14:10 +00:00
|
|
|
"sync"
|
|
|
|
|
2022-09-24 18:48:56 +00:00
|
|
|
lru "github.com/hashicorp/golang-lru"
|
2022-09-25 18:39:09 +00:00
|
|
|
"github.com/ledgerwatch/log/v3"
|
2022-09-25 00:14:10 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/host"
|
2022-09-24 18:48:56 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
maxBadPeers = 10_000
|
2022-09-25 00:14:10 +00:00
|
|
|
DefaultMaxPeers = 33
|
2022-09-24 18:48:56 +00:00
|
|
|
MaxBadResponses = 10
|
|
|
|
)
|
|
|
|
|
|
|
|
type Peers struct {
|
|
|
|
badPeers *lru.Cache
|
|
|
|
badResponses map[peer.ID]int
|
2022-09-25 00:14:10 +00:00
|
|
|
host host.Host
|
|
|
|
mu sync.Mutex
|
2022-09-24 18:48:56 +00:00
|
|
|
}
|
|
|
|
|
2022-09-25 00:14:10 +00:00
|
|
|
func New(host host.Host) *Peers {
|
2022-09-24 18:48:56 +00:00
|
|
|
badPeers, err := lru.New(maxBadPeers)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return &Peers{
|
|
|
|
badPeers: badPeers,
|
|
|
|
badResponses: make(map[peer.ID]int),
|
2022-09-25 00:14:10 +00:00
|
|
|
host: host,
|
2022-09-24 18:48:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Peers) IsBadPeer(pid peer.ID) bool {
|
2022-09-25 00:14:10 +00:00
|
|
|
p.mu.Lock()
|
|
|
|
defer p.mu.Unlock()
|
2022-09-24 18:48:56 +00:00
|
|
|
return p.badPeers.Contains(pid)
|
|
|
|
}
|
|
|
|
|
2022-09-25 00:14:10 +00:00
|
|
|
func (p *Peers) Penalize(pid peer.ID) {
|
|
|
|
p.mu.Lock()
|
|
|
|
defer p.mu.Unlock()
|
2022-09-24 18:48:56 +00:00
|
|
|
if _, has := p.badResponses[pid]; !has {
|
|
|
|
p.badResponses[pid] = 1
|
|
|
|
}
|
2022-09-25 00:14:10 +00:00
|
|
|
|
2022-09-24 18:48:56 +00:00
|
|
|
p.badResponses[pid]++
|
2022-09-25 00:14:10 +00:00
|
|
|
// Drop peer and delete the map element.
|
|
|
|
if p.badResponses[pid] > MaxBadResponses {
|
2022-09-25 18:39:09 +00:00
|
|
|
p.banBadPeer(pid)
|
2022-09-25 00:14:10 +00:00
|
|
|
delete(p.badResponses, pid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-25 18:39:09 +00:00
|
|
|
func (p *Peers) banBadPeer(pid peer.ID) {
|
2022-09-27 20:34:06 +00:00
|
|
|
p.DisconnectPeer(pid)
|
2022-09-25 00:14:10 +00:00
|
|
|
p.badPeers.Add(pid, []byte{0})
|
2022-09-25 18:39:09 +00:00
|
|
|
log.Warn("[Peers] bad peers has been banned", "peer-id", pid)
|
2022-09-24 18:48:56 +00:00
|
|
|
}
|
2022-09-27 20:34:06 +00:00
|
|
|
|
|
|
|
func (p *Peers) DisconnectPeer(pid peer.ID) {
|
|
|
|
log.Info("[Peers] disconnecting from peer", "peer-id", pid)
|
|
|
|
p.host.Peerstore().RemovePeer(pid)
|
|
|
|
}
|