github.com/klaytn/klaytn@v1.12.1/networks/p2p/discover/discover_storage.go (about)

     1  // Copyright 2019 The klaytn Authors
     2  // This file is part of the klaytn library.
     3  //
     4  // The klaytn 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 klaytn 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 klaytn library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package discover
    18  
    19  import (
    20  	"github.com/klaytn/klaytn/common"
    21  )
    22  
    23  type discoverStorage interface {
    24  	name() string
    25  	setTable(t *Table)
    26  	setTargetNodeType(tType NodeType)
    27  	init()
    28  
    29  	add(n *Node)
    30  	delete(n *Node)
    31  	len() (n int)
    32  	nodeAll() []*Node
    33  	readRandomNodes(buf []*Node) (n int)
    34  	closest(target common.Hash, nresults int) *nodesByDistance
    35  	stuff(nodes []*Node)
    36  	copyBondedNodes()
    37  
    38  	lookup(targetID NodeID, refreshIfEmpty bool, targetType NodeType) []*Node
    39  	getNodes(max int) []*Node
    40  	doRevalidate()
    41  	doRefresh()
    42  
    43  	isAuthorized(id NodeID) bool
    44  
    45  	// API
    46  	getBucketEntries() []*Node
    47  	getAuthorizedNodes() []*Node
    48  	putAuthorizedNode(node *Node)
    49  	deleteAuthorizedNode(id NodeID)
    50  }
    51  
    52  // pushNode adds n to the front of list, keeping at most max items.
    53  func pushNode(list []*Node, n *Node, max int) ([]*Node, *Node) {
    54  	if len(list) < max {
    55  		list = append(list, nil)
    56  	}
    57  	removed := list[len(list)-1]
    58  	copy(list[1:], list)
    59  	list[0] = n
    60  	return list, removed
    61  }
    62  
    63  // deleteNode removes n from list.
    64  func deleteNode(list []*Node, n *Node) []*Node {
    65  	for i := range list {
    66  		if list[i].ID == n.ID {
    67  			return append(list[:i], list[i+1:]...)
    68  		}
    69  	}
    70  	return list
    71  }
    72  
    73  // nodeTypeName converts NodeType to string.
    74  func nodeTypeName(nt NodeType) string { // TODO-Klaytn-Node Consolidate p2p.NodeType and common.ConnType
    75  	switch nt {
    76  	case NodeTypeCN:
    77  		return "CN"
    78  	case NodeTypePN:
    79  		return "PN"
    80  	case NodeTypeEN:
    81  		return "EN"
    82  	case NodeTypeBN:
    83  		return "BN"
    84  	default:
    85  		return "Unknown Node Type"
    86  	}
    87  }