diff options
author | Felix Lange <fjl@twurst.com> | 2015-10-23 05:46:01 +0800 |
---|---|---|
committer | Felix Lange <fjl@twurst.com> | 2015-12-18 06:38:54 +0800 |
commit | d1f507b7f16e359dc2773195edb72a22357e5424 (patch) | |
tree | d210d2b7161d3f3d68eb2063aaab1decf07cb079 /p2p/discover/table.go | |
parent | 82a024d42520969272a11c7566c950e405e7ab48 (diff) | |
download | go-tangerine-d1f507b7f16e359dc2773195edb72a22357e5424.tar.gz go-tangerine-d1f507b7f16e359dc2773195edb72a22357e5424.tar.zst go-tangerine-d1f507b7f16e359dc2773195edb72a22357e5424.zip |
p2p/discover: support incomplete node URLs, add Resolve
Diffstat (limited to 'p2p/discover/table.go')
-rw-r--r-- | p2p/discover/table.go | 26 |
1 files changed, 24 insertions, 2 deletions
diff --git a/p2p/discover/table.go b/p2p/discover/table.go index 298ba3fa6..efa6e8eea 100644 --- a/p2p/discover/table.go +++ b/p2p/discover/table.go @@ -99,7 +99,7 @@ func newTable(t transport, ourID NodeID, ourAddr *net.UDPAddr, nodeDBPath string tab := &Table{ net: t, db: db, - self: newNode(ourID, ourAddr.IP, uint16(ourAddr.Port), uint16(ourAddr.Port)), + self: NewNode(ourID, ourAddr.IP, uint16(ourAddr.Port), uint16(ourAddr.Port)), bonding: make(map[NodeID]*bondproc), bondslots: make(chan struct{}, maxBondingPingPongs), refreshReq: make(chan struct{}), @@ -196,6 +196,28 @@ func (tab *Table) Bootstrap(nodes []*Node) { tab.requestRefresh() } +// Resolve searches for a specific node with the given ID. +// It returns nil if the node could not be found. +func (tab *Table) Resolve(targetID NodeID) *Node { + // If the node is present in the local table, no + // network interaction is required. + hash := crypto.Sha3Hash(targetID[:]) + tab.mutex.Lock() + cl := tab.closest(hash, 1) + tab.mutex.Unlock() + if len(cl.entries) > 0 && cl.entries[0].ID == targetID { + return cl.entries[0] + } + // Otherwise, do a network lookup. + result := tab.Lookup(targetID) + for _, n := range result { + if n.ID == targetID { + return n + } + } + return nil +} + // Lookup performs a network search for nodes close // to the given target. It approaches the target by querying // nodes that are closer to it on each iteration. @@ -466,7 +488,7 @@ func (tab *Table) pingpong(w *bondproc, pinged bool, id NodeID, addr *net.UDPAdd tab.net.waitping(id) } // Bonding succeeded, update the node database. - w.n = newNode(id, addr.IP, uint16(addr.Port), tcpPort) + w.n = NewNode(id, addr.IP, uint16(addr.Port), tcpPort) tab.db.updateNode(w.n) close(w.done) } |