Skip to content

Commit b63bffe

Browse files
authored
les, p2p/simulations/adapters: fix issues found while simulating les (#21761)
This adds a few tiny fixes for les and the p2p simulation framework: LES Parts - Keep the LES-SERVER connection even it's non-synced We had this idea to reject the connections in LES protocol if the les-server itself is not synced. However, in LES protocol we will also receive the connection from another les-server. In this case even the local node is not synced yet, we should keep the tcp connection for other protocols(e.g. eth protocol). - Don't count "invalid message" for non-existing GetBlockHeadersMsg request In the eth syncing mechanism (full sync, fast sync, light sync), it will try to fetch some non-existent blocks or headers(to ensure we indeed download all the missing chain). In this case, it's possible that the les-server will receive the request for non-existent headers. So don't count it as the "invalid message" for scheduling dropping. - Copy the announce object in the closure Before the les-server pushes the latest headers to all connected clients, it will create a closure and queue it in the underlying request scheduler. In some scenarios it's problematic. E.g, in private networks, the block can be mined very fast. So before the first closure is executed, we may already update the latest_announce object. So actually the "announce" object we want to send is replaced. The downsize is the client will receive two announces with the same td and then drop the server. P2P Simulation Framework - Don't double register the protocol services in p2p-simulation "Start". The protocols upon the devp2p are registered in the "New node stage". So don't reigster them again when starting a node in the p2p simulation framework - Add one more new config field "ExternalSigner", in order to use clef service in the framework.
1 parent b63e3c3 commit b63bffe

3 files changed

Lines changed: 25 additions & 14 deletions

File tree

‎les/server_handler.go‎

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,7 @@ func (h *serverHandler) handle(p *clientPeer) error {
126126
p.Log().Debug("Light Ethereum handshake failed", "err", err)
127127
return err
128128
}
129-
// Reject light clients if server is not synced.
130-
if !h.synced() {
131-
p.Log().Debug("Light server not synced, rejecting peer")
132-
return p2p.DiscRequested
133-
}
129+
// Reject the duplicated peer, otherwise register it to peerset.
134130
var registered bool
135131
if err := h.server.ns.Operation(func() {
136132
if h.server.ns.GetField(p.Node(), clientPeerField) != nil {
@@ -156,7 +152,14 @@ func (h *serverHandler) handle(p *clientPeer) error {
156152
_, err := p.rw.ReadMsg()
157153
return err
158154
}
159-
155+
// Reject light clients if server is not synced.
156+
//
157+
// Put this checking here, so that "non-synced" les-server peers are still allowed
158+
// to keep the connection.
159+
if !h.synced() {
160+
p.Log().Debug("Light server not synced, rejecting peer")
161+
return p2p.DiscRequested
162+
}
160163
// Disconnect the inbound peer if it's rejected by clientPool
161164
if cap, err := h.server.clientPool.connect(p); cap != p.fcParams.MinRecharge || err != nil {
162165
p.Log().Debug("Light Ethereum peer rejected", "err", errFullClientPool)
@@ -348,7 +351,6 @@ func (h *serverHandler) handleMsg(p *clientPeer, wg *sync.WaitGroup) error {
348351
origin = h.blockchain.GetHeaderByNumber(query.Origin.Number)
349352
}
350353
if origin == nil {
351-
p.bumpInvalid()
352354
break
353355
}
354356
headers = append(headers, origin)
@@ -1006,18 +1008,24 @@ func (b *broadcaster) sendTo(node *enode.Node) {
10061008
}
10071009
if p, _ := b.ns.GetField(node, clientPeerField).(*clientPeer); p != nil {
10081010
if p.headInfo.Td == nil || b.lastAnnounce.Td.Cmp(p.headInfo.Td) > 0 {
1011+
announce := b.lastAnnounce
10091012
switch p.announceType {
10101013
case announceTypeSimple:
1011-
if !p.queueSend(func() { p.sendAnnounce(b.lastAnnounce) }) {
1012-
log.Debug("Drop announcement because queue is full", "number", b.lastAnnounce.Number, "hash", b.lastAnnounce.Hash)
1014+
if !p.queueSend(func() { p.sendAnnounce(announce) }) {
1015+
log.Debug("Drop announcement because queue is full", "number", announce.Number, "hash", announce.Hash)
1016+
} else {
1017+
log.Debug("Sent announcement", "number", announce.Number, "hash", announce.Hash)
10131018
}
10141019
case announceTypeSigned:
10151020
if b.signedAnnounce.Hash != b.lastAnnounce.Hash {
10161021
b.signedAnnounce = b.lastAnnounce
10171022
b.signedAnnounce.sign(b.privateKey)
10181023
}
1019-
if !p.queueSend(func() { p.sendAnnounce(b.signedAnnounce) }) {
1020-
log.Debug("Drop announcement because queue is full", "number", b.lastAnnounce.Number, "hash", b.lastAnnounce.Hash)
1024+
announce := b.signedAnnounce
1025+
if !p.queueSend(func() { p.sendAnnounce(announce) }) {
1026+
log.Debug("Drop announcement because queue is full", "number", announce.Number, "hash", announce.Hash)
1027+
} else {
1028+
log.Debug("Sent announcement", "number", announce.Number, "hash", announce.Hash)
10211029
}
10221030
}
10231031
p.headInfo = blockInfo{b.lastAnnounce.Hash, b.lastAnnounce.Number, b.lastAnnounce.Td}

‎p2p/simulations/adapters/inproc.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,9 @@ func (s *SimAdapter) NewNode(config *NodeConfig) (Node, error) {
9999
Dialer: s,
100100
EnableMsgEvents: config.EnableMsgEvents,
101101
},
102-
NoUSB: true,
103-
Logger: log.New("node.id", id.String()),
102+
ExternalSigner: config.ExternalSigner,
103+
NoUSB: true,
104+
Logger: log.New("node.id", id.String()),
104105
})
105106
if err != nil {
106107
return nil, err
@@ -263,7 +264,6 @@ func (sn *SimNode) Start(snapshots map[string][]byte) error {
263264
continue
264265
}
265266
sn.running[name] = service
266-
sn.node.RegisterLifecycle(service)
267267
}
268268
})
269269
if regErr != nil {

‎p2p/simulations/adapters/types.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ type NodeConfig struct {
107107
// These values need to be checked and acted upon by node Services
108108
Properties []string
109109

110+
// ExternalSigner specifies an external URI for a clef-type signer
111+
ExternalSigner string
112+
110113
// Enode
111114
node *enode.Node
112115

0 commit comments

Comments
 (0)