Skip to content

Commit ac7ebab

Browse files
erikzhangshargon
andauthored
Nullable (#4287)
* Nullable * Fix ConsensusContext.cs * Update src/Neo/Cryptography/ECC/ECPoint.cs Co-authored-by: Shargon <[email protected]> --------- Co-authored-by: Shargon <[email protected]>
1 parent 86f4f0f commit ac7ebab

File tree

260 files changed

+1765
-1651
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

260 files changed

+1765
-1651
lines changed

src/Neo.CLI/CLI/MainService.Block.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
using Neo.Ledger;
1616
using Neo.Network.P2P;
1717
using Neo.Network.P2P.Payloads;
18-
using Neo.SmartContract;
1918
using Neo.SmartContract.Native;
2019
using System;
2120
using System.Collections.Generic;
@@ -193,7 +192,7 @@ private void WriteBlocks(uint start, uint count, string path, bool writeStart)
193192
{
194193
for (uint i = start; i <= end; i++)
195194
{
196-
Block block = NativeContract.Ledger.GetBlock(NeoSystem.StoreView, i);
195+
Block block = NativeContract.Ledger.GetBlock(NeoSystem.StoreView, i)!;
197196
byte[] array = block.ToArray();
198197
fs.Write(BitConverter.GetBytes(array.Length), 0, sizeof(int));
199198
fs.Write(array, 0, array.Length);
@@ -220,11 +219,7 @@ private async Task ImportBlocksFromFile(bool verifyImport)
220219
blocksToImport.Add(blocksBeingImported.Current);
221220
}
222221
if (blocksToImport.Count == 0) break;
223-
await NeoSystem.Blockchain.Ask<Blockchain.ImportCompleted>(new Blockchain.Import
224-
{
225-
Blocks = blocksToImport,
226-
Verify = verifyImport
227-
});
222+
await NeoSystem.Blockchain.Ask<Blockchain.ImportCompleted>(new Blockchain.Import(blocksToImport, verifyImport));
228223
if (NeoSystem is null) return;
229224
}
230225
}

src/Neo.CLI/CLI/MainService.Blockchain.cs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public void OnShowTransactionCommand(UInt256 hash)
9999
return;
100100
}
101101

102-
var block = NativeContract.Ledger.GetHeader(NeoSystem.StoreView, tx.BlockIndex);
102+
var block = NativeContract.Ledger.GetHeader(NeoSystem.StoreView, tx.BlockIndex)!;
103103

104104
var transactionDatetime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
105105
transactionDatetime = transactionDatetime.AddMilliseconds(block.Timestamp).ToLocalTime();
@@ -126,20 +126,20 @@ public void OnShowTransactionCommand(UInt256 hash)
126126

127127
foreach (var signer in tx.Transaction.Signers)
128128
{
129-
if (signer.Rules.Length == 0)
130-
ConsoleHelper.Info("", " Rules: ", "[]");
131-
else
129+
if (signer.Rules?.Length > 0)
132130
ConsoleHelper.Info("", " Rules: ", $"[{string.Join(", ", signer.Rules.Select(s => $"\"{s.ToJson()}\""))}]");
131+
else
132+
ConsoleHelper.Info("", " Rules: ", "[]");
133133
ConsoleHelper.Info("", " Account: ", $"{signer.Account}");
134134
ConsoleHelper.Info("", " Scopes: ", $"{signer.Scopes}");
135-
if (signer.AllowedContracts.Length == 0)
136-
ConsoleHelper.Info("", " AllowedContracts: ", "[]");
137-
else
135+
if (signer.AllowedContracts?.Length > 0)
138136
ConsoleHelper.Info("", " AllowedContracts: ", $"[{string.Join(", ", signer.AllowedContracts.Select(s => s.ToString()))}]");
139-
if (signer.AllowedGroups.Length == 0)
140-
ConsoleHelper.Info("", " AllowedGroups: ", "[]");
141137
else
138+
ConsoleHelper.Info("", " AllowedContracts: ", "[]");
139+
if (signer.AllowedGroups?.Length > 0)
142140
ConsoleHelper.Info("", " AllowedGroups: ", $"[{string.Join(", ", signer.AllowedGroups.Select(s => s.ToString()))}]");
141+
else
142+
ConsoleHelper.Info("", " AllowedGroups: ", "[]");
143143
ConsoleHelper.Info("", " Size: ", $"{signer.Size} Byte(s)");
144144
ConsoleHelper.Info();
145145
}
@@ -209,15 +209,17 @@ public void OnShowContractCommand(string nameOrHash)
209209
{
210210
ContractState? contract = null;
211211
NativeContract? nativeContract = null;
212-
var isHash = UInt160.TryParse(nameOrHash, out var scriptHash);
213-
if (isHash)
212+
bool isHash;
213+
if (UInt160.TryParse(nameOrHash, out var scriptHash))
214214
{
215+
isHash = true;
215216
contract = NativeContract.ContractManagement.GetContract(NeoSystem.StoreView, scriptHash);
216217
if (contract is null)
217218
nativeContract = NativeContract.Contracts.SingleOrDefault(s => s.Hash == scriptHash);
218219
}
219220
else
220221
{
222+
isHash = false;
221223
nativeContract = NativeContract.Contracts.SingleOrDefault(s => s.Name.Equals(nameOrHash, StringComparison.InvariantCultureIgnoreCase));
222224
if (nativeContract != null)
223225
contract = NativeContract.ContractManagement.GetContract(NeoSystem.StoreView, nativeContract.Hash);

src/Neo.CLI/CLI/MainService.Contracts.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ private void OnUpdateCommand(UInt160 scriptHash, string filePath, string manifes
101101
ConsoleHelper.Error(GetExceptionMessage(e));
102102
return;
103103
}
104-
ContractState contract = NativeContract.ContractManagement.GetContract(NeoSystem.StoreView, scriptHash);
104+
ContractState? contract = NativeContract.ContractManagement.GetContract(NeoSystem.StoreView, scriptHash);
105105
if (contract == null)
106106
{
107107
ConsoleHelper.Warning($"Can't upgrade, contract hash not exist: {scriptHash}");
@@ -220,17 +220,17 @@ private void OnInvokeAbiCommand(UInt160 scriptHash, string operation,
220220
// Find the method in the ABI with matching parameter count
221221
var paramCount = args?.Count ?? 0;
222222
var method = contract.Manifest.Abi.GetMethod(operation, paramCount);
223-
if (method == null)
223+
if (method is null)
224224
{
225225
// Try to find any method with that name for a better error message
226226
var anyMethod = contract.Manifest.Abi.GetMethod(operation, -1);
227-
if (anyMethod != null)
227+
if (anyMethod is null)
228228
{
229-
ConsoleHelper.Error($"Method '{operation}' exists but expects {anyMethod.Parameters.Length} parameters, not {paramCount}.");
229+
ConsoleHelper.Error($"Method '{operation}' does not exist in this contract.");
230230
}
231231
else
232232
{
233-
ConsoleHelper.Error($"Method '{operation}' does not exist in this contract.");
233+
ConsoleHelper.Error($"Method '{operation}' exists but expects {anyMethod.Parameters.Length} parameters, not {paramCount}.");
234234
}
235235
return;
236236
}

src/Neo.CLI/CLI/MainService.NEP17.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ private void OnBalanceOfCommand(UInt160 tokenHash, UInt160 address)
105105
[ConsoleCommand("name", Category = "NEP17 Commands")]
106106
private void OnNameCommand(UInt160 tokenHash)
107107
{
108-
ContractState contract = NativeContract.ContractManagement.GetContract(NeoSystem.StoreView, tokenHash);
108+
ContractState? contract = NativeContract.ContractManagement.GetContract(NeoSystem.StoreView, tokenHash);
109109
if (contract == null) Console.WriteLine($"Contract hash not exist: {tokenHash}");
110110
else ConsoleHelper.Info("Result: ", contract.Manifest.Name);
111111
}

src/Neo.CLI/CLI/MainService.Network.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ private void OnBroadcastAddressCommand(IPAddress payload, ushort port)
5454
[ConsoleCommand("broadcast block", Category = "Network Commands")]
5555
private void OnBroadcastGetBlocksByHashCommand(UInt256 hash)
5656
{
57-
OnBroadcastCommand(MessageCommand.Block, NativeContract.Ledger.GetBlock(NeoSystem.StoreView, hash));
57+
Block? block = NativeContract.Ledger.GetBlock(NeoSystem.StoreView, hash);
58+
if (block is null)
59+
ConsoleHelper.Error("Block is not found.");
60+
else
61+
OnBroadcastCommand(MessageCommand.Block, block);
5862
}
5963

6064
/// <summary>
@@ -64,7 +68,11 @@ private void OnBroadcastGetBlocksByHashCommand(UInt256 hash)
6468
[ConsoleCommand("broadcast block", Category = "Network Commands")]
6569
private void OnBroadcastGetBlocksByHeightCommand(uint height)
6670
{
67-
OnBroadcastCommand(MessageCommand.Block, NativeContract.Ledger.GetBlock(NeoSystem.StoreView, height));
71+
Block? block = NativeContract.Ledger.GetBlock(NeoSystem.StoreView, height);
72+
if (block is null)
73+
ConsoleHelper.Error("Block is not found.");
74+
else
75+
OnBroadcastCommand(MessageCommand.Block, block);
6876
}
6977

7078
/// <summary>

src/Neo.CLI/CLI/MainService.Tools.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ private static string Base64Fixed(string str)
263263
{
264264
try
265265
{
266-
UInt160 scriptHash;
266+
UInt160? scriptHash;
267267
if (script.StartsWith("0x"))
268268
{
269269
if (!UInt160.TryParse(script, out scriptHash))
@@ -273,7 +273,7 @@ private static string Base64Fixed(string str)
273273
}
274274
else
275275
{
276-
if (!UInt160.TryParse(script, out UInt160 littleEndScript))
276+
if (!UInt160.TryParse(script, out UInt160? littleEndScript))
277277
{
278278
return null;
279279
}

src/Neo.CLI/CLI/MainService.Wallet.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,11 @@ private void OnExportKeyCommand(string? path = null, UInt160? scriptHash = null)
203203
}
204204
IEnumerable<KeyPair> keys;
205205
if (scriptHash == null)
206-
keys = CurrentWallet.GetAccounts().Where(p => p.HasKey).Select(p => p.GetKey());
206+
keys = CurrentWallet.GetAccounts().Where(p => p.HasKey).Select(p => p.GetKey()!);
207207
else
208208
{
209209
var account = CurrentWallet.GetAccount(scriptHash);
210-
keys = account?.HasKey != true ? Array.Empty<KeyPair>() : new[] { account.GetKey() };
210+
keys = account?.HasKey != true ? Array.Empty<KeyPair>() : new[] { account.GetKey()! };
211211
}
212212
if (path == null)
213213
foreach (KeyPair key in keys)
@@ -273,7 +273,7 @@ private void OnImportMultisigAddress(ushort m, ECPoint[] publicKeys)
273273
}
274274

275275
Contract multiSignContract = Contract.CreateMultiSigContract(m, publicKeys);
276-
KeyPair? keyPair = CurrentWallet!.GetAccounts().FirstOrDefault(p => p.HasKey && publicKeys.Contains(p.GetKey().PublicKey))?.GetKey();
276+
KeyPair? keyPair = CurrentWallet!.GetAccounts().FirstOrDefault(p => p.HasKey && publicKeys.Contains(p.GetKey()!.PublicKey))?.GetKey();
277277

278278
CurrentWallet.CreateAccount(multiSignContract, keyPair);
279279
if (CurrentWallet is NEP6Wallet wallet)
@@ -333,7 +333,7 @@ private void OnImportKeyCommand(string wifOrFile)
333333
WalletAccount account = CurrentWallet!.CreateAccount(prikey);
334334
Array.Clear(prikey, 0, prikey.Length);
335335
ConsoleHelper.Info("Address: ", account.Address);
336-
ConsoleHelper.Info(" Pubkey: ", account.GetKey().PublicKey.EncodePoint(true).ToHexString());
336+
ConsoleHelper.Info(" Pubkey: ", account.GetKey()!.PublicKey.EncodePoint(true).ToHexString());
337337
}
338338
if (CurrentWallet is NEP6Wallet wallet)
339339
wallet.Save();
@@ -383,7 +383,7 @@ private void OnImportWatchOnlyCommand(string addressOrFile)
383383
}
384384
else
385385
{
386-
WalletAccount account = CurrentWallet!.GetAccount(address);
386+
WalletAccount? account = CurrentWallet!.GetAccount(address);
387387
if (account is not null)
388388
{
389389
ConsoleHelper.Warning("This address is already in your wallet");
@@ -415,7 +415,7 @@ private void OnListAddressCommand()
415415
{
416416
type = "WatchOnly";
417417
}
418-
else if (IsMultiSigContract(contract.Script))
418+
else if (IsMultiSigContract(contract!.Script))
419419
{
420420
type = "MultiSignature";
421421
}
@@ -467,7 +467,7 @@ private void OnListKeyCommand()
467467
{
468468
ConsoleHelper.Info(" Address: ", account.Address);
469469
ConsoleHelper.Info("ScriptHash: ", account.ScriptHash.ToString());
470-
ConsoleHelper.Info(" PublicKey: ", account.GetKey().PublicKey.EncodePoint(true).ToHexString());
470+
ConsoleHelper.Info(" PublicKey: ", account.GetKey()!.PublicKey.EncodePoint(true).ToHexString());
471471
Console.WriteLine();
472472
}
473473
}
@@ -594,7 +594,7 @@ private void OnCancelCommand(UInt256 txid, UInt160? sender = null, UInt160[]? si
594594
{
595595
if (NoWallet()) return;
596596

597-
TransactionState state = NativeContract.Ledger.GetTransactionState(NeoSystem.StoreView, txid);
597+
TransactionState? state = NativeContract.Ledger.GetTransactionState(NeoSystem.StoreView, txid);
598598
if (state != null)
599599
{
600600
ConsoleHelper.Error("This tx is already confirmed, can't be cancelled.");

src/Neo.CLI/CLI/MainService.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ namespace Neo.CLI
4242
{
4343
public partial class MainService : ConsoleServiceBase, IWalletProvider
4444
{
45-
public event EventHandler<Wallet?>? WalletChanged = null;
45+
public event EventHandler<Wallet?>? WalletChanged;
4646

4747
public const long TestModeGas = 20_00000000;
4848

@@ -142,7 +142,7 @@ public override void RunConsole()
142142

143143
public void CreateWallet(string path, string password, bool createDefaultAccount = true, string? walletName = null)
144144
{
145-
Wallet wallet = Wallet.Create(walletName, path, password, NeoSystem.Settings);
145+
Wallet? wallet = Wallet.Create(walletName, path, password, NeoSystem.Settings);
146146
if (wallet == null)
147147
{
148148
ConsoleHelper.Warning("Wallet files in that format are not supported, please use a .json or .db3 file extension.");
@@ -152,7 +152,7 @@ public void CreateWallet(string path, string password, bool createDefaultAccount
152152
{
153153
WalletAccount account = wallet.CreateAccount();
154154
ConsoleHelper.Info(" Address: ", account.Address);
155-
ConsoleHelper.Info(" Pubkey: ", account.GetKey().PublicKey.EncodePoint(true).ToHexString());
155+
ConsoleHelper.Info(" Pubkey: ", account.GetKey()!.PublicKey.EncodePoint(true).ToHexString());
156156
ConsoleHelper.Info("ScriptHash: ", $"{account.ScriptHash}");
157157
}
158158
wallet.Save();
@@ -503,7 +503,7 @@ private bool OnInvokeWithResult(UInt160 scriptHash, string operation, out StackI
503503
}
504504
else
505505
{
506-
if (contract.Manifest.Abi.GetMethod(operation, parameters.Count) == null)
506+
if (contract.Manifest.Abi.GetMethod(operation, parameters.Count) is null)
507507
{
508508
ConsoleHelper.Error("This method does not not exist in this contract.");
509509
result = StackItem.Null;
@@ -539,7 +539,7 @@ private void PrintExecutionOutput(ApplicationEngine engine, bool showStack = tru
539539
ConsoleHelper.Info("Result Stack: ", new JArray(engine.ResultStack.Select(p => p.ToJson())).ToString());
540540

541541
if (engine.State == VMState.FAULT)
542-
ConsoleHelper.Error(GetExceptionMessage(engine.FaultException));
542+
ConsoleHelper.Error(GetExceptionMessage(engine.FaultException!));
543543
}
544544

545545
static string GetExceptionMessage(Exception exception)
@@ -570,7 +570,7 @@ static string GetExceptionMessage(Exception exception)
570570
{
571571
try
572572
{
573-
var addressData = data.GetString();
573+
var addressData = data.GetString()!;
574574
if (UInt160.TryParse(addressData, out var address))
575575
return address;
576576
else

src/Neo.CLI/Settings.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
using Neo.Network.P2P;
1414
using Neo.Persistence.Providers;
1515
using System;
16-
using System.IO;
1716
using System.Reflection;
1817
using System.Threading;
1918

@@ -47,7 +46,7 @@ public static Settings Default
4746
{
4847
if (s_default == null)
4948
{
50-
var configFile = ProtocolSettings.FindFile("config.json", Environment.CurrentDirectory);
49+
var configFile = ProtocolSettings.FindFile("config.json", Environment.CurrentDirectory)!;
5150
var config = new ConfigurationBuilder().AddJsonFile(configFile, optional: true).Build();
5251
Initialize(config);
5352
}

src/Neo.Cryptography.MPTTrie/Cache.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public void Commit()
102102
{
103103
case TrackState.Added:
104104
case TrackState.Changed:
105-
_store.Put(Key(item.Key), item.Value.Node.ToArray());
105+
_store.Put(Key(item.Key), item.Value.Node!.ToArray());
106106
break;
107107
case TrackState.Deleted:
108108
_store.Delete(Key(item.Key));

0 commit comments

Comments
 (0)