1
- #nullable disable
2
1
using System ;
3
2
using System . Collections . Concurrent ;
4
3
using System . Collections . Generic ;
@@ -39,9 +38,9 @@ public partial class Swarm : IDisposable
39
38
40
39
private readonly ILogger _logger ;
41
40
private readonly IStore _store ;
42
- private readonly ConsensusReactor _consensusReactor ;
41
+ private readonly ConsensusReactor ? _consensusReactor ;
43
42
44
- private CancellationTokenSource _workerCancellationTokenSource ;
43
+ private CancellationTokenSource ? _workerCancellationTokenSource ;
45
44
private CancellationToken _cancellationToken ;
46
45
47
46
private bool _disposed ;
@@ -66,8 +65,8 @@ public Swarm(
66
65
BlockChain blockChain ,
67
66
PrivateKey privateKey ,
68
67
ITransport transport ,
69
- SwarmOptions options = null ,
70
- ITransport consensusTransport = null ,
68
+ SwarmOptions ? options = null ,
69
+ ITransport ? consensusTransport = null ,
71
70
ConsensusReactorOption ? consensusOption = null )
72
71
{
73
72
BlockChain = blockChain ?? throw new ArgumentNullException ( nameof ( blockChain ) ) ;
@@ -95,7 +94,7 @@ public Swarm(
95
94
// code, the portion initializing the swarm in Agent.cs in NineChronicles should be
96
95
// fixed. for context, refer to
97
96
// https://github.com/planetarium/libplanet/discussions/2303.
98
- Transport = transport ;
97
+ Transport = transport ?? throw new ArgumentNullException ( nameof ( transport ) ) ;
99
98
_processBlockDemandSessions = new ConcurrentDictionary < BoundPeer , int > ( ) ;
100
99
Transport . ProcessMessageHandler . Register ( ProcessMessageHandlerAsync ) ;
101
100
PeerDiscovery = new KademliaProtocol ( RoutingTable , Transport , Address ) ;
@@ -138,11 +137,11 @@ public Swarm(
138
137
139
138
public bool ConsensusRunning => _consensusReactor ? . Running ?? false ;
140
139
141
- public DnsEndPoint EndPoint => AsPeer is BoundPeer boundPeer ? boundPeer . EndPoint : null ;
140
+ public DnsEndPoint EndPoint => AsPeer . EndPoint ;
142
141
143
142
public Address Address => _privateKey . Address ;
144
143
145
- public BoundPeer AsPeer => Transport ? . AsPeer ;
144
+ public BoundPeer AsPeer => Transport . AsPeer ;
146
145
147
146
/// <summary>
148
147
/// The last time when any message was arrived.
@@ -159,7 +158,7 @@ public Swarm(
159
158
/// Returns list of the validators that consensus has in its routing table.
160
159
/// If the node is not joining consensus, returns <c>null</c>.
161
160
/// </summary>
162
- public IReadOnlyList < BoundPeer > Validators => _consensusReactor ? . Validators ;
161
+ public IReadOnlyList < BoundPeer > ? Validators => _consensusReactor ? . Validators ;
163
162
164
163
/// <summary>
165
164
/// The <see cref="BlockChain"/> instance this <see cref="Swarm"/> instance
@@ -183,7 +182,7 @@ public Swarm(
183
182
184
183
internal TxCompletion < BoundPeer > TxCompletion { get ; }
185
184
186
- internal AsyncAutoResetEvent TxReceived => TxCompletion ? . TxReceived ;
185
+ internal AsyncAutoResetEvent TxReceived => TxCompletion . TxReceived ;
187
186
188
187
internal AsyncAutoResetEvent BlockHeaderReceived { get ; }
189
188
@@ -200,23 +199,24 @@ public Swarm(
200
199
internal SwarmOptions Options { get ; }
201
200
202
201
// FIXME: This should be exposed in a better way.
203
- internal ConsensusReactor ConsensusReactor => _consensusReactor ;
202
+ internal ConsensusReactor ConsensusReactor => _consensusReactor ??
203
+ throw new InvalidOperationException ( ) ;
204
204
205
205
/// <summary>
206
206
/// Waits until this <see cref="Swarm"/> instance gets started to run.
207
207
/// </summary>
208
208
/// <seealso cref="ITransport.WaitForRunningAsync()"/>
209
209
/// <returns>A <see cref="Task"/> completed when <see cref="ITransport.Running"/>
210
210
/// property becomes <see langword="true"/>.</returns>
211
- public Task WaitForRunningAsync ( ) => Transport ? . WaitForRunningAsync ( ) ;
211
+ public Task WaitForRunningAsync ( ) => Transport . WaitForRunningAsync ( ) ;
212
212
213
213
public void Dispose ( )
214
214
{
215
215
if ( ! _disposed )
216
216
{
217
217
_workerCancellationTokenSource ? . Cancel ( ) ;
218
218
TxCompletion ? . Dispose ( ) ;
219
- Transport ? . Dispose ( ) ;
219
+ Transport . Dispose ( ) ;
220
220
_consensusReactor ? . Dispose ( ) ;
221
221
_workerCancellationTokenSource ? . Dispose ( ) ;
222
222
_disposed = true ;
@@ -516,7 +516,7 @@ CancellationToken cancellationToken
516
516
/// <exception cref="AggregateException">Thrown when the given the block downloading is
517
517
/// failed.</exception>
518
518
public async Task PreloadAsync (
519
- IProgress < BlockSyncState > progress = null ,
519
+ IProgress < BlockSyncState > ? progress = null ,
520
520
CancellationToken cancellationToken = default )
521
521
{
522
522
await PreloadAsync (
@@ -556,7 +556,7 @@ await PreloadAsync(
556
556
public async Task PreloadAsync (
557
557
TimeSpan ? dialTimeout ,
558
558
long tipDeltaThreshold ,
559
- IProgress < BlockSyncState > progress = null ,
559
+ IProgress < BlockSyncState > ? progress = null ,
560
560
CancellationToken cancellationToken = default )
561
561
{
562
562
using CancellationTokenRegistration ctr = cancellationToken . Register ( ( ) =>
@@ -663,7 +663,7 @@ await ConsumeBlockCandidates(
663
663
/// A <see cref="BoundPeer"/> with <see cref="Address"/> of <paramref name="target"/>.
664
664
/// Returns <see langword="null"/> if the peer with address does not exist.
665
665
/// </returns>
666
- public async Task < BoundPeer > FindSpecificPeerAsync (
666
+ public async Task < BoundPeer ? > FindSpecificPeerAsync (
667
667
Address target ,
668
668
int depth = 3 ,
669
669
TimeSpan ? timeout = null ,
@@ -805,7 +805,7 @@ internal async IAsyncEnumerable<Tuple<long, BlockHash>> GetBlockHashes(
805
805
throw new InvalidMessageContentException ( errorMessage , parsedMessage . Content ) ;
806
806
}
807
807
808
- internal async IAsyncEnumerable < ( Block , BlockCommit ) > GetBlocksAsync (
808
+ internal async IAsyncEnumerable < ( Block , BlockCommit ? ) > GetBlocksAsync (
809
809
BoundPeer peer ,
810
810
IEnumerable < BlockHash > blockHashes ,
811
811
[ EnumeratorCancellation ] CancellationToken cancellationToken
@@ -870,7 +870,7 @@ [EnumeratorCancellation] CancellationToken cancellationToken
870
870
cancellationToken . ThrowIfCancellationRequested ( ) ;
871
871
Block block = BlockMarshaler . UnmarshalBlock (
872
872
( Bencodex . Types . Dictionary ) Codec . Decode ( blockPayload ) ) ;
873
- BlockCommit commit = commitPayload . Length == 0
873
+ BlockCommit ? commit = commitPayload . Length == 0
874
874
? null
875
875
: new BlockCommit ( Codec . Decode ( commitPayload ) ) ;
876
876
@@ -985,7 +985,7 @@ internal async IAsyncEnumerable<Transaction> GetTxsAsync(
985
985
BlockChain blockChain ,
986
986
IList < ( BoundPeer , IBlockExcerpt ) > peersWithExcerpts ,
987
987
int chunkSize = int . MaxValue ,
988
- IProgress < BlockSyncState > progress = null ,
988
+ IProgress < BlockSyncState > ? progress = null ,
989
989
[ EnumeratorCancellation ] CancellationToken cancellationToken = default
990
990
)
991
991
{
@@ -1008,7 +1008,7 @@ internal async IAsyncEnumerable<Transaction> GetTxsAsync(
1008
1008
int totalBlockHashesToDownload = - 1 ;
1009
1009
int chunkBlockHashesToDownload = - 1 ;
1010
1010
var pairsToYield = new List < Tuple < long , BlockHash > > ( ) ;
1011
- Exception error = null ;
1011
+ Exception ? error = null ;
1012
1012
try
1013
1013
{
1014
1014
var downloaded = new List < BlockHash > ( ) ;
@@ -1181,7 +1181,7 @@ private void BroadcastBlock(Address? except, Block block)
1181
1181
BroadcastMessage ( except , message ) ;
1182
1182
}
1183
1183
1184
- private void BroadcastTxs ( BoundPeer except , IEnumerable < Transaction > txs )
1184
+ private void BroadcastTxs ( BoundPeer ? except , IEnumerable < Transaction > txs )
1185
1185
{
1186
1186
List < TxId > txIds = txs . Select ( tx => tx . Id ) . ToList ( ) ;
1187
1187
_logger . Information ( "Broadcasting {Count} txIds..." , txIds . Count ) ;
@@ -1221,7 +1221,7 @@ private void BroadcastMessage(Address? except, MessageContent message)
1221
1221
pair => pair . Item2 is { } chainStatus &&
1222
1222
genesisHash . Equals ( chainStatus . GenesisHash ) &&
1223
1223
chainStatus . TipIndex > tip . Index )
1224
- . Select ( pair => ( pair . Item1 , ( IBlockExcerpt ) pair . Item2 ) )
1224
+ . Select ( pair => ( pair . Item1 , ( IBlockExcerpt ) pair . Item2 ! ) )
1225
1225
. OrderByDescending ( pair => pair . Item2 . Index )
1226
1226
. ToList ( ) ;
1227
1227
}
@@ -1241,7 +1241,7 @@ private void BroadcastMessage(Address? except, MessageContent message)
1241
1241
/// of <see cref="BoundPeer"/> and <see cref="ChainStatusMsg"/> where
1242
1242
/// <see cref="ChainStatusMsg"/> can be <see langword="null"/> if dialing fails for
1243
1243
/// a selected <see cref="BoundPeer"/>.</returns>
1244
- private Task < ( BoundPeer , ChainStatusMsg ) [ ] > DialExistingPeers (
1244
+ private Task < ( BoundPeer , ChainStatusMsg ? ) [ ] > DialExistingPeers (
1245
1245
TimeSpan ? dialTimeout ,
1246
1246
int maxPeersToDial ,
1247
1247
CancellationToken cancellationToken )
@@ -1268,15 +1268,15 @@ void LogException(BoundPeer peer, Task<Message> task)
1268
1268
}
1269
1269
1270
1270
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 ( ) )
1272
1272
. Take ( maxPeersToDial )
1273
1273
. Select (
1274
1274
peer => Transport . SendMessageAsync (
1275
1275
peer ,
1276
1276
new GetChainStatusMsg ( ) ,
1277
1277
dialTimeout ,
1278
1278
cancellationToken
1279
- ) . ContinueWith < ( BoundPeer , ChainStatusMsg ) > (
1279
+ ) . ContinueWith < ( BoundPeer , ChainStatusMsg ? ) > (
1280
1280
task =>
1281
1281
{
1282
1282
if ( task . IsFaulted || task . IsCanceled ||
@@ -1300,7 +1300,7 @@ void LogException(BoundPeer peer, Task<Message> task)
1300
1300
{
1301
1301
if ( task . IsFaulted )
1302
1302
{
1303
- throw task . Exception ;
1303
+ throw task . Exception ! ;
1304
1304
}
1305
1305
1306
1306
return task . Result . ToArray ( ) ;
0 commit comments