Skip to content

Commit dbdc5fd

Browse files
committed
p2p: delete Server.Broadcast
1 parent 2f249fe commit dbdc5fd

4 files changed

Lines changed: 0 additions & 136 deletions

File tree

‎p2p/peer.go‎

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -273,20 +273,6 @@ func (p *Peer) getProto(code uint64) (*protoRW, error) {
273273
return nil, newPeerError(errInvalidMsgCode, "%d", code)
274274
}
275275

276-
// writeProtoMsg sends the given message on behalf of the given named protocol.
277-
// this exists because of Server.Broadcast.
278-
func (p *Peer) writeProtoMsg(protoName string, msg Msg) error {
279-
proto, ok := p.running[protoName]
280-
if !ok {
281-
return fmt.Errorf("protocol %s not handled by peer", protoName)
282-
}
283-
if msg.Code >= proto.Length {
284-
return newPeerError(errInvalidMsgCode, "code %x is out of range for protocol %q", msg.Code, protoName)
285-
}
286-
msg.Code += proto.offset
287-
return p.rw.WriteMsg(msg)
288-
}
289-
290276
type protoRW struct {
291277
Protocol
292278
in chan Msg

‎p2p/peer_test.go‎

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package p2p
22

33
import (
4-
"bytes"
54
"errors"
65
"fmt"
76
"math/rand"
@@ -107,44 +106,6 @@ func TestPeerProtoEncodeMsg(t *testing.T) {
107106
}
108107
}
109108

110-
func TestPeerWriteForBroadcast(t *testing.T) {
111-
closer, rw, peer, peerErr := testPeer([]Protocol{discard})
112-
defer closer()
113-
114-
emptymsg := func(code uint64) Msg {
115-
return Msg{Code: code, Size: 0, Payload: bytes.NewReader(nil)}
116-
}
117-
118-
// test write errors
119-
if err := peer.writeProtoMsg("b", emptymsg(3)); err == nil {
120-
t.Errorf("expected error for unknown protocol, got nil")
121-
}
122-
if err := peer.writeProtoMsg("discard", emptymsg(8)); err == nil {
123-
t.Errorf("expected error for out-of-range msg code, got nil")
124-
} else if perr, ok := err.(*peerError); !ok || perr.Code != errInvalidMsgCode {
125-
t.Errorf("wrong error for out-of-range msg code, got %#v", err)
126-
}
127-
128-
// setup for reading the message on the other end
129-
read := make(chan struct{})
130-
go func() {
131-
if err := ExpectMsg(rw, 16, nil); err != nil {
132-
t.Error(err)
133-
}
134-
close(read)
135-
}()
136-
137-
// test successful write
138-
if err := peer.writeProtoMsg("discard", emptymsg(0)); err != nil {
139-
t.Errorf("expect no error for known protocol: %v", err)
140-
}
141-
select {
142-
case <-read:
143-
case err := <-peerErr:
144-
t.Fatalf("peer stopped: %v", err)
145-
}
146-
}
147-
148109
func TestPeerPing(t *testing.T) {
149110
closer, rw, _, _ := testPeer(nil)
150111
defer closer()

‎p2p/server.go‎

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package p2p
22

33
import (
4-
"bytes"
54
"crypto/ecdsa"
65
"crypto/rand"
76
"errors"
@@ -14,7 +13,6 @@ import (
1413
"github.com/ethereum/go-ethereum/logger/glog"
1514
"github.com/ethereum/go-ethereum/p2p/discover"
1615
"github.com/ethereum/go-ethereum/p2p/nat"
17-
"github.com/ethereum/go-ethereum/rlp"
1816
)
1917

2018
const (
@@ -159,45 +157,6 @@ func (srv *Server) AddPeer(node *discover.Node) {
159157
srv.staticNodes[node.ID] = node
160158
}
161159

162-
// Broadcast sends an RLP-encoded message to all connected peers.
163-
// This method is deprecated and will be removed later.
164-
func (srv *Server) Broadcast(protocol string, code uint64, data interface{}) error {
165-
return srv.BroadcastLimited(protocol, code, func(i float64) float64 { return i }, data)
166-
}
167-
168-
// BroadcastsRange an RLP-encoded message to a random set of peers using the limit function to limit the amount
169-
// of peers.
170-
func (srv *Server) BroadcastLimited(protocol string, code uint64, limit func(float64) float64, data interface{}) error {
171-
var payload []byte
172-
if data != nil {
173-
var err error
174-
payload, err = rlp.EncodeToBytes(data)
175-
if err != nil {
176-
return err
177-
}
178-
}
179-
srv.lock.RLock()
180-
defer srv.lock.RUnlock()
181-
182-
i, max := 0, int(limit(float64(len(srv.peers))))
183-
for _, peer := range srv.peers {
184-
if i >= max {
185-
break
186-
}
187-
188-
if peer != nil {
189-
var msg = Msg{Code: code}
190-
if data != nil {
191-
msg.Payload = bytes.NewReader(payload)
192-
msg.Size = uint32(len(payload))
193-
}
194-
peer.writeProtoMsg(protocol, msg)
195-
i++
196-
}
197-
}
198-
return nil
199-
}
200-
201160
// Start starts running the server.
202161
// Servers can be re-used and started again after stopping.
203162
func (srv *Server) Start() (err error) {

‎p2p/server_test.go‎

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
package p2p
22

33
import (
4-
"bytes"
54
"crypto/ecdsa"
6-
"io"
75
"math/rand"
86
"net"
9-
"sync"
107
"testing"
118
"time"
129

@@ -121,45 +118,6 @@ func TestServerDial(t *testing.T) {
121118
}
122119
}
123120

124-
func TestServerBroadcast(t *testing.T) {
125-
var connected sync.WaitGroup
126-
srv := startTestServer(t, func(p *Peer) {
127-
p.running = matchProtocols([]Protocol{discard}, []Cap{discard.cap()}, p.rw)
128-
connected.Done()
129-
})
130-
defer srv.Stop()
131-
132-
// create a few peers
133-
var conns = make([]net.Conn, 8)
134-
connected.Add(len(conns))
135-
deadline := time.Now().Add(3 * time.Second)
136-
dialer := &net.Dialer{Deadline: deadline}
137-
for i := range conns {
138-
conn, err := dialer.Dial("tcp", srv.ListenAddr)
139-
if err != nil {
140-
t.Fatalf("conn %d: dial error: %v", i, err)
141-
}
142-
defer conn.Close()
143-
conn.SetDeadline(deadline)
144-
conns[i] = conn
145-
}
146-
connected.Wait()
147-
148-
// broadcast one message
149-
srv.Broadcast("discard", 0, []string{"foo"})
150-
golden := unhex("66e94d166f0a2c3b884cfa59ca34")
151-
152-
// check that the message has been written everywhere
153-
for i, conn := range conns {
154-
buf := make([]byte, len(golden))
155-
if _, err := io.ReadFull(conn, buf); err != nil {
156-
t.Errorf("conn %d: read error: %v", i, err)
157-
} else if !bytes.Equal(buf, golden) {
158-
t.Errorf("conn %d: msg mismatch\ngot: %x\nwant: %x", i, buf, golden)
159-
}
160-
}
161-
}
162-
163121
// This test checks that connections are disconnected
164122
// just after the encryption handshake when the server is
165123
// at capacity.

0 commit comments

Comments
 (0)