p2p/discv5: add deprecation warning and remove unused code (#20367)

* p2p/discv5: add deprecation warning and remove unused code

* p2p/discv5: remove unused variables
pull/20375/head
Felix Lange 5 years ago committed by Péter Szilágyi
parent 0abcf03fde
commit fdff182f11
  1. 4
      p2p/discv5/README
  2. 25
      p2p/discv5/database.go
  3. 23
      p2p/discv5/net.go
  4. 44
      p2p/discv5/net_test.go
  5. 25
      p2p/discv5/node.go
  6. 126
      p2p/discv5/ntp.go
  7. 25
      p2p/discv5/sim_test.go
  8. 8
      p2p/discv5/table.go
  9. 99
      p2p/discv5/table_test.go
  10. 70
      p2p/discv5/ticket.go
  11. 16
      p2p/discv5/udp.go
  12. 450
      p2p/discv5/udp_test.go

@ -0,0 +1,4 @@
This package is an early prototype of Discovery v5. Do not use this code.
See https://github.com/ethereum/devp2p/blob/master/discv5/discv5.md for the
current Discovery v5 specification.

@ -58,12 +58,11 @@ var (
nodeDBVersionKey = []byte("version") // Version of the database to flush if changes nodeDBVersionKey = []byte("version") // Version of the database to flush if changes
nodeDBItemPrefix = []byte("n:") // Identifier to prefix node entries with nodeDBItemPrefix = []byte("n:") // Identifier to prefix node entries with
nodeDBDiscoverRoot = ":discover" nodeDBDiscoverRoot = ":discover"
nodeDBDiscoverPing = nodeDBDiscoverRoot + ":lastping" nodeDBDiscoverPing = nodeDBDiscoverRoot + ":lastping"
nodeDBDiscoverPong = nodeDBDiscoverRoot + ":lastpong" nodeDBDiscoverPong = nodeDBDiscoverRoot + ":lastpong"
nodeDBDiscoverFindFails = nodeDBDiscoverRoot + ":findfail" nodeDBDiscoverFindFails = nodeDBDiscoverRoot + ":findfail"
nodeDBDiscoverLocalEndpoint = nodeDBDiscoverRoot + ":localendpoint" nodeDBTopicRegTickets = ":tickets"
nodeDBTopicRegTickets = ":tickets"
) )
// newNodeDB creates a new node database for storing and retrieving infos about // newNodeDB creates a new node database for storing and retrieving infos about
@ -311,20 +310,6 @@ func (db *nodeDB) updateFindFails(id NodeID, fails int) error {
return db.storeInt64(makeKey(id, nodeDBDiscoverFindFails), int64(fails)) return db.storeInt64(makeKey(id, nodeDBDiscoverFindFails), int64(fails))
} }
// localEndpoint returns the last local endpoint communicated to the
// given remote node.
func (db *nodeDB) localEndpoint(id NodeID) *rpcEndpoint {
var ep rpcEndpoint
if err := db.fetchRLP(makeKey(id, nodeDBDiscoverLocalEndpoint), &ep); err != nil {
return nil
}
return &ep
}
func (db *nodeDB) updateLocalEndpoint(id NodeID, ep rpcEndpoint) error {
return db.storeRLP(makeKey(id, nodeDBDiscoverLocalEndpoint), &ep)
}
// querySeeds retrieves random nodes to be used as potential seed nodes // querySeeds retrieves random nodes to be used as potential seed nodes
// for bootstrapping. // for bootstrapping.
func (db *nodeDB) querySeeds(n int, maxAge time.Duration) []*Node { func (db *nodeDB) querySeeds(n int, maxAge time.Duration) []*Node {

@ -77,14 +77,6 @@ type Network struct {
nursery []*Node nursery []*Node
nodes map[NodeID]*Node // tracks active nodes with state != known nodes map[NodeID]*Node // tracks active nodes with state != known
timeoutTimers map[timeoutEvent]*time.Timer timeoutTimers map[timeoutEvent]*time.Timer
// Revalidation queues.
// Nodes put on these queues will be pinged eventually.
slowRevalidateQueue []*Node
fastRevalidateQueue []*Node
// Buffers for state transition.
sendBuf []*ingressPacket
} }
// transport is implemented by the UDP transport. // transport is implemented by the UDP transport.
@ -104,10 +96,9 @@ type transport interface {
} }
type findnodeQuery struct { type findnodeQuery struct {
remote *Node remote *Node
target common.Hash target common.Hash
reply chan<- []*Node reply chan<- []*Node
nresults int // counter for received nodes
} }
type topicRegisterReq struct { type topicRegisterReq struct {
@ -650,10 +641,10 @@ loop:
if net.conn != nil { if net.conn != nil {
net.conn.Close() net.conn.Close()
} }
if refreshDone != nil { // TODO: wait for pending refresh.
// TODO: wait for pending refresh. // if refreshDone != nil {
//<-refreshResults // <-refreshResults
} // }
// Cancel all pending timeouts. // Cancel all pending timeouts.
for _, timer := range net.timeoutTimers { for _, timer := range net.timeoutTimers {
timer.Stop() timer.Stop()

@ -17,7 +17,6 @@
package discv5 package discv5
import ( import (
"fmt"
"net" "net"
"testing" "testing"
"time" "time"
@ -265,10 +264,6 @@ type preminedTestnet struct {
net *Network net *Network
} }
func (tn *preminedTestnet) sendFindnode(to *Node, target NodeID) {
panic("sendFindnode called")
}
func (tn *preminedTestnet) sendFindnodeHash(to *Node, target common.Hash) { func (tn *preminedTestnet) sendFindnodeHash(to *Node, target common.Hash) {
// current log distance is encoded in port number // current log distance is encoded in port number
// fmt.Println("findnode query at dist", toaddr.Port) // fmt.Println("findnode query at dist", toaddr.Port)
@ -316,10 +311,6 @@ func (tn *preminedTestnet) sendNeighbours(to *Node, nodes []*Node) {
panic("sendNeighbours called") panic("sendNeighbours called")
} }
func (tn *preminedTestnet) sendTopicQuery(to *Node, topic Topic) {
panic("sendTopicQuery called")
}
func (tn *preminedTestnet) sendTopicNodes(to *Node, queryHash common.Hash, nodes []*Node) { func (tn *preminedTestnet) sendTopicNodes(to *Node, queryHash common.Hash, nodes []*Node) {
panic("sendTopicNodes called") panic("sendTopicNodes called")
} }
@ -334,41 +325,6 @@ func (*preminedTestnet) localAddr() *net.UDPAddr {
return &net.UDPAddr{IP: net.ParseIP("10.0.1.1"), Port: 40000} return &net.UDPAddr{IP: net.ParseIP("10.0.1.1"), Port: 40000}
} }
// mine generates a testnet struct literal with nodes at
// various distances to the given target.
func (tn *preminedTestnet) mine(target NodeID) {
tn.target = target
tn.targetSha = crypto.Keccak256Hash(tn.target[:])
found := 0
for found < bucketSize*10 {
k := newkey()
id := PubkeyID(&k.PublicKey)
sha := crypto.Keccak256Hash(id[:])
ld := logdist(tn.targetSha, sha)
if len(tn.dists[ld]) < bucketSize {
tn.dists[ld] = append(tn.dists[ld], id)
fmt.Println("found ID with ld", ld)
found++
}
}
fmt.Println("&preminedTestnet{")
fmt.Printf(" target: %#v,\n", tn.target)
fmt.Printf(" targetSha: %#v,\n", tn.targetSha)
fmt.Printf(" dists: [%d][]NodeID{\n", len(tn.dists))
for ld, ns := range &tn.dists {
if len(ns) == 0 {
continue
}
fmt.Printf(" %d: []NodeID{\n", ld)
for _, n := range ns {
fmt.Printf(" MustHexID(\"%x\"),\n", n[:])
}
fmt.Println(" },")
}
fmt.Println(" },")
fmt.Println("}")
}
func injectResponse(net *Network, from *Node, ev nodeEvent, packet interface{}) { func injectResponse(net *Network, from *Node, ev nodeEvent, packet interface{}) {
go net.reqReadPacket(ingressPacket{remoteID: from.ID, remoteAddr: from.addr(), ev: ev, data: packet}) go net.reqReadPacket(ingressPacket{remoteID: from.ID, remoteAddr: from.addr(), ev: ev, data: packet})
} }

@ -66,23 +66,6 @@ func (n *Node) addr() *net.UDPAddr {
return &net.UDPAddr{IP: n.IP, Port: int(n.UDP)} return &net.UDPAddr{IP: n.IP, Port: int(n.UDP)}
} }
func (n *Node) setAddr(a *net.UDPAddr) {
n.IP = a.IP
if ipv4 := a.IP.To4(); ipv4 != nil {
n.IP = ipv4
}
n.UDP = uint16(a.Port)
}
// compares the given address against the stored values.
func (n *Node) addrEqual(a *net.UDPAddr) bool {
ip := a.IP
if ipv4 := a.IP.To4(); ipv4 != nil {
ip = ipv4
}
return n.UDP == uint16(a.Port) && n.IP.Equal(ip)
}
// Incomplete returns true for nodes with no IP address. // Incomplete returns true for nodes with no IP address.
func (n *Node) Incomplete() bool { func (n *Node) Incomplete() bool {
return n.IP == nil return n.IP == nil
@ -326,14 +309,6 @@ func (n NodeID) Pubkey() (*ecdsa.PublicKey, error) {
return p, nil return p, nil
} }
func (id NodeID) mustPubkey() ecdsa.PublicKey {
pk, err := id.Pubkey()
if err != nil {
panic(err)
}
return *pk
}
// recoverNodeID computes the public key used to sign the // recoverNodeID computes the public key used to sign the
// given hash from the signature. // given hash from the signature.
func recoverNodeID(hash, sig []byte) (id NodeID, err error) { func recoverNodeID(hash, sig []byte) (id NodeID, err error) {

@ -1,126 +0,0 @@
// Copyright 2016 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
// Contains the NTP time drift detection via the SNTP protocol:
// https://tools.ietf.org/html/rfc4330
package discv5
import (
"fmt"
"net"
"sort"
"strings"
"time"
"github.com/ethereum/go-ethereum/log"
)
const (
ntpPool = "pool.ntp.org" // ntpPool is the NTP server to query for the current time
ntpChecks = 3 // Number of measurements to do against the NTP server
)
// durationSlice attaches the methods of sort.Interface to []time.Duration,
// sorting in increasing order.
type durationSlice []time.Duration
func (s durationSlice) Len() int { return len(s) }
func (s durationSlice) Less(i, j int) bool { return s[i] < s[j] }
func (s durationSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
// checkClockDrift queries an NTP server for clock drifts and warns the user if
// one large enough is detected.
func checkClockDrift() {
drift, err := sntpDrift(ntpChecks)
if err != nil {
return
}
if drift < -driftThreshold || drift > driftThreshold {
warning := fmt.Sprintf("System clock seems off by %v, which can prevent network connectivity", drift)
howtofix := fmt.Sprintf("Please enable network time synchronisation in system settings")
separator := strings.Repeat("-", len(warning))
log.Warn(separator)
log.Warn(warning)
log.Warn(howtofix)
log.Warn(separator)
} else {
log.Debug(fmt.Sprintf("Sanity NTP check reported %v drift, all ok", drift))
}
}
// sntpDrift does a naive time resolution against an NTP server and returns the
// measured drift. This method uses the simple version of NTP. It's not precise
// but should be fine for these purposes.
//
// Note, it executes two extra measurements compared to the number of requested
// ones to be able to discard the two extremes as outliers.
func sntpDrift(measurements int) (time.Duration, error) {
// Resolve the address of the NTP server
addr, err := net.ResolveUDPAddr("udp", ntpPool+":123")
if err != nil {
return 0, err
}
// Construct the time request (empty package with only 2 fields set):
// Bits 3-5: Protocol version, 3
// Bits 6-8: Mode of operation, client, 3
request := make([]byte, 48)
request[0] = 3<<3 | 3
// Execute each of the measurements
drifts := []time.Duration{}
for i := 0; i < measurements+2; i++ {
// Dial the NTP server and send the time retrieval request
conn, err := net.DialUDP("udp", nil, addr)
if err != nil {
return 0, err
}
defer conn.Close()
sent := time.Now()
if _, err = conn.Write(request); err != nil {
return 0, err
}
// Retrieve the reply and calculate the elapsed time
conn.SetDeadline(time.Now().Add(5 * time.Second))
reply := make([]byte, 48)
if _, err = conn.Read(reply); err != nil {
return 0, err
}
elapsed := time.Since(sent)
// Reconstruct the time from the reply data
sec := uint64(reply[43]) | uint64(reply[42])<<8 | uint64(reply[41])<<16 | uint64(reply[40])<<24
frac := uint64(reply[47]) | uint64(reply[46])<<8 | uint64(reply[45])<<16 | uint64(reply[44])<<24
nanosec := sec*1e9 + (frac*1e9)>>32
t := time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC).Add(time.Duration(nanosec)).Local()
// Calculate the drift based on an assumed answer time of RRT/2
drifts = append(drifts, sent.Sub(t)+elapsed/2)
}
// Calculate average drif (drop two extremities to avoid outliers)
sort.Sort(durationSlice(drifts))
drift := time.Duration(0)
for i := 1; i < len(drifts)-1; i++ {
drift += drifts[i]
}
return drift / time.Duration(measurements), nil
}

@ -294,15 +294,6 @@ func (s *simulation) launchNode(log bool) *Network {
return net return net
} }
func (s *simulation) dropNode(id NodeID) {
s.mu.Lock()
n := s.nodes[id]
delete(s.nodes, id)
s.mu.Unlock()
n.Close()
}
type simTransport struct { type simTransport struct {
joinTime time.Time joinTime time.Time
sender NodeID sender NodeID
@ -358,22 +349,6 @@ func (st *simTransport) sendPing(remote *Node, remoteAddr *net.UDPAddr, topics [
return hash return hash
} }
func (st *simTransport) sendPong(remote *Node, pingHash []byte) {
raddr := remote.addr()
st.sendPacket(remote.ID, ingressPacket{
remoteID: st.sender,
remoteAddr: st.senderAddr,
hash: st.nextHash(),
ev: pongPacket,
data: &pong{
To: rpcEndpoint{IP: raddr.IP, UDP: uint16(raddr.Port), TCP: 30303},
ReplyTok: pingHash,
Expiration: uint64(time.Now().Unix() + int64(expiration)),
},
})
}
func (st *simTransport) sendFindnodeHash(remote *Node, target common.Hash) { func (st *simTransport) sendFindnodeHash(remote *Node, target common.Hash) {
st.sendPacket(remote.ID, ingressPacket{ st.sendPacket(remote.ID, ingressPacket{
remoteID: st.sender, remoteID: st.sender,

@ -14,12 +14,8 @@
// You should have received a copy of the GNU Lesser General Public License // You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
// Package discv5 implements the RLPx v5 Topic Discovery Protocol. // Package discv5 is a prototype implementation of Discvery v5.
// // Deprecated: do not use this package.
// The Topic Discovery protocol provides a way to find RLPx nodes that
// can be connected to. It uses a Kademlia-like protocol to maintain a
// distributed database of the IDs and endpoints of all listening
// nodes.
package discv5 package discv5
import ( import (

@ -31,72 +31,6 @@ import (
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
) )
type nullTransport struct{}
func (nullTransport) sendPing(remote *Node, remoteAddr *net.UDPAddr) []byte { return []byte{1} }
func (nullTransport) sendPong(remote *Node, pingHash []byte) {}
func (nullTransport) sendFindnode(remote *Node, target NodeID) {}
func (nullTransport) sendNeighbours(remote *Node, nodes []*Node) {}
func (nullTransport) localAddr() *net.UDPAddr { return new(net.UDPAddr) }
func (nullTransport) Close() {}
// func TestTable_pingReplace(t *testing.T) {
// doit := func(newNodeIsResponding, lastInBucketIsResponding bool) {
// transport := newPingRecorder()
// tab, _ := newTable(transport, NodeID{}, &net.UDPAddr{})
// defer tab.Close()
// pingSender := NewNode(MustHexID("a502af0f59b2aab7746995408c79e9ca312d2793cc997e44fc55eda62f0150bbb8c59a6f9269ba3a081518b62699ee807c7c19c20125ddfccca872608af9e370"), net.IP{}, 99, 99)
//
// // fill up the sender's bucket.
// last := fillBucket(tab, 253)
//
// // this call to bond should replace the last node
// // in its bucket if the node is not responding.
// transport.responding[last.ID] = lastInBucketIsResponding
// transport.responding[pingSender.ID] = newNodeIsResponding
// tab.bond(true, pingSender.ID, &net.UDPAddr{}, 0)
//
// // first ping goes to sender (bonding pingback)
// if !transport.pinged[pingSender.ID] {
// t.Error("table did not ping back sender")
// }
// if newNodeIsResponding {
// // second ping goes to oldest node in bucket
// // to see whether it is still alive.
// if !transport.pinged[last.ID] {
// t.Error("table did not ping last node in bucket")
// }
// }
//
// tab.mutex.Lock()
// defer tab.mutex.Unlock()
// if l := len(tab.buckets[253].entries); l != bucketSize {
// t.Errorf("wrong bucket size after bond: got %d, want %d", l, bucketSize)
// }
//
// if lastInBucketIsResponding || !newNodeIsResponding {
// if !contains(tab.buckets[253].entries, last.ID) {
// t.Error("last entry was removed")
// }
// if contains(tab.buckets[253].entries, pingSender.ID) {
// t.Error("new entry was added")
// }
// } else {
// if contains(tab.buckets[253].entries, last.ID) {
// t.Error("last entry was not removed")
// }
// if !contains(tab.buckets[253].entries, pingSender.ID) {
// t.Error("new entry was not added")
// }
// }
// }
//
// doit(true, true)
// doit(false, true)
// doit(true, false)
// doit(false, false)
// }
func TestBucket_bumpNoDuplicates(t *testing.T) { func TestBucket_bumpNoDuplicates(t *testing.T) {
t.Parallel() t.Parallel()
cfg := &quick.Config{ cfg := &quick.Config{
@ -139,17 +73,6 @@ func TestBucket_bumpNoDuplicates(t *testing.T) {
} }
} }
// fillBucket inserts nodes into the given bucket until
// it is full. The node's IDs dont correspond to their
// hashes.
func fillBucket(tab *Table, ld int) (last *Node) {
b := tab.buckets[ld]
for len(b.entries) < bucketSize {
b.entries = append(b.entries, nodeAtDistance(tab.self.sha, ld))
}
return b.entries[bucketSize-1]
}
// nodeAtDistance creates a node for which logdist(base, n.sha) == ld. // nodeAtDistance creates a node for which logdist(base, n.sha) == ld.
// The node's ID does not correspond to n.sha. // The node's ID does not correspond to n.sha.
func nodeAtDistance(base common.Hash, ld int) (n *Node) { func nodeAtDistance(base common.Hash, ld int) (n *Node) {
@ -159,28 +82,6 @@ func nodeAtDistance(base common.Hash, ld int) (n *Node) {
return n return n
} }
type pingRecorder struct{ responding, pinged map[NodeID]bool }
func newPingRecorder() *pingRecorder {
return &pingRecorder{make(map[NodeID]bool), make(map[NodeID]bool)}
}
func (t *pingRecorder) findnode(toid NodeID, toaddr *net.UDPAddr, target NodeID) ([]*Node, error) {
panic("findnode called on pingRecorder")
}
func (t *pingRecorder) close() {}
func (t *pingRecorder) waitping(from NodeID) error {
return nil // remote always pings
}
func (t *pingRecorder) ping(toid NodeID, toaddr *net.UDPAddr) error {
t.pinged[toid] = true
if t.responding[toid] {
return nil
} else {
return errTimeout
}
}
func TestTable_closest(t *testing.T) { func TestTable_closest(t *testing.T) {
t.Parallel() t.Parallel()

@ -22,7 +22,6 @@ import (
"fmt" "fmt"
"math" "math"
"math/rand" "math/rand"
"sort"
"time" "time"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
@ -33,8 +32,6 @@ import (
const ( const (
ticketTimeBucketLen = time.Minute ticketTimeBucketLen = time.Minute
timeWindow = 10 // * ticketTimeBucketLen
wantTicketsInWindow = 10
collectFrequency = time.Second * 30 collectFrequency = time.Second * 30
registerFrequency = time.Second * 60 registerFrequency = time.Second * 60
maxCollectDebt = 10 maxCollectDebt = 10
@ -139,7 +136,6 @@ type ticketStore struct {
lastBucketFetched timeBucket lastBucketFetched timeBucket
nextTicketCached *ticketRef nextTicketCached *ticketRef
nextTicketReg mclock.AbsTime
searchTopicMap map[Topic]searchTopic searchTopicMap map[Topic]searchTopic
nextTopicQueryCleanup mclock.AbsTime nextTopicQueryCleanup mclock.AbsTime
@ -268,57 +264,6 @@ func (s *ticketStore) nextSearchLookup(topic Topic) lookupInfo {
return target return target
} }
// ticketsInWindow returns the tickets of a given topic in the registration window.
func (s *ticketStore) ticketsInWindow(topic Topic) []ticketRef {
// Sanity check that the topic still exists before operating on it
if s.tickets[topic] == nil {
log.Warn("Listing non-existing discovery tickets", "topic", topic)
return nil
}
// Gather all the tickers in the next time window
var tickets []ticketRef
buckets := s.tickets[topic].buckets
for idx := timeBucket(0); idx < timeWindow; idx++ {
tickets = append(tickets, buckets[s.lastBucketFetched+idx]...)
}
log.Trace("Retrieved discovery registration tickets", "topic", topic, "from", s.lastBucketFetched, "tickets", len(tickets))
return tickets
}
func (s *ticketStore) removeExcessTickets(t Topic) {
tickets := s.ticketsInWindow(t)
if len(tickets) <= wantTicketsInWindow {
return
}
sort.Sort(ticketRefByWaitTime(tickets))
for _, r := range tickets[wantTicketsInWindow:] {
s.removeTicketRef(r)
}
}
type ticketRefByWaitTime []ticketRef
// Len is the number of elements in the collection.
func (s ticketRefByWaitTime) Len() int {
return len(s)
}
func (ref ticketRef) waitTime() mclock.AbsTime {
return ref.t.regTime[ref.idx] - ref.t.issueTime
}
// Less reports whether the element with
// index i should sort before the element with index j.
func (s ticketRefByWaitTime) Less(i, j int) bool {
return s[i].waitTime() < s[j].waitTime()
}
// Swap swaps the elements with indexes i and j.
func (s ticketRefByWaitTime) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s *ticketStore) addTicketRef(r ticketRef) { func (s *ticketStore) addTicketRef(r ticketRef) {
topic := r.t.topics[r.idx] topic := r.t.topics[r.idx]
tickets := s.tickets[topic] tickets := s.tickets[topic]
@ -565,15 +510,6 @@ func (s *ticketStore) addTicket(localTime mclock.AbsTime, pingHash []byte, ticke
} }
} }
func (s *ticketStore) getNodeTicket(node *Node) *ticket {
if s.nodes[node] == nil {
log.Trace("Retrieving node ticket", "node", node.ID, "serial", nil)
} else {
log.Trace("Retrieving node ticket", "node", node.ID, "serial", s.nodes[node].serial)
}
return s.nodes[node]
}
func (s *ticketStore) canQueryTopic(node *Node, topic Topic) bool { func (s *ticketStore) canQueryTopic(node *Node, topic Topic) bool {
qq := s.queriesSent[node] qq := s.queriesSent[node]
if qq != nil { if qq != nil {
@ -770,12 +706,6 @@ func globalRandRead(b []byte) {
} }
} }
func (r *topicRadius) isInRadius(addrHash common.Hash) bool {
nodePrefix := binary.BigEndian.Uint64(addrHash[0:8])
dist := nodePrefix ^ r.topicHashPrefix
return dist < r.radius
}
func (r *topicRadius) chooseLookupBucket(a, b int) int { func (r *topicRadius) chooseLookupBucket(a, b int) int {
if a < 0 { if a < 0 {
a = 0 a = 0

@ -27,7 +27,6 @@ import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/p2p/nat"
"github.com/ethereum/go-ethereum/p2p/netutil" "github.com/ethereum/go-ethereum/p2p/netutil"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
) )
@ -38,15 +37,12 @@ const Version = 4
var ( var (
errPacketTooSmall = errors.New("too small") errPacketTooSmall = errors.New("too small")
errBadPrefix = errors.New("bad prefix") errBadPrefix = errors.New("bad prefix")
errTimeout = errors.New("RPC timeout")
) )
// Timeouts // Timeouts
const ( const (
respTimeout = 500 * time.Millisecond respTimeout = 500 * time.Millisecond
expiration = 20 * time.Second expiration = 20 * time.Second
driftThreshold = 10 * time.Second // Allowed clock drift before warning user
) )
// RPC request structures // RPC request structures
@ -187,10 +183,6 @@ func makeEndpoint(addr *net.UDPAddr, tcpPort uint16) rpcEndpoint {
return rpcEndpoint{IP: ip, UDP: uint16(addr.Port), TCP: tcpPort} return rpcEndpoint{IP: ip, UDP: uint16(addr.Port), TCP: tcpPort}
} }
func (e1 rpcEndpoint) equal(e2 rpcEndpoint) bool {
return e1.UDP == e2.UDP && e1.TCP == e2.TCP && e1.IP.Equal(e2.IP)
}
func nodeFromRPC(sender *net.UDPAddr, rn rpcNode) (*Node, error) { func nodeFromRPC(sender *net.UDPAddr, rn rpcNode) (*Node, error) {
if err := netutil.CheckRelayIP(sender.IP, rn.IP); err != nil { if err := netutil.CheckRelayIP(sender.IP, rn.IP); err != nil {
return nil, err return nil, err
@ -225,7 +217,6 @@ type udp struct {
conn conn conn conn
priv *ecdsa.PrivateKey priv *ecdsa.PrivateKey
ourEndpoint rpcEndpoint ourEndpoint rpcEndpoint
nat nat.Interface
net *Network net *Network
} }
@ -274,13 +265,6 @@ func (t *udp) sendPing(remote *Node, toaddr *net.UDPAddr, topics []Topic) (hash
return hash return hash
} }
func (t *udp) sendFindnode(remote *Node, target NodeID) {
t.sendPacket(remote.ID, remote.addr(), byte(findnodePacket), findnode{
Target: target,
Expiration: uint64(time.Now().Add(expiration).Unix()),
})
}
func (t *udp) sendNeighbours(remote *Node, results []*Node) { func (t *udp) sendNeighbours(remote *Node, results []*Node) {
// Send neighbors in chunks with at most maxNeighbors per packet // Send neighbors in chunks with at most maxNeighbors per packet
// to stay below the 1280 byte limit. // to stay below the 1280 byte limit.

@ -1,450 +0,0 @@
// Copyright 2016 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package discv5
import (
"encoding/hex"
"errors"
"io"
"net"
"reflect"
"sync"
"testing"
"github.com/davecgh/go-spew/spew"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/rlp"
)
func init() {
spew.Config.DisableMethods = true
}
// shared test variables
var (
testLocal = rpcEndpoint{IP: net.ParseIP("3.3.3.3").To4(), UDP: 5, TCP: 6}
)
// type udpTest struct {
// t *testing.T
// pipe *dgramPipe
// table *Table
// udp *udp
// sent [][]byte
// localkey, remotekey *ecdsa.PrivateKey
// remoteaddr *net.UDPAddr
// }
//
// func newUDPTest(t *testing.T) *udpTest {
// test := &udpTest{
// t: t,
// pipe: newpipe(),
// localkey: newkey(),
// remotekey: newkey(),
// remoteaddr: &net.UDPAddr{IP: net.IP{1, 2, 3, 4}, Port: 30303},
// }
// test.table, test.udp, _ = newUDP(test.localkey, test.pipe, nil, "")
// return test
// }
//
// // handles a packet as if it had been sent to the transport.
// func (test *udpTest) packetIn(wantError error, ptype byte, data packet) error {
// enc, err := encodePacket(test.remotekey, ptype, data)
// if err != nil {
// return test.errorf("packet (%d) encode error: %v", ptype, err)
// }
// test.sent = append(test.sent, enc)
// if err = test.udp.handlePacket(test.remoteaddr, enc); err != wantError {
// return test.errorf("error mismatch: got %q, want %q", err, wantError)
// }
// return nil
// }
//
// // waits for a packet to be sent by the transport.
// // validate should have type func(*udpTest, X) error, where X is a packet type.
// func (test *udpTest) waitPacketOut(validate interface{}) error {
// dgram := test.pipe.waitPacketOut()
// p, _, _, err := decodePacket(dgram)
// if err != nil {
// return test.errorf("sent packet decode error: %v", err)
// }
// fn := reflect.ValueOf(validate)
// exptype := fn.Type().In(0)
// if reflect.TypeOf(p) != exptype {
// return test.errorf("sent packet type mismatch, got: %v, want: %v", reflect.TypeOf(p), exptype)
// }
// fn.Call([]reflect.Value{reflect.ValueOf(p)})
// return nil
// }
//
// func (test *udpTest) errorf(format string, args ...interface{}) error {
// _, file, line, ok := runtime.Caller(2) // errorf + waitPacketOut
// if ok {
// file = filepath.Base(file)
// } else {
// file = "???"
// line = 1
// }
// err := fmt.Errorf(format, args...)
// fmt.Printf("\t%s:%d: %v\n", file, line, err)
// test.t.Fail()
// return err
// }
//
// func TestUDP_packetErrors(t *testing.T) {
// test := newUDPTest(t)
// defer test.table.Close()
//
// test.packetIn(errExpired, pingPacket, &ping{From: testRemote, To: testLocalAnnounced, Version: Version})
// test.packetIn(errUnsolicitedReply, pongPacket, &pong{ReplyTok: []byte{}, Expiration: futureExp})
// test.packetIn(errUnknownNode, findnodePacket, &findnode{Expiration: futureExp})
// test.packetIn(errUnsolicitedReply, neighborsPacket, &neighbors{Expiration: futureExp})
// }
//
// func TestUDP_findnode(t *testing.T) {
// test := newUDPTest(t)
// defer test.table.Close()
//
// // put a few nodes into the table. their exact
// // distribution shouldn't matter much, although we need to
// // take care not to overflow any bucket.
// targetHash := crypto.Keccak256Hash(testTarget[:])
// nodes := &nodesByDistance{target: targetHash}
// for i := 0; i < bucketSize; i++ {
// nodes.push(nodeAtDistance(test.table.self.sha, i+2), bucketSize)
// }
// test.table.stuff(nodes.entries)
//
// // ensure there's a bond with the test node,
// // findnode won't be accepted otherwise.
// test.table.db.updateNode(NewNode(
// PubkeyID(&test.remotekey.PublicKey),
// test.remoteaddr.IP,
// uint16(test.remoteaddr.Port),
// 99,
// ))
// // check that closest neighbors are returned.
// test.packetIn(nil, findnodePacket, &findnode{Target: testTarget, Expiration: futureExp})
// expected := test.table.closest(targetHash, bucketSize)
//
// waitNeighbors := func(want []*Node) {
// test.waitPacketOut(func(p *neighbors) {
// if len(p.Nodes) != len(want) {
// t.Errorf("wrong number of results: got %d, want %d", len(p.Nodes), bucketSize)
// }
// for i := range p.Nodes {
// if p.Nodes[i].ID != want[i].ID {
// t.Errorf("result mismatch at %d:\n got: %v\n want: %v", i, p.Nodes[i], expected.entries[i])
// }
// }
// })
// }
// waitNeighbors(expected.entries[:maxNeighbors])
// waitNeighbors(expected.entries[maxNeighbors:])
// }
//
// func TestUDP_findnodeMultiReply(t *testing.T) {
// test := newUDPTest(t)
// defer test.table.Close()
//
// // queue a pending findnode request
// resultc, errc := make(chan []*Node), make(chan error)
// go func() {
// rid := PubkeyID(&test.remotekey.PublicKey)
// ns, err := test.udp.findnode(rid, test.remoteaddr, testTarget)
// if err != nil && len(ns) == 0 {
// errc <- err
// } else {
// resultc <- ns
// }
// }()
//
// // wait for the findnode to be sent.
// // after it is sent, the transport is waiting for a reply
// test.waitPacketOut(func(p *findnode) {
// if p.Target != testTarget {
// t.Errorf("wrong target: got %v, want %v", p.Target, testTarget)
// }
// })
//
// // send the reply as two packets.
// list := []*Node{
// MustParseNode("enode://ba85011c70bcc5c04d8607d3a0ed29aa6179c092cbdda10d5d32684fb33ed01bd94f588ca8f91ac48318087dcb02eaf36773a7a453f0eedd6742af668097b29c@10.0.1.16:30303?discport=30304"),
// MustParseNode("enode://81fa361d25f157cd421c60dcc28d8dac5ef6a89476633339c5df30287474520caca09627da18543d9079b5b288698b542d56167aa5c09111e55acdbbdf2ef799@10.0.1.16:30303"),
// MustParseNode("enode://9bffefd833d53fac8e652415f4973bee289e8b1a5c6c4cbe70abf817ce8a64cee11b823b66a987f51aaa9fba0d6a91b3e6bf0d5a5d1042de8e9eeea057b217f8@10.0.1.36:30301?discport=17"),
// MustParseNode("enode://1b5b4aa662d7cb44a7221bfba67302590b643028197a7d5214790f3bac7aaa4a3241be9e83c09cf1f6c69d007c634faae3dc1b1221793e8446c0b3a09de65960@10.0.1.16:30303"),
// }
// rpclist := make([]rpcNode, len(list))
// for i := range list {
// rpclist[i] = nodeToRPC(list[i])
// }
// test.packetIn(nil, neighborsPacket, &neighbors{Expiration: futureExp, Nodes: rpclist[:2]})
// test.packetIn(nil, neighborsPacket, &neighbors{Expiration: futureExp, Nodes: rpclist[2:]})
//
// // check that the sent neighbors are all returned by findnode
// select {
// case result := <-resultc:
// if !reflect.DeepEqual(result, list) {
// t.Errorf("neighbors mismatch:\n got: %v\n want: %v", result, list)
// }
// case err := <-errc:
// t.Errorf("findnode error: %v", err)
// case <-time.After(5 * time.Second):
// t.Error("findnode did not return within 5 seconds")
// }
// }
//
// func TestUDP_successfulPing(t *testing.T) {
// test := newUDPTest(t)
// added := make(chan *Node, 1)
// test.table.nodeAddedHook = func(n *Node) { added <- n }
// defer test.table.Close()
//
// // The remote side sends a ping packet to initiate the exchange.
// go test.packetIn(nil, pingPacket, &ping{From: testRemote, To: testLocalAnnounced, Version: Version, Expiration: futureExp})
//
// // the ping is replied to.
// test.waitPacketOut(func(p *pong) {
// pinghash := test.sent[0][:macSize]
// if !bytes.Equal(p.ReplyTok, pinghash) {
// t.Errorf("got pong.ReplyTok %x, want %x", p.ReplyTok, pinghash)
// }
// wantTo := rpcEndpoint{
// // The mirrored UDP address is the UDP packet sender
// IP: test.remoteaddr.IP, UDP: uint16(test.remoteaddr.Port),
// // The mirrored TCP port is the one from the ping packet
// TCP: testRemote.TCP,
// }
// if !reflect.DeepEqual(p.To, wantTo) {
// t.Errorf("got pong.To %v, want %v", p.To, wantTo)
// }
// })
//
// // remote is unknown, the table pings back.
// test.waitPacketOut(func(p *ping) error {
// if !reflect.DeepEqual(p.From, test.udp.ourEndpoint) {
// t.Errorf("got ping.From %v, want %v", p.From, test.udp.ourEndpoint)
// }
// wantTo := rpcEndpoint{
// // The mirrored UDP address is the UDP packet sender.
// IP: test.remoteaddr.IP, UDP: uint16(test.remoteaddr.Port),
// TCP: 0,
// }
// if !reflect.DeepEqual(p.To, wantTo) {
// t.Errorf("got ping.To %v, want %v", p.To, wantTo)
// }
// return nil
// })
// test.packetIn(nil, pongPacket, &pong{Expiration: futureExp})
//
// // the node should be added to the table shortly after getting the
// // pong packet.
// select {
// case n := <-added:
// rid := PubkeyID(&test.remotekey.PublicKey)
// if n.ID != rid {
// t.Errorf("node has wrong ID: got %v, want %v", n.ID, rid)
// }
// if !bytes.Equal(n.IP, test.remoteaddr.IP) {
// t.Errorf("node has wrong IP: got %v, want: %v", n.IP, test.remoteaddr.IP)
// }
// if int(n.UDP) != test.remoteaddr.Port {
// t.Errorf("node has wrong UDP port: got %v, want: %v", n.UDP, test.remoteaddr.Port)
// }
// if n.TCP != testRemote.TCP {
// t.Errorf("node has wrong TCP port: got %v, want: %v", n.TCP, testRemote.TCP)
// }
// case <-time.After(2 * time.Second):
// t.Errorf("node was not added within 2 seconds")
// }
// }
var testPackets = []struct {
input string
wantPacket interface{}
}{
{
input: "71dbda3a79554728d4f94411e42ee1f8b0d561c10e1e5f5893367948c6a7d70bb87b235fa28a77070271b6c164a2dce8c7e13a5739b53b5e96f2e5acb0e458a02902f5965d55ecbeb2ebb6cabb8b2b232896a36b737666c55265ad0a68412f250001ea04cb847f000001820cfa8215a8d790000000000000000000000000000000018208ae820d058443b9a355",
wantPacket: &ping{
Version: 4,
From: rpcEndpoint{net.ParseIP("127.0.0.1").To4(), 3322, 5544},
To: rpcEndpoint{net.ParseIP("::1"), 2222, 3333},
Expiration: 1136239445,
Rest: []rlp.RawValue{},
},
},
{
input: "e9614ccfd9fc3e74360018522d30e1419a143407ffcce748de3e22116b7e8dc92ff74788c0b6663aaa3d67d641936511c8f8d6ad8698b820a7cf9e1be7155e9a241f556658c55428ec0563514365799a4be2be5a685a80971ddcfa80cb422cdd0101ec04cb847f000001820cfa8215a8d790000000000000000000000000000000018208ae820d058443b9a3550102",
wantPacket: &ping{
Version: 4,
From: rpcEndpoint{net.ParseIP("127.0.0.1").To4(), 3322, 5544},
To: rpcEndpoint{net.ParseIP("::1"), 2222, 3333},
Expiration: 1136239445,
Rest: []rlp.RawValue{{0x01}, {0x02}},
},
},
{
input: "577be4349c4dd26768081f58de4c6f375a7a22f3f7adda654d1428637412c3d7fe917cadc56d4e5e7ffae1dbe3efffb9849feb71b262de37977e7c7a44e677295680e9e38ab26bee2fcbae207fba3ff3d74069a50b902a82c9903ed37cc993c50001f83e82022bd79020010db83c4d001500000000abcdef12820cfa8215a8d79020010db885a308d313198a2e037073488208ae82823a8443b9a355c5010203040531b9019afde696e582a78fa8d95ea13ce3297d4afb8ba6433e4154caa5ac6431af1b80ba76023fa4090c408f6b4bc3701562c031041d4702971d102c9ab7fa5eed4cd6bab8f7af956f7d565ee1917084a95398b6a21eac920fe3dd1345ec0a7ef39367ee69ddf092cbfe5b93e5e568ebc491983c09c76d922dc3",
wantPacket: &ping{
Version: 555,
From: rpcEndpoint{net.ParseIP("2001:db8:3c4d:15::abcd:ef12"), 3322, 5544},
To: rpcEndpoint{net.ParseIP("2001:db8:85a3:8d3:1319:8a2e:370:7348"), 2222, 33338},
Expiration: 1136239445,
Rest: []rlp.RawValue{{0xC5, 0x01, 0x02, 0x03, 0x04, 0x05}},
},
},
{
input: "09b2428d83348d27cdf7064ad9024f526cebc19e4958f0fdad87c15eb598dd61d08423e0bf66b2069869e1724125f820d851c136684082774f870e614d95a2855d000f05d1648b2d5945470bc187c2d2216fbe870f43ed0909009882e176a46b0102f846d79020010db885a308d313198a2e037073488208ae82823aa0fbc914b16819237dcd8801d7e53f69e9719adecb3cc0e790c57e91ca4461c9548443b9a355c6010203c2040506a0c969a58f6f9095004c0177a6b47f451530cab38966a25cca5cb58f055542124e",
wantPacket: &pong{
To: rpcEndpoint{net.ParseIP("2001:db8:85a3:8d3:1319:8a2e:370:7348"), 2222, 33338},
ReplyTok: common.Hex2Bytes("fbc914b16819237dcd8801d7e53f69e9719adecb3cc0e790c57e91ca4461c954"),
Expiration: 1136239445,
Rest: []rlp.RawValue{{0xC6, 0x01, 0x02, 0x03, 0xC2, 0x04, 0x05}, {0x06}},
},
},
{
input: "c7c44041b9f7c7e41934417ebac9a8e1a4c6298f74553f2fcfdcae6ed6fe53163eb3d2b52e39fe91831b8a927bf4fc222c3902202027e5e9eb812195f95d20061ef5cd31d502e47ecb61183f74a504fe04c51e73df81f25c4d506b26db4517490103f84eb840ca634cae0d49acb401d8a4c6b6fe8c55b70d115bf400769cc1400f3258cd31387574077f301b421bc84df7266c44e9e6d569fc56be00812904767bf5ccd1fc7f8443b9a35582999983999999280dc62cc8255c73471e0a61da0c89acdc0e035e260add7fc0c04ad9ebf3919644c91cb247affc82b69bd2ca235c71eab8e49737c937a2c396",
wantPacket: &findnode{
Target: MustHexID("ca634cae0d49acb401d8a4c6b6fe8c55b70d115bf400769cc1400f3258cd31387574077f301b421bc84df7266c44e9e6d569fc56be00812904767bf5ccd1fc7f"),
Expiration: 1136239445,
Rest: []rlp.RawValue{{0x82, 0x99, 0x99}, {0x83, 0x99, 0x99, 0x99}},
},
},
{
input: "c679fc8fe0b8b12f06577f2e802d34f6fa257e6137a995f6f4cbfc9ee50ed3710faf6e66f932c4c8d81d64343f429651328758b47d3dbc02c4042f0fff6946a50f4a49037a72bb550f3a7872363a83e1b9ee6469856c24eb4ef80b7535bcf99c0004f9015bf90150f84d846321163782115c82115db8403155e1427f85f10a5c9a7755877748041af1bcd8d474ec065eb33df57a97babf54bfd2103575fa829115d224c523596b401065a97f74010610fce76382c0bf32f84984010203040101b840312c55512422cf9b8a4097e9a6ad79402e87a15ae909a4bfefa22398f03d20951933beea1e4dfa6f968212385e829f04c2d314fc2d4e255e0d3bc08792b069dbf8599020010db83c4d001500000000abcdef12820d05820d05b84038643200b172dcfef857492156971f0e6aa2c538d8b74010f8e140811d53b98c765dd2d96126051913f44582e8c199ad7c6d6819e9a56483f637feaac9448aacf8599020010db885a308d313198a2e037073488203e78203e8b8408dcab8618c3253b558d459da53bd8fa68935a719aff8b811197101a4b2b47dd2d47295286fc00cc081bb542d760717d1bdd6bec2c37cd72eca367d6dd3b9df738443b9a355010203b525a138aa34383fec3d2719a0",
wantPacket: &neighbors{
Nodes: []rpcNode{
{
ID: MustHexID("3155e1427f85f10a5c9a7755877748041af1bcd8d474ec065eb33df57a97babf54bfd2103575fa829115d224c523596b401065a97f74010610fce76382c0bf32"),
IP: net.ParseIP("99.33.22.55").To4(),
UDP: 4444,
TCP: 4445,
},
{
ID: MustHexID("312c55512422cf9b8a4097e9a6ad79402e87a15ae909a4bfefa22398f03d20951933beea1e4dfa6f968212385e829f04c2d314fc2d4e255e0d3bc08792b069db"),
IP: net.ParseIP("1.2.3.4").To4(),
UDP: 1,
TCP: 1,
},
{
ID: MustHexID("38643200b172dcfef857492156971f0e6aa2c538d8b74010f8e140811d53b98c765dd2d96126051913f44582e8c199ad7c6d6819e9a56483f637feaac9448aac"),
IP: net.ParseIP("2001:db8:3c4d:15::abcd:ef12"),
UDP: 3333,
TCP: 3333,
},
{
ID: MustHexID("8dcab8618c3253b558d459da53bd8fa68935a719aff8b811197101a4b2b47dd2d47295286fc00cc081bb542d760717d1bdd6bec2c37cd72eca367d6dd3b9df73"),
IP: net.ParseIP("2001:db8:85a3:8d3:1319:8a2e:370:7348"),
UDP: 999,
TCP: 1000,
},
},
Expiration: 1136239445,
Rest: []rlp.RawValue{{0x01}, {0x02}, {0x03}},
},
},
}
func TestForwardCompatibility(t *testing.T) {
t.Skip("skipped while working on discovery v5")
testkey, _ := crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
wantNodeID := PubkeyID(&testkey.PublicKey)
for _, test := range testPackets {
input, err := hex.DecodeString(test.input)
if err != nil {
t.Fatalf("invalid hex: %s", test.input)
}
var pkt ingressPacket
if err := decodePacket(input, &pkt); err != nil {
t.Errorf("did not accept packet %s\n%v", test.input, err)
continue
}
if !reflect.DeepEqual(pkt.data, test.wantPacket) {
t.Errorf("got %s\nwant %s", spew.Sdump(pkt.data), spew.Sdump(test.wantPacket))
}
if pkt.remoteID != wantNodeID {
t.Errorf("got id %v\nwant id %v", pkt.remoteID, wantNodeID)
}
}
}
// dgramPipe is a fake UDP socket. It queues all sent datagrams.
type dgramPipe struct {
mu *sync.Mutex
cond *sync.Cond
closing chan struct{}
closed bool
queue [][]byte
}
func newpipe() *dgramPipe {
mu := new(sync.Mutex)
return &dgramPipe{
closing: make(chan struct{}),
cond: &sync.Cond{L: mu},
mu: mu,
}
}
// WriteToUDP queues a datagram.
func (c *dgramPipe) WriteToUDP(b []byte, to *net.UDPAddr) (n int, err error) {
msg := make([]byte, len(b))
copy(msg, b)
c.mu.Lock()
defer c.mu.Unlock()
if c.closed {
return 0, errors.New("closed")
}
c.queue = append(c.queue, msg)
c.cond.Signal()
return len(b), nil
}
// ReadFromUDP just hangs until the pipe is closed.
func (c *dgramPipe) ReadFromUDP(b []byte) (n int, addr *net.UDPAddr, err error) {
<-c.closing
return 0, nil, io.EOF
}
func (c *dgramPipe) Close() error {
c.mu.Lock()
defer c.mu.Unlock()
if !c.closed {
close(c.closing)
c.closed = true
}
return nil
}
func (c *dgramPipe) LocalAddr() net.Addr {
return &net.UDPAddr{IP: testLocal.IP, Port: int(testLocal.UDP)}
}
func (c *dgramPipe) waitPacketOut() []byte {
c.mu.Lock()
defer c.mu.Unlock()
for len(c.queue) == 0 {
c.cond.Wait()
}
p := c.queue[0]
copy(c.queue, c.queue[1:])
c.queue = c.queue[:len(c.queue)-1]
return p
}
Loading…
Cancel
Save