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

dkg: fix wait peer issue #671

Merged
merged 3 commits into from
Jun 7, 2022
Merged
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
20 changes: 14 additions & 6 deletions dkg/dkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ type Config struct {

// Run executes a dkg ceremony and writes secret share keystore and cluster lock files as output to disk.
func Run(ctx context.Context, conf Config) (err error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()

ctx = log.WithTopic(ctx, "dkg")
defer func() {
if err != nil {
Expand Down Expand Up @@ -128,7 +131,7 @@ func Run(ctx context.Context, conf Config) (err error) {

log.Info(ctx, "Connecting to peers...")

ctx, cancel, err := waitPeers(ctx, tcpNode, peers)
ctx, cancel, err = waitPeers(ctx, tcpNode, peers)
if err != nil {
return err
}
Expand Down Expand Up @@ -541,12 +544,20 @@ func waitPeers(ctx context.Context, tcpNode host.Host, peers []p2p.Peer) (contex
tuples <- tuple{Peer: pID, RTT: rtt}

// Wait for disconnect and cancel the context.
var err error
for result := range results {
if result.Error != nil {
log.Error(ctx, "Peer connection lost", result.Error, z.Str("peer", p2p.PeerName(pID)))
cancel()
err = result.Error
break
}
}

if ctx.Err() == nil {
log.Error(ctx, "Peer connection lost", err, z.Str("peer", p2p.PeerName(pID)))
cancel()
}

return
}
}(p.ID)
}
Expand All @@ -571,9 +582,6 @@ func waitPeers(ctx context.Context, tcpNode host.Host, peers []p2p.Peer) (contex

// waitConnect blocks until a libp2p connection (ping) is established returning the ping result chan, with the peer or the context is cancelled.
func waitConnect(ctx context.Context, tcpNode host.Host, p peer.ID) (<-chan ping.Result, time.Duration, bool) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()

resp := ping.Ping(ctx, tcpNode, p)
for result := range resp {
if result.Error == nil {
Expand Down