Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed PeerStore usage. #4706

Merged
merged 1 commit into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions node/node_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,16 +421,17 @@ func (node *Node) BootstrapConsensus() error {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
min := node.Consensus.MinPeers
enoughMinPeers := make(chan struct{})
enoughMinPeers := make(chan struct{}, 1)
GheisMohammadi marked this conversation as resolved.
Show resolved Hide resolved
const checkEvery = 3 * time.Second
go func() {
for {
<-time.After(checkEvery)
numPeersNow := node.host.GetPeerCount()
if numPeersNow >= min {
connectedPeers := len(node.host.Network().Peers())
if connectedPeers >= min {
utils.Logger().Info().Msg("[bootstrap] StartConsensus")
enoughMinPeers <- struct{}{}
fmt.Println("Bootstrap consensus done.", numPeersNow, " peers are connected")
fmt.Printf("Bootstrap consensus done. Connected %d, known %d, shard: %d\n", connectedPeers, numPeersNow, node.Consensus.ShardID)
return
}
utils.Logger().Info().
Expand Down
19 changes: 8 additions & 11 deletions p2p/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type Host interface {
GetID() libp2p_peer.ID
GetP2PHost() libp2p_host.Host
GetDiscovery() discovery.Discovery
Network() libp2p_network.Network
GetPeerCount() int
ConnectHostPeer(Peer) error
// AddStreamProtocol add the given protocol
Expand Down Expand Up @@ -443,17 +444,9 @@ func (host *HostV2) Close() error {

// PeerConnectivity returns total number of known, connected and not connected peers.
func (host *HostV2) PeerConnectivity() (int, int, int) {
connected, not := 0, 0
peers := host.h.Peerstore().Peers()
for _, peer := range peers {
result := host.h.Network().Connectedness(peer)
if result == libp2p_network.Connected {
connected++
} else if result == libp2p_network.NotConnected {
not++
}
}
return len(peers), connected, not
GheisMohammadi marked this conversation as resolved.
Show resolved Hide resolved
known := len(host.h.Peerstore().Peers())
connected := len(host.h.Network().Peers())
return known, connected, known - connected
}

// AddStreamProtocol adds the stream protocols to the host to be started and closed
Expand Down Expand Up @@ -589,6 +582,10 @@ func (host *HostV2) GetPeerCount() int {
return host.h.Peerstore().Peers().Len()
}

func (host *HostV2) Network() libp2p_network.Network {
return host.h.Network()
}

// ConnectHostPeer connects to peer host
func (host *HostV2) ConnectHostPeer(peer Peer) error {
ctx := context.Background()
Expand Down