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

Removal of '#nullable disable' from the Libplanet.Net project (etc) #3669

Merged
merged 2 commits into from
Feb 14, 2024
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
12 changes: 12 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ To be released.

### Backward-incompatible network protocol changes

- (Libplanet.Net) Changed some types due to removal of 'nullable keyword'.
[[#3669]]
- Changed `blocks` parameter type of `Branch` class constructor from
`IEnumerable<(Block, BlockCommit)>` to
`IEnumerable<(Block, BlockCommit?)>`.
- Changed `AppProtocolVersion.Extra` field type from `IValue` to `IValue?`.
- Changed `extra` parameter type of `AppProtocolVersion` class constructor
from `IValue` to `IValue?`.
- Changed `extra` parameter type of `AppProtocolVersion.Sign` method
from `IValue` to `IValue?`.

### Backward-incompatible storage format changes

### Added APIs
Expand All @@ -33,6 +44,7 @@ To be released.
[#3622]: https://github.com/planetarium/libplanet/pull/3622
[#3644]: https://github.com/planetarium/libplanet/pull/3644
[#3651]: https://github.com/planetarium/libplanet/pull/3651
[#3669]: https://github.com/planetarium/libplanet/pull/3669


Version 4.0.4
Expand Down
4 changes: 2 additions & 2 deletions Libplanet.Net.Tests/BlockCandidateTableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ public void Add()

// Ignore existing key
var firstBranch = new Branch(
new List<(Block, BlockCommit)>
new List<(Block, BlockCommit?)>
{
(_fx.Block2, TestUtils.CreateBlockCommit(_fx.Block2)),
(_fx.Block3, TestUtils.CreateBlockCommit(_fx.Block3)),
(_fx.Block4, TestUtils.CreateBlockCommit(_fx.Block4)),
});
var secondBranch = new Branch(
new List<(Block, BlockCommit)>
new List<(Block, BlockCommit?)>
{
(_fx.Block3, TestUtils.CreateBlockCommit(_fx.Block3)),
(_fx.Block4, TestUtils.CreateBlockCommit(_fx.Block4)),
Expand Down
13 changes: 6 additions & 7 deletions Libplanet.Net/AppProtocolVersion.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#nullable disable
using System;
using System.Collections.Immutable;
using System.Diagnostics.Contracts;
Expand Down Expand Up @@ -34,7 +33,7 @@ namespace Libplanet.Net
/// Optional extra data about the version. This can be used for any purpose
/// by apps, such as a URL to download the software.
/// </summary>
public readonly IValue Extra;
public readonly IValue? Extra;

/// <summary>
/// A signer who claims presence of a version.
Expand All @@ -54,7 +53,7 @@ namespace Libplanet.Net
/// <param name="signer">Gets the <see cref="Signer"/>.</param>
public AppProtocolVersion(
int version,
IValue extra,
IValue? extra,
ImmutableArray<byte> signature,
Address signer
)
Expand Down Expand Up @@ -121,7 +120,7 @@ public string Token
/// <returns>A signed version claim.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="signer"/> is
/// <see langword="null"/>.</exception>
public static AppProtocolVersion Sign(PrivateKey signer, int version, IValue extra = null)
public static AppProtocolVersion Sign(PrivateKey signer, int version, IValue? extra = null)
{
if (signer is null)
{
Expand Down Expand Up @@ -201,7 +200,7 @@ public static AppProtocolVersion FromToken(string token)
throw new FormatException($"Failed to parse a signature: {token}", e);
}

IValue extra = null;
IValue? extra = null;
if (pos >= 0)
{
pos++;
Expand Down Expand Up @@ -246,7 +245,7 @@ other field values. A value of this struct can represent an invalid claim. */

/// <inheritdoc/>
[Pure]
public override bool Equals(object obj) =>
public override bool Equals(object? obj) =>
obj is AppProtocolVersion other && Equals(other);

/// <inheritdoc/>
Expand Down Expand Up @@ -279,7 +278,7 @@ public override string ToString() => string.Format(
/// </summary>
/// <returns>A deterministic message to sign.</returns>
[Pure]
private static byte[] GetMessage(int version, IValue extra)
private static byte[] GetMessage(int version, IValue? extra)
{
byte[] msg = NetworkOrderBitsConverter.GetBytes(version);
if (!(extra is null))
Expand Down
1 change: 0 additions & 1 deletion Libplanet.Net/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#nullable disable
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Libplanet.Benchmarks")]
Expand Down
22 changes: 11 additions & 11 deletions Libplanet.Net/BlockCompletion.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#nullable disable
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
Expand All @@ -16,16 +15,17 @@
namespace Libplanet.Net
{
internal class BlockCompletion<TPeer>
where TPeer : notnull
{
private readonly ILogger _logger;
private readonly Func<BlockHash, bool> _completionPredicate;
private readonly Func<BlockHash, bool>? _completionPredicate;
private readonly int _window;
private readonly ConcurrentDictionary<BlockHash, bool> _satisfiedBlocks;
private readonly ConcurrentQueue<BlockHash> _demands;
private readonly SemaphoreSlim _demandEnqueued;
private bool _started;

public BlockCompletion(Func<BlockHash, bool> completionPredicate = null, int window = 100)
public BlockCompletion(Func<BlockHash, bool>? completionPredicate = null, int window = 100)
{
_logger = Log.ForContext<BlockCompletion<TPeer>>();
_completionPredicate = completionPredicate;
Expand All @@ -36,7 +36,7 @@ public BlockCompletion(Func<BlockHash, bool> completionPredicate = null, int win
_demandEnqueued = new SemaphoreSlim(0);
}

public delegate IAsyncEnumerable<(Block, BlockCommit)> BlockFetcher(
public delegate IAsyncEnumerable<(Block, BlockCommit?)> BlockFetcher(
TPeer peer,
IEnumerable<BlockHash> blockHashes,
CancellationToken cancellationToken
Expand Down Expand Up @@ -182,7 +182,7 @@ public bool Satisfy(Block block)
/// for the task to complete.</param>
/// <returns>An async enumerable that yields pairs of a fetched block and its source
/// peer. It terminates when all demands are satisfied.</returns>
public async IAsyncEnumerable<Tuple<Block, BlockCommit, TPeer>> Complete(
public async IAsyncEnumerable<Tuple<Block, BlockCommit?, TPeer>> Complete(
IReadOnlyList<TPeer> peers,
BlockFetcher blockFetcher,
[EnumeratorCancellation] CancellationToken cancellationToken = default
Expand All @@ -195,7 +195,7 @@ public async IAsyncEnumerable<Tuple<Block, BlockCommit, TPeer>> Complete(

var pool = new PeerPool(peers);
var queue =
new AsyncProducerConsumerQueue<Tuple<Block, BlockCommit, TPeer>>();
new AsyncProducerConsumerQueue<Tuple<Block, BlockCommit?, TPeer>>();

Task producer = Task.Run(async () =>
{
Expand Down Expand Up @@ -229,7 +229,7 @@ await pool.SpawnAsync(

while (await queue.OutputAvailableAsync(cancellationToken))
{
Tuple<Block, BlockCommit, TPeer> triple;
Tuple<Block, BlockCommit?, TPeer> triple;
try
{
triple = await queue.DequeueAsync(cancellationToken);
Expand Down Expand Up @@ -291,7 +291,7 @@ private Func<TPeer, CancellationToken, Task> CreateEnqueuing(
IList<BlockHash> blockHashes,
BlockFetcher blockFetcher,
CancellationToken cancellationToken,
AsyncProducerConsumerQueue<Tuple<Block, BlockCommit, TPeer>> queue
AsyncProducerConsumerQueue<Tuple<Block, BlockCommit?, TPeer>> queue
) =>
async (peer, ct) =>
{
Expand All @@ -307,10 +307,10 @@ AsyncProducerConsumerQueue<Tuple<Block, BlockCommit, TPeer>> queue

try
{
ConfiguredCancelableAsyncEnumerable<(Block, BlockCommit)> blocks =
ConfiguredCancelableAsyncEnumerable<(Block, BlockCommit?)> blocks =
blockFetcher(peer, blockHashes, cancellationToken)
.WithCancellation(cancellationToken);
await foreach ((Block block, BlockCommit commit) in blocks)
await foreach ((Block block, BlockCommit? commit) in blocks)
{
_logger.Debug(
"Downloaded block #{BlockIndex} {BlockHash} from {Peer}",
Expand Down Expand Up @@ -405,7 +405,7 @@ public async Task SpawnAsync(
cancellationToken.ThrowIfCancellationRequested();
}

if (_tasks.TryRemove(peer, out Task completeTask))
if (_tasks.TryRemove(peer, out Task? completeTask))
{
await completeTask;
}
Expand Down
6 changes: 3 additions & 3 deletions Libplanet/Blockchain/Branch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public class Branch
/// </description></item>
/// </list>
/// </exception>
public Branch(IEnumerable<(Block, BlockCommit)> blocks)
public Branch(IEnumerable<(Block, BlockCommit?)> blocks)
{
ImmutableArray<(Block, BlockCommit)> sorted =
ImmutableArray<(Block, BlockCommit?)> sorted =
blocks.OrderBy(block => block.Item1.Index).ToImmutableArray();
if (!sorted.Any())
{
Expand Down Expand Up @@ -71,6 +71,6 @@ public Branch(IEnumerable<(Block, BlockCommit)> blocks)
/// </description></item>
/// </list>
/// </summary>
public ImmutableArray<(Block, BlockCommit)> Blocks { get; }
public ImmutableArray<(Block, BlockCommit?)> Blocks { get; }
}
}
Loading