p2p: delete Server.Broadcast
This commit is contained in:
		
							
								
								
									
										14
									
								
								p2p/peer.go
									
									
									
									
									
								
							
							
						
						
									
										14
									
								
								p2p/peer.go
									
									
									
									
									
								
							@@ -273,20 +273,6 @@ func (p *Peer) getProto(code uint64) (*protoRW, error) {
 | 
			
		||||
	return nil, newPeerError(errInvalidMsgCode, "%d", code)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// writeProtoMsg sends the given message on behalf of the given named protocol.
 | 
			
		||||
// this exists because of Server.Broadcast.
 | 
			
		||||
func (p *Peer) writeProtoMsg(protoName string, msg Msg) error {
 | 
			
		||||
	proto, ok := p.running[protoName]
 | 
			
		||||
	if !ok {
 | 
			
		||||
		return fmt.Errorf("protocol %s not handled by peer", protoName)
 | 
			
		||||
	}
 | 
			
		||||
	if msg.Code >= proto.Length {
 | 
			
		||||
		return newPeerError(errInvalidMsgCode, "code %x is out of range for protocol %q", msg.Code, protoName)
 | 
			
		||||
	}
 | 
			
		||||
	msg.Code += proto.offset
 | 
			
		||||
	return p.rw.WriteMsg(msg)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type protoRW struct {
 | 
			
		||||
	Protocol
 | 
			
		||||
	in     chan Msg
 | 
			
		||||
 
 | 
			
		||||
@@ -1,7 +1,6 @@
 | 
			
		||||
package p2p
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"bytes"
 | 
			
		||||
	"errors"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"math/rand"
 | 
			
		||||
@@ -107,44 +106,6 @@ func TestPeerProtoEncodeMsg(t *testing.T) {
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestPeerWriteForBroadcast(t *testing.T) {
 | 
			
		||||
	closer, rw, peer, peerErr := testPeer([]Protocol{discard})
 | 
			
		||||
	defer closer()
 | 
			
		||||
 | 
			
		||||
	emptymsg := func(code uint64) Msg {
 | 
			
		||||
		return Msg{Code: code, Size: 0, Payload: bytes.NewReader(nil)}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// test write errors
 | 
			
		||||
	if err := peer.writeProtoMsg("b", emptymsg(3)); err == nil {
 | 
			
		||||
		t.Errorf("expected error for unknown protocol, got nil")
 | 
			
		||||
	}
 | 
			
		||||
	if err := peer.writeProtoMsg("discard", emptymsg(8)); err == nil {
 | 
			
		||||
		t.Errorf("expected error for out-of-range msg code, got nil")
 | 
			
		||||
	} else if perr, ok := err.(*peerError); !ok || perr.Code != errInvalidMsgCode {
 | 
			
		||||
		t.Errorf("wrong error for out-of-range msg code, got %#v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// setup for reading the message on the other end
 | 
			
		||||
	read := make(chan struct{})
 | 
			
		||||
	go func() {
 | 
			
		||||
		if err := ExpectMsg(rw, 16, nil); err != nil {
 | 
			
		||||
			t.Error(err)
 | 
			
		||||
		}
 | 
			
		||||
		close(read)
 | 
			
		||||
	}()
 | 
			
		||||
 | 
			
		||||
	// test successful write
 | 
			
		||||
	if err := peer.writeProtoMsg("discard", emptymsg(0)); err != nil {
 | 
			
		||||
		t.Errorf("expect no error for known protocol: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
	select {
 | 
			
		||||
	case <-read:
 | 
			
		||||
	case err := <-peerErr:
 | 
			
		||||
		t.Fatalf("peer stopped: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestPeerPing(t *testing.T) {
 | 
			
		||||
	closer, rw, _, _ := testPeer(nil)
 | 
			
		||||
	defer closer()
 | 
			
		||||
 
 | 
			
		||||
@@ -1,7 +1,6 @@
 | 
			
		||||
package p2p
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"bytes"
 | 
			
		||||
	"crypto/ecdsa"
 | 
			
		||||
	"crypto/rand"
 | 
			
		||||
	"errors"
 | 
			
		||||
@@ -14,7 +13,6 @@ import (
 | 
			
		||||
	"github.com/ethereum/go-ethereum/logger/glog"
 | 
			
		||||
	"github.com/ethereum/go-ethereum/p2p/discover"
 | 
			
		||||
	"github.com/ethereum/go-ethereum/p2p/nat"
 | 
			
		||||
	"github.com/ethereum/go-ethereum/rlp"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
@@ -159,45 +157,6 @@ func (srv *Server) AddPeer(node *discover.Node) {
 | 
			
		||||
	srv.staticNodes[node.ID] = node
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Broadcast sends an RLP-encoded message to all connected peers.
 | 
			
		||||
// This method is deprecated and will be removed later.
 | 
			
		||||
func (srv *Server) Broadcast(protocol string, code uint64, data interface{}) error {
 | 
			
		||||
	return srv.BroadcastLimited(protocol, code, func(i float64) float64 { return i }, data)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// BroadcastsRange an RLP-encoded message to a random set of peers using the limit function to limit the amount
 | 
			
		||||
// of peers.
 | 
			
		||||
func (srv *Server) BroadcastLimited(protocol string, code uint64, limit func(float64) float64, data interface{}) error {
 | 
			
		||||
	var payload []byte
 | 
			
		||||
	if data != nil {
 | 
			
		||||
		var err error
 | 
			
		||||
		payload, err = rlp.EncodeToBytes(data)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	srv.lock.RLock()
 | 
			
		||||
	defer srv.lock.RUnlock()
 | 
			
		||||
 | 
			
		||||
	i, max := 0, int(limit(float64(len(srv.peers))))
 | 
			
		||||
	for _, peer := range srv.peers {
 | 
			
		||||
		if i >= max {
 | 
			
		||||
			break
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if peer != nil {
 | 
			
		||||
			var msg = Msg{Code: code}
 | 
			
		||||
			if data != nil {
 | 
			
		||||
				msg.Payload = bytes.NewReader(payload)
 | 
			
		||||
				msg.Size = uint32(len(payload))
 | 
			
		||||
			}
 | 
			
		||||
			peer.writeProtoMsg(protocol, msg)
 | 
			
		||||
			i++
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Start starts running the server.
 | 
			
		||||
// Servers can be re-used and started again after stopping.
 | 
			
		||||
func (srv *Server) Start() (err error) {
 | 
			
		||||
 
 | 
			
		||||
@@ -1,12 +1,9 @@
 | 
			
		||||
package p2p
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"bytes"
 | 
			
		||||
	"crypto/ecdsa"
 | 
			
		||||
	"io"
 | 
			
		||||
	"math/rand"
 | 
			
		||||
	"net"
 | 
			
		||||
	"sync"
 | 
			
		||||
	"testing"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
@@ -121,45 +118,6 @@ func TestServerDial(t *testing.T) {
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestServerBroadcast(t *testing.T) {
 | 
			
		||||
	var connected sync.WaitGroup
 | 
			
		||||
	srv := startTestServer(t, func(p *Peer) {
 | 
			
		||||
		p.running = matchProtocols([]Protocol{discard}, []Cap{discard.cap()}, p.rw)
 | 
			
		||||
		connected.Done()
 | 
			
		||||
	})
 | 
			
		||||
	defer srv.Stop()
 | 
			
		||||
 | 
			
		||||
	// create a few peers
 | 
			
		||||
	var conns = make([]net.Conn, 8)
 | 
			
		||||
	connected.Add(len(conns))
 | 
			
		||||
	deadline := time.Now().Add(3 * time.Second)
 | 
			
		||||
	dialer := &net.Dialer{Deadline: deadline}
 | 
			
		||||
	for i := range conns {
 | 
			
		||||
		conn, err := dialer.Dial("tcp", srv.ListenAddr)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			t.Fatalf("conn %d: dial error: %v", i, err)
 | 
			
		||||
		}
 | 
			
		||||
		defer conn.Close()
 | 
			
		||||
		conn.SetDeadline(deadline)
 | 
			
		||||
		conns[i] = conn
 | 
			
		||||
	}
 | 
			
		||||
	connected.Wait()
 | 
			
		||||
 | 
			
		||||
	// broadcast one message
 | 
			
		||||
	srv.Broadcast("discard", 0, []string{"foo"})
 | 
			
		||||
	golden := unhex("66e94d166f0a2c3b884cfa59ca34")
 | 
			
		||||
 | 
			
		||||
	// check that the message has been written everywhere
 | 
			
		||||
	for i, conn := range conns {
 | 
			
		||||
		buf := make([]byte, len(golden))
 | 
			
		||||
		if _, err := io.ReadFull(conn, buf); err != nil {
 | 
			
		||||
			t.Errorf("conn %d: read error: %v", i, err)
 | 
			
		||||
		} else if !bytes.Equal(buf, golden) {
 | 
			
		||||
			t.Errorf("conn %d: msg mismatch\ngot:  %x\nwant: %x", i, buf, golden)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// This test checks that connections are disconnected
 | 
			
		||||
// just after the encryption handshake when the server is
 | 
			
		||||
// at capacity.
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user