forked from bnb-chain/bsc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler_bsc.go
More file actions
74 lines (63 loc) · 2.31 KB
/
Copy pathhandler_bsc.go
File metadata and controls
74 lines (63 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package eth
import (
"fmt"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/eth/protocols/bsc"
"github.com/ethereum/go-ethereum/p2p/enode"
)
// bscHandler implements the bsc.Backend interface to handle the various network
// packets that are sent as broadcasts.
type bscHandler handler
func (h *bscHandler) Chain() *core.BlockChain { return h.chain }
// RunPeer is invoked when a peer joins on the `bsc` protocol.
func (h *bscHandler) RunPeer(peer *bsc.Peer, hand bsc.Handler) error {
if err := peer.Handshake(); err != nil {
// ensure that waitBscExtension receives the exit signal normally
// otherwise, can't graceful shutdown
ps := h.peers
id := peer.ID()
// Ensure nobody can double connect
ps.lock.Lock()
if wait, ok := ps.bscWait[id]; ok {
delete(ps.bscWait, id)
peer.Log().Error("Bsc extension Handshake failed", "err", err)
wait <- nil
}
ps.lock.Unlock()
return err
}
return (*handler)(h).runBscExtension(peer, hand)
}
// PeerInfo retrieves all known `bsc` information about a peer.
func (h *bscHandler) PeerInfo(id enode.ID) interface{} {
if p := h.peers.peer(id.String()); p != nil && p.bscExt != nil {
return p.bscExt.info()
}
return nil
}
// Handle is invoked from a peer's message handler when it receives a new remote
// message that the handler couldn't consume and serve itself.
func (h *bscHandler) Handle(peer *bsc.Peer, packet bsc.Packet) error {
// DeliverSnapPacket is invoked from a peer's message handler when it transmits a
// data packet for the local node to consume.
switch packet := packet.(type) {
case *bsc.VotesPacket:
return h.handleVotesBroadcast(peer, packet.Votes)
default:
return fmt.Errorf("unexpected bsc packet type: %T", packet)
}
}
// handleVotesBroadcast is invoked from a peer's message handler when it transmits a
// votes broadcast for the local node to process.
func (h *bscHandler) handleVotesBroadcast(peer *bsc.Peer, votes []*types.VoteEnvelope) error {
if peer.IsOverLimitAfterReceiving() {
return nil
}
// Here we only put the first vote, to avoid ddos attack by sending a large batch of votes.
// This won't abandon any valid vote, because one vote is sent every time referring to func voteBroadcastLoop
if len(votes) > 0 {
h.votepool.PutVote(votes[0])
}
return nil
}