diff options
author | Felix Lange <fjl@users.noreply.github.com> | 2018-09-25 06:59:00 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-09-25 06:59:00 +0800 |
commit | 30cd5c18549f645002aedb4c00e5bab683cb0835 (patch) | |
tree | 9a9098c6ff5a746758660295dfc1880d22e75434 /swarm/network/protocol.go | |
parent | 0ae462fb80b8a95e38af08d894ea9ecf9e45f2e7 (diff) | |
download | dexon-30cd5c18549f645002aedb4c00e5bab683cb0835.tar dexon-30cd5c18549f645002aedb4c00e5bab683cb0835.tar.gz dexon-30cd5c18549f645002aedb4c00e5bab683cb0835.tar.bz2 dexon-30cd5c18549f645002aedb4c00e5bab683cb0835.tar.lz dexon-30cd5c18549f645002aedb4c00e5bab683cb0835.tar.xz dexon-30cd5c18549f645002aedb4c00e5bab683cb0835.tar.zst dexon-30cd5c18549f645002aedb4c00e5bab683cb0835.zip |
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.
Diffstat (limited to 'swarm/network/protocol.go')
-rw-r--r-- | swarm/network/protocol.go | 85 |
1 files changed, 32 insertions, 53 deletions
diff --git a/swarm/network/protocol.go b/swarm/network/protocol.go index d509d157b..66ae94a88 100644 --- a/swarm/network/protocol.go +++ b/swarm/network/protocol.go @@ -26,7 +26,7 @@ import ( "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/p2p" - "github.com/ethereum/go-ethereum/p2p/discover" + "github.com/ethereum/go-ethereum/p2p/enode" "github.com/ethereum/go-ethereum/p2p/protocols" "github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/swarm/log" @@ -78,7 +78,7 @@ type Bzz struct { LightNode bool localAddr *BzzAddr mtx sync.Mutex - handshakes map[discover.NodeID]*HandshakeMsg + handshakes map[enode.ID]*HandshakeMsg streamerSpec *protocols.Spec streamerRun func(*BzzPeer) error } @@ -94,7 +94,7 @@ func NewBzz(config *BzzConfig, kad *Kademlia, store state.Store, streamerSpec *p NetworkID: config.NetworkID, LightNode: config.LightNode, localAddr: &BzzAddr{config.OverlayAddr, config.UnderlayAddr}, - handshakes: make(map[discover.NodeID]*HandshakeMsg), + handshakes: make(map[enode.ID]*HandshakeMsg), streamerRun: streamerRun, streamerSpec: streamerSpec, } @@ -183,7 +183,6 @@ func (b *Bzz) RunProtocol(spec *protocols.Spec, run func(*BzzPeer) error) func(* // the handshake has succeeded so construct the BzzPeer and run the protocol peer := &BzzPeer{ Peer: protocols.NewPeer(p, rw, spec), - localAddr: b.localAddr, BzzAddr: handshake.peerAddr, lastActive: time.Now(), LightNode: handshake.LightNode, @@ -218,14 +217,14 @@ func (b *Bzz) performHandshake(p *protocols.Peer, handshake *HandshakeMsg) error func (b *Bzz) runBzz(p *p2p.Peer, rw p2p.MsgReadWriter) error { handshake, _ := b.GetHandshake(p.ID()) if !<-handshake.init { - return fmt.Errorf("%08x: bzz already started on peer %08x", b.localAddr.Over()[:4], ToOverlayAddr(p.ID().Bytes())[:4]) + return fmt.Errorf("%08x: bzz already started on peer %08x", b.localAddr.Over()[:4], p.ID().Bytes()[:4]) } close(handshake.init) defer b.removeHandshake(p.ID()) peer := protocols.NewPeer(p, rw, BzzSpec) err := b.performHandshake(peer, handshake) if err != nil { - log.Warn(fmt.Sprintf("%08x: handshake failed with remote peer %08x: %v", b.localAddr.Over()[:4], ToOverlayAddr(p.ID().Bytes())[:4], err)) + log.Warn(fmt.Sprintf("%08x: handshake failed with remote peer %08x: %v", b.localAddr.Over()[:4], p.ID().Bytes()[:4], err)) return err } @@ -242,18 +241,13 @@ func (b *Bzz) runBzz(p *p2p.Peer, rw p2p.MsgReadWriter) error { // implements the Peer interface and all interfaces Peer implements: Addr, OverlayPeer type BzzPeer struct { *protocols.Peer // represents the connection for online peers - localAddr *BzzAddr // local Peers address *BzzAddr // remote address -> implements Addr interface = protocols.Peer lastActive time.Time // time is updated whenever mutexes are releasing LightNode bool } -func NewBzzPeer(p *protocols.Peer, addr *BzzAddr) *BzzPeer { - return &BzzPeer{ - Peer: p, - localAddr: addr, - BzzAddr: NewAddrFromNodeID(p.ID()), - } +func NewBzzPeer(p *protocols.Peer) *BzzPeer { + return &BzzPeer{Peer: p, BzzAddr: NewAddr(p.Node())} } // LastActive returns the time the peer was last active @@ -261,6 +255,14 @@ func (p *BzzPeer) LastActive() time.Time { return p.lastActive } +// ID returns the peer's underlay node identifier. +func (p *BzzPeer) ID() enode.ID { + // This is here to resolve a method tie: both protocols.Peer and BzzAddr are embedded + // into the struct and provide ID(). The protocols.Peer version is faster, ensure it + // gets used. + return p.Peer.ID() +} + /* Handshake @@ -301,14 +303,14 @@ func (b *Bzz) checkHandshake(hs interface{}) error { // removeHandshake removes handshake for peer with peerID // from the bzz handshake store -func (b *Bzz) removeHandshake(peerID discover.NodeID) { +func (b *Bzz) removeHandshake(peerID enode.ID) { b.mtx.Lock() defer b.mtx.Unlock() delete(b.handshakes, peerID) } // GetHandshake returns the bzz handhake that the remote peer with peerID sent -func (b *Bzz) GetHandshake(peerID discover.NodeID) (*HandshakeMsg, bool) { +func (b *Bzz) GetHandshake(peerID enode.ID) (*HandshakeMsg, bool) { b.mtx.Lock() defer b.mtx.Unlock() handshake, found := b.handshakes[peerID] @@ -336,24 +338,28 @@ type BzzAddr struct { UAddr []byte } -// Address implements OverlayPeer interface to be used in Overlay +// Address implements OverlayPeer interface to be used in Overlay. func (a *BzzAddr) Address() []byte { return a.OAddr } -// Over returns the overlay address +// Over returns the overlay address. func (a *BzzAddr) Over() []byte { return a.OAddr } -// Under returns the underlay address +// Under returns the underlay address. func (a *BzzAddr) Under() []byte { return a.UAddr } -// ID returns the nodeID from the underlay enode address -func (a *BzzAddr) ID() discover.NodeID { - return discover.MustParseNode(string(a.UAddr)).ID +// ID returns the node identifier in the underlay. +func (a *BzzAddr) ID() enode.ID { + n, err := enode.ParseV4(string(a.UAddr)) + if err != nil { + return enode.ID{} + } + return n.ID() } // Update updates the underlay address of a peer record @@ -372,38 +378,11 @@ func RandomAddr() *BzzAddr { if err != nil { panic("unable to generate key") } - pubkey := crypto.FromECDSAPub(&key.PublicKey) - var id discover.NodeID - copy(id[:], pubkey[1:]) - return NewAddrFromNodeID(id) -} - -// NewNodeIDFromAddr transforms the underlay address to an adapters.NodeID -func NewNodeIDFromAddr(addr *BzzAddr) discover.NodeID { - log.Info(fmt.Sprintf("uaddr=%s", string(addr.UAddr))) - node := discover.MustParseNode(string(addr.UAddr)) - return node.ID -} - -// NewAddrFromNodeID constucts a BzzAddr from a discover.NodeID -// the overlay address is derived as the hash of the nodeID -func NewAddrFromNodeID(id discover.NodeID) *BzzAddr { - return &BzzAddr{ - OAddr: ToOverlayAddr(id.Bytes()), - UAddr: []byte(discover.NewNode(id, net.IP{127, 0, 0, 1}, 30303, 30303).String()), - } -} - -// NewAddrFromNodeIDAndPort constucts a BzzAddr from a discover.NodeID and port uint16 -// the overlay address is derived as the hash of the nodeID -func NewAddrFromNodeIDAndPort(id discover.NodeID, host net.IP, port uint16) *BzzAddr { - return &BzzAddr{ - OAddr: ToOverlayAddr(id.Bytes()), - UAddr: []byte(discover.NewNode(id, host, port, port).String()), - } + node := enode.NewV4(&key.PublicKey, net.IP{127, 0, 0, 1}, 30303, 30303) + return NewAddr(node) } -// ToOverlayAddr creates an overlayaddress from a byte slice -func ToOverlayAddr(id []byte) []byte { - return crypto.Keccak256(id) +// NewAddr constucts a BzzAddr from a node record. +func NewAddr(node *enode.Node) *BzzAddr { + return &BzzAddr{OAddr: node.ID().Bytes(), UAddr: []byte(node.String())} } |