Browse Source

p2p: rename PubKeyToID to NodeIDFromPubKey

pull/5850/head
Erik Grinaker 4 years ago
committed by Erik Grinaker
parent
commit
91bef75f62
5 changed files with 14 additions and 14 deletions
  1. +8
    -8
      p2p/key.go
  2. +1
    -1
      p2p/peer.go
  3. +3
    -3
      p2p/peer_test.go
  4. +1
    -1
      p2p/transport_mconn.go
  5. +1
    -1
      p2p/transport_memory.go

+ 8
- 8
p2p/key.go View File

@ -21,6 +21,12 @@ type NodeID string
// FIXME: support other length addresses? // FIXME: support other length addresses?
const NodeIDByteLength = crypto.AddressSize const NodeIDByteLength = crypto.AddressSize
// NodeIDFromPubKey returns the noe ID corresponding to the given PubKey. It's
// the hex-encoding of the pubKey.Address().
func NodeIDFromPubKey(pubKey crypto.PubKey) NodeID {
return NodeID(hex.EncodeToString(pubKey.Address()))
}
// Bytes converts the node ID to it's binary byte representation. // Bytes converts the node ID to it's binary byte representation.
func (id NodeID) Bytes() ([]byte, error) { func (id NodeID) Bytes() ([]byte, error) {
bz, err := hex.DecodeString(string(id)) bz, err := hex.DecodeString(string(id))
@ -76,12 +82,6 @@ func (nodeKey NodeKey) SaveAs(filePath string) error {
return nil return nil
} }
// PubKeyToID returns the ID corresponding to the given PubKey.
// It's the hex-encoding of the pubKey.Address().
func PubKeyToID(pubKey crypto.PubKey) NodeID {
return NodeID(hex.EncodeToString(pubKey.Address()))
}
// LoadOrGenNodeKey attempts to load the NodeKey from the given filePath. If // LoadOrGenNodeKey attempts to load the NodeKey from the given filePath. If
// the file does not exist, it generates and saves a new NodeKey. // the file does not exist, it generates and saves a new NodeKey.
func LoadOrGenNodeKey(filePath string) (NodeKey, error) { func LoadOrGenNodeKey(filePath string) (NodeKey, error) {
@ -106,7 +106,7 @@ func LoadOrGenNodeKey(filePath string) (NodeKey, error) {
func GenNodeKey() NodeKey { func GenNodeKey() NodeKey {
privKey := ed25519.GenPrivKey() privKey := ed25519.GenPrivKey()
return NodeKey{ return NodeKey{
ID: PubKeyToID(privKey.PubKey()),
ID: NodeIDFromPubKey(privKey.PubKey()),
PrivKey: privKey, PrivKey: privKey,
} }
} }
@ -122,6 +122,6 @@ func LoadNodeKey(filePath string) (NodeKey, error) {
if err != nil { if err != nil {
return NodeKey{}, err return NodeKey{}, err
} }
nodeKey.ID = PubKeyToID(nodeKey.PubKey())
nodeKey.ID = NodeIDFromPubKey(nodeKey.PubKey())
return nodeKey, nil return nodeKey, nil
} }

+ 1
- 1
p2p/peer.go View File

@ -162,7 +162,7 @@ func newPeerConn(outbound, persistent bool, conn Connection) peerConn {
// ID only exists for SecretConnection. // ID only exists for SecretConnection.
func (pc peerConn) ID() NodeID { func (pc peerConn) ID() NodeID {
return PubKeyToID(pc.conn.PubKey())
return NodeIDFromPubKey(pc.conn.PubKey())
} }
// Return the IP from the connection RemoteAddr // Return the IP from the connection RemoteAddr


+ 3
- 3
p2p/peer_test.go View File

@ -81,7 +81,7 @@ func createOutboundPeerAndPerformHandshake(
{ID: testCh, Priority: 1}, {ID: testCh, Priority: 1},
} }
pk := ed25519.GenPrivKey() pk := ed25519.GenPrivKey()
ourNodeInfo := testNodeInfo(PubKeyToID(pk.PubKey()), "host_peer")
ourNodeInfo := testNodeInfo(NodeIDFromPubKey(pk.PubKey()), "host_peer")
transport := NewMConnTransport(log.TestingLogger(), ourNodeInfo, pk, mConfig) transport := NewMConnTransport(log.TestingLogger(), ourNodeInfo, pk, mConfig)
transport.SetChannelDescriptors(chDescs) transport.SetChannelDescriptors(chDescs)
reactorsByCh := map[byte]Reactor{testCh: NewTestReactor(chDescs, true)} reactorsByCh := map[byte]Reactor{testCh: NewTestReactor(chDescs, true)}
@ -154,7 +154,7 @@ func (rp *remotePeer) Addr() *NetAddress {
} }
func (rp *remotePeer) ID() NodeID { func (rp *remotePeer) ID() NodeID {
return PubKeyToID(rp.PrivKey.PubKey())
return NodeIDFromPubKey(rp.PrivKey.PubKey())
} }
func (rp *remotePeer) Start() { func (rp *remotePeer) Start() {
@ -167,7 +167,7 @@ func (rp *remotePeer) Start() {
golog.Fatalf("net.Listen tcp :0: %+v", e) golog.Fatalf("net.Listen tcp :0: %+v", e)
} }
rp.listener = l rp.listener = l
rp.addr = NewNetAddress(PubKeyToID(rp.PrivKey.PubKey()), l.Addr())
rp.addr = NewNetAddress(NodeIDFromPubKey(rp.PrivKey.PubKey()), l.Addr())
if rp.channels == nil { if rp.channels == nil {
rp.channels = []byte{testCh} rp.channels = []byte{testCh}
} }


+ 1
- 1
p2p/transport_mconn.go View File

@ -466,7 +466,7 @@ func newMConnConnection(
// For outgoing conns, ensure connection key matches dialed key. // For outgoing conns, ensure connection key matches dialed key.
if expectPeerID != "" { if expectPeerID != "" {
peerID := PubKeyToID(c.PubKey())
peerID := NodeIDFromPubKey(c.PubKey())
if expectPeerID != peerID { if expectPeerID != peerID {
err = ErrRejected{ err = ErrRejected{
conn: tcpConn, conn: tcpConn,


+ 1
- 1
p2p/transport_memory.go View File

@ -65,7 +65,7 @@ func (n *MemoryNetwork) CreateTransport(
// Transport.Endpoints(). // Transport.Endpoints().
func (n *MemoryNetwork) GenerateTransport() *MemoryTransport { func (n *MemoryNetwork) GenerateTransport() *MemoryTransport {
privKey := ed25519.GenPrivKey() privKey := ed25519.GenPrivKey()
nodeID := PubKeyToID(privKey.PubKey())
nodeID := NodeIDFromPubKey(privKey.PubKey())
nodeInfo := NodeInfo{ nodeInfo := NodeInfo{
NodeID: nodeID, NodeID: nodeID,
ListenAddr: fmt.Sprintf("%v:%v", MemoryProtocol, nodeID), ListenAddr: fmt.Sprintf("%v:%v", MemoryProtocol, nodeID),


Loading…
Cancel
Save