Skip to content

Commit 2cbfc48

Browse files
committed
style: Removed '#nullable disable' from the Libplanet.Net project (swarm)
1 parent eed6a69 commit 2cbfc48

File tree

4 files changed

+42
-45
lines changed

4 files changed

+42
-45
lines changed

Libplanet.Net/Swarm.BlockCandidate.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#nullable disable
21
using System;
32
using System.Collections.Concurrent;
43
using System.Collections.Generic;
@@ -19,7 +18,7 @@ public partial class Swarm
1918
private async Task ConsumeBlockCandidates(
2019
TimeSpan? checkInterval = null,
2120
bool render = true,
22-
IProgress<BlockSyncState> progress = null,
21+
IProgress<BlockSyncState>? progress = null,
2322
CancellationToken cancellationToken = default)
2423
{
2524
while (!cancellationToken.IsCancellationRequested)
@@ -63,10 +62,10 @@ private async Task ConsumeBlockCandidates(
6362
private bool BlockCandidateProcess(
6463
Branch candidate,
6564
bool render,
66-
IProgress<BlockSyncState> progress,
65+
IProgress<BlockSyncState>? progress,
6766
CancellationToken cancellationToken)
6867
{
69-
BlockChain synced = null;
68+
BlockChain? synced = null;
7069
System.Action renderSwap = () => { };
7170
try
7271
{
@@ -99,7 +98,7 @@ private bool BlockCandidateProcess(
9998
}
10099

101100
if (synced is { } syncedB
102-
&& !syncedB.Id.Equals(BlockChain?.Id)
101+
&& !syncedB.Id.Equals(BlockChain.Id)
103102
&& BlockChain.Tip.Index < syncedB.Tip.Index)
104103
{
105104
_logger.Debug(
@@ -126,15 +125,15 @@ private BlockChain AppendPreviousBlocks(
126125
BlockChain blockChain,
127126
Branch candidate,
128127
bool render,
129-
IProgress<BlockSyncState> progress)
128+
IProgress<BlockSyncState>? progress)
130129
{
131130
BlockChain workspace = blockChain;
132131
List<Guid> scope = new List<Guid>();
133132
bool forked = false;
134133

135134
Block oldTip = workspace.Tip;
136135
Block newTip = candidate.Blocks.Last().Item1;
137-
List<(Block, BlockCommit)> blocks = candidate.Blocks.ToList();
136+
List<(Block, BlockCommit?)> blocks = candidate.Blocks.ToList();
138137
Block branchpoint = FindBranchpoint(
139138
oldTip,
140139
newTip,
@@ -429,7 +428,7 @@ private async Task<bool> BlockCandidateDownload(
429428
return false;
430429
}
431430

432-
IAsyncEnumerable<(Block, BlockCommit)> blocksAsync = GetBlocksAsync(
431+
IAsyncEnumerable<(Block, BlockCommit?)> blocksAsync = GetBlocksAsync(
433432
peer,
434433
hashes.Select(pair => pair.Item2),
435434
cancellationToken);

Libplanet.Net/Swarm.BlockSync.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#nullable disable
21
using System;
32
using System.Collections.Generic;
43
using System.Linq;
@@ -53,7 +52,7 @@ internal async Task PullBlocksAsync(
5352
TimeSpan? timeout,
5453
int maximumPollPeers,
5554
int chunkSize,
56-
IProgress<BlockSyncState> progress,
55+
IProgress<BlockSyncState>? progress,
5756
CancellationToken cancellationToken)
5857
{
5958
if (maximumPollPeers <= 0)
@@ -72,7 +71,7 @@ await GetPeersWithExcerpts(
7271
private async Task PullBlocksAsync(
7372
List<(BoundPeer, IBlockExcerpt)> peersWithBlockExcerpt,
7473
int chunkSize,
75-
IProgress<BlockSyncState> progress,
74+
IProgress<BlockSyncState>? progress,
7675
CancellationToken cancellationToken)
7776
{
7877
if (!peersWithBlockExcerpt.Any())
@@ -84,7 +83,7 @@ private async Task PullBlocksAsync(
8483
long totalBlocksToDownload = 0L;
8584
long receivedBlockCount = 0L;
8685
Block tempTip = BlockChain.Tip;
87-
var blocks = new List<(Block, BlockCommit)>();
86+
var blocks = new List<(Block, BlockCommit?)>();
8887

8988
try
9089
{
@@ -142,15 +141,15 @@ private async Task PullBlocksAsync(
142141
return;
143142
}
144143

145-
IAsyncEnumerable<Tuple<Block, BlockCommit, BoundPeer>> completedBlocks =
144+
IAsyncEnumerable<Tuple<Block, BlockCommit?, BoundPeer>> completedBlocks =
146145
blockCompletion.Complete(
147146
peers: peersWithBlockExcerpt.Select(pair => pair.Item1).ToList(),
148147
blockFetcher: GetBlocksAsync,
149148
cancellationToken: cancellationToken
150149
);
151150

152151
await foreach (
153-
(Block block, BlockCommit commit, BoundPeer sourcePeer)
152+
(Block block, BlockCommit? commit, BoundPeer sourcePeer)
154153
in completedBlocks.WithCancellation(cancellationToken))
155154
{
156155
_logger.Verbose(
@@ -208,13 +207,13 @@ in completedBlocks.WithCancellation(cancellationToken))
208207
}
209208

210209
BlockHash? previousHash = blocks.First().Item1.PreviousHash;
211-
Block branchpoint;
212-
BlockCommit branchpointCommit;
210+
Block? branchpoint;
211+
BlockCommit? branchpointCommit;
213212
if (previousHash != null)
214213
{
215214
branchpoint = BlockChain.Store.GetBlock(
216215
(BlockHash)previousHash);
217-
branchpointCommit = BlockChain.GetBlockCommit(branchpoint.Hash);
216+
branchpointCommit = BlockChain.GetBlockCommit(branchpoint!.Hash);
218217
}
219218
else
220219
{
@@ -321,7 +320,7 @@ await PullBlocksAsync(
321320
}
322321
}
323322

324-
private void OnBlockChainTipChanged(object sender, (Block OldTip, Block NewTip) e)
323+
private void OnBlockChainTipChanged(object? sender, (Block OldTip, Block NewTip) e)
325324
{
326325
if (Running)
327326
{

Libplanet.Net/Swarm.MessageHandlers.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#nullable disable
21
using System;
32
using System.Collections.Generic;
43
using System.Linq;

Libplanet.Net/Swarm.cs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#nullable disable
21
using System;
32
using System.Collections.Concurrent;
43
using System.Collections.Generic;
@@ -39,9 +38,9 @@ public partial class Swarm : IDisposable
3938

4039
private readonly ILogger _logger;
4140
private readonly IStore _store;
42-
private readonly ConsensusReactor _consensusReactor;
41+
private readonly ConsensusReactor? _consensusReactor;
4342

44-
private CancellationTokenSource _workerCancellationTokenSource;
43+
private CancellationTokenSource? _workerCancellationTokenSource;
4544
private CancellationToken _cancellationToken;
4645

4746
private bool _disposed;
@@ -66,8 +65,8 @@ public Swarm(
6665
BlockChain blockChain,
6766
PrivateKey privateKey,
6867
ITransport transport,
69-
SwarmOptions options = null,
70-
ITransport consensusTransport = null,
68+
SwarmOptions? options = null,
69+
ITransport? consensusTransport = null,
7170
ConsensusReactorOption? consensusOption = null)
7271
{
7372
BlockChain = blockChain ?? throw new ArgumentNullException(nameof(blockChain));
@@ -95,7 +94,7 @@ public Swarm(
9594
// code, the portion initializing the swarm in Agent.cs in NineChronicles should be
9695
// fixed. for context, refer to
9796
// https://github.com/planetarium/libplanet/discussions/2303.
98-
Transport = transport;
97+
Transport = transport ?? throw new ArgumentNullException(nameof(transport));
9998
_processBlockDemandSessions = new ConcurrentDictionary<BoundPeer, int>();
10099
Transport.ProcessMessageHandler.Register(ProcessMessageHandlerAsync);
101100
PeerDiscovery = new KademliaProtocol(RoutingTable, Transport, Address);
@@ -138,11 +137,11 @@ public Swarm(
138137

139138
public bool ConsensusRunning => _consensusReactor?.Running ?? false;
140139

141-
public DnsEndPoint EndPoint => AsPeer is BoundPeer boundPeer ? boundPeer.EndPoint : null;
140+
public DnsEndPoint EndPoint => AsPeer.EndPoint;
142141

143142
public Address Address => _privateKey.Address;
144143

145-
public BoundPeer AsPeer => Transport?.AsPeer;
144+
public BoundPeer AsPeer => Transport.AsPeer;
146145

147146
/// <summary>
148147
/// The last time when any message was arrived.
@@ -159,7 +158,7 @@ public Swarm(
159158
/// Returns list of the validators that consensus has in its routing table.
160159
/// If the node is not joining consensus, returns <c>null</c>.
161160
/// </summary>
162-
public IReadOnlyList<BoundPeer> Validators => _consensusReactor?.Validators;
161+
public IReadOnlyList<BoundPeer>? Validators => _consensusReactor?.Validators;
163162

164163
/// <summary>
165164
/// The <see cref="BlockChain"/> instance this <see cref="Swarm"/> instance
@@ -183,7 +182,7 @@ public Swarm(
183182

184183
internal TxCompletion<BoundPeer> TxCompletion { get; }
185184

186-
internal AsyncAutoResetEvent TxReceived => TxCompletion?.TxReceived;
185+
internal AsyncAutoResetEvent TxReceived => TxCompletion.TxReceived;
187186

188187
internal AsyncAutoResetEvent BlockHeaderReceived { get; }
189188

@@ -200,23 +199,24 @@ public Swarm(
200199
internal SwarmOptions Options { get; }
201200

202201
// FIXME: This should be exposed in a better way.
203-
internal ConsensusReactor ConsensusReactor => _consensusReactor;
202+
internal ConsensusReactor ConsensusReactor => _consensusReactor ??
203+
throw new InvalidOperationException();
204204

205205
/// <summary>
206206
/// Waits until this <see cref="Swarm"/> instance gets started to run.
207207
/// </summary>
208208
/// <seealso cref="ITransport.WaitForRunningAsync()"/>
209209
/// <returns>A <see cref="Task"/> completed when <see cref="ITransport.Running"/>
210210
/// property becomes <see langword="true"/>.</returns>
211-
public Task WaitForRunningAsync() => Transport?.WaitForRunningAsync();
211+
public Task WaitForRunningAsync() => Transport.WaitForRunningAsync();
212212

213213
public void Dispose()
214214
{
215215
if (!_disposed)
216216
{
217217
_workerCancellationTokenSource?.Cancel();
218218
TxCompletion?.Dispose();
219-
Transport?.Dispose();
219+
Transport.Dispose();
220220
_consensusReactor?.Dispose();
221221
_workerCancellationTokenSource?.Dispose();
222222
_disposed = true;
@@ -516,7 +516,7 @@ CancellationToken cancellationToken
516516
/// <exception cref="AggregateException">Thrown when the given the block downloading is
517517
/// failed.</exception>
518518
public async Task PreloadAsync(
519-
IProgress<BlockSyncState> progress = null,
519+
IProgress<BlockSyncState>? progress = null,
520520
CancellationToken cancellationToken = default)
521521
{
522522
await PreloadAsync(
@@ -556,7 +556,7 @@ await PreloadAsync(
556556
public async Task PreloadAsync(
557557
TimeSpan? dialTimeout,
558558
long tipDeltaThreshold,
559-
IProgress<BlockSyncState> progress = null,
559+
IProgress<BlockSyncState>? progress = null,
560560
CancellationToken cancellationToken = default)
561561
{
562562
using CancellationTokenRegistration ctr = cancellationToken.Register(() =>
@@ -663,7 +663,7 @@ await ConsumeBlockCandidates(
663663
/// A <see cref="BoundPeer"/> with <see cref="Address"/> of <paramref name="target"/>.
664664
/// Returns <see langword="null"/> if the peer with address does not exist.
665665
/// </returns>
666-
public async Task<BoundPeer> FindSpecificPeerAsync(
666+
public async Task<BoundPeer?> FindSpecificPeerAsync(
667667
Address target,
668668
int depth = 3,
669669
TimeSpan? timeout = null,
@@ -805,7 +805,7 @@ internal async IAsyncEnumerable<Tuple<long, BlockHash>> GetBlockHashes(
805805
throw new InvalidMessageContentException(errorMessage, parsedMessage.Content);
806806
}
807807

808-
internal async IAsyncEnumerable<(Block, BlockCommit)> GetBlocksAsync(
808+
internal async IAsyncEnumerable<(Block, BlockCommit?)> GetBlocksAsync(
809809
BoundPeer peer,
810810
IEnumerable<BlockHash> blockHashes,
811811
[EnumeratorCancellation] CancellationToken cancellationToken
@@ -870,7 +870,7 @@ [EnumeratorCancellation] CancellationToken cancellationToken
870870
cancellationToken.ThrowIfCancellationRequested();
871871
Block block = BlockMarshaler.UnmarshalBlock(
872872
(Bencodex.Types.Dictionary)Codec.Decode(blockPayload));
873-
BlockCommit commit = commitPayload.Length == 0
873+
BlockCommit? commit = commitPayload.Length == 0
874874
? null
875875
: new BlockCommit(Codec.Decode(commitPayload));
876876

@@ -985,7 +985,7 @@ internal async IAsyncEnumerable<Transaction> GetTxsAsync(
985985
BlockChain blockChain,
986986
IList<(BoundPeer, IBlockExcerpt)> peersWithExcerpts,
987987
int chunkSize = int.MaxValue,
988-
IProgress<BlockSyncState> progress = null,
988+
IProgress<BlockSyncState>? progress = null,
989989
[EnumeratorCancellation] CancellationToken cancellationToken = default
990990
)
991991
{
@@ -1008,7 +1008,7 @@ internal async IAsyncEnumerable<Transaction> GetTxsAsync(
10081008
int totalBlockHashesToDownload = -1;
10091009
int chunkBlockHashesToDownload = -1;
10101010
var pairsToYield = new List<Tuple<long, BlockHash>>();
1011-
Exception error = null;
1011+
Exception? error = null;
10121012
try
10131013
{
10141014
var downloaded = new List<BlockHash>();
@@ -1181,7 +1181,7 @@ private void BroadcastBlock(Address? except, Block block)
11811181
BroadcastMessage(except, message);
11821182
}
11831183

1184-
private void BroadcastTxs(BoundPeer except, IEnumerable<Transaction> txs)
1184+
private void BroadcastTxs(BoundPeer? except, IEnumerable<Transaction> txs)
11851185
{
11861186
List<TxId> txIds = txs.Select(tx => tx.Id).ToList();
11871187
_logger.Information("Broadcasting {Count} txIds...", txIds.Count);
@@ -1221,7 +1221,7 @@ private void BroadcastMessage(Address? except, MessageContent message)
12211221
pair => pair.Item2 is { } chainStatus &&
12221222
genesisHash.Equals(chainStatus.GenesisHash) &&
12231223
chainStatus.TipIndex > tip.Index)
1224-
.Select(pair => (pair.Item1, (IBlockExcerpt)pair.Item2))
1224+
.Select(pair => (pair.Item1, (IBlockExcerpt)pair.Item2!))
12251225
.OrderByDescending(pair => pair.Item2.Index)
12261226
.ToList();
12271227
}
@@ -1241,7 +1241,7 @@ private void BroadcastMessage(Address? except, MessageContent message)
12411241
/// of <see cref="BoundPeer"/> and <see cref="ChainStatusMsg"/> where
12421242
/// <see cref="ChainStatusMsg"/> can be <see langword="null"/> if dialing fails for
12431243
/// a selected <see cref="BoundPeer"/>.</returns>
1244-
private Task<(BoundPeer, ChainStatusMsg)[]> DialExistingPeers(
1244+
private Task<(BoundPeer, ChainStatusMsg?)[]> DialExistingPeers(
12451245
TimeSpan? dialTimeout,
12461246
int maxPeersToDial,
12471247
CancellationToken cancellationToken)
@@ -1268,15 +1268,15 @@ void LogException(BoundPeer peer, Task<Message> task)
12681268
}
12691269

12701270
var rnd = new System.Random();
1271-
IEnumerable<Task<(BoundPeer, ChainStatusMsg)>> tasks = Peers.OrderBy(_ => rnd.Next())
1271+
IEnumerable<Task<(BoundPeer, ChainStatusMsg?)>> tasks = Peers.OrderBy(_ => rnd.Next())
12721272
.Take(maxPeersToDial)
12731273
.Select(
12741274
peer => Transport.SendMessageAsync(
12751275
peer,
12761276
new GetChainStatusMsg(),
12771277
dialTimeout,
12781278
cancellationToken
1279-
).ContinueWith<(BoundPeer, ChainStatusMsg)>(
1279+
).ContinueWith<(BoundPeer, ChainStatusMsg?)>(
12801280
task =>
12811281
{
12821282
if (task.IsFaulted || task.IsCanceled ||
@@ -1300,7 +1300,7 @@ void LogException(BoundPeer peer, Task<Message> task)
13001300
{
13011301
if (task.IsFaulted)
13021302
{
1303-
throw task.Exception;
1303+
throw task.Exception!;
13041304
}
13051305

13061306
return task.Result.ToArray();

0 commit comments

Comments
 (0)