fix: rpc routing table info

This commit is contained in:
fearlessfe 2024-02-08 14:00:44 +08:00 committed by Chen Kai
parent b85b1b751f
commit 5e657f8423
3 changed files with 32 additions and 16 deletions

View file

@ -27,8 +27,8 @@ type NodeInfo struct {
}
type RoutingTableInfo struct {
Buckets []string `json:"buckets"`
LocalNodeId string `json:"localNodeId"`
Buckets [][]string `json:"buckets"`
LocalNodeId string `json:"localNodeId"`
}
type DiscV5PongResp struct {
@ -64,14 +64,8 @@ func (d *DiscV5API) NodeInfo() *NodeInfo {
func (d *DiscV5API) RoutingTableInfo() *RoutingTableInfo {
n := d.DiscV5.LocalNode().Node()
closestNodes := d.DiscV5.AllNodes()
buckets := make([]string, len(closestNodes))
for _, e := range closestNodes {
buckets = append(buckets, e.ID().String())
}
return &RoutingTableInfo{
Buckets: buckets,
Buckets: d.DiscV5.RoutingTableInfo(),
LocalNodeId: n.ID().String(),
}
}
@ -219,14 +213,8 @@ func (p *PortalAPI) NodeInfo() *NodeInfo {
func (p *PortalAPI) HistoryRoutingTableInfo() *RoutingTableInfo {
n := p.portalProtocol.localNode.Node()
closestNodes := p.portalProtocol.table.Nodes()
buckets := make([]string, len(closestNodes))
for _, e := range closestNodes {
buckets = append(buckets, e.ID().String())
}
return &RoutingTableInfo{
Buckets: buckets,
Buckets: p.portalProtocol.RoutingTableInfo(),
LocalNodeId: n.ID().String(),
}
}

View file

@ -260,6 +260,20 @@ func (p *PortalProtocol) Stop() {
p.log.Error("failed to close utp listener", "err", err)
}
}
func (p *PortalProtocol) RoutingTableInfo() [][]string {
p.table.mutex.Lock()
defer p.table.mutex.Unlock()
nodes := make([][]string, 0)
for _, b := range &p.table.buckets {
bucketNodes := make([]string, 0)
for _, n := range b.entries {
bucketNodes = append(bucketNodes, unwrapNode(n).ID().String())
}
nodes = append(nodes, bucketNodes)
}
p.log.Trace("rouingTableInfo nodes:", nodes)
return nodes
}
func (p *PortalProtocol) setupUDPListening() (*net.UDPConn, error) {
listenAddr := p.ListenAddr

View file

@ -268,6 +268,20 @@ func (t *UDPv5) AllNodes() []*enode.Node {
return nodes
}
func (t *UDPv5) RoutingTableInfo() [][]string {
t.tab.mutex.Lock()
defer t.tab.mutex.Unlock()
nodes := make([][]string, 0)
for _, b := range &t.tab.buckets {
bucketNodes := make([]string, 0)
for _, n := range b.entries {
bucketNodes = append(bucketNodes, unwrapNode(n).ID().String())
}
nodes = append(nodes, bucketNodes)
}
return nodes
}
// LocalNode returns the current local Node running the
// protocol.
func (t *UDPv5) LocalNode() *enode.LocalNode {