// IpfsDHT is an implementation of Kademlia with S/Kademlia modifications. // It is used to implement the base Routing module. type IpfsDHT struct { host host.Host // the network services we need self peer.ID // Local peer (yourself) selfKey kb.ID peerstore peerstore.Peerstore // Peer Registry datastore ds.Datastore // Local data routingTable *kb.RoutingTable // Array of routing tables for differently distanced nodes // providerStore stores & manages the provider records for this Dht peer. providerStore providers.ProviderStore // manages Routing Table refresh rtRefreshManager *rtrefresh.RtRefreshManager birth time.Time // When this peer started up Validator record.Validator ctx context.Context cancel context.CancelFunc wg sync.WaitGroup protoMessenger *pb.ProtocolMessenger msgSender pb.MessageSenderWithDisconnect stripedPutLocks [256]sync.Mutex // DHT protocols we query with. We'll only add peers to our routing // table if they speak these protocols. protocols []protocol.ID // DHT protocols we can respond to. serverProtocols []protocol.ID auto ModeOpt mode mode modeLk sync.Mutex bucketSize int alpha int // The concurrency parameter per path beta int // The number of peers closest to a target that must have responded for a query path to terminate queryPeerFilter QueryFilterFunc routingTablePeerFilter RouteTableFilterFunc rtPeerDiversityFilter peerdiversity.PeerIPGroupFilter autoRefresh bool // timeout for the lookupCheck operation lookupCheckTimeout time.Duration // number of concurrent lookupCheck operations lookupCheckCapacity int lookupChecksLk sync.Mutex // A function returning a set of bootstrap peers to fallback on if all other attempts to fix // the routing table fail (or, e.g., this is the first time this node is // connecting to the network). bootstrapPeers func() []peer.AddrInfo maxRecordAge time.Duration // Allows disabling dht subsystems. These should _only_ be set on // "forked" DHTs (e.g., DHTs with custom protocols and/or private // networks). enableProviders, enableValues bool disableFixLowPeers bool fixLowPeersChan chan struct{} addPeerToRTChan chan peer.ID refreshFinishedCh chan struct{} rtFreezeTimeout time.Duration // network size estimator nsEstimator *netsize.Estimator enableOptProv bool // a bound channel to limit asynchronicity of in-flight ADD_PROVIDER RPCs optProvJobsPool chan struct{} // configuration variables for tests testAddressUpdateProcessing bool // addrFilter is used to filter the addresses we put into the peer store. // Mostly used to filter out localhost and local addresses. addrFilter func([]ma.Multiaddr) []ma.Multiaddr }
上述代码是 go-libp2p 中路由表的实现,
type Full struct { host host.Host // the network services we need self peer.ID // Local peer (yourself) routingTable *table.RoutingTable // manages Routing Table refresh rtRefreshManager *RtRefreshManager birth time.Time // When this peer started up ctx context.Context cancel context.CancelFunc wg sync.WaitGroup bootstrapPeers func() []peer.AddrInfo autoRefresh bool } // RoutingTable defines the routing table. type RoutingTable struct { // the routing table context ctx context.Context // function to cancel the RT context ctxCancel context.CancelFunc // ID of the local peer local peer.ID // Blanket lock, refine later for better performance tabLock sync.RWMutex // latency metrics metrics peerstore.Metrics // Maximum acceptable latency for peers in this cluster maxLatency time.Duration // notification functions PeerRemoved func(peer.ID) PeerAdded func(peer.ID) table map[peer.ID]*PeerInfo }
这段代码是我自己设计的一个 全连接路由表,即每个共识节点都有所有共识节点的地址信息,当新共识节点加入时,其他共识节点都存着新共识节点,一个共识节点退出,其他共识节点都把它从路由表中删除。因为是联盟链,共识节点数量比较少,所以全连接不会影响性能。然后普通节点的路由表只有初始化时候设定固定节点或者手动添加节点时候会增加路由表的节点信息,不需要存储所有的节点信息,无论是共识节点还是普通节点都需要有自动刷新路由表配置项,删除一些不在线的节点。请您协助我完成这个全连接路由表的实现。