Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable null checker on Libplanet project's top-level files #1022

Merged
merged 2 commits into from
Sep 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ To be released.
constructor. [[#1012]]
- Added `cacheSize` optional parameter to `BlockSet<T>()` constructor.
[[#1013]]
- Removed `Address(SerializationInfo, StreamingContext)` constructor.
[[#1022]]
- Removed constructors from `InvalidMessageException` class. [[#1021]]

### Backward-incompatible network protocol changes
Expand Down Expand Up @@ -366,6 +368,7 @@ To be released.
[#1012]: https://github.com/planetarium/libplanet/pull/1012
[#1013]: https://github.com/planetarium/libplanet/pull/1013
[#1021]: https://github.com/planetarium/libplanet/pull/1021
[#1022]: https://github.com/planetarium/libplanet/pull/1022
[sleep mode]: https://en.wikipedia.org/wiki/Sleep_mode


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.1.0" />
<PackageReference Include="Serilog.Enrichers.Thread" Version="3.0.0" />
<PackageReference Include="Serilog.Sinks.XUnit" Version="1.0.7" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.205">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>
runtime; build; native; contentfiles; analyzers
Expand Down
2 changes: 1 addition & 1 deletion Libplanet.RocksDBStore/Libplanet.RocksDBStore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
</IncludeAssets>
</PackageReference>
<PackageReference Include="Planetarium.RocksDbSharp" Version="6.2.2.3-planetarium" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.205">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>
runtime; build; native; contentfiles; analyzers
Expand Down
2 changes: 1 addition & 1 deletion Libplanet.Stun.Tests/Libplanet.Stun.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.1.0" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.205">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>
runtime; build; native; contentfiles; analyzers
Expand Down
2 changes: 1 addition & 1 deletion Libplanet.Stun/Libplanet.Stun.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
runtime; build; native; contentfiles; analyzers; buildtransitive
</IncludeAssets>
</PackageReference>
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.205">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>
runtime; build; native; contentfiles; analyzers
Expand Down
10 changes: 5 additions & 5 deletions Libplanet.Stun/Stun/Messages/StunMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,21 +115,21 @@ public static async Task<StunMessage> Parse(Stream stream)
MessageMethod.Binding => new BindingSuccessResponse(),
MessageMethod.CreatePermission => new CreatePermissionSuccessResponse(),
MessageMethod.Refresh => new RefreshSuccessResponse(),
_ => rv
_ => rv,
},
MessageClass.ErrorResponse => method switch
{
MessageMethod.Allocate => new AllocateErrorResponse(),
MessageMethod.CreatePermission => new CreatePermissionErrorResponse(),
MessageMethod.Refresh => new RefreshErrorResponse(),
_ => rv
_ => rv,
},
MessageClass.Indication => method switch
{
MessageMethod.ConnectionAttempt => new ConnectionAttempt(),
_ => rv
_ => rv,
},
_ => rv
_ => rv,
};

if (rv is null)
Expand Down Expand Up @@ -252,7 +252,7 @@ internal static IEnumerable<Attribute> ParseAttributes(
XorRelayedAddress.Parse(payload, transactionId),
Attribute.AttributeType.ConnectionId => new ConnectionId(payload),
Attribute.AttributeType.Lifetime => new Lifetime((int)payload.ToUInt()),
_ => null
_ => null,
};

if (!(attr is null))
Expand Down
4 changes: 4 additions & 0 deletions Libplanet.Tests.ruleset
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
<Rule Id="SA1005" Action="None" />
<!-- Closing parenthesis should be on line of opening parenthesis -->
<Rule Id="SA1112" Action="None" />
<!-- Allow tuple types in signatures omit element names. -->
<Rule Id="SA1414" Action="None" />
<!-- Allow tuple fields to be referred by index (i.e. ItemN). -->
<Rule Id="SA1142" Action="None" />
<!-- Single-line comment should be preceded by blank line. -->
<Rule Id="SA1515" Action="None" />
<!-- TODO: Write copyright -->
Expand Down
4 changes: 2 additions & 2 deletions Libplanet.Tests/Action/ActionContextTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class ActionContextTest
[Fact]
public void RandomShouldBeDeterministic()
{
(int seed, int expected)[] testCases =
(int Seed, int Expected)[] testCases =
{
(0, 1559595546),
(1, 534011718),
Expand Down Expand Up @@ -60,7 +60,7 @@ public void GuidShouldBeDeterministic()
randomSeed: 1
);

(Guid expected, Guid diff)[] testCases =
(Guid Expected, Guid Diff)[] testCases =
{
(
new Guid("6f460c1a-755d-48e4-ad67-65d5f519dbc8"),
Expand Down
4 changes: 2 additions & 2 deletions Libplanet.Tests/Blockchain/BlockChainTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2248,7 +2248,7 @@ public async void BlockActionWithMultipleAddress()
/// 10 addresses[4] Present
/// </code>
/// </summary>
internal static (Address, Address[] addresses, BlockChain<DumbAction> chain)
internal static (Address, Address[] Addresses, BlockChain<DumbAction> Chain)
MakeIncompleteBlockStates(
IStore store,
IBlockStatesStore blockStatesStore,
Expand Down Expand Up @@ -2333,7 +2333,7 @@ void BuildIndex(Guid id, Block<DumbAction> block)
return (signer, addresses, chain);
}

private (Address, Address[] addresses, BlockChain<DumbAction> chain)
private (Address, Address[] Addresses, BlockChain<DumbAction> Chain)
MakeIncompleteBlockStates() =>
MakeIncompleteBlockStates(_fx.Store, _fx.BlockStatesStore);

Expand Down
2 changes: 1 addition & 1 deletion Libplanet.Tests/Libplanet.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<PackageReference Include="Serilog.Enrichers.Thread" Version="3.0.0" />
<PackageReference Include="Serilog.Sinks.TestCorrelator" Version="3.2.0" />
<PackageReference Include="Serilog.Sinks.XUnit" Version="1.0.7" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.205">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>
runtime; build; native; contentfiles; analyzers
Expand Down
4 changes: 4 additions & 0 deletions Libplanet.ruleset
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
<Rule Id="SA1309" Action="None" />
<!-- Allow an expression not to declare parenthese. -->
<Rule Id="SA1407" Action="None" />
<!-- Allow tuple types in signatures omit element names. -->
<Rule Id="SA1414" Action="None" />
<!-- Allow tuple fields to be referred by index (i.e. ItemN). -->
<Rule Id="SA1142" Action="None" />
<!-- Allow a rich text in a XML doc comment's <summary>. -->
<Rule Id="SA1462" Action="None" />
<Rule Id="SA1642" Action="None" />
Expand Down
22 changes: 11 additions & 11 deletions Libplanet/Address.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#nullable enable
using System;
using System.Collections.Immutable;
using System.Diagnostics.Contracts;
Expand Down Expand Up @@ -93,13 +94,6 @@ public Address(byte[] address)
{
}

public Address(
SerializationInfo info,
StreamingContext context)
: this(info.GetValue<byte[]>("address"))
{
}

/// <summary>
/// Derives the corresponding <see cref="Address"/> from a <see
/// cref="PublicKey"/>.
Expand Down Expand Up @@ -138,6 +132,14 @@ public Address(string hex)
{
}

private Address(
SerializationInfo info,
StreamingContext context)
: this(info?.GetValue<byte[]>("address") ??
throw new SerializationException("Missing the address field."))
{
}

/// <summary>
/// An immutable array of 20 <see cref="byte"/>s that represent this
/// <see cref="Address"/>.
Expand Down Expand Up @@ -223,9 +225,7 @@ public override string ToString()
}

/// <inheritdoc />
public void GetObjectData(
SerializationInfo info,
StreamingContext context)
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("address", ToByteArray());
}
Expand All @@ -246,7 +246,7 @@ int IComparable<Address>.CompareTo(Address other)
return 0;
}

int IComparable.CompareTo(object obj)
int IComparable.CompareTo(object? obj)
{
if (obj is Address other)
{
Expand Down
1 change: 1 addition & 0 deletions Libplanet/AddressExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#nullable enable
using Libplanet.Crypto;

namespace Libplanet
Expand Down
3 changes: 2 additions & 1 deletion Libplanet/ByteArrayExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#nullable enable
using System;
using System.Diagnostics.Contracts;

Expand All @@ -22,7 +23,7 @@ public static class ByteArrayExtensions
/// <paramref name="bytes"/> or <paramref name="prefix"/> is null.
/// </exception>
[Pure]
public static bool StartsWith(this byte[] bytes, byte[] prefix)
public static bool StartsWith(this byte[]? bytes, byte[]? prefix)
{
if (bytes is null)
{
Expand Down
2 changes: 1 addition & 1 deletion Libplanet/ByteUtil.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#nullable enable
using System;
using System.Collections.Immutable;
using System.Diagnostics.Contracts;
Expand Down Expand Up @@ -74,7 +75,6 @@ public static string Hex(byte[] bytes)
}

string s = BitConverter.ToString(bytes);

return s.Replace("-", string.Empty).ToLower(CultureInfo.InvariantCulture);
}

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

namespace Libplanet
Expand Down
9 changes: 6 additions & 3 deletions Libplanet/HashDigest.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#nullable enable
using System;
using System.Collections.Immutable;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Numerics;
using System.Reflection;
using System.Security.Cryptography;

namespace Libplanet
Expand Down Expand Up @@ -35,8 +37,9 @@ namespace Libplanet

static HashDigest()
{
var thunk = (T)typeof(T).GetMethod("Create", new Type[0]).Invoke(
null, new object[0]);
MethodInfo? method = typeof(T).GetMethod("Create", new Type[0]);
T thunk = method?.Invoke(null, new object[0]) as T
?? throw new InvalidCastException($"Failed to instantiate {typeof(T).FullName}.");
Size = thunk.HashSize / 8;

_defaultByteArray = new byte[Size];
Expand Down Expand Up @@ -206,7 +209,7 @@ public override string ToString()
}

[Pure]
public override bool Equals(object obj)
public override bool Equals(object? obj)
{
return obj is IEquatable<HashDigest<T>> other
? other.Equals(this)
Expand Down
1 change: 1 addition & 0 deletions Libplanet/Hashcash.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#nullable enable
using System;
using System.Security.Cryptography;
using System.Threading;
Expand Down
2 changes: 1 addition & 1 deletion Libplanet/KeyStore/Kdfs/Pbkdf2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ internal static IKdf FromJson(in JsonElement element)
throw new InvalidKeyJsonException(
"The \"prf\" field must not be null, but a string."),
_ =>
throw new UnsupportedKeyJsonException($"Unsupported \"prf\" type: \"{prf}\".")
throw new UnsupportedKeyJsonException($"Unsupported \"prf\" type: \"{prf}\"."),
};
}
}
Expand Down
4 changes: 2 additions & 2 deletions Libplanet/KeyStore/ProtectedPrivateKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ byte[] GetHexProperty(JsonElement element, string fieldName)
"aes-128-ctr" => Aes128Ctr.FromJson(cipherParamsElement),
_ =>
throw new UnsupportedKeyJsonException(
$"Unsupported cipher type: \"{cipherType}\".")
$"Unsupported cipher type: \"{cipherType}\"."),
};

IKdf kdf;
Expand All @@ -282,7 +282,7 @@ byte[] GetHexProperty(JsonElement element, string fieldName)
"scrypt" => Scrypt.FromJson(kdfParamsElement),
_ =>
throw new UnsupportedKeyJsonException(
$"Unsupported cipher type: \"{kdfType}\".")
$"Unsupported cipher type: \"{kdfType}\"."),
};
}
catch (ArgumentException e)
Expand Down
2 changes: 1 addition & 1 deletion Libplanet/Libplanet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ https://docs.libplanet.io/</Description>
<PackageReference Include="Planetarium.NetMQ" Version="4.0.0.260-planetarium" />
<PackageReference Include="Nito.AsyncEx" Version="5.0.0" />
<PackageReference Include="Serilog" Version="2.8.0" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.205">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>
runtime; build; native; contentfiles; analyzers
Expand Down
1 change: 1 addition & 0 deletions Libplanet/Nonce.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#nullable enable
using System;
using System.Collections.Immutable;
using System.Diagnostics.Contracts;
Expand Down