Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
Geth-Modification
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
张蕾
Geth-Modification
Commits
dbdc5fd4
Commit
dbdc5fd4
authored
May 14, 2015
by
Felix Lange
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
p2p: delete Server.Broadcast
parent
2f249fea
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
0 additions
and
136 deletions
+0
-136
peer.go
p2p/peer.go
+0
-14
peer_test.go
p2p/peer_test.go
+0
-39
server.go
p2p/server.go
+0
-41
server_test.go
p2p/server_test.go
+0
-42
No files found.
p2p/peer.go
View file @
dbdc5fd4
...
...
@@ -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
...
...
p2p/peer_test.go
View file @
dbdc5fd4
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
()
...
...
p2p/server.go
View file @
dbdc5fd4
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
)
{
...
...
p2p/server_test.go
View file @
dbdc5fd4
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
\n
got: %x
\n
want: %x"
,
i
,
buf
,
golden
)
}
}
}
// This test checks that connections are disconnected
// just after the encryption handshake when the server is
// at capacity.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment