github.com/klaytn/klaytn@v1.12.1/networks/p2p/discover/discover_api.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 "errors" 21 ) 22 23 func (tab *Table) Name() string { return "TableDiscovery" } 24 25 // CreateUpdateNodeOnDB inserts - potentially overwriting - a node into the peer database. 26 func (tab *Table) CreateUpdateNodeOnDB(n *Node) error { 27 return tab.db.updateNode(n) 28 } 29 30 // CreateUpdateNodeOnDB inserts - potentially overwriting - a node into the associated storage. 31 func (tab *Table) CreateUpdateNodeOnTable(n *Node) error { 32 tab.add(n) 33 return nil 34 } 35 36 // GetNodeFromDB returns a node which has id in peer database. 37 func (tab *Table) GetNodeFromDB(id NodeID) (*Node, error) { 38 node := tab.db.node(id) 39 if node == nil { 40 return nil, errors.New("failed to retrieve the node with the given id") 41 } 42 return node, nil 43 } 44 45 // GetBucketEntries returns nodes in peer databases. 46 func (tab *Table) GetBucketEntries() []*Node { 47 var ret []*Node 48 tab.storagesMu.RLock() 49 defer tab.storagesMu.RUnlock() 50 for _, bs := range tab.storages { 51 ret = append(ret, bs.getBucketEntries()...) 52 } 53 return ret 54 } 55 56 // GetBucketEntries returns nodes which place in replacements in peer databases. 57 func (tab *Table) GetReplacements() []*Node { 58 var ret []*Node 59 tab.storagesMu.RLock() 60 defer tab.storagesMu.RUnlock() 61 for _, bs := range tab.storages { 62 if _, ok := bs.(*KademliaStorage); ok { // TODO-Klaytn-Node Are there any change SimpleStorage use this method? 63 ret = append(ret, bs.(*KademliaStorage).getReplacements()...) 64 } 65 } 66 return ret 67 } 68 69 // DeleteNodeFromDB deletes node which has id in peer database. 70 func (tab *Table) DeleteNodeFromDB(n *Node) error { 71 return tab.db.deleteNode(n.ID) 72 } 73 74 // DeleteNodeFromDB deletes node which has id in the associated table. 75 func (tab *Table) DeleteNodeFromTable(n *Node) error { 76 tab.storagesMu.RLock() 77 defer tab.storagesMu.RUnlock() 78 tab.storages[n.NType].delete(n) 79 return nil 80 } 81 82 func (tab *Table) GetAuthorizedNodes() []*Node { 83 tab.storagesMu.RLock() 84 defer tab.storagesMu.RUnlock() 85 var ret []*Node 86 for _, storage := range tab.storages { 87 ret = append(ret, storage.getAuthorizedNodes()...) 88 } 89 return ret 90 } 91 92 func (tab *Table) PutAuthorizedNodes(nodes []*Node) { 93 tab.storagesMu.RLock() 94 defer tab.storagesMu.RUnlock() 95 for _, node := range nodes { 96 if tab.storages[node.NType] != nil { 97 tab.storages[node.NType].putAuthorizedNode(node) 98 } 99 } 100 } 101 102 func (tab *Table) DeleteAuthorizedNodes(nodes []*Node) { 103 tab.storagesMu.RLock() 104 defer tab.storagesMu.RUnlock() 105 for _, node := range nodes { 106 if tab.storages[node.NType] != nil { 107 tab.storages[node.NType].deleteAuthorizedNode(node.ID) 108 } 109 } 110 }