Skip to content

Commit

Permalink
Allow null to Peer.AppProtocolVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
longfin committed Jun 19, 2019
1 parent 9508810 commit 630e12b
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 18 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Expand Up @@ -13,6 +13,8 @@ To be released.
- Added `LiteDBStore` backend that uses [LiteDB] under the hood. [[#269]]
- All `*Async()` methods belonging to `TurnClient` class became to have
`cancellationToken` option. [[#287]]
- Added a version-less `Peer` constructor to create a `Peer` whose version
is unknown.

### Behavioral changes

Expand Down
80 changes: 71 additions & 9 deletions Libplanet.Tests/Net/PeerTest.cs
@@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Runtime.Serialization.Formatters.Binary;
Expand All @@ -9,16 +10,62 @@ namespace Libplanet.Tests.Net
{
public class PeerTest
{
[Fact]
public void Serialize()
public static IEnumerable<object[]> GetPeers()
{
yield return new object[]
{
new Peer(
new PublicKey(new byte[]
{
0x04, 0xb5, 0xa2, 0x4a, 0xa2, 0x11, 0x27, 0x20, 0x42, 0x3b,
0xad, 0x39, 0xa0, 0x20, 0x51, 0x82, 0x37, 0x9d, 0x6f, 0x2b,
0x33, 0xe3, 0x48, 0x7c, 0x9a, 0xb6, 0xcc, 0x8f, 0xc4, 0x96,
0xf8, 0xa5, 0x48, 0x34, 0x40, 0xef, 0xbb, 0xef, 0x06, 0x57,
0xac, 0x2e, 0xf6, 0xc6, 0xee, 0x05, 0xdb, 0x06, 0xa9, 0x45,
0x32, 0xfd, 0xa7, 0xdd, 0xc4, 0x4a, 0x16, 0x95, 0xe5, 0xce,
0x1a, 0x3d, 0x3c, 0x76, 0xdb,
}),
new DnsEndPoint("0.0.0.0", 1234),
1,
IPAddress.IPv6Loopback),
};
yield return new object[]
{
new Peer(
new PublicKey(new byte[]
{
0x04, 0xb5, 0xa2, 0x4a, 0xa2, 0x11, 0x27, 0x20, 0x42, 0x3b,
0xad, 0x39, 0xa0, 0x20, 0x51, 0x82, 0x37, 0x9d, 0x6f, 0x2b,
0x33, 0xe3, 0x48, 0x7c, 0x9a, 0xb6, 0xcc, 0x8f, 0xc4, 0x96,
0xf8, 0xa5, 0x48, 0x34, 0x40, 0xef, 0xbb, 0xef, 0x06, 0x57,
0xac, 0x2e, 0xf6, 0xc6, 0xee, 0x05, 0xdb, 0x06, 0xa9, 0x45,
0x32, 0xfd, 0xa7, 0xdd, 0xc4, 0x4a, 0x16, 0x95, 0xe5, 0xce,
0x1a, 0x3d, 0x3c, 0x76, 0xdb,
}),
new DnsEndPoint("0.0.0.0", 1234),
1),
};
yield return new object[]
{
new Peer(
new PublicKey(new byte[]
{
0x04, 0xb5, 0xa2, 0x4a, 0xa2, 0x11, 0x27, 0x20, 0x42, 0x3b,
0xad, 0x39, 0xa0, 0x20, 0x51, 0x82, 0x37, 0x9d, 0x6f, 0x2b,
0x33, 0xe3, 0x48, 0x7c, 0x9a, 0xb6, 0xcc, 0x8f, 0xc4, 0x96,
0xf8, 0xa5, 0x48, 0x34, 0x40, 0xef, 0xbb, 0xef, 0x06, 0x57,
0xac, 0x2e, 0xf6, 0xc6, 0xee, 0x05, 0xdb, 0x06, 0xa9, 0x45,
0x32, 0xfd, 0xa7, 0xdd, 0xc4, 0x4a, 0x16, 0x95, 0xe5, 0xce,
0x1a, 0x3d, 0x3c, 0x76, 0xdb,
}),
new DnsEndPoint("0.0.0.0", 1234)),
};
}

[Theory]
[MemberData(nameof(GetPeers))]
public void Serialize(Peer peer)
{
var key = new PublicKey(
ByteUtil.ParseHex(
"038f92e8098c897c2a9ae3226eb6337eb" +
"7ca8dbad5e1c8c9b130a9d39171a44134"
));
var endPoint = new DnsEndPoint("0.0.0.0", 1234);
var peer = new Peer(key, endPoint, 1, IPAddress.IPv6Loopback);
var formatter = new BinaryFormatter();
using (var stream = new MemoryStream())
{
Expand All @@ -29,5 +76,20 @@ public void Serialize()
Assert.Equal(peer, deserialized);
}
}

[Fact]
public void WithAppProtocolVersion()
{
var peerWithoutVersion = new Peer(
new PrivateKey().PublicKey,
new DnsEndPoint("0.0.0.0", 1234));
Peer peerWithVersion = peerWithoutVersion.WithAppProtocolVersion(42);
var expected = new Peer(
peerWithoutVersion.PublicKey,
peerWithoutVersion.EndPoint,
42
);
Assert.Equal(expected, peerWithVersion);
}
}
}
32 changes: 28 additions & 4 deletions Libplanet/Net/Peer.cs
Expand Up @@ -15,6 +15,20 @@ namespace Libplanet.Net
[Equals]
public class Peer : ISerializable
{
/// <summary>
/// Initializes a new instance of the <see cref="Peer"/> class.
/// </summary>
/// <param name="publicKey">A <see cref="PublicKey"/> of the
/// <see cref="Peer"/>.</param>
/// <param name="endPoint">A <see cref="DnsEndPoint"/> consisting of the
/// host and port of the <see cref="Peer"/>.</param>
public Peer(
PublicKey publicKey,
DnsEndPoint endPoint)
: this(publicKey, endPoint, default(int?), null)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="Peer"/> class.
/// </summary>
Expand All @@ -27,15 +41,15 @@ public class Peer : ISerializable
public Peer(
PublicKey publicKey,
DnsEndPoint endPoint,
int appProtocolVersion)
int? appProtocolVersion)
: this(publicKey, endPoint, appProtocolVersion, null)
{
}

internal Peer(
PublicKey publicKey,
DnsEndPoint endPoint,
int appProtocolVersion,
int? appProtocolVersion,
IPAddress publicIPAddress)
{
PublicKey = publicKey ??
Expand All @@ -52,7 +66,8 @@ protected Peer(SerializationInfo info, StreamingContext context)
EndPoint = new DnsEndPoint(
info.GetString("end_point_host"),
info.GetInt32("end_point_port"));
AppProtocolVersion = info.GetInt32("app_protocol_version");
AppProtocolVersion =
info.GetValueOrDefault<int?>("app_protocol_version", null);
string addressStr = info.GetString("public_ip_address");
if (addressStr != null)
{
Expand All @@ -79,7 +94,7 @@ protected Peer(SerializationInfo info, StreamingContext context)
/// <seealso cref="Swarm.DifferentVersionPeerEncountered"/>
[IgnoreDuringEquals]
[Pure]
public int AppProtocolVersion { get; }
public int? AppProtocolVersion { get; }

/// <summary>The peer's address which is derived from
/// its <see cref="PublicKey"/>.
Expand Down Expand Up @@ -110,5 +125,14 @@ public override string ToString()
{
return $"{Address}.{EndPoint}.{AppProtocolVersion}";
}

internal Peer WithAppProtocolVersion(int appProtocolVersion)
{
return new Peer(
PublicKey,
EndPoint,
appProtocolVersion,
PublicIPAddress);
}
}
}
12 changes: 7 additions & 5 deletions Libplanet/Net/Swarm.cs
Expand Up @@ -287,11 +287,12 @@ private set
try
{
_logger.Debug($"Trying to DialPeerAsync({peer})...");
await DialPeerAsync(peer, cancellationToken);
Pong pong = await DialPeerAsync(peer, cancellationToken);
_logger.Debug($"DialPeerAsync({peer}) is complete.");

_peers[peer] = timestamp.Value;
addedPeers.Add(peer);
Peer peerWithVersion = peer.WithAppProtocolVersion(pong.AppProtocolVersion);
_peers[peerWithVersion] = timestamp.Value;
addedPeers.Add(peerWithVersion);
}
catch (IOException e)
{
Expand Down Expand Up @@ -1413,12 +1414,13 @@ CancellationToken cancellationToken
if (IsUnknownPeer(sender))
{
_logger.Debug("The sender of delta is unknown.");
if (IsDifferentProtocolVersion(sender))
if (IsDifferentProtocolVersion(sender) &&
sender.AppProtocolVersion is int senderVersion)
{
var args = new DifferentProtocolVersionEventArgs
{
ExpectedVersion = _appProtocolVersion,
ActualVersion = sender.AppProtocolVersion,
ActualVersion = senderVersion,
};
DifferentVersionPeerEncountered?.Invoke(this, args);
return;
Expand Down

0 comments on commit 630e12b

Please sign in to comment.