In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Today, I would like to talk to you about the principle and implementation of P2P Server in Ethernet Square, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.
The principle and implementation of P2P in Ethernet Square
The de-centralization of blockchain technology depends on the underlying networking technology, and the bottom layer of Taifang implements p2pServer, which can be divided into three layers.
The underlying routing table. Encapsulate kad routing, node data structure and computing records, node search, verification and other functions.
Middle-level peer abstraction, message open sending interface, server provides peer detection, initialization, event subscription, peer status query, start, stop and other functions
The peer,peerset in the top layer of Ethernet Square is re-encapsulated, and through the Run function of the protocol, when the peer is started in the middle layer, the peer is obtained, and finally the stable peer is intercepted through a loop and packaged for use in peerset.
Underlying routing table
The simplification problem here only discusses Node Discovery Protocol. This layer maintains one buckets bucket with a total of 17 buckets, each with 16 nodes and 10 replacement nodes. The distance between hash and localNode should be calculated before Node is put in. Then select a bucket according to the distance, and calculate the target and the examples of objects in each bucket one by one. Refer to the closest function in detail, which will be posted later.
The distance formula satisfies: F (x) = 256-8*n-map (x [n + 1] ^ y [n = 1]) Note: n is the same number of nodes map is a negative correlation mapping relation.
To put it simply, the more similar, the smaller the value. See Node.go 's logdist function for details. Here we need to know the algorithm Kademlia.
.├── database.go / / encapsulates node database related operations ├── node.go / / node data structure ├── ntp.go / / synchronization time ├── table.go / / routing table ├── udp.go / / network related operations
The most important of these is the table object. The table public methods are:
NewTable instance creation
Self local node acquisition
ReadRandomNodes randomly reads several nodes
Close shuts down
Resolve looks for a node around.
Lookup finds the neighboring nodes of a node
Analyze these methods one by one:
NewTable
1: generate object instance (get database client, LocalNode etc)
/ / If no node database was given, use an in-memory one db, err: = newNodeDB (nodeDBPath, Version, ourID) if err! = nil {return nil, err} tab: = & Table {net: t, db: db, self: NewNode (ourID, ourAddr.IP, uint16 (ourAddr.Port), uint16 (ourAddr.Port)), bonding: make (map [NodeID] * bondproc) Bondslots: make (chan struct {}, maxBondingPingPongs), refreshReq: make (chan chan struct {}), initDone: make (chan struct {}), closeReq: make (chan struct {}), closed: make (chan struct {}), rand: mrand.New (mrand.NewSource (0)), ips: netutil.DistinctNetSet {Subnet: tableSubnet, Limit: tableIPLimit},}
2: load the boot node and initialize the k bucket.
If err: = tab.setFallbackNodes (bootnodes); err! = nil {return nil, err} for I: = 0; I
< cap(tab.bondslots); i++ { tab.bondslots 0 }) if len(h.entries) < maxElems { h.entries = append(h.entries, n) } if ix == len(h.entries) { // farther away than all nodes we already have. // if there was room for it, the node is now the last element. } else { // slide existing entries down to make room // this will overwrite the entry we just appended. //近的靠前边 copy(h.entries[ix+1:], h.entries[ix:]) h.entries[ix] = n } }ReadRandomNodes 整体思路是先拷贝出来,再逐个桶的抽最上面的一个,剩下空桶移除,剩下的桶合并后,下一轮再抽桶的第一个节点,直到填满给定数据或者桶全部空掉。最后返回填到数组里面的数量。 // ReadRandomNodes fills the given slice with random nodes from the // table. It will not write the same node more than once. The nodes in // the slice are copies and can be modified by the caller. func (tab *Table) ReadRandomNodes(buf []*Node) (n int) { if !tab.isInitDone() { return 0 } tab.mutex.Lock() defer tab.mutex.Unlock() // Find all non-empty buckets and get a fresh slice of their entries. var buckets [][]*Node //拷贝节点 for _, b := range tab.buckets { if len(b.entries) >0 {buckets = append (buckets, b.entries [:])}} if len (buckets) = 0 {return 0} / / Shuffle the buckets. For I: = len (buckets)-1; I > 0; iMurt-{j: = tab.rand.Intn (len (buckets)) buckets [I], buckets [j] = buckets [j], buckets [I]} / / Move head of each bucket into buf, removing buckets that become empty. Var I, j int for; I
< len(buf); i, j = i+1, (j+1)%len(buckets) { b := buckets[j] buf[i] = &(*b[0]) //取第一个节点 buckets[j] = b[1:] //移除第一个 if len(b) == 1 { //空桶移除 buckets = append(buckets[:j], buckets[j+1:]...) } if len(buckets) == 0 { break } } return i + 1 }Lookup lookup会要求已知节点查找邻居节点,查找的邻居节点又递归的找它周边的节点 for { // ask the alpha closest nodes that we haven't asked yet for i := 0; i < len(result.entries) && pendingQueries < alpha; i++ { n := result.entries[i] if !asked[n.ID] { asked[n.ID] = true pendingQueries++ go func() { // Find potential neighbors to bond with r, err := tab.net.findnode(n.ID, n.addr(), targetID) if err != nil { // Bump the failure counter to detect and evacuate non-bonded entries fails := tab.db.findFails(n.ID) + 1 tab.db.updateFindFails(n.ID, fails) log.Trace("Bumping findnode failure counter", "id", n.ID, "failcount", fails) if fails >= maxFindnodeFailures {log.Trace ("Too many findnode failures, dropping", "id", n.ID, "failcount", fails) tab.delete (n)} reply
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.