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
413ace37
Commit
413ace37
authored
Apr 30, 2015
by
Péter Szilágyi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
eth, p2p: rename trusted nodes to static, drop inbound extra slots
parent
701591b4
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
59 additions
and
57 deletions
+59
-57
backend.go
eth/backend.go
+11
-11
handshake.go
p2p/handshake.go
+7
-7
handshake_test.go
p2p/handshake_test.go
+2
-2
server.go
p2p/server.go
+32
-32
server_test.go
p2p/server_test.go
+7
-5
No files found.
eth/backend.go
View file @
413ace37
...
...
@@ -41,8 +41,8 @@ var (
discover
.
MustParseNode
(
"enode://487611428e6c99a11a9795a6abe7b529e81315ca6aad66e2a2fc76e3adf263faba0d35466c2f8f68d561dbefa8878d4df5f1f2ddb1fbeab7f42ffb8cd328bd4a@5.1.83.226:30303"
),
}
// Path within <datadir> to search for the
trusted
node list
trustedNodes
=
"trusted
-nodes.json"
// Path within <datadir> to search for the
static
node list
staticNodes
=
"static
-nodes.json"
)
type
Config
struct
{
...
...
@@ -102,23 +102,23 @@ func (cfg *Config) parseBootNodes() []*discover.Node {
return
ns
}
// parse
Trusted
Nodes parses a list of discovery node URLs either given literally,
// parse
Static
Nodes parses a list of discovery node URLs either given literally,
// or loaded from a .json file.
func
(
cfg
*
Config
)
parse
Trusted
Nodes
()
[]
*
discover
.
Node
{
// Short circuit if no
trusted
node config is present
path
:=
filepath
.
Join
(
cfg
.
DataDir
,
trusted
Nodes
)
func
(
cfg
*
Config
)
parse
Static
Nodes
()
[]
*
discover
.
Node
{
// Short circuit if no
static
node config is present
path
:=
filepath
.
Join
(
cfg
.
DataDir
,
static
Nodes
)
if
_
,
err
:=
os
.
Stat
(
path
);
err
!=
nil
{
return
nil
}
// Load the
trusted
nodes from the config file
// Load the
static
nodes from the config file
blob
,
err
:=
ioutil
.
ReadFile
(
path
)
if
err
!=
nil
{
glog
.
V
(
logger
.
Error
)
.
Infof
(
"Failed to access
trusted
nodes: %v"
,
err
)
glog
.
V
(
logger
.
Error
)
.
Infof
(
"Failed to access
static
nodes: %v"
,
err
)
return
nil
}
nodelist
:=
[]
string
{}
if
err
:=
json
.
Unmarshal
(
blob
,
&
nodelist
);
err
!=
nil
{
glog
.
V
(
logger
.
Error
)
.
Infof
(
"Failed to load
trusted
nodes: %v"
,
err
)
glog
.
V
(
logger
.
Error
)
.
Infof
(
"Failed to load
static
nodes: %v"
,
err
)
return
nil
}
// Interpret the list as a discovery node array
...
...
@@ -129,7 +129,7 @@ func (cfg *Config) parseTrustedNodes() []*discover.Node {
}
node
,
err
:=
discover
.
ParseNode
(
url
)
if
err
!=
nil
{
glog
.
V
(
logger
.
Error
)
.
Infof
(
"
Trusted
node URL %s: %v
\n
"
,
url
,
err
)
glog
.
V
(
logger
.
Error
)
.
Infof
(
"
Static
node URL %s: %v
\n
"
,
url
,
err
)
continue
}
nodes
=
append
(
nodes
,
node
)
...
...
@@ -288,7 +288,7 @@ func New(config *Config) (*Ethereum, error) {
NAT
:
config
.
NAT
,
NoDial
:
!
config
.
Dial
,
BootstrapNodes
:
config
.
parseBootNodes
(),
TrustedNodes
:
config
.
parseTrusted
Nodes
(),
StaticNodes
:
config
.
parseStatic
Nodes
(),
NodeDatabase
:
nodeDb
,
}
if
len
(
config
.
Port
)
>
0
{
...
...
p2p/handshake.go
View file @
413ace37
...
...
@@ -70,21 +70,21 @@ type protoHandshake struct {
// If dial is non-nil, the connection the local node is the initiator.
// If atcap is true, the connection will be disconnected with DiscTooManyPeers
// after the key exchange.
func
setupConn
(
fd
net
.
Conn
,
prv
*
ecdsa
.
PrivateKey
,
our
*
protoHandshake
,
dial
*
discover
.
Node
,
atcap
bool
,
trust
map
[
discover
.
NodeID
]
bool
)
(
*
conn
,
error
)
{
func
setupConn
(
fd
net
.
Conn
,
prv
*
ecdsa
.
PrivateKey
,
our
*
protoHandshake
,
dial
*
discover
.
Node
,
atcap
bool
)
(
*
conn
,
error
)
{
if
dial
==
nil
{
return
setupInboundConn
(
fd
,
prv
,
our
,
atcap
,
trust
)
return
setupInboundConn
(
fd
,
prv
,
our
,
atcap
)
}
else
{
return
setupOutboundConn
(
fd
,
prv
,
our
,
dial
,
atcap
,
trust
)
return
setupOutboundConn
(
fd
,
prv
,
our
,
dial
,
atcap
)
}
}
func
setupInboundConn
(
fd
net
.
Conn
,
prv
*
ecdsa
.
PrivateKey
,
our
*
protoHandshake
,
atcap
bool
,
trust
map
[
discover
.
NodeID
]
bool
)
(
*
conn
,
error
)
{
func
setupInboundConn
(
fd
net
.
Conn
,
prv
*
ecdsa
.
PrivateKey
,
our
*
protoHandshake
,
atcap
bool
)
(
*
conn
,
error
)
{
secrets
,
err
:=
receiverEncHandshake
(
fd
,
prv
,
nil
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"encryption handshake failed: %v"
,
err
)
}
rw
:=
newRlpxFrameRW
(
fd
,
secrets
)
if
atcap
&&
!
trust
[
secrets
.
RemoteID
]
{
if
atcap
{
SendItems
(
rw
,
discMsg
,
DiscTooManyPeers
)
return
nil
,
errors
.
New
(
"we have too many peers"
)
}
...
...
@@ -99,13 +99,13 @@ func setupInboundConn(fd net.Conn, prv *ecdsa.PrivateKey, our *protoHandshake, a
return
&
conn
{
rw
,
rhs
},
nil
}
func
setupOutboundConn
(
fd
net
.
Conn
,
prv
*
ecdsa
.
PrivateKey
,
our
*
protoHandshake
,
dial
*
discover
.
Node
,
atcap
bool
,
trust
map
[
discover
.
NodeID
]
bool
)
(
*
conn
,
error
)
{
func
setupOutboundConn
(
fd
net
.
Conn
,
prv
*
ecdsa
.
PrivateKey
,
our
*
protoHandshake
,
dial
*
discover
.
Node
,
atcap
bool
)
(
*
conn
,
error
)
{
secrets
,
err
:=
initiatorEncHandshake
(
fd
,
prv
,
dial
.
ID
,
nil
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"encryption handshake failed: %v"
,
err
)
}
rw
:=
newRlpxFrameRW
(
fd
,
secrets
)
if
atcap
&&
!
trust
[
secrets
.
RemoteID
]
{
if
atcap
{
SendItems
(
rw
,
discMsg
,
DiscTooManyPeers
)
return
nil
,
errors
.
New
(
"we have too many peers"
)
}
...
...
p2p/handshake_test.go
View file @
413ace37
...
...
@@ -143,7 +143,7 @@ func TestSetupConn(t *testing.T) {
done
:=
make
(
chan
struct
{})
go
func
()
{
defer
close
(
done
)
conn0
,
err
:=
setupConn
(
fd0
,
prv0
,
hs0
,
node1
,
false
,
nil
)
conn0
,
err
:=
setupConn
(
fd0
,
prv0
,
hs0
,
node1
,
false
)
if
err
!=
nil
{
t
.
Errorf
(
"outbound side error: %v"
,
err
)
return
...
...
@@ -156,7 +156,7 @@ func TestSetupConn(t *testing.T) {
}
}()
conn1
,
err
:=
setupConn
(
fd1
,
prv1
,
hs1
,
nil
,
false
,
nil
)
conn1
,
err
:=
setupConn
(
fd1
,
prv1
,
hs1
,
nil
,
false
)
if
err
!=
nil
{
t
.
Fatalf
(
"inbound side error: %v"
,
err
)
}
...
...
p2p/server.go
View file @
413ace37
...
...
@@ -60,9 +60,9 @@ type Server struct {
// with the rest of the network.
BootstrapNodes
[]
*
discover
.
Node
//
Trusted nodes are used as privileged connections which are always accepted
//
and also always maintained
.
Trusted
Nodes
[]
*
discover
.
Node
//
Static nodes are used as pre-configured connections which are always
//
maintained and re-connected on disconnects
.
Static
Nodes
[]
*
discover
.
Node
// NodeDatabase is the path to the database containing the previously seen
// live nodes in the network.
...
...
@@ -100,11 +100,11 @@ type Server struct {
ourHandshake
*
protoHandshake
lock
sync
.
RWMutex
// protects running, peers and the trust fields
running
bool
peers
map
[
discover
.
NodeID
]
*
Peer
trusts
map
[
discover
.
NodeID
]
*
discover
.
Node
// Map of currently trusted
remote nodes
trustDial
chan
*
discover
.
Node
// Dial request channel reserved for the trusted
nodes
lock
sync
.
RWMutex
// protects running, peers and the trust fields
running
bool
peers
map
[
discover
.
NodeID
]
*
Peer
statics
map
[
discover
.
NodeID
]
*
discover
.
Node
// Map of currently static
remote nodes
staticDial
chan
*
discover
.
Node
// Dial request channel reserved for the static
nodes
ntab
*
discover
.
Table
listener
net
.
Listener
...
...
@@ -114,7 +114,7 @@ type Server struct {
peerWG
sync
.
WaitGroup
// active peer goroutines
}
type
setupFunc
func
(
net
.
Conn
,
*
ecdsa
.
PrivateKey
,
*
protoHandshake
,
*
discover
.
Node
,
bool
,
map
[
discover
.
NodeID
]
bool
)
(
*
conn
,
error
)
type
setupFunc
func
(
net
.
Conn
,
*
ecdsa
.
PrivateKey
,
*
protoHandshake
,
*
discover
.
Node
,
bool
)
(
*
conn
,
error
)
type
newPeerHook
func
(
*
Peer
)
// Peers returns all connected peers.
...
...
@@ -144,7 +144,7 @@ func (srv *Server) AddPeer(node *discover.Node) {
srv
.
lock
.
Lock
()
defer
srv
.
lock
.
Unlock
()
srv
.
trust
s
[
node
.
ID
]
=
node
srv
.
static
s
[
node
.
ID
]
=
node
}
// Broadcast sends an RLP-encoded message to all connected peers.
...
...
@@ -207,11 +207,11 @@ func (srv *Server) Start() (err error) {
srv
.
peers
=
make
(
map
[
discover
.
NodeID
]
*
Peer
)
// Create the current trust map, and the associated dialing channel
srv
.
trust
s
=
make
(
map
[
discover
.
NodeID
]
*
discover
.
Node
)
for
_
,
node
:=
range
srv
.
Trusted
Nodes
{
srv
.
trust
s
[
node
.
ID
]
=
node
srv
.
static
s
=
make
(
map
[
discover
.
NodeID
]
*
discover
.
Node
)
for
_
,
node
:=
range
srv
.
Static
Nodes
{
srv
.
static
s
[
node
.
ID
]
=
node
}
srv
.
trust
Dial
=
make
(
chan
*
discover
.
Node
)
srv
.
static
Dial
=
make
(
chan
*
discover
.
Node
)
if
srv
.
setupFunc
==
nil
{
srv
.
setupFunc
=
setupConn
...
...
@@ -246,8 +246,8 @@ func (srv *Server) Start() (err error) {
if
srv
.
NoDial
&&
srv
.
ListenAddr
==
""
{
glog
.
V
(
logger
.
Warn
)
.
Infoln
(
"I will be kind-of useless, neither dialing nor listening."
)
}
// maintain the
trusted
peers
go
srv
.
trusted
NodesLoop
()
// maintain the
static
peers
go
srv
.
static
NodesLoop
()
srv
.
running
=
true
return
nil
...
...
@@ -342,9 +342,9 @@ func (srv *Server) listenLoop() {
}
}
//
trustedNodesLoop is responsible for periodically checking that trusted
//
staticNodesLoop is responsible for periodically checking that static
// connections are actually live, and requests dialing if not.
func
(
srv
*
Server
)
trusted
NodesLoop
()
{
func
(
srv
*
Server
)
static
NodesLoop
()
{
tick
:=
time
.
Tick
(
trustedPeerCheckInterval
)
for
{
select
{
...
...
@@ -352,10 +352,10 @@ func (srv *Server) trustedNodesLoop() {
return
case
<-
tick
:
// Collect all the non-connected
trusted
nodes
// Collect all the non-connected
static
nodes
needed
:=
[]
*
discover
.
Node
{}
srv
.
lock
.
RLock
()
for
id
,
node
:=
range
srv
.
trust
s
{
for
id
,
node
:=
range
srv
.
static
s
{
if
_
,
ok
:=
srv
.
peers
[
id
];
!
ok
{
needed
=
append
(
needed
,
node
)
}
...
...
@@ -364,9 +364,9 @@ func (srv *Server) trustedNodesLoop() {
// Try to dial each of them (don't hang if server terminates)
for
_
,
node
:=
range
needed
{
glog
.
V
(
logger
.
Debug
)
.
Infof
(
"Dialing
trusted
peer %v"
,
node
)
glog
.
V
(
logger
.
Debug
)
.
Infof
(
"Dialing
static
peer %v"
,
node
)
select
{
case
srv
.
trust
Dial
<-
node
:
case
srv
.
static
Dial
<-
node
:
case
<-
srv
.
quit
:
return
}
...
...
@@ -425,7 +425,7 @@ func (srv *Server) dialLoop() {
// below MaxPeers.
refresh
.
Reset
(
refreshPeersInterval
)
}
case
dest
:=
<-
srv
.
trust
Dial
:
case
dest
:=
<-
srv
.
static
Dial
:
dial
(
dest
)
case
dests
:=
<-
findresults
:
for
_
,
dest
:=
range
dests
{
...
...
@@ -469,17 +469,17 @@ func (srv *Server) startPeer(fd net.Conn, dest *discover.Node) {
// the callers of startPeer added the peer to the wait group already.
fd
.
SetDeadline
(
time
.
Now
()
.
Add
(
handshakeTimeout
))
// Check capacity
and trust list
// Check capacity
, but override for static nodes
srv
.
lock
.
RLock
()
atcap
:=
len
(
srv
.
peers
)
==
srv
.
MaxPeers
trust
:=
make
(
map
[
discover
.
NodeID
]
bool
)
for
id
,
_
:=
range
srv
.
trusts
{
trust
[
id
]
=
true
if
dest
!=
nil
{
if
_
,
ok
:=
srv
.
statics
[
dest
.
ID
];
ok
{
atcap
=
false
}
}
srv
.
lock
.
RUnlock
()
conn
,
err
:=
srv
.
setupFunc
(
fd
,
srv
.
PrivateKey
,
srv
.
ourHandshake
,
dest
,
atcap
,
trust
)
conn
,
err
:=
srv
.
setupFunc
(
fd
,
srv
.
PrivateKey
,
srv
.
ourHandshake
,
dest
,
atcap
)
if
err
!=
nil
{
fd
.
Close
()
glog
.
V
(
logger
.
Debug
)
.
Infof
(
"Handshake with %v failed: %v"
,
fd
.
RemoteAddr
(),
err
)
...
...
@@ -535,14 +535,14 @@ func (srv *Server) addPeer(id discover.NodeID, p *Peer) (bool, DiscReason) {
// checkPeer verifies whether a peer looks promising and should be allowed/kept
// in the pool, or if it's of no use.
func
(
srv
*
Server
)
checkPeer
(
id
discover
.
NodeID
)
(
bool
,
DiscReason
)
{
// First up, figure out if the peer is
trusted
_
,
trusted
:=
srv
.
trust
s
[
id
]
// First up, figure out if the peer is
static
_
,
static
:=
srv
.
static
s
[
id
]
// Make sure the peer passes all required checks
switch
{
case
!
srv
.
running
:
return
false
,
DiscQuitting
case
!
trusted
&&
len
(
srv
.
peers
)
>=
srv
.
MaxPeers
:
case
!
static
&&
len
(
srv
.
peers
)
>=
srv
.
MaxPeers
:
return
false
,
DiscTooManyPeers
case
srv
.
peers
[
id
]
!=
nil
:
return
false
,
DiscAlreadyConnected
...
...
p2p/server_test.go
View file @
413ace37
...
...
@@ -22,7 +22,7 @@ func startTestServer(t *testing.T, pf newPeerHook) *Server {
ListenAddr
:
"127.0.0.1:0"
,
PrivateKey
:
newkey
(),
newPeerHook
:
pf
,
setupFunc
:
func
(
fd
net
.
Conn
,
prv
*
ecdsa
.
PrivateKey
,
our
*
protoHandshake
,
dial
*
discover
.
Node
,
atcap
bool
,
trust
map
[
discover
.
NodeID
]
bool
)
(
*
conn
,
error
)
{
setupFunc
:
func
(
fd
net
.
Conn
,
prv
*
ecdsa
.
PrivateKey
,
our
*
protoHandshake
,
dial
*
discover
.
Node
,
atcap
bool
)
(
*
conn
,
error
)
{
id
:=
randomID
()
rw
:=
newRlpxFrameRW
(
fd
,
secrets
{
MAC
:
zero16
,
...
...
@@ -102,7 +102,7 @@ func TestServerDial(t *testing.T) {
// tell the server to connect
tcpAddr
:=
listener
.
Addr
()
.
(
*
net
.
TCPAddr
)
srv
.
trust
Dial
<-
&
discover
.
Node
{
IP
:
tcpAddr
.
IP
,
TCPPort
:
tcpAddr
.
Port
}
srv
.
static
Dial
<-
&
discover
.
Node
{
IP
:
tcpAddr
.
IP
,
TCPPort
:
tcpAddr
.
Port
}
select
{
case
conn
:=
<-
accepted
:
...
...
@@ -200,7 +200,7 @@ func TestServerDisconnectAtCap(t *testing.T) {
// Run the handshakes just like a real peer would.
key
:=
newkey
()
hs
:=
&
protoHandshake
{
Version
:
baseProtocolVersion
,
ID
:
discover
.
PubkeyID
(
&
key
.
PublicKey
)}
_
,
err
=
setupConn
(
conn
,
key
,
hs
,
srv
.
Self
(),
false
,
nil
)
_
,
err
=
setupConn
(
conn
,
key
,
hs
,
srv
.
Self
(),
false
)
if
i
==
nconns
-
1
{
// When handling the last connection, the server should
// disconnect immediately instead of running the protocol
...
...
@@ -219,6 +219,7 @@ func TestServerDisconnectAtCap(t *testing.T) {
}
}
/*
// Tests that trusted peers and can connect above max peer caps.
func TestServerTrustedPeers(t *testing.T) {
defer testlog(t).detach()
...
...
@@ -250,7 +251,7 @@ func TestServerTrustedPeers(t *testing.T) {
// Run the handshakes just like a real peer would, and wait for completion
key := newkey()
shake := &protoHandshake{Version: baseProtocolVersion, ID: discover.PubkeyID(&key.PublicKey)}
if
_
,
err
=
setupConn
(
conn
,
key
,
shake
,
server
.
Self
(),
false
,
nil
);
err
!=
nil
{
if _, err = setupConn(conn, key, shake, server.Self(), false); err != nil {
t.Fatalf("conn %d: unexpected error: %v", i, err)
}
<-started
...
...
@@ -269,7 +270,7 @@ func TestServerTrustedPeers(t *testing.T) {
defer conn.Close()
shake := &protoHandshake{Version: baseProtocolVersion, ID: trusted.ID}
if
_
,
err
=
setupConn
(
conn
,
key
,
shake
,
server
.
Self
(),
false
,
nil
);
err
!=
nil
{
if _, err = setupConn(conn, key, shake, server.Self(), false); err != nil {
t.Fatalf("trusted node: unexpected error: %v", err)
}
select {
...
...
@@ -280,6 +281,7 @@ func TestServerTrustedPeers(t *testing.T) {
t.Fatalf("trusted node timeout")
}
}
*/
func
newkey
()
*
ecdsa
.
PrivateKey
{
key
,
err
:=
crypto
.
GenerateKey
()
...
...
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