2015-07-06 19:54:22 -05:00
|
|
|
// Copyright 2014 The go-ethereum Authors
|
2015-07-22 11:48:40 -05:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-06 19:54:22 -05:00
|
|
|
//
|
2015-07-23 11:35:11 -05:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-06 19:54:22 -05:00
|
|
|
// 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.
|
|
|
|
//
|
2015-07-22 11:48:40 -05:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-06 19:54:22 -05:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 11:48:40 -05:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-06 19:54:22 -05:00
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
2015-07-22 11:48:40 -05:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-06 19:54:22 -05:00
|
|
|
|
2014-10-23 10:57:54 -05:00
|
|
|
package p2p
|
|
|
|
|
|
|
|
import (
|
2018-06-20 07:06:27 -05:00
|
|
|
"errors"
|
2014-10-23 10:57:54 -05:00
|
|
|
"fmt"
|
2014-11-21 14:48:49 -06:00
|
|
|
"io"
|
2014-10-23 10:57:54 -05:00
|
|
|
"net"
|
2014-11-21 14:48:49 -06:00
|
|
|
"sort"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2017-02-24 02:58:04 -06:00
|
|
|
"github.com/ethereum/go-ethereum/common/mclock"
|
2017-09-25 03:08:07 -05:00
|
|
|
"github.com/ethereum/go-ethereum/event"
|
2017-02-22 06:10:07 -06:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2019-09-27 09:41:47 -05:00
|
|
|
"github.com/ethereum/go-ethereum/metrics"
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-24 17:59:00 -05:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/enr"
|
2015-02-04 20:07:58 -06:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2014-10-23 10:57:54 -05:00
|
|
|
)
|
|
|
|
|
2018-06-20 07:06:27 -05:00
|
|
|
var (
|
|
|
|
ErrShuttingDown = errors.New("shutting down")
|
|
|
|
)
|
|
|
|
|
2015-02-04 20:07:58 -06:00
|
|
|
const (
|
2017-09-26 08:54:49 -05:00
|
|
|
baseProtocolVersion = 5
|
2015-02-04 20:07:58 -06:00
|
|
|
baseProtocolLength = uint64(16)
|
2015-05-17 18:14:35 -05:00
|
|
|
baseProtocolMaxMsgSize = 2 * 1024
|
2015-02-13 07:44:00 -06:00
|
|
|
|
2017-09-26 08:54:49 -05:00
|
|
|
snappyProtocolVersion = 5
|
|
|
|
|
2015-04-13 10:34:08 -05:00
|
|
|
pingInterval = 15 * time.Second
|
2015-02-04 20:07:58 -06:00
|
|
|
)
|
2014-11-21 14:48:49 -06:00
|
|
|
|
2015-02-04 20:07:58 -06:00
|
|
|
const (
|
|
|
|
// devp2p message codes
|
|
|
|
handshakeMsg = 0x00
|
|
|
|
discMsg = 0x01
|
|
|
|
pingMsg = 0x02
|
|
|
|
pongMsg = 0x03
|
|
|
|
)
|
2014-11-21 14:48:49 -06:00
|
|
|
|
2015-05-15 17:38:28 -05:00
|
|
|
// protoHandshake is the RLP structure of the protocol handshake.
|
|
|
|
type protoHandshake struct {
|
|
|
|
Version uint64
|
|
|
|
Name string
|
|
|
|
Caps []Cap
|
|
|
|
ListenPort uint64
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-24 17:59:00 -05:00
|
|
|
ID []byte // secp256k1 public key
|
2015-12-22 18:48:55 -06:00
|
|
|
|
|
|
|
// Ignore additional fields (for forward compatibility).
|
|
|
|
Rest []rlp.RawValue `rlp:"tail"`
|
2015-05-15 17:38:28 -05:00
|
|
|
}
|
|
|
|
|
2017-09-25 03:08:07 -05:00
|
|
|
// PeerEventType is the type of peer events emitted by a p2p.Server
|
|
|
|
type PeerEventType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
// PeerEventTypeAdd is the type of event emitted when a peer is added
|
|
|
|
// to a p2p.Server
|
|
|
|
PeerEventTypeAdd PeerEventType = "add"
|
|
|
|
|
|
|
|
// PeerEventTypeDrop is the type of event emitted when a peer is
|
|
|
|
// dropped from a p2p.Server
|
|
|
|
PeerEventTypeDrop PeerEventType = "drop"
|
|
|
|
|
|
|
|
// PeerEventTypeMsgSend is the type of event emitted when a
|
|
|
|
// message is successfully sent to a peer
|
|
|
|
PeerEventTypeMsgSend PeerEventType = "msgsend"
|
|
|
|
|
|
|
|
// PeerEventTypeMsgRecv is the type of event emitted when a
|
|
|
|
// message is received from a peer
|
|
|
|
PeerEventTypeMsgRecv PeerEventType = "msgrecv"
|
|
|
|
)
|
|
|
|
|
|
|
|
// PeerEvent is an event emitted when peers are either added or dropped from
|
|
|
|
// a p2p.Server or when a message is sent or received on a peer connection
|
|
|
|
type PeerEvent struct {
|
2019-07-05 13:27:13 -05:00
|
|
|
Type PeerEventType `json:"type"`
|
|
|
|
Peer enode.ID `json:"peer"`
|
|
|
|
Error string `json:"error,omitempty"`
|
|
|
|
Protocol string `json:"protocol,omitempty"`
|
|
|
|
MsgCode *uint64 `json:"msg_code,omitempty"`
|
|
|
|
MsgSize *uint32 `json:"msg_size,omitempty"`
|
|
|
|
LocalAddress string `json:"local,omitempty"`
|
|
|
|
RemoteAddress string `json:"remote,omitempty"`
|
2017-09-25 03:08:07 -05:00
|
|
|
}
|
|
|
|
|
2015-02-04 20:07:58 -06:00
|
|
|
// Peer represents a connected remote node.
|
2014-10-23 10:57:54 -05:00
|
|
|
type Peer struct {
|
2015-02-18 18:52:03 -06:00
|
|
|
rw *conn
|
|
|
|
running map[string]*protoRW
|
2017-02-24 02:58:04 -06:00
|
|
|
log log.Logger
|
|
|
|
created mclock.AbsTime
|
2014-11-21 14:48:49 -06:00
|
|
|
|
2015-04-08 10:37:11 -05:00
|
|
|
wg sync.WaitGroup
|
2014-11-21 14:48:49 -06:00
|
|
|
protoErr chan error
|
|
|
|
closed chan struct{}
|
|
|
|
disc chan DiscReason
|
2017-09-25 03:08:07 -05:00
|
|
|
|
|
|
|
// events receives message send / receive events if set
|
|
|
|
events *event.Feed
|
2014-11-21 14:48:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewPeer returns a peer for testing purposes.
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-24 17:59:00 -05:00
|
|
|
func NewPeer(id enode.ID, name string, caps []Cap) *Peer {
|
2015-02-18 18:52:03 -06:00
|
|
|
pipe, _ := net.Pipe()
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-24 17:59:00 -05:00
|
|
|
node := enode.SignNull(new(enr.Record), id)
|
|
|
|
conn := &conn{fd: pipe, transport: nil, node: node, caps: caps, name: name}
|
2019-06-11 05:45:33 -05:00
|
|
|
peer := newPeer(log.Root(), conn, nil)
|
2015-02-04 20:07:58 -06:00
|
|
|
close(peer.closed) // ensures Disconnect doesn't block
|
2014-10-23 10:57:54 -05:00
|
|
|
return peer
|
|
|
|
}
|
|
|
|
|
2015-02-04 20:07:58 -06:00
|
|
|
// ID returns the node's public key.
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-24 17:59:00 -05:00
|
|
|
func (p *Peer) ID() enode.ID {
|
|
|
|
return p.rw.node.ID()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Node returns the peer's node descriptor.
|
|
|
|
func (p *Peer) Node() *enode.Node {
|
|
|
|
return p.rw.node
|
2014-11-21 14:48:49 -06:00
|
|
|
}
|
|
|
|
|
2015-02-04 20:07:58 -06:00
|
|
|
// Name returns the node name that the remote node advertised.
|
|
|
|
func (p *Peer) Name() string {
|
2015-05-15 17:38:28 -05:00
|
|
|
return p.rw.name
|
2015-01-18 01:59:54 -06:00
|
|
|
}
|
|
|
|
|
2014-11-21 14:48:49 -06:00
|
|
|
// Caps returns the capabilities (supported subprotocols) of the remote peer.
|
|
|
|
func (p *Peer) Caps() []Cap {
|
2015-02-18 18:52:03 -06:00
|
|
|
// TODO: maybe return copy
|
2015-05-15 17:38:28 -05:00
|
|
|
return p.rw.caps
|
2014-11-21 14:48:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoteAddr returns the remote address of the network connection.
|
|
|
|
func (p *Peer) RemoteAddr() net.Addr {
|
2015-05-15 17:38:28 -05:00
|
|
|
return p.rw.fd.RemoteAddr()
|
2014-11-21 14:48:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// LocalAddr returns the local address of the network connection.
|
|
|
|
func (p *Peer) LocalAddr() net.Addr {
|
2015-05-15 17:38:28 -05:00
|
|
|
return p.rw.fd.LocalAddr()
|
2014-11-21 14:48:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Disconnect terminates the peer connection with the given reason.
|
|
|
|
// It returns immediately and does not wait until the connection is closed.
|
|
|
|
func (p *Peer) Disconnect(reason DiscReason) {
|
|
|
|
select {
|
|
|
|
case p.disc <- reason:
|
|
|
|
case <-p.closed:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// String implements fmt.Stringer.
|
|
|
|
func (p *Peer) String() string {
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-24 17:59:00 -05:00
|
|
|
id := p.ID()
|
|
|
|
return fmt.Sprintf("Peer %x %v", id[:8], p.RemoteAddr())
|
2015-02-04 20:07:58 -06:00
|
|
|
}
|
|
|
|
|
2017-12-01 05:49:04 -06:00
|
|
|
// Inbound returns true if the peer is an inbound connection
|
|
|
|
func (p *Peer) Inbound() bool {
|
2018-06-07 20:50:08 -05:00
|
|
|
return p.rw.is(inboundConn)
|
2017-12-01 05:49:04 -06:00
|
|
|
}
|
|
|
|
|
2019-06-11 05:45:33 -05:00
|
|
|
func newPeer(log log.Logger, conn *conn, protocols []Protocol) *Peer {
|
2015-05-15 17:38:28 -05:00
|
|
|
protomap := matchProtocols(protocols, conn.caps, conn)
|
2015-02-18 18:52:03 -06:00
|
|
|
p := &Peer{
|
|
|
|
rw: conn,
|
2015-04-08 10:37:11 -05:00
|
|
|
running: protomap,
|
2017-02-24 02:58:04 -06:00
|
|
|
created: mclock.Now(),
|
2015-02-18 18:52:03 -06:00
|
|
|
disc: make(chan DiscReason),
|
2015-04-08 10:37:11 -05:00
|
|
|
protoErr: make(chan error, len(protomap)+1), // protocols + pingLoop
|
2015-02-18 18:52:03 -06:00
|
|
|
closed: make(chan struct{}),
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-24 17:59:00 -05:00
|
|
|
log: log.New("id", conn.node.ID(), "conn", conn.flags),
|
2014-10-23 10:57:54 -05:00
|
|
|
}
|
2015-02-18 18:52:03 -06:00
|
|
|
return p
|
2015-02-04 20:07:58 -06:00
|
|
|
}
|
2014-11-21 14:48:49 -06:00
|
|
|
|
2017-02-24 02:58:04 -06:00
|
|
|
func (p *Peer) Log() log.Logger {
|
|
|
|
return p.log
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Peer) run() (remoteRequested bool, err error) {
|
2015-06-15 06:42:44 -05:00
|
|
|
var (
|
|
|
|
writeStart = make(chan struct{}, 1)
|
|
|
|
writeErr = make(chan error, 1)
|
|
|
|
readErr = make(chan error, 1)
|
2017-02-24 02:58:04 -06:00
|
|
|
reason DiscReason // sent to the peer
|
2015-06-15 06:42:44 -05:00
|
|
|
)
|
2015-04-08 10:37:11 -05:00
|
|
|
p.wg.Add(2)
|
|
|
|
go p.readLoop(readErr)
|
|
|
|
go p.pingLoop()
|
2015-02-04 20:07:58 -06:00
|
|
|
|
2015-06-15 06:42:44 -05:00
|
|
|
// Start all protocol handlers.
|
|
|
|
writeStart <- struct{}{}
|
|
|
|
p.startProtocols(writeStart, writeErr)
|
2015-02-19 09:53:52 -06:00
|
|
|
|
2015-02-06 17:13:22 -06:00
|
|
|
// Wait for an error or disconnect.
|
2015-06-15 06:42:44 -05:00
|
|
|
loop:
|
|
|
|
for {
|
|
|
|
select {
|
2017-02-24 02:58:04 -06:00
|
|
|
case err = <-writeErr:
|
2015-06-15 06:42:44 -05:00
|
|
|
// A write finished. Allow the next write to start if
|
|
|
|
// there was no error.
|
|
|
|
if err != nil {
|
|
|
|
reason = DiscNetworkError
|
|
|
|
break loop
|
|
|
|
}
|
|
|
|
writeStart <- struct{}{}
|
2017-02-24 02:58:04 -06:00
|
|
|
case err = <-readErr:
|
2015-06-15 06:42:44 -05:00
|
|
|
if r, ok := err.(DiscReason); ok {
|
2017-02-24 02:58:04 -06:00
|
|
|
remoteRequested = true
|
2015-06-15 06:42:44 -05:00
|
|
|
reason = r
|
|
|
|
} else {
|
|
|
|
reason = DiscNetworkError
|
|
|
|
}
|
|
|
|
break loop
|
2017-02-24 02:58:04 -06:00
|
|
|
case err = <-p.protoErr:
|
2015-06-15 06:42:44 -05:00
|
|
|
reason = discReasonForError(err)
|
|
|
|
break loop
|
2017-02-24 02:58:04 -06:00
|
|
|
case err = <-p.disc:
|
2018-05-08 18:20:20 -05:00
|
|
|
reason = discReasonForError(err)
|
2015-06-15 06:42:44 -05:00
|
|
|
break loop
|
2015-02-19 09:53:52 -06:00
|
|
|
}
|
2014-11-21 14:48:49 -06:00
|
|
|
}
|
2015-06-15 06:42:44 -05:00
|
|
|
|
2015-04-08 10:37:11 -05:00
|
|
|
close(p.closed)
|
2015-05-15 17:38:28 -05:00
|
|
|
p.rw.close(reason)
|
2015-04-08 10:37:11 -05:00
|
|
|
p.wg.Wait()
|
2017-02-24 02:58:04 -06:00
|
|
|
return remoteRequested, err
|
2014-11-21 14:48:49 -06:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:37:11 -05:00
|
|
|
func (p *Peer) pingLoop() {
|
2017-09-04 02:24:52 -05:00
|
|
|
ping := time.NewTimer(pingInterval)
|
2015-04-08 10:37:11 -05:00
|
|
|
defer p.wg.Done()
|
|
|
|
defer ping.Stop()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ping.C:
|
|
|
|
if err := SendItems(p.rw, pingMsg); err != nil {
|
|
|
|
p.protoErr <- err
|
|
|
|
return
|
|
|
|
}
|
2017-09-04 02:24:52 -05:00
|
|
|
ping.Reset(pingInterval)
|
2015-04-08 10:37:11 -05:00
|
|
|
case <-p.closed:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Peer) readLoop(errc chan<- error) {
|
|
|
|
defer p.wg.Done()
|
2015-02-04 20:07:58 -06:00
|
|
|
for {
|
|
|
|
msg, err := p.rw.ReadMsg()
|
|
|
|
if err != nil {
|
2015-04-08 10:37:11 -05:00
|
|
|
errc <- err
|
|
|
|
return
|
2015-02-04 20:07:58 -06:00
|
|
|
}
|
2015-04-29 15:49:58 -05:00
|
|
|
msg.ReceivedAt = time.Now()
|
2015-02-04 20:07:58 -06:00
|
|
|
if err = p.handle(msg); err != nil {
|
2015-04-08 10:37:11 -05:00
|
|
|
errc <- err
|
|
|
|
return
|
2015-02-04 20:07:58 -06:00
|
|
|
}
|
|
|
|
}
|
2014-11-21 14:48:49 -06:00
|
|
|
}
|
|
|
|
|
2015-02-04 20:07:58 -06:00
|
|
|
func (p *Peer) handle(msg Msg) error {
|
|
|
|
switch {
|
|
|
|
case msg.Code == pingMsg:
|
|
|
|
msg.Discard()
|
2015-03-19 09:11:02 -05:00
|
|
|
go SendItems(p.rw, pongMsg)
|
2015-02-04 20:07:58 -06:00
|
|
|
case msg.Code == discMsg:
|
2015-03-04 05:03:43 -06:00
|
|
|
var reason [1]DiscReason
|
2015-04-08 10:37:11 -05:00
|
|
|
// This is the last message. We don't need to discard or
|
|
|
|
// check errors because, the connection will be closed after it.
|
2015-02-04 20:07:58 -06:00
|
|
|
rlp.Decode(msg.Payload, &reason)
|
2015-05-13 20:04:04 -05:00
|
|
|
return reason[0]
|
2015-02-04 20:07:58 -06:00
|
|
|
case msg.Code < baseProtocolLength:
|
|
|
|
// ignore other base protocol messages
|
|
|
|
return msg.Discard()
|
|
|
|
default:
|
|
|
|
// it's a subprotocol message
|
|
|
|
proto, err := p.getProto(msg.Code)
|
2014-11-21 14:48:49 -06:00
|
|
|
if err != nil {
|
2015-02-04 20:07:58 -06:00
|
|
|
return fmt.Errorf("msg code out of range: %v", msg.Code)
|
2014-11-21 14:48:49 -06:00
|
|
|
}
|
2019-09-27 09:41:47 -05:00
|
|
|
if metrics.Enabled {
|
2020-02-17 05:22:15 -06:00
|
|
|
m := fmt.Sprintf("%s/%s/%d/%#02x", ingressMeterName, proto.Name, proto.Version, msg.Code-proto.offset)
|
|
|
|
metrics.GetOrRegisterMeter(m, nil).Mark(int64(msg.meterSize))
|
2019-09-27 09:41:47 -05:00
|
|
|
}
|
2015-04-08 10:37:11 -05:00
|
|
|
select {
|
|
|
|
case proto.in <- msg:
|
|
|
|
return nil
|
|
|
|
case <-p.closed:
|
|
|
|
return io.EOF
|
|
|
|
}
|
2014-11-21 14:48:49 -06:00
|
|
|
}
|
2015-02-04 20:07:58 -06:00
|
|
|
return nil
|
2014-11-21 14:48:49 -06:00
|
|
|
}
|
|
|
|
|
2015-05-08 09:09:38 -05:00
|
|
|
func countMatchingProtocols(protocols []Protocol, caps []Cap) int {
|
|
|
|
n := 0
|
|
|
|
for _, cap := range caps {
|
|
|
|
for _, proto := range protocols {
|
|
|
|
if proto.Name == cap.Name && proto.Version == cap.Version {
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2015-02-18 18:52:03 -06:00
|
|
|
// matchProtocols creates structures for matching named subprotocols.
|
|
|
|
func matchProtocols(protocols []Protocol, caps []Cap, rw MsgReadWriter) map[string]*protoRW {
|
2015-06-26 07:48:50 -05:00
|
|
|
sort.Sort(capsByNameAndVersion(caps))
|
2014-11-21 14:48:49 -06:00
|
|
|
offset := baseProtocolLength
|
2015-02-18 18:52:03 -06:00
|
|
|
result := make(map[string]*protoRW)
|
2015-06-26 07:48:50 -05:00
|
|
|
|
2014-11-21 14:48:49 -06:00
|
|
|
outer:
|
|
|
|
for _, cap := range caps {
|
2015-02-18 18:52:03 -06:00
|
|
|
for _, proto := range protocols {
|
2015-06-26 07:48:50 -05:00
|
|
|
if proto.Name == cap.Name && proto.Version == cap.Version {
|
|
|
|
// If an old protocol version matched, revert it
|
|
|
|
if old := result[cap.Name]; old != nil {
|
|
|
|
offset -= old.Length
|
|
|
|
}
|
|
|
|
// Assign the new match
|
2015-02-18 18:52:03 -06:00
|
|
|
result[cap.Name] = &protoRW{Protocol: proto, offset: offset, in: make(chan Msg), w: rw}
|
2014-11-21 14:48:49 -06:00
|
|
|
offset += proto.Length
|
2015-06-26 07:48:50 -05:00
|
|
|
|
2014-11-21 14:48:49 -06:00
|
|
|
continue outer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-18 18:52:03 -06:00
|
|
|
return result
|
2014-11-21 14:48:49 -06:00
|
|
|
}
|
|
|
|
|
2015-06-15 06:42:44 -05:00
|
|
|
func (p *Peer) startProtocols(writeStart <-chan struct{}, writeErr chan<- error) {
|
2015-04-08 10:37:11 -05:00
|
|
|
p.wg.Add(len(p.running))
|
2015-02-18 18:52:03 -06:00
|
|
|
for _, proto := range p.running {
|
|
|
|
proto := proto
|
2015-04-08 10:37:11 -05:00
|
|
|
proto.closed = p.closed
|
2015-06-15 06:42:44 -05:00
|
|
|
proto.wstart = writeStart
|
|
|
|
proto.werr = writeErr
|
2017-09-25 03:08:07 -05:00
|
|
|
var rw MsgReadWriter = proto
|
|
|
|
if p.events != nil {
|
2019-07-05 13:27:13 -05:00
|
|
|
rw = newMsgEventer(rw, p.events, p.ID(), proto.Name, p.Info().Network.RemoteAddress, p.Info().Network.LocalAddress)
|
2017-09-25 03:08:07 -05:00
|
|
|
}
|
2017-02-24 02:58:04 -06:00
|
|
|
p.log.Trace(fmt.Sprintf("Starting protocol %s/%d", proto.Name, proto.Version))
|
2015-02-18 18:52:03 -06:00
|
|
|
go func() {
|
2017-09-25 03:08:07 -05:00
|
|
|
err := proto.Run(p, rw)
|
2015-02-18 18:52:03 -06:00
|
|
|
if err == nil {
|
2017-02-24 02:58:04 -06:00
|
|
|
p.log.Trace(fmt.Sprintf("Protocol %s/%d returned", proto.Name, proto.Version))
|
|
|
|
err = errProtocolReturned
|
2015-05-06 16:19:14 -05:00
|
|
|
} else if err != io.EOF {
|
2017-02-24 02:58:04 -06:00
|
|
|
p.log.Trace(fmt.Sprintf("Protocol %s/%d failed", proto.Name, proto.Version), "err", err)
|
2015-02-18 18:52:03 -06:00
|
|
|
}
|
2015-04-08 10:37:11 -05:00
|
|
|
p.protoErr <- err
|
|
|
|
p.wg.Done()
|
2015-02-18 18:52:03 -06:00
|
|
|
}()
|
2014-11-21 14:48:49 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// getProto finds the protocol responsible for handling
|
|
|
|
// the given message code.
|
2015-02-18 18:52:03 -06:00
|
|
|
func (p *Peer) getProto(code uint64) (*protoRW, error) {
|
2014-11-21 14:48:49 -06:00
|
|
|
for _, proto := range p.running {
|
2015-02-18 18:52:03 -06:00
|
|
|
if code >= proto.offset && code < proto.offset+proto.Length {
|
2014-11-21 14:48:49 -06:00
|
|
|
return proto, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, newPeerError(errInvalidMsgCode, "%d", code)
|
|
|
|
}
|
|
|
|
|
2015-02-18 18:52:03 -06:00
|
|
|
type protoRW struct {
|
|
|
|
Protocol
|
2018-07-18 02:41:18 -05:00
|
|
|
in chan Msg // receives read messages
|
2015-06-15 06:42:44 -05:00
|
|
|
closed <-chan struct{} // receives when peer is shutting down
|
|
|
|
wstart <-chan struct{} // receives when write may start
|
|
|
|
werr chan<- error // for write results
|
2015-02-18 18:52:03 -06:00
|
|
|
offset uint64
|
|
|
|
w MsgWriter
|
2014-11-21 14:48:49 -06:00
|
|
|
}
|
|
|
|
|
2015-06-15 06:42:44 -05:00
|
|
|
func (rw *protoRW) WriteMsg(msg Msg) (err error) {
|
2015-02-18 18:52:03 -06:00
|
|
|
if msg.Code >= rw.Length {
|
2014-11-21 14:48:49 -06:00
|
|
|
return newPeerError(errInvalidMsgCode, "not handled")
|
|
|
|
}
|
2019-09-27 09:41:47 -05:00
|
|
|
msg.meterCap = rw.cap()
|
|
|
|
msg.meterCode = msg.Code
|
|
|
|
|
2014-11-21 14:48:49 -06:00
|
|
|
msg.Code += rw.offset
|
2019-09-27 09:41:47 -05:00
|
|
|
|
2015-06-15 06:42:44 -05:00
|
|
|
select {
|
|
|
|
case <-rw.wstart:
|
|
|
|
err = rw.w.WriteMsg(msg)
|
|
|
|
// Report write status back to Peer.run. It will initiate
|
|
|
|
// shutdown if the error is non-nil and unblock the next write
|
|
|
|
// otherwise. The calling protocol code should exit for errors
|
|
|
|
// as well but we don't want to rely on that.
|
|
|
|
rw.werr <- err
|
|
|
|
case <-rw.closed:
|
2018-06-20 07:06:27 -05:00
|
|
|
err = ErrShuttingDown
|
2015-06-15 06:42:44 -05:00
|
|
|
}
|
|
|
|
return err
|
2014-10-23 10:57:54 -05:00
|
|
|
}
|
|
|
|
|
2015-02-18 18:52:03 -06:00
|
|
|
func (rw *protoRW) ReadMsg() (Msg, error) {
|
2015-04-08 10:37:11 -05:00
|
|
|
select {
|
|
|
|
case msg := <-rw.in:
|
|
|
|
msg.Code -= rw.offset
|
|
|
|
return msg, nil
|
|
|
|
case <-rw.closed:
|
|
|
|
return Msg{}, io.EOF
|
2014-11-21 14:48:49 -06:00
|
|
|
}
|
2014-10-23 10:57:54 -05:00
|
|
|
}
|
2015-10-27 08:10:30 -05:00
|
|
|
|
|
|
|
// PeerInfo represents a short summary of the information known about a connected
|
|
|
|
// peer. Sub-protocol independent fields are contained and initialized here, with
|
|
|
|
// protocol specifics delegated to all connected sub-protocols.
|
|
|
|
type PeerInfo struct {
|
2019-07-19 04:25:43 -05:00
|
|
|
ENR string `json:"enr,omitempty"` // Ethereum Node Record
|
|
|
|
Enode string `json:"enode"` // Node URL
|
|
|
|
ID string `json:"id"` // Unique node identifier
|
|
|
|
Name string `json:"name"` // Name of the node, including client type, version, OS, custom data
|
|
|
|
Caps []string `json:"caps"` // Protocols advertised by this peer
|
2015-10-27 08:10:30 -05:00
|
|
|
Network struct {
|
|
|
|
LocalAddress string `json:"localAddress"` // Local endpoint of the TCP data connection
|
|
|
|
RemoteAddress string `json:"remoteAddress"` // Remote endpoint of the TCP data connection
|
2018-02-12 06:36:09 -06:00
|
|
|
Inbound bool `json:"inbound"`
|
|
|
|
Trusted bool `json:"trusted"`
|
|
|
|
Static bool `json:"static"`
|
2015-10-27 08:10:30 -05:00
|
|
|
} `json:"network"`
|
|
|
|
Protocols map[string]interface{} `json:"protocols"` // Sub-protocol specific metadata fields
|
|
|
|
}
|
|
|
|
|
|
|
|
// Info gathers and returns a collection of metadata known about a peer.
|
|
|
|
func (p *Peer) Info() *PeerInfo {
|
|
|
|
// Gather the protocol capabilities
|
|
|
|
var caps []string
|
|
|
|
for _, cap := range p.Caps() {
|
|
|
|
caps = append(caps, cap.String())
|
|
|
|
}
|
|
|
|
// Assemble the generic peer metadata
|
|
|
|
info := &PeerInfo{
|
2019-07-19 04:25:43 -05:00
|
|
|
Enode: p.Node().URLv4(),
|
2015-10-27 08:10:30 -05:00
|
|
|
ID: p.ID().String(),
|
|
|
|
Name: p.Name(),
|
|
|
|
Caps: caps,
|
|
|
|
Protocols: make(map[string]interface{}),
|
|
|
|
}
|
2019-07-19 04:25:43 -05:00
|
|
|
if p.Node().Seq() > 0 {
|
|
|
|
info.ENR = p.Node().String()
|
|
|
|
}
|
2015-10-27 08:10:30 -05:00
|
|
|
info.Network.LocalAddress = p.LocalAddr().String()
|
|
|
|
info.Network.RemoteAddress = p.RemoteAddr().String()
|
2018-02-12 06:36:09 -06:00
|
|
|
info.Network.Inbound = p.rw.is(inboundConn)
|
|
|
|
info.Network.Trusted = p.rw.is(trustedConn)
|
|
|
|
info.Network.Static = p.rw.is(staticDialedConn)
|
2015-10-27 08:10:30 -05:00
|
|
|
|
|
|
|
// Gather all the running protocol infos
|
|
|
|
for _, proto := range p.running {
|
|
|
|
protoInfo := interface{}("unknown")
|
|
|
|
if query := proto.Protocol.PeerInfo; query != nil {
|
|
|
|
if metadata := query(p.ID()); metadata != nil {
|
|
|
|
protoInfo = metadata
|
|
|
|
} else {
|
|
|
|
protoInfo = "handshake"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
info.Protocols[proto.Name] = protoInfo
|
|
|
|
}
|
|
|
|
return info
|
|
|
|
}
|