Skip to content

Commit

Permalink
fix(Swarm): rename RegisterPeerAsync to RegisterPeerAddress
Browse files Browse the repository at this point in the history
  • Loading branch information
richardschneider committed Aug 21, 2019
1 parent 41bc2cf commit 1b741e6
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 67 deletions.
33 changes: 9 additions & 24 deletions src/Swarm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,9 @@ public Peer LocalPeer
/// </summary>
/// <value>
/// Contains any peer address that has been
/// <see cref="RegisterPeerAsync">discovered</see>.
/// <see cref="RegisterPeerAddress">discovered</see>.
/// </value>
/// <seealso cref="RegisterPeerAsync"/>
/// <seealso cref="RegisterPeerAddress"/>
public IEnumerable<MultiAddress> KnownPeerAddresses
{
get
Expand All @@ -198,9 +198,9 @@ public IEnumerable<MultiAddress> KnownPeerAddresses
/// </summary>
/// <value>
/// Contains any peer that has been
/// <see cref="RegisterPeerAsync">discovered</see>.
/// <see cref="RegisterPeerAddress">discovered</see>.
/// </value>
/// <seealso cref="RegisterPeerAsync"/>
/// <seealso cref="RegisterPeerAddress"/>
public IEnumerable<Peer> KnownPeers
{
get
Expand All @@ -215,12 +215,8 @@ public IEnumerable<Peer> KnownPeers
/// <param name="address">
/// An address to the peer. It must end with the peer ID.
/// </param>
/// <param name="cancel">
/// Is used to stop the task. When cancelled, the <see cref="TaskCanceledException"/> is raised.
/// </param>
/// <returns>
/// A task that represents the asynchronous operation. The task's result
/// is the <see cref="Peer"/> that is registered.
/// The <see cref="Peer"/> that is registered.
/// </returns>
/// <exception cref="Exception">
/// The <see cref="BlackList"/> or <see cref="WhiteList"/> policies forbid it.
Expand All @@ -231,26 +227,15 @@ public IEnumerable<Peer> KnownPeers
/// added to the <see cref="KnownPeerAddresses"/>.
/// </remarks>
/// <seealso cref="RegisterPeer(Peer)"/>
public Task<Peer> 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<MultiAddress> { address }
};

return Task.FromResult(RegisterPeer(peer));
return RegisterPeer(peer);
}

/// <summary>
Expand Down Expand Up @@ -468,7 +453,7 @@ public async Task StopAsync()
/// </remarks>
public async Task<PeerConnection> 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);
}

Expand Down
8 changes: 4 additions & 4 deletions test/AutoDialerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
32 changes: 16 additions & 16 deletions test/Routing/Dht1Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down
46 changes: 23 additions & 23 deletions test/SwarmTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand All @@ -62,13 +62,13 @@ public void NewPeerAddress_Self()
var selfAddress = "/ip4/178.62.158.247/tcp/4001/ipfs/" + self.Id;
ExceptionAssert.Throws<Exception>(() =>
{
var _ = swarm.RegisterPeerAsync(selfAddress).Result;
var _ = swarm.RegisterPeerAddress(selfAddress);
});

selfAddress = "/ip4/178.62.158.247/tcp/4001/p2p/" + self.Id;
ExceptionAssert.Throws<Exception>(() =>
{
var _ = swarm.RegisterPeerAsync(selfAddress).Result;
var _ = swarm.RegisterPeerAddress(selfAddress);
});
}

Expand All @@ -80,11 +80,11 @@ public void NewPeerAddress_BlackList()

ExceptionAssert.Throws<Exception>(() =>
{
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));
}

Expand All @@ -96,11 +96,11 @@ public void NewPeerAddress_WhiteList()

ExceptionAssert.Throws<Exception>(() =>
{
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));
}

Expand All @@ -110,38 +110,38 @@ public void NewPeerAddress_InvalidAddress_MissingPeerId()
var swarm = new Swarm { LocalPeer = self };
ExceptionAssert.Throws<Exception>(() =>
{
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());
}
Expand Down Expand Up @@ -1046,20 +1046,20 @@ public async Task Dial_Peer()
}

[TestMethod]
public async Task PeerDiscovered()
public void PeerDiscovered()
{
var swarm = new Swarm { LocalPeer = self };
var peerCount = 0;
swarm.PeerDiscovered += (s, e) =>
{
++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" });

Expand Down

0 comments on commit 1b741e6

Please sign in to comment.