github.com/core-coin/go-core/v2@v2.1.9/p2p/discover/node.go (about)

     1  // Copyright 2015 by the Authors
     2  // This file is part of the go-core library.
     3  //
     4  // The go-core library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-core library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-core library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package discover
    18  
    19  import (
    20  	"net"
    21  	"time"
    22  
    23  	"github.com/core-coin/go-core/v2/crypto"
    24  	"github.com/core-coin/go-core/v2/p2p/enode"
    25  )
    26  
    27  // node represents a host on the network.
    28  // The fields of Node may not be modified.
    29  type node struct {
    30  	enode.Node
    31  	addedAt        time.Time // time when the node was added to the table
    32  	livenessChecks uint      // how often liveness was checked
    33  }
    34  
    35  type encPubkey [57]byte
    36  
    37  func encodePubkey(key *crypto.PublicKey) encPubkey {
    38  	var e encPubkey
    39  	copy(e[:], key[:])
    40  	return e
    41  }
    42  
    43  func decodePubkey(e []byte) (*crypto.PublicKey, error) {
    44  	return crypto.UnmarshalPubKey(e[:])
    45  }
    46  
    47  func (e encPubkey) id() enode.ID {
    48  	return enode.ID(crypto.SHA3Hash(e[:]))
    49  }
    50  
    51  func wrapNode(n *enode.Node) *node {
    52  	return &node{Node: *n}
    53  }
    54  
    55  func wrapNodes(ns []*enode.Node) []*node {
    56  	result := make([]*node, len(ns))
    57  	for i, n := range ns {
    58  		result[i] = wrapNode(n)
    59  	}
    60  	return result
    61  }
    62  
    63  func unwrapNode(n *node) *enode.Node {
    64  	return &n.Node
    65  }
    66  
    67  func unwrapNodes(ns []*node) []*enode.Node {
    68  	result := make([]*enode.Node, len(ns))
    69  	for i, n := range ns {
    70  		result[i] = unwrapNode(n)
    71  	}
    72  	return result
    73  }
    74  
    75  func (n *node) addr() *net.UDPAddr {
    76  	return &net.UDPAddr{IP: n.IP(), Port: n.UDP()}
    77  }
    78  
    79  func (n *node) String() string {
    80  	return n.Node.String()
    81  }