mirror of
https://github.com/tendermint/tendermint.git
synced 2026-09-27 02:14:25 +00:00
p2p: peer.Key -> peer.ID
This commit is contained in:
+14
-3
@@ -2,6 +2,7 @@ package p2p
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
@@ -21,8 +22,14 @@ type NodeKey struct {
|
||||
PrivKey crypto.PrivKey `json:"priv_key"` // our priv key
|
||||
}
|
||||
|
||||
type ID string
|
||||
|
||||
// ID returns the peer's canonical ID - the hash of its public key.
|
||||
func (nodeKey *NodeKey) ID() []byte {
|
||||
func (nodeKey *NodeKey) ID() ID {
|
||||
return ID(hex.EncodeToString(nodeKey.id()))
|
||||
}
|
||||
|
||||
func (nodeKey *NodeKey) id() []byte {
|
||||
return nodeKey.PrivKey.PubKey().Address()
|
||||
}
|
||||
|
||||
@@ -31,6 +38,10 @@ func (nodeKey *NodeKey) PubKey() crypto.PubKey {
|
||||
return nodeKey.PrivKey.PubKey()
|
||||
}
|
||||
|
||||
func (nodeKey *NodeKey) SatisfiesTarget(target []byte) bool {
|
||||
return bytes.Compare(nodeKey.id(), target) < 0
|
||||
}
|
||||
|
||||
// LoadOrGenNodeKey attempts to load the NodeKey from the given filePath,
|
||||
// and checks that the corresponding ID is less than the target.
|
||||
// If the file does not exist, it generates and saves a new NodeKey
|
||||
@@ -41,8 +52,8 @@ func LoadOrGenNodeKey(filePath string, target []byte) (*NodeKey, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if bytes.Compare(nodeKey.ID(), target) >= 0 {
|
||||
return nil, fmt.Errorf("Loaded ID (%X) does not satisfy target (%X)", nodeKey.ID(), target)
|
||||
if !nodeKey.SatisfiesTarget(target) {
|
||||
return nil, fmt.Errorf("Loaded ID (%s) does not satisfy target (%X)", nodeKey.ID(), target)
|
||||
}
|
||||
return nodeKey, nil
|
||||
} else {
|
||||
|
||||
+8
-8
@@ -1,6 +1,7 @@
|
||||
package p2p
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"net"
|
||||
"time"
|
||||
@@ -17,7 +18,7 @@ import (
|
||||
type Peer interface {
|
||||
cmn.Service
|
||||
|
||||
Key() string
|
||||
ID() ID
|
||||
IsOutbound() bool
|
||||
IsPersistent() bool
|
||||
NodeInfo() *NodeInfo
|
||||
@@ -282,15 +283,15 @@ func (p *peer) CanSend(chID byte) bool {
|
||||
// String representation.
|
||||
func (p *peer) String() string {
|
||||
if p.outbound {
|
||||
return fmt.Sprintf("Peer{%v %v out}", p.mconn, p.Key())
|
||||
return fmt.Sprintf("Peer{%v %v out}", p.mconn, p.ID())
|
||||
}
|
||||
|
||||
return fmt.Sprintf("Peer{%v %v in}", p.mconn, p.Key())
|
||||
return fmt.Sprintf("Peer{%v %v in}", p.mconn, p.ID())
|
||||
}
|
||||
|
||||
// Equals reports whenever 2 peers are actually represent the same node.
|
||||
func (p *peer) Equals(other Peer) bool {
|
||||
return p.Key() == other.Key()
|
||||
return p.ID() == other.ID()
|
||||
}
|
||||
|
||||
// Get the data for a given key.
|
||||
@@ -303,10 +304,9 @@ func (p *peer) Set(key string, data interface{}) {
|
||||
p.Data.Set(key, data)
|
||||
}
|
||||
|
||||
// Key returns the peer's id key.
|
||||
// TODO: call this ID
|
||||
func (p *peer) Key() string {
|
||||
return p.nodeInfo.ListenAddr // XXX: should probably be PubKey.KeyString()
|
||||
// Key returns the peer's ID - the hex encoded hash of its pubkey.
|
||||
func (p *peer) ID() ID {
|
||||
return ID(hex.EncodeToString(p.nodeInfo.PubKey.Address()))
|
||||
}
|
||||
|
||||
// NodeInfo returns a copy of the peer's NodeInfo.
|
||||
|
||||
+12
-12
@@ -6,8 +6,8 @@ import (
|
||||
|
||||
// IPeerSet has a (immutable) subset of the methods of PeerSet.
|
||||
type IPeerSet interface {
|
||||
Has(key string) bool
|
||||
Get(key string) Peer
|
||||
Has(key ID) bool
|
||||
Get(key ID) Peer
|
||||
List() []Peer
|
||||
Size() int
|
||||
}
|
||||
@@ -18,7 +18,7 @@ type IPeerSet interface {
|
||||
// Iteration over the peers is super fast and thread-safe.
|
||||
type PeerSet struct {
|
||||
mtx sync.Mutex
|
||||
lookup map[string]*peerSetItem
|
||||
lookup map[ID]*peerSetItem
|
||||
list []Peer
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ type peerSetItem struct {
|
||||
// NewPeerSet creates a new peerSet with a list of initial capacity of 256 items.
|
||||
func NewPeerSet() *PeerSet {
|
||||
return &PeerSet{
|
||||
lookup: make(map[string]*peerSetItem),
|
||||
lookup: make(map[ID]*peerSetItem),
|
||||
list: make([]Peer, 0, 256),
|
||||
}
|
||||
}
|
||||
@@ -40,7 +40,7 @@ func NewPeerSet() *PeerSet {
|
||||
func (ps *PeerSet) Add(peer Peer) error {
|
||||
ps.mtx.Lock()
|
||||
defer ps.mtx.Unlock()
|
||||
if ps.lookup[peer.Key()] != nil {
|
||||
if ps.lookup[peer.ID()] != nil {
|
||||
return ErrSwitchDuplicatePeer
|
||||
}
|
||||
|
||||
@@ -48,13 +48,13 @@ func (ps *PeerSet) Add(peer Peer) error {
|
||||
// Appending is safe even with other goroutines
|
||||
// iterating over the ps.list slice.
|
||||
ps.list = append(ps.list, peer)
|
||||
ps.lookup[peer.Key()] = &peerSetItem{peer, index}
|
||||
ps.lookup[peer.ID()] = &peerSetItem{peer, index}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Has returns true iff the PeerSet contains
|
||||
// the peer referred to by this peerKey.
|
||||
func (ps *PeerSet) Has(peerKey string) bool {
|
||||
func (ps *PeerSet) Has(peerKey ID) bool {
|
||||
ps.mtx.Lock()
|
||||
_, ok := ps.lookup[peerKey]
|
||||
ps.mtx.Unlock()
|
||||
@@ -62,7 +62,7 @@ func (ps *PeerSet) Has(peerKey string) bool {
|
||||
}
|
||||
|
||||
// Get looks up a peer by the provided peerKey.
|
||||
func (ps *PeerSet) Get(peerKey string) Peer {
|
||||
func (ps *PeerSet) Get(peerKey ID) Peer {
|
||||
ps.mtx.Lock()
|
||||
defer ps.mtx.Unlock()
|
||||
item, ok := ps.lookup[peerKey]
|
||||
@@ -77,7 +77,7 @@ func (ps *PeerSet) Get(peerKey string) Peer {
|
||||
func (ps *PeerSet) Remove(peer Peer) {
|
||||
ps.mtx.Lock()
|
||||
defer ps.mtx.Unlock()
|
||||
item := ps.lookup[peer.Key()]
|
||||
item := ps.lookup[peer.ID()]
|
||||
if item == nil {
|
||||
return
|
||||
}
|
||||
@@ -90,18 +90,18 @@ func (ps *PeerSet) Remove(peer Peer) {
|
||||
// If it's the last peer, that's an easy special case.
|
||||
if index == len(ps.list)-1 {
|
||||
ps.list = newList
|
||||
delete(ps.lookup, peer.Key())
|
||||
delete(ps.lookup, peer.ID())
|
||||
return
|
||||
}
|
||||
|
||||
// Replace the popped item with the last item in the old list.
|
||||
lastPeer := ps.list[len(ps.list)-1]
|
||||
lastPeerKey := lastPeer.Key()
|
||||
lastPeerKey := lastPeer.ID()
|
||||
lastPeerItem := ps.lookup[lastPeerKey]
|
||||
newList[index] = lastPeer
|
||||
lastPeerItem.index = index
|
||||
ps.list = newList
|
||||
delete(ps.lookup, peer.Key())
|
||||
delete(ps.lookup, peer.ID())
|
||||
}
|
||||
|
||||
// Size returns the number of unique items in the peerSet.
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
crypto "github.com/tendermint/go-crypto"
|
||||
cmn "github.com/tendermint/tmlibs/common"
|
||||
)
|
||||
|
||||
@@ -16,6 +17,7 @@ func randPeer() *peer {
|
||||
nodeInfo: &NodeInfo{
|
||||
RemoteAddr: cmn.Fmt("%v.%v.%v.%v:46656", rand.Int()%256, rand.Int()%256, rand.Int()%256, rand.Int()%256),
|
||||
ListenAddr: cmn.Fmt("%v.%v.%v.%v:46656", rand.Int()%256, rand.Int()%256, rand.Int()%256, rand.Int()%256),
|
||||
PubKey: crypto.GenPrivKeyEd25519().Wrap().PubKey(),
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -39,7 +41,7 @@ func TestPeerSetAddRemoveOne(t *testing.T) {
|
||||
peerSet.Remove(peerAtFront)
|
||||
wantSize := n - i - 1
|
||||
for j := 0; j < 2; j++ {
|
||||
assert.Equal(t, false, peerSet.Has(peerAtFront.Key()), "#%d Run #%d: failed to remove peer", i, j)
|
||||
assert.Equal(t, false, peerSet.Has(peerAtFront.ID()), "#%d Run #%d: failed to remove peer", i, j)
|
||||
assert.Equal(t, wantSize, peerSet.Size(), "#%d Run #%d: failed to remove peer and decrement size", i, j)
|
||||
// Test the route of removing the now non-existent element
|
||||
peerSet.Remove(peerAtFront)
|
||||
@@ -58,7 +60,7 @@ func TestPeerSetAddRemoveOne(t *testing.T) {
|
||||
for i := n - 1; i >= 0; i-- {
|
||||
peerAtEnd := peerList[i]
|
||||
peerSet.Remove(peerAtEnd)
|
||||
assert.Equal(t, false, peerSet.Has(peerAtEnd.Key()), "#%d: failed to remove item at end", i)
|
||||
assert.Equal(t, false, peerSet.Has(peerAtEnd.ID()), "#%d: failed to remove item at end", i)
|
||||
assert.Equal(t, i, peerSet.Size(), "#%d: differing sizes after peerSet.Remove(atEndPeer)", i)
|
||||
}
|
||||
}
|
||||
@@ -82,7 +84,7 @@ func TestPeerSetAddRemoveMany(t *testing.T) {
|
||||
|
||||
for i, peer := range peers {
|
||||
peerSet.Remove(peer)
|
||||
if peerSet.Has(peer.Key()) {
|
||||
if peerSet.Has(peer.ID()) {
|
||||
t.Errorf("Failed to remove peer")
|
||||
}
|
||||
if peerSet.Size() != len(peers)-i-1 {
|
||||
@@ -129,7 +131,7 @@ func TestPeerSetGet(t *testing.T) {
|
||||
t.Parallel()
|
||||
peerSet := NewPeerSet()
|
||||
peer := randPeer()
|
||||
assert.Nil(t, peerSet.Get(peer.Key()), "expecting a nil lookup, before .Add")
|
||||
assert.Nil(t, peerSet.Get(peer.ID()), "expecting a nil lookup, before .Add")
|
||||
|
||||
if err := peerSet.Add(peer); err != nil {
|
||||
t.Fatalf("Failed to add new peer: %v", err)
|
||||
@@ -142,7 +144,7 @@ func TestPeerSetGet(t *testing.T) {
|
||||
wg.Add(1)
|
||||
go func(i int) {
|
||||
defer wg.Done()
|
||||
got, want := peerSet.Get(peer.Key()), peer
|
||||
got, want := peerSet.Get(peer.ID()), peer
|
||||
assert.Equal(t, got, want, "#%d: got=%v want=%v", i, got, want)
|
||||
}(i)
|
||||
}
|
||||
|
||||
+2
-1
@@ -265,7 +265,8 @@ func (r *PEXReactor) ensurePeers() {
|
||||
continue
|
||||
}
|
||||
// XXX: Should probably use pubkey as peer key ...
|
||||
if connected := r.Switch.Peers().Has(try.String()); connected {
|
||||
// TODO: use the ID correctly
|
||||
if connected := r.Switch.Peers().Has(ID(try.String())); connected {
|
||||
continue
|
||||
}
|
||||
r.Logger.Info("Will dial address", "addr", try)
|
||||
|
||||
+1
-1
@@ -257,7 +257,7 @@ func (sw *Switch) addPeer(peer *peer) error {
|
||||
}
|
||||
|
||||
// Avoid duplicate
|
||||
if sw.peers.Has(peer.Key()) {
|
||||
if sw.peers.Has(peer.ID()) {
|
||||
return ErrSwitchDuplicatePeer
|
||||
|
||||
}
|
||||
|
||||
+2
-2
@@ -28,7 +28,7 @@ func init() {
|
||||
}
|
||||
|
||||
type PeerMessage struct {
|
||||
PeerKey string
|
||||
PeerID ID
|
||||
Bytes []byte
|
||||
Counter int
|
||||
}
|
||||
@@ -77,7 +77,7 @@ func (tr *TestReactor) Receive(chID byte, peer Peer, msgBytes []byte) {
|
||||
tr.mtx.Lock()
|
||||
defer tr.mtx.Unlock()
|
||||
//fmt.Printf("Received: %X, %X\n", chID, msgBytes)
|
||||
tr.msgsReceived[chID] = append(tr.msgsReceived[chID], PeerMessage{peer.Key(), msgBytes, tr.msgsCounter})
|
||||
tr.msgsReceived[chID] = append(tr.msgsReceived[chID], PeerMessage{peer.ID(), msgBytes, tr.msgsCounter})
|
||||
tr.msgsCounter++
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user