From de118dff7e83a304c0f6c6c88cc8af63293fc0a6 Mon Sep 17 00:00:00 2001 From: Sabyasachi Patra Date: Thu, 7 Oct 2021 12:01:24 +0530 Subject: [PATCH] dont engage d2d discovery if nodes already connected (#20) * dont engage d2d discovery if nodes already connected * add some matrix tests * version update --- .version | 4 +-- CHANGELOG.md | 10 ++++++ internal/matrix/matrix.go | 4 +++ internal/matrix/matrix_test.go | 64 ++++++++++++++++++++++++++++++++++ mobile/discovery.go | 14 ++++++++ 5 files changed, 94 insertions(+), 2 deletions(-) diff --git a/.version b/.version index 6f07398..b07a433 100644 --- a/.version +++ b/.version @@ -1,2 +1,2 @@ -mobile=0.0.11 -cli=0.0.2 \ No newline at end of file +mobile=0.0.12 +cli=0.0.3 \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 372e2e6..d63bfcd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,16 @@ # Change Log All notable changes to this project will be documented in this file. +## ## [v0.0.12] - 2021-10-07 + +### Added + +- [655b263](https://github.com/datahop/ipfs-lite/commit/655b263) Auto Disconnect after content replication for D2D discovery + +### Changed + +- [84b7b42](https://github.com/datahop/ipfs-lite/commit/84b7b42) Do not initiate D2D Discovery in nodes are pre-connected + ## [v0.0.11] - 2021-09-22 ### Added diff --git a/internal/matrix/matrix.go b/internal/matrix/matrix.go index a48b6dc..96117a0 100644 --- a/internal/matrix/matrix.go +++ b/internal/matrix/matrix.go @@ -276,6 +276,10 @@ func (mKeeper *MatrixKeeper) NodeConnectionFailed(address string) { nodeMatrix.ConnectionFailureCount++ nodeMatrix.BLEDiscoveredAt = 0 + nodeMatrix.WifiConnectedAt = 0 + nodeMatrix.RSSI = 0 + nodeMatrix.Speed = 0 + nodeMatrix.Frequency = 0 } // NodeDisconnected updates connectivity disconnection time info with another node diff --git a/internal/matrix/matrix_test.go b/internal/matrix/matrix_test.go index 9572bd3..68042dc 100644 --- a/internal/matrix/matrix_test.go +++ b/internal/matrix/matrix_test.go @@ -9,6 +9,7 @@ import ( ds "github.com/ipfs/go-datastore" syncds "github.com/ipfs/go-datastore/sync" leveldb "github.com/ipfs/go-ds-leveldb" + "github.com/libp2p/go-libp2p-core/peer" ) var root = filepath.Join("./test", "root1") @@ -117,6 +118,69 @@ func TestMatrixKeeperFlushWithData(t *testing.T) { } } +func TestMatrixKeeperNodeMatrixOps(t *testing.T) { + <-time.After(time.Second) + defer removeRepo(t) + mKeeper := NewMatrixKeeper(initDatastore(t)) + if mKeeper.NodeMatrix == nil { + t.Fatal("NodeMatrix keeper should not be null") + } + defer mKeeper.db.Close() + address := "discoveredNodeOne" + mKeeper.BLEDiscovered(address) + if mKeeper.GetNodeStat(address).BLEDiscoveredAt == 0 { + t.Fatal("BLEDiscoveredAt not updated") + } + mKeeper.WifiConnected(address, 5, 5, 5) + if mKeeper.GetNodeStat(address).WifiConnectedAt == 0 { + t.Fatal("WifiConnectedAt not updated") + } + mKeeper.NodeConnectionFailed(address) + if mKeeper.GetNodeStat(address).ConnectionFailureCount != 1 { + t.Fatal("ConnectionFailureCount not updated") + } + + mKeeper.BLEDiscovered(address) + mKeeper.WifiConnected(address, 5, 5, 5) + mKeeper.NodeConnected(address) + if mKeeper.GetNodeStat(address).ConnectionSuccessCount != 1 { + t.Fatal("ConnectionFailureCount not updated") + } + + mKeeper.NodeDisconnected(address) + if len(mKeeper.GetNodeStat(address).ConnectionHistory) != 1 { + t.Fatal("ConnectionHistory not updated") + } +} + +func TestMatrixKeeperContentMatrixOps(t *testing.T) { + <-time.After(time.Second) + defer removeRepo(t) + mKeeper := NewMatrixKeeper(initDatastore(t)) + if mKeeper.NodeMatrix == nil { + t.Fatal("NodeMatrix keeper should not be null") + } + defer mKeeper.db.Close() + hash := "Hash" + address := "discoveredNodeOne" + tag := "Tag" + var size int64 = 256 + mKeeper.ContentDownloadStarted(tag, hash, size) + cs := mKeeper.GetContentStat(hash) + if cs.Tag != tag && cs.DownloadStartedAt == 0 { + t.Fatal("ContentDownloadStart failed") + } + mKeeper.ContentDownloadFinished(hash) + cs = mKeeper.GetContentStat(hash) + if cs.AvgSpeed == 0 && cs.DownloadFinishedAt == 0 { + t.Fatal("ContentDownloadFinished failed") + } + mKeeper.ContentAddProvider(hash, peer.ID(address)) + if len(mKeeper.GetContentStat(hash).ProvidedBy) != 1 { + t.Fatal("ContentAddProvider not updated") + } +} + func TestMatrixKeeperTicker(t *testing.T) { <-time.After(time.Second) defer removeRepo(t) diff --git a/mobile/discovery.go b/mobile/discovery.go index 848ba58..36ac22c 100644 --- a/mobile/discovery.go +++ b/mobile/discovery.go @@ -7,6 +7,7 @@ import ( "sync" "time" + p2pnet "github.com/libp2p/go-libp2p-core/network" "github.com/libp2p/go-libp2p-core/peer" pubsub "github.com/libp2p/go-libp2p-pubsub" ) @@ -188,6 +189,13 @@ func (b *discoveryService) DiscoveryPeerDifferentStatus(device string, topic str log.Errorf("failed parsing peerinfo %s", err.Error()) return } + + conn := hop.peer.Host.Network().Connectedness(peerInfo.ID) + if conn == p2pnet.Connected { + log.Debug("Peer already connected") + return + } + hop.peer.Repo.Matrix().BLEDiscovered(peerInfo.ID.String()) hop.wifiCon.Connect(network, pass, "192.168.49.2", peerInfo.ID.String()) b.handleConnectionRequest = func() { @@ -204,6 +212,12 @@ func (b *discoveryService) AdvertiserPeerDifferentStatus(topic string, value []b log.Debugf("advertising new peer device different status : %s", string(value)) log.Debugf("peerinfo: %s", id) + conn := hop.peer.Host.Network().Connectedness(peer.ID(id)) + if conn == p2pnet.Connected { + log.Debug("Peer already connected") + return + } + hop.peer.Repo.Matrix().BLEDiscovered(id) b.discovery.Stop() b.wifiHS.Start()