From 1b741e6f9ad3100976b67f9f1b095741fe88b74d Mon Sep 17 00:00:00 2001 From: Richard Schneider Date: Wed, 21 Aug 2019 12:49:57 +1200 Subject: [PATCH] fix(Swarm): rename RegisterPeerAsync to RegisterPeerAddress --- src/Swarm.cs | 33 ++++++++-------------------- test/AutoDialerTest.cs | 8 +++---- test/Routing/Dht1Test.cs | 32 ++++++++++++++-------------- test/SwarmTest.cs | 46 ++++++++++++++++++++-------------------- 4 files changed, 52 insertions(+), 67 deletions(-) diff --git a/src/Swarm.cs b/src/Swarm.cs index 1d9ee53..76be904 100644 --- a/src/Swarm.cs +++ b/src/Swarm.cs @@ -180,9 +180,9 @@ public Peer LocalPeer /// /// /// Contains any peer address that has been - /// discovered. + /// discovered. /// - /// + /// public IEnumerable KnownPeerAddresses { get @@ -198,9 +198,9 @@ public IEnumerable KnownPeerAddresses /// /// /// Contains any peer that has been - /// discovered. + /// discovered. /// - /// + /// public IEnumerable KnownPeers { get @@ -215,12 +215,8 @@ public IEnumerable KnownPeers /// /// An address to the peer. It must end with the peer ID. /// - /// - /// Is used to stop the task. When cancelled, the is raised. - /// /// - /// A task that represents the asynchronous operation. The task's result - /// is the that is registered. + /// The that is registered. /// /// /// The or policies forbid it. @@ -231,26 +227,15 @@ public IEnumerable KnownPeers /// added to the . /// /// - public Task RegisterPeerAsync(MultiAddress address, CancellationToken cancel = default(CancellationToken)) + public Peer RegisterPeerAddress(MultiAddress address) { - var peerId = address.PeerId; - if (peerId == LocalPeer.Id) - { - throw new Exception("Cannot register to self."); - } - - if (!IsAllowed(address)) - { - throw new Exception($"Communication with '{address}' is not allowed."); - } - var peer = new Peer { - Id = peerId, + Id = address.PeerId, Addresses = new List { address } }; - return Task.FromResult(RegisterPeer(peer)); + return RegisterPeer(peer); } /// @@ -468,7 +453,7 @@ public async Task StopAsync() /// public async Task ConnectAsync(MultiAddress address, CancellationToken cancel = default(CancellationToken)) { - var peer = await RegisterPeerAsync(address, cancel).ConfigureAwait(false); + var peer = RegisterPeerAddress(address); return await ConnectAsync(peer, cancel).ConfigureAwait(false); } diff --git a/test/AutoDialerTest.cs b/test/AutoDialerTest.cs index 6fdec7a..72f4d99 100644 --- a/test/AutoDialerTest.cs +++ b/test/AutoDialerTest.cs @@ -56,7 +56,7 @@ public async Task Connects_OnPeerDiscovered_When_Below_MinConnections() { using (var dialer = new AutoDialer(swarmA)) { - var other = await swarmA.RegisterPeerAsync(peerBAddress); + var other = swarmA.RegisterPeerAddress(peerBAddress); // wait for the connection. var endTime = DateTime.Now.AddSeconds(3); @@ -90,7 +90,7 @@ public async Task Noop_OnPeerDiscovered_When_NotBelow_MinConnections() { using (var dialer = new AutoDialer(swarmA) { MinConnections = 0 }) { - var other = await swarmA.RegisterPeerAsync(peerBAddress); + var other = swarmA.RegisterPeerAddress(peerBAddress); // wait for the connection. var endTime = DateTime.Now.AddSeconds(3); @@ -136,8 +136,8 @@ public async Task Connects_OnPeerDisconnected_When_Below_MinConnections() { using (var dialer = new AutoDialer(swarmA) { MinConnections = 1 }) { - var b = await swarmA.RegisterPeerAsync(peerBAddress); - var c = await swarmA.RegisterPeerAsync(peerCAddress); + var b = swarmA.RegisterPeerAddress(peerBAddress); + var c = swarmA.RegisterPeerAddress(peerCAddress); // wait for the peer B connection. var endTime = DateTime.Now.AddSeconds(3); diff --git a/test/Routing/Dht1Test.cs b/test/Routing/Dht1Test.cs index 82dc1fa..8f88ea0 100644 --- a/test/Routing/Dht1Test.cs +++ b/test/Routing/Dht1Test.cs @@ -44,7 +44,7 @@ public async Task StoppedEventRaised() public async Task SeedsRoutingTableFromSwarm() { var swarm = new Swarm { LocalPeer = self }; - var peer = await swarm.RegisterPeerAsync("/ip4/127.0.0.1/tcp/4001/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1h"); + var peer = swarm.RegisterPeerAddress("/ip4/127.0.0.1/tcp/4001/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1h"); var dht = new Dht1 { Swarm = swarm }; await dht.StartAsync(); try @@ -65,7 +65,7 @@ public async Task AddDiscoveredPeerToRoutingTable() await dht.StartAsync(); try { - var peer = await swarm.RegisterPeerAsync("/ip4/127.0.0.1/tcp/4001/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1h"); + var peer = swarm.RegisterPeerAddress("/ip4/127.0.0.1/tcp/4001/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1h"); Assert.IsTrue(dht.RoutingTable.Contains(peer)); } finally @@ -82,7 +82,7 @@ public async Task RemovesPeerFromRoutingTable() await dht.StartAsync(); try { - var peer = await swarm.RegisterPeerAsync("/ip4/127.0.0.1/tcp/4001/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1h"); + var peer = swarm.RegisterPeerAddress("/ip4/127.0.0.1/tcp/4001/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1h"); Assert.IsTrue(dht.RoutingTable.Contains(peer)); swarm.DeregisterPeer(peer); @@ -151,7 +151,7 @@ public async Task ProcessFindNodeMessage_InRoutingTable() public async Task ProcessFindNodeMessage_InSwarm() { var swarm = new Swarm { LocalPeer = self }; - var other = await swarm.RegisterPeerAsync("/ip4/127.0.0.1/tcp/4001/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1h"); + var other = swarm.RegisterPeerAddress("/ip4/127.0.0.1/tcp/4001/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1h"); var dht = new Dht1 { Swarm = swarm }; await dht.StartAsync(); try @@ -181,11 +181,11 @@ public async Task ProcessFindNodeMessage_InSwarm() public async Task ProcessFindNodeMessage_Closest() { var swarm = new Swarm { LocalPeer = self }; - await swarm.RegisterPeerAsync("/ip4/127.0.0.1/tcp/4001/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1a"); - await swarm.RegisterPeerAsync("/ip4/127.0.0.2/tcp/4001/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1b"); - await swarm.RegisterPeerAsync("/ip4/127.0.0.3/tcp/4001/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1c"); - await swarm.RegisterPeerAsync("/ip4/127.0.0.4/tcp/4001/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1d"); - await swarm.RegisterPeerAsync("/ip4/127.0.0.5/tcp/4001/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1e"); + swarm.RegisterPeerAddress("/ip4/127.0.0.1/tcp/4001/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1a"); + swarm.RegisterPeerAddress("/ip4/127.0.0.2/tcp/4001/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1b"); + swarm.RegisterPeerAddress("/ip4/127.0.0.3/tcp/4001/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1c"); + swarm.RegisterPeerAddress("/ip4/127.0.0.4/tcp/4001/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1d"); + swarm.RegisterPeerAddress("/ip4/127.0.0.5/tcp/4001/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1e"); var dht = new Dht1 { Swarm = swarm, CloserPeerCount = 3 }; await dht.StartAsync(); try @@ -209,11 +209,11 @@ public async Task ProcessFindNodeMessage_Closest() public async Task ProcessFindNodeMessage_BadNodeId() { var swarm = new Swarm { LocalPeer = self }; - await swarm.RegisterPeerAsync("/ip4/127.0.0.1/tcp/4001/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1a"); - await swarm.RegisterPeerAsync("/ip4/127.0.0.2/tcp/4001/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1b"); - await swarm.RegisterPeerAsync("/ip4/127.0.0.3/tcp/4001/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1c"); - await swarm.RegisterPeerAsync("/ip4/127.0.0.4/tcp/4001/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1d"); - await swarm.RegisterPeerAsync("/ip4/127.0.0.5/tcp/4001/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1e"); + swarm.RegisterPeerAddress("/ip4/127.0.0.1/tcp/4001/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1a"); + swarm.RegisterPeerAddress("/ip4/127.0.0.2/tcp/4001/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1b"); + swarm.RegisterPeerAddress("/ip4/127.0.0.3/tcp/4001/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1c"); + swarm.RegisterPeerAddress("/ip4/127.0.0.4/tcp/4001/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1d"); + swarm.RegisterPeerAddress("/ip4/127.0.0.5/tcp/4001/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1e"); var dht = new Dht1 { Swarm = swarm, CloserPeerCount = 3 }; await dht.StartAsync(); try @@ -349,8 +349,8 @@ public async Task QueryIsCancelled_WhenDhtStops() { var unknownPeer = new MultiHash("QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCxxx"); var swarm = new Swarm { LocalPeer = self }; - await swarm.RegisterPeerAsync("/ip4/178.62.158.247/tcp/4001/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd"); - await swarm.RegisterPeerAsync("/ip4/104.236.76.40/tcp/4001/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64"); + swarm.RegisterPeerAddress("/ip4/178.62.158.247/tcp/4001/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd"); + swarm.RegisterPeerAddress("/ip4/104.236.76.40/tcp/4001/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64"); var dht = new Dht1 { Swarm = swarm }; await dht.StartAsync(); var task = dht.FindPeerAsync(unknownPeer); diff --git a/test/SwarmTest.cs b/test/SwarmTest.cs index 730f9e7..34c7524 100644 --- a/test/SwarmTest.cs +++ b/test/SwarmTest.cs @@ -48,10 +48,10 @@ public void Start_NoLocalPeer() } [TestMethod] - public async Task NewPeerAddress() + public void NewPeerAddress() { var swarm = new Swarm { LocalPeer = self }; - await swarm.RegisterPeerAsync(mars); + swarm.RegisterPeerAddress(mars); Assert.IsTrue(swarm.KnownPeerAddresses.Contains(mars)); } @@ -62,13 +62,13 @@ public void NewPeerAddress_Self() var selfAddress = "/ip4/178.62.158.247/tcp/4001/ipfs/" + self.Id; ExceptionAssert.Throws(() => { - var _ = swarm.RegisterPeerAsync(selfAddress).Result; + var _ = swarm.RegisterPeerAddress(selfAddress); }); selfAddress = "/ip4/178.62.158.247/tcp/4001/p2p/" + self.Id; ExceptionAssert.Throws(() => { - var _ = swarm.RegisterPeerAsync(selfAddress).Result; + var _ = swarm.RegisterPeerAddress(selfAddress); }); } @@ -80,11 +80,11 @@ public void NewPeerAddress_BlackList() ExceptionAssert.Throws(() => { - var _ = swarm.RegisterPeerAsync(mars).Result; + var _ = swarm.RegisterPeerAddress(mars); }); Assert.IsFalse(swarm.KnownPeerAddresses.Contains(mars)); - Assert.IsNotNull(swarm.RegisterPeerAsync(venus).Result); + Assert.IsNotNull(swarm.RegisterPeerAddress(venus)); Assert.IsTrue(swarm.KnownPeerAddresses.Contains(venus)); } @@ -96,11 +96,11 @@ public void NewPeerAddress_WhiteList() ExceptionAssert.Throws(() => { - var _ = swarm.RegisterPeerAsync(mars).Result; + var _ = swarm.RegisterPeerAddress(mars); }); Assert.IsFalse(swarm.KnownPeerAddresses.Contains(mars)); - Assert.IsNotNull(swarm.RegisterPeerAsync(venus).Result); + Assert.IsNotNull(swarm.RegisterPeerAddress(venus)); Assert.IsTrue(swarm.KnownPeerAddresses.Contains(venus)); } @@ -110,38 +110,38 @@ public void NewPeerAddress_InvalidAddress_MissingPeerId() var swarm = new Swarm { LocalPeer = self }; ExceptionAssert.Throws(() => { - var _ = swarm.RegisterPeerAsync("/ip4/10.1.10.10/tcp/29087").Result; + var _ = swarm.RegisterPeerAddress("/ip4/10.1.10.10/tcp/29087"); }); Assert.AreEqual(0, swarm.KnownPeerAddresses.Count()); } [TestMethod] - public async Task NewPeerAddress_Duplicate() + public void NewPeerAddress_Duplicate() { var swarm = new Swarm { LocalPeer = self }; - await swarm.RegisterPeerAsync(mars); + swarm.RegisterPeerAddress(mars); Assert.AreEqual(1, swarm.KnownPeerAddresses.Count()); - await swarm.RegisterPeerAsync(mars); + swarm.RegisterPeerAddress(mars); Assert.AreEqual(1, swarm.KnownPeerAddresses.Count()); } [TestMethod] - public async Task KnownPeers() + public void KnownPeers() { var swarm = new Swarm { LocalPeer = self }; Assert.AreEqual(0, swarm.KnownPeers.Count()); Assert.AreEqual(0, swarm.KnownPeerAddresses.Count()); - await swarm.RegisterPeerAsync("/ip4/10.1.10.10/tcp/29087/ipfs/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3"); + swarm.RegisterPeerAddress("/ip4/10.1.10.10/tcp/29087/ipfs/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3"); Assert.AreEqual(1, swarm.KnownPeers.Count()); Assert.AreEqual(1, swarm.KnownPeerAddresses.Count()); - await swarm.RegisterPeerAsync("/ip4/10.1.10.11/tcp/29087/p2p/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3"); + swarm.RegisterPeerAddress("/ip4/10.1.10.11/tcp/29087/p2p/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3"); Assert.AreEqual(1, swarm.KnownPeers.Count()); Assert.AreEqual(2, swarm.KnownPeerAddresses.Count()); - await swarm.RegisterPeerAsync(venus); + swarm.RegisterPeerAddress(venus); Assert.AreEqual(2, swarm.KnownPeers.Count()); Assert.AreEqual(3, swarm.KnownPeerAddresses.Count()); } @@ -1046,7 +1046,7 @@ public async Task Dial_Peer() } [TestMethod] - public async Task PeerDiscovered() + public void PeerDiscovered() { var swarm = new Swarm { LocalPeer = self }; var peerCount = 0; @@ -1054,12 +1054,12 @@ public async Task PeerDiscovered() { ++peerCount; }; - await swarm.RegisterPeerAsync("/ip4/127.0.0.1/tcp/4001/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1h"); - await swarm.RegisterPeerAsync("/ip4/127.0.0.2/tcp/4001/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1h"); - await swarm.RegisterPeerAsync("/ip4/127.0.0.3/tcp/4001/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1h"); - await swarm.RegisterPeerAsync("/ip4/127.0.0.1/tcp/4001/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1i"); - await swarm.RegisterPeerAsync("/ip4/127.0.0.2/tcp/4001/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1i"); - await swarm.RegisterPeerAsync("/ip4/127.0.0.3/tcp/4001/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1i"); + swarm.RegisterPeerAddress("/ip4/127.0.0.1/tcp/4001/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1h"); + swarm.RegisterPeerAddress("/ip4/127.0.0.2/tcp/4001/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1h"); + swarm.RegisterPeerAddress("/ip4/127.0.0.3/tcp/4001/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1h"); + swarm.RegisterPeerAddress("/ip4/127.0.0.1/tcp/4001/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1i"); + swarm.RegisterPeerAddress("/ip4/127.0.0.2/tcp/4001/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1i"); + swarm.RegisterPeerAddress("/ip4/127.0.0.3/tcp/4001/ipfs/QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1i"); swarm.RegisterPeer(new Peer { Id = "QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1j" }); swarm.RegisterPeer(new Peer { Id = "QmdpwjdB94eNm2Lcvp9JqoCxswo3AKQqjLuNZyLixmCM1j" });