Skip to content

Commit

Permalink
Enable null checker on exception files
Browse files Browse the repository at this point in the history
[changelog skip]
  • Loading branch information
dahlia committed Sep 27, 2020
1 parent 74c1ddb commit c103c4b
Show file tree
Hide file tree
Showing 40 changed files with 120 additions and 43 deletions.
3 changes: 2 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,12 @@ To be released.
- `BlockPolicy<T>.DoesTransactionFollowPolicy()` method and
`IBlockPolicy.DoesTransactionFollowPolicy()` method became to take
additional `BlockChain<T>` parameter as its context. [[#1012]]
- `doesTransactionFollowPolicy` parameter became
- `doesTransactionFollowPolicy` parameter became
`Func<Transaction<T>, BlockChain<T>, bool>` on `BlockPolicy<T>()`
constructor. [[#1012]]
- Added `cacheSize` optional parameter to `BlockSet<T>()` constructor.
[[#1013]]
- Removed constructors from `InvalidMessageException` class.

### Backward-incompatible network protocol changes

Expand Down
1 change: 1 addition & 0 deletions Libplanet/Action/MissingActionTypeException.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#nullable enable
using System;

namespace Libplanet.Action
Expand Down
38 changes: 32 additions & 6 deletions Libplanet/Action/UnexpectedlyTerminatedActionException.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#nullable enable
using System;
using System.Runtime.Serialization;
using System.Security.Cryptography;
Expand Down Expand Up @@ -73,14 +74,39 @@ StreamingContext context
TxId = new TxId(txId);
}

if (info.TryGetValue($"{nameof(Action)}_type", out string actionType))
string actionKey = $"{nameof(Action)}_type";
if (info.TryGetValue(actionKey, out string actionType))
{
var values = (Dictionary)new Codec().Decode(
info.GetValue<byte[]>($"{nameof(Action)}_values")
);
string valuesKey = $"{nameof(Action)}_values";
if (!(info.GetValue<byte[]>(valuesKey) is byte[] valuesBytes))
{
throw new SerializationException($"Missing the {valuesKey} field.");
}

Action = (IAction)Activator.CreateInstance(Type.GetType(actionType, true, true));
Action?.LoadPlainValue(values);
if (!(new Codec().Decode(valuesBytes) is Dictionary values))
{
throw new SerializationException(
$"{valuesKey} field must be a Bencodex dictionary."
);
}

Type type = Type.GetType(actionType, true, true)
?? throw new SerializationException($"Failed to find the type: {actionType}.");
if (Activator.CreateInstance(type) is IAction action)
{
Action = action;
Action.LoadPlainValue(values);
}
else
{
throw new SerializationException(
$"Failed to instantiate the action: {actionType}."
);
}
}
else
{
throw new SerializationException($"Missing the {actionKey} field.");
}
}

Expand Down
3 changes: 2 additions & 1 deletion Libplanet/Blockchain/IncompleteBlockStatesException.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#nullable enable
using System;
using System.Security.Cryptography;
using Libplanet.Blocks;
Expand All @@ -21,7 +22,7 @@ public class IncompleteBlockStatesException : Exception
/// </param>
public IncompleteBlockStatesException(
HashDigest<SHA256> blockHash,
string message = null)
string? message = null)
: base(
message is null
? $"The block {blockHash} lacks its states"
Expand Down
1 change: 1 addition & 0 deletions Libplanet/Blocks/InvalidBlockDifficultyException.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#nullable enable
using System;

namespace Libplanet.Blocks
Expand Down
1 change: 1 addition & 0 deletions Libplanet/Blocks/InvalidBlockException.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#nullable enable
using System;
using System.Runtime.Serialization;

Expand Down
1 change: 1 addition & 0 deletions Libplanet/Blocks/InvalidBlockHashException.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#nullable enable
using System;

namespace Libplanet.Blocks
Expand Down
1 change: 1 addition & 0 deletions Libplanet/Blocks/InvalidBlockIndexException.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#nullable enable
using System;

namespace Libplanet.Blocks
Expand Down
1 change: 1 addition & 0 deletions Libplanet/Blocks/InvalidBlockPreviousHashException.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#nullable enable
using System;

namespace Libplanet.Blocks
Expand Down
1 change: 1 addition & 0 deletions Libplanet/Blocks/InvalidBlockTimestampException.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#nullable enable
using System;

namespace Libplanet.Blocks
Expand Down
1 change: 1 addition & 0 deletions Libplanet/Blocks/InvalidGenesisBlockException.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#nullable enable
using System.Diagnostics.Contracts;
using System.Security.Cryptography;
using Libplanet.Blockchain;
Expand Down
1 change: 1 addition & 0 deletions Libplanet/Crypto/InvalidCiphertextException.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#nullable enable
using System;
using System.Runtime.Serialization;

Expand Down
1 change: 1 addition & 0 deletions Libplanet/KeyStore/IncorrectPassphraseException.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#nullable enable
using System;
using System.Collections.Immutable;

Expand Down
1 change: 1 addition & 0 deletions Libplanet/KeyStore/InvalidKeyJsonException.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#nullable enable
namespace Libplanet.KeyStore
{
/// <summary>
Expand Down
1 change: 1 addition & 0 deletions Libplanet/KeyStore/KeyJsonException.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#nullable enable
using System;

namespace Libplanet.KeyStore
Expand Down
1 change: 1 addition & 0 deletions Libplanet/KeyStore/KeyStoreException.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#nullable enable
using System;

namespace Libplanet.KeyStore
Expand Down
1 change: 1 addition & 0 deletions Libplanet/KeyStore/MismatchedAddressException.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#nullable enable
using System;

namespace Libplanet.KeyStore
Expand Down
1 change: 1 addition & 0 deletions Libplanet/KeyStore/NoKeyException.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#nullable enable
using System;

namespace Libplanet.KeyStore
Expand Down
1 change: 1 addition & 0 deletions Libplanet/KeyStore/UnsupportedKeyJsonException.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#nullable enable
namespace Libplanet.KeyStore
{
/// <summary>
Expand Down
1 change: 1 addition & 0 deletions Libplanet/Net/DifferentAppProtocolVersionException.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#nullable enable
#pragma warning disable S3871
using System;
using Libplanet.Net.Messages;
Expand Down
1 change: 1 addition & 0 deletions Libplanet/Net/IceServerException.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#nullable enable
using System;

namespace Libplanet.Net
Expand Down
28 changes: 18 additions & 10 deletions Libplanet/Net/InvalidMessageException.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#nullable enable
using System;
using System.Runtime.Serialization;
using Libplanet.Net.Messages;
Expand All @@ -7,18 +8,14 @@ namespace Libplanet.Net
[Serializable]
public class InvalidMessageException : Exception
{
public InvalidMessageException()
{
}

public InvalidMessageException(string message)
: base(message)
{
}

public InvalidMessageException(string message, Exception innerException)
internal InvalidMessageException(
string message,
Message receivedMessage,
Exception innerException
)
: base(message, innerException)
{
ReceivedMessage = receivedMessage;
}

internal InvalidMessageException(string message, Message receivedMessage)
Expand All @@ -33,8 +30,19 @@ StreamingContext context
)
: base(info, context)
{
ReceivedMessage = info.GetValue(nameof(ReceivedMessage), typeof(Message)) is Message m
? m
: throw new SerializationException(
$"{nameof(ReceivedMessage)} is expected to be a non-null {nameof(Message)}.");
}

internal Message ReceivedMessage { get; }

public override void GetObjectData(
SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
info.AddValue(nameof(ReceivedMessage), ReceivedMessage);
}
}
}
1 change: 1 addition & 0 deletions Libplanet/Net/InvalidStateTargetException.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#nullable enable
using System;
using System.Runtime.Serialization;

Expand Down
10 changes: 5 additions & 5 deletions Libplanet/Net/Messages/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,10 @@ public static Message Parse(

if (!types.TryGetValue(rawType, out Type type))
{
throw new InvalidMessageException(
$"Can't determine NetMQMessage. [type: {rawType}]");
throw new ArgumentException(
$"Can't determine NetMQMessage. [type: {rawType}]",
nameof(raw)
);
}

var message = (Message)Activator.CreateInstance(
Expand All @@ -241,9 +243,7 @@ public static Message Parse(

if (!message.Remote.PublicKey.Verify(body.ToByteArray(), signature))
{
throw new InvalidMessageException(
"The message signature is invalid"
);
throw new InvalidMessageException("The message signature is invalid", message);
}

if (!reply)
Expand Down
1 change: 1 addition & 0 deletions Libplanet/Net/NoSwarmContextException.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#nullable enable
using System;
using System.Runtime.Serialization;

Expand Down
1 change: 1 addition & 0 deletions Libplanet/Net/PeerNotFoundException.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#nullable enable
using System;
using System.Runtime.Serialization;

Expand Down
23 changes: 14 additions & 9 deletions Libplanet/Net/Protocols/KademliaProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -423,20 +423,20 @@ internal async Task PingAsync(
try
{
_logger.Debug("Trying to ping async to {Peer}.", target);
if (!(await _transport.SendMessageWithReplyAsync(
Message reply = await _transport.SendMessageWithReplyAsync(
target,
new Ping(),
timeout,
cancellationToken) is Pong pong))
cancellationToken
);
if (!(reply is Pong pong))
{
throw new InvalidMessageException(
"Received pong is invalid.");
throw new InvalidMessageException("Received pong is invalid.", reply);
}

if (pong.Remote.Address.Equals(_address))
{
throw new InvalidMessageException(
"Cannot receive pong from self");
throw new InvalidMessageException("Cannot receive pong from self", pong);
}
}
catch (TimeoutException)
Expand Down Expand Up @@ -592,13 +592,18 @@ private async Task<IEnumerable<BoundPeer>> GetNeighbors(
var findPeer = new FindNeighbors(target);
try
{
if (!(await _transport.SendMessageWithReplyAsync(
Message reply = await _transport.SendMessageWithReplyAsync(
addressee,
findPeer,
timeout,
cancellationToken) is Neighbors neighbors))
cancellationToken
);
if (!(reply is Neighbors neighbors))
{
throw new InvalidMessageException("Reply of FindNeighbors is invalid.");
throw new InvalidMessageException(
$"Reply to {nameof(FindNeighbors)} is invalid.",
reply
);
}

return neighbors.Found;
Expand Down
1 change: 1 addition & 0 deletions Libplanet/Net/Protocols/PeerDiscoveryException.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#nullable enable
using System;
using System.Runtime.Serialization;

Expand Down
8 changes: 6 additions & 2 deletions Libplanet/Net/Protocols/PingTimeoutException.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#nullable enable
using System;
using System.Runtime.Serialization;

Expand Down Expand Up @@ -27,7 +28,10 @@ public PingTimeoutException(BoundPeer target, string message, Exception innerExc
protected PingTimeoutException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
Target = (BoundPeer)info.GetValue("Target", typeof(BoundPeer));
Target = info.GetValue(nameof(Target), typeof(BoundPeer)) is BoundPeer target
? target
: throw new SerializationException(
$"{nameof(Target)} is expected to be a non-null {nameof(BoundPeer)}.");
}

public BoundPeer Target { get; private set; }
Expand All @@ -41,7 +45,7 @@ public override void GetObjectData(
}

base.GetObjectData(info, context);
info.AddValue("Target", Target);
info.AddValue(nameof(Target), Target);
}
}
}
18 changes: 9 additions & 9 deletions Libplanet/Net/Swarm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1034,10 +1034,10 @@ internal async IAsyncEnumerable<Tuple<long, HashDigest<SHA256>>> GetBlockHashes(
yield break;
}

throw new InvalidMessageException(
string errorMessage =
$"The response of {nameof(GetBlockHashes)} is expected to be " +
$"{nameof(BlockHashes)}, not {parsedMessage.GetType().Name}: {parsedMessage}"
);
$"{nameof(BlockHashes)}, not {parsedMessage.GetType().Name}: {parsedMessage}";
throw new InvalidMessageException(errorMessage, parsedMessage);
}

internal async IAsyncEnumerable<Block<T>> GetBlocksAsync(
Expand Down Expand Up @@ -1097,11 +1097,11 @@ [EnumeratorCancellation] CancellationToken cancellationToken
}
else
{
throw new InvalidMessageException(
string errorMessage =
$"Expected a {nameof(Blocks)} message as a response of " +
$"the {nameof(GetBlocks)} message, but got a {message.GetType().Name} " +
$"message instead: {message}"
);
$"message instead: {message}";
throw new InvalidMessageException(errorMessage, message);
}
}

Expand Down Expand Up @@ -1146,11 +1146,11 @@ internal async IAsyncEnumerable<Transaction<T>> GetTxsAsync(
}
else
{
throw new InvalidMessageException(
string errorMessage =
$"Expected {nameof(Tx)} messages as response of " +
$"the {nameof(GetTxs)} message, but got a {message.GetType().Name} " +
$"message instead: {message}"
);
$"message instead: {message}";
throw new InvalidMessageException(errorMessage, message);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions Libplanet/Net/SwarmException.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#nullable enable
using System;
using System.Runtime.Serialization;

Expand Down
1 change: 1 addition & 0 deletions Libplanet/Store/ChainIdNotFoundException.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#nullable enable
using System;

namespace Libplanet.Store
Expand Down
1 change: 1 addition & 0 deletions Libplanet/Tx/InvalidTxException.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#nullable enable
using System;

namespace Libplanet.Tx
Expand Down
Loading

0 comments on commit c103c4b

Please sign in to comment.