Skip to content
Permalink
Browse files

Apply suggestions from code review

Co-Authored-By: Hong Minhee <hong.minhee@gmail.com>
  • Loading branch information...
longfin and dahlia committed Sep 30, 2019
1 parent f2f9b39 commit b7187adb262b7959ca8620df8b9d2c47836f6368
Showing with 34 additions and 20 deletions.
  1. +2 −1 .azure-pipelines/windows-net471.yml
  2. +12 −4 Libplanet/Net/Protocols/KademliaProtocol.cs
  3. +20 −15 Libplanet/Net/Swarm.cs
@@ -15,7 +15,8 @@ steps:
inputs:
targetType: inline
script: |
foreach ($path in (gci *.Tests/*.Tests.csproj) + (gci Libplanet.Benchmarks/*.csproj)) {
$projs = (gci *.Tests/*.Tests.csproj) + (gci Libplanet.Benchmarks/*.csproj)
foreach ($path in $projs) {
[xml]$csproj = Get-Content $path
foreach ($pg in $csproj.Project.PropertyGroup) {
if ($pg.TargetFramework -ne $null) {
@@ -304,11 +304,15 @@ public string Trace()
"Cannot receive pong from self");
}

_logger.Verbose("Trying to UpdateAsync() with pong: {0}", pong);
_logger.Verbose(
$"Trying to {nameof(UpdateAsync)}() with pong: {{Message}}",
pong);

// update process required
await UpdateAsync(pong.Remote, cancellationToken);
_logger.Verbose("UpdateAsync() finished with pong: {0}", pong);
_logger.Verbose(
$"{nameof(UpdateAsync)}() finished with pong: {{Pong}}",
pong);
}
catch (TimeoutException)
{
@@ -358,7 +362,9 @@ public string Trace()
}
else
{
_logger.Verbose("Need to evict {0}; trying...", evictionCandidate);
_logger.Verbose(
"Need to evict {Candidate}; trying...",
evictionCandidate);
try
{
_routing.BucketOf(peer).ReplacementCache.Add(peer);
@@ -370,7 +376,9 @@ public string Trace()
catch (TimeoutException)
{
await RemovePeerAsync(evictionCandidate);
_logger.Verbose("Peer[{0}] has been evicted.", evictionCandidate);
_logger.Verbose(
"Peer ({Candidate}) has been evicted.",
evictionCandidate);
}
}
}
@@ -765,7 +765,7 @@ public string TraceTable()
}
}

_logger.Verbose("Trying to ping all peers. (count: {0})", tasks.Count);
_logger.Verbose("Trying to ping all {PeersNumber} peers.", tasks.Count);
await Task.WhenAll(tasks);
_logger.Verbose("Update complete.");
}
@@ -827,7 +827,7 @@ internal async Task SendMessageAsync(BoundPeer peer, Message message)
Guid reqId = Guid.NewGuid();
try
{
_logger.Verbose("Adding request[{0}] to queue...", reqId);
_logger.Verbose("Adding request ({RequestId}) to queue...", reqId);
var tcs = new TaskCompletionSource<IEnumerable<Message>>();
await _requests.AddAsync(
new MessageRequest
@@ -843,7 +843,10 @@ internal async Task SendMessageAsync(BoundPeer peer, Message message)
_logger.Verbose("Request Added. waiting for reply...");
IEnumerable<Message> reply = await tcs.Task;

_logger.Debug($"Received [{reply}] from [{peer.Address.ToHex()}]...");
_logger.Debug(
"Received {@Reply} from {PeerAddress}...",
reply,
peer.Address);

return reply;
}
@@ -961,7 +964,7 @@ internal async Task SendMessageAsync(BoundPeer peer, Message message)
var request = new GetTxs(txIdsAsArray);
int txCount = txIdsAsArray.Count();

_logger.Debug($"Required tx count: {txCount}.");
_logger.Debug("Required tx count: {Count}.", txCount);

IEnumerable<Message> replies = await SendMessageWithReplyAsync(
peer,
@@ -1115,7 +1118,7 @@ bool render
cancellationToken.ThrowIfCancellationRequested();
var request = new GetRecentStates(baseLocator, blockHash);

_logger.Debug("Requests recent states to a peer ({0}).", peer);
_logger.Debug("Requests recent states to a peer ({@Peer}).", peer);
Message reply;
try
{
@@ -1125,12 +1128,12 @@ bool render
timeout: TimeSpan.FromSeconds(30),
cancellationToken: cancellationToken
);
_logger.Debug("Received recent states from a peer ({0}).", peer);
_logger.Debug("Received recent states from a peer ({@Peer}).", peer);
}
catch (TimeoutException e)
{
_logger.Error(
"Failed to receive recent states from a peer ({0}): " + e,
"Failed to receive recent states from a peer ({@Peer}): " + e,
peer
);
continue;
@@ -1148,7 +1151,7 @@ bool render
Guid chainId = blockChain.Id;

int count = 0, totalCount = recentStates.StateReferences.Count;
_logger.Debug("Starts to store state refs received from {0}.", peer);
_logger.Debug("Starts to store state refs received from {@Peer}.", peer);

var d = new Dictionary<HashDigest<SHA256>, ISet<Address>>();
foreach (var pair in recentStates.StateReferences)
@@ -1185,7 +1188,7 @@ bool render

count = 0;
totalCount = recentStates.BlockStates.Count;
_logger.Debug("Starts to store block states received from {0}.", peer);
_logger.Debug("Starts to store block states received from {@Peer}.", peer);
foreach (var pair in recentStates.BlockStates)
{
cancellationToken.ThrowIfCancellationRequested();
@@ -1209,7 +1212,7 @@ bool render
}

_logger.Debug(
"A message received from {0} is not a RecentStates but {1}.",
"A message received from {Peer} is not a RecentStates but {@Reply}.",
peer,
reply
);
@@ -2011,13 +2014,15 @@ private void DoReply(object sender, NetMQQueueEventArgs<Message> e)
{
_logger.Verbose("Waiting for new request...");
var req = await _requests.TakeAsync(cancellationToken);
_logger.Verbose("Request[{0}] taken.", req.Id);
_logger.Verbose("Request {RequestId} taken.", req.Id);

using (var dealer = new DealerSocket(ToNetMQAddress(req.Peer)))
{
dealer.Options.Linger = _linger;
_logger.Debug(
$"Trying to send [{req.Message}] to [{req.Peer.Address.ToHex()}]..."
"Trying to send {@Message} to {PeerAddress}...",
req.Message,
req.Peer.Address
);
var message = req.Message.ToNetMQMessage(_privateKey, AsPeer);
var result = new List<Message>();
@@ -2030,7 +2035,7 @@ private void DoReply(object sender, NetMQQueueEventArgs<Message> e)
cancellationToken: cancellationToken
);

_logger.Debug($"Message[{req.Message}] sent.");
_logger.Debug("A message {@Message} sent.", req.Message);

foreach (var i in Enumerable.Range(0, req.ExpectedResponses))
{
@@ -2039,12 +2044,12 @@ private void DoReply(object sender, NetMQQueueEventArgs<Message> e)
cancellationToken: cancellationToken
);
_logger.Verbose(
"A raw message [frame count: {0}] has replied.",
"A raw message ({FrameCount} frames) has replied.",
raw.FrameCount
);
Message reply = Message.Parse(raw, true);
_logger.Debug(
"A reply has parsed: {0}, from {1}",
"A reply has parsed: {@Reply} from {@ReplyRemote}",
reply,
reply.Remote
);

0 comments on commit b7187ad

Please sign in to comment.
You can’t perform that action at this time.