Skip to content

Commit

Permalink
fix: fix test error
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-bisonai committed Feb 23, 2024
1 parent 78ab4f3 commit b4e5277
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
6 changes: 5 additions & 1 deletion node/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ func main() {
}

log.Debug().Msg("establishing connection")
go libp2p.DiscoverPeers(context.Background(), h, discoverString, "")
go func() {
if err := libp2p.DiscoverPeers(context.Background(), h, discoverString, ""); err != nil {
log.Error().Err(err).Msg("Error from DiscoverPeers")
}
}()

aggregator, err := aggregator.NewAggregator(h, ps, "orakl-aggregator-2024-gazuaa")
if err != nil {
Expand Down
13 changes: 9 additions & 4 deletions node/pkg/libp2p/libp2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package libp2p
import (
"context"
"crypto/rand"
"errors"
"fmt"

"strings"
Expand Down Expand Up @@ -92,14 +93,14 @@ func initDHT(ctx context.Context, h host.Host, bootstrap string) *dht.IpfsDHT {
for _, peerAddr := range bootstrapPeers {
peerinfo, err := peer.AddrInfoFromP2pAddr(peerAddr)
if err != nil {
log.Info().Err(err).Msg("Error getting AddrInfo from p2p address")
log.Debug().Err(err).Msg("Error getting AddrInfo from p2p address")
continue
}
wg.Add(1)
go func() {
defer wg.Done()
if err := h.Connect(ctx, *peerinfo); err != nil {
log.Info().Err(err).Msg("Bootstrap warning")
log.Debug().Err(err).Msg("Bootstrap warning")
}
}()
}
Expand All @@ -108,7 +109,7 @@ func initDHT(ctx context.Context, h host.Host, bootstrap string) *dht.IpfsDHT {
return kademliaDHT
}

func DiscoverPeers(ctx context.Context, h host.Host, topicName string, bootstrap string) {
func DiscoverPeers(ctx context.Context, h host.Host, topicName string, bootstrap string) error {
kademliaDHT := initDHT(ctx, h, bootstrap)
routingDiscovery := drouting.NewRoutingDiscovery(kademliaDHT)
dutil.Advertise(ctx, routingDiscovery, topicName)
Expand All @@ -120,7 +121,7 @@ func DiscoverPeers(ctx context.Context, h host.Host, topicName string, bootstrap
log.Debug().Msg("Searching for peers...")
peerChan, err := routingDiscovery.FindPeers(ctx, topicName)
if err != nil {
panic(err)
return err
}
for p := range peerChan {
if p.ID == h.ID() {
Expand All @@ -141,5 +142,9 @@ func DiscoverPeers(ctx context.Context, h host.Host, topicName string, bootstrap
}
}
wg.Wait()
if !anyConnected {
return errors.New("no peers connected")
}
log.Debug().Msg("Peer discovery complete")
return nil
}
21 changes: 7 additions & 14 deletions node/pkg/libp2p/libp2p_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
//nolint:all
package libp2p

import (
"bytes"
"context"
"log"
"os"
"strings"
"testing"

"github.com/rs/zerolog"
)

func TestMakeHost(t *testing.T) {
Expand Down Expand Up @@ -38,23 +37,17 @@ func TestInitDHT(t *testing.T) {
}

func TestDiscoverPeers(t *testing.T) {
var buf bytes.Buffer
log.SetOutput(&buf)
defer func() {
log.SetOutput(os.Stderr)
}()

zerolog.SetGlobalLevel(zerolog.InfoLevel)
h1, _ := MakeHost(10001)
h2, _ := MakeHost(10002)

defer h1.Close()
defer h2.Close()

go DiscoverPeers(context.Background(), h1, "test-discover-peers", h2.Addrs()[0].String())
DiscoverPeers(context.Background(), h2, "test-discover-peers", h1.Addrs()[0].String())
err := DiscoverPeers(context.Background(), h2, "test-discover-peers", h1.Addrs()[0].String())

str := buf.String()
if !strings.Contains(str, "Peer discovery complete") {
t.Errorf("Expected 'Peer discovery complete' but it was not found")
if err != nil {
t.Errorf("Failed to discover peers: %v", err)
}
}

0 comments on commit b4e5277

Please sign in to comment.