p2p/discover: add initial discovery v5 implementation (#20750)

This adds an implementation of the current discovery v5 spec.

There is full integration with cmd/devp2p and enode.Iterator in this
version. In theory we could enable the new protocol as a replacement of
discovery v4 at any time. In practice, there will likely be a few more
changes to the spec and implementation before this can happen.
This commit is contained in:
Felix Lange
2020-04-08 09:57:23 +02:00
committed by GitHub
parent 671f22be38
commit b7394d7942
19 changed files with 2976 additions and 80 deletions

View File

@ -41,10 +41,6 @@ import (
"github.com/ethereum/go-ethereum/rlp"
)
func init() {
spew.Config.DisableMethods = true
}
// shared test variables
var (
futureExp = uint64(time.Now().Add(10 * time.Hour).Unix())
@ -117,9 +113,12 @@ func (test *udpTest) packetInFrom(wantError error, key *ecdsa.PrivateKey, addr *
func (test *udpTest) waitPacketOut(validate interface{}) (closed bool) {
test.t.Helper()
dgram, ok := test.pipe.receive()
if !ok {
dgram, err := test.pipe.receive()
if err == errClosed {
return true
} else if err != nil {
test.t.Error("packet receive error:", err)
return false
}
p, _, hash, err := decodeV4(dgram.data)
if err != nil {
@ -671,17 +670,30 @@ func (c *dgramPipe) LocalAddr() net.Addr {
return &net.UDPAddr{IP: testLocal.IP, Port: int(testLocal.UDP)}
}
func (c *dgramPipe) receive() (dgram, bool) {
func (c *dgramPipe) receive() (dgram, error) {
c.mu.Lock()
defer c.mu.Unlock()
for len(c.queue) == 0 && !c.closed {
var timedOut bool
timer := time.AfterFunc(3*time.Second, func() {
c.mu.Lock()
timedOut = true
c.mu.Unlock()
c.cond.Broadcast()
})
defer timer.Stop()
for len(c.queue) == 0 && !c.closed && !timedOut {
c.cond.Wait()
}
if c.closed {
return dgram{}, false
return dgram{}, errClosed
}
if timedOut {
return dgram{}, errTimeout
}
p := c.queue[0]
copy(c.queue, c.queue[1:])
c.queue = c.queue[:len(c.queue)-1]
return p, true
return p, nil
}