From b314f95bfe27bf5fef31793ff8b833f2807c5658 Mon Sep 17 00:00:00 2001 From: Fernando Diaz Toledano Date: Fri, 29 Dec 2023 12:44:37 +0100 Subject: [PATCH] Remove more nullables --- src/Neo.CLI/CLI/MainService.Blockchain.cs | 14 +++--- src/Neo.CLI/CLI/MainService.Contracts.cs | 6 +-- src/Neo.CLI/CLI/MainService.Wallet.cs | 52 ++++++++++++----------- src/Neo.CLI/CLI/MainService.cs | 2 +- 4 files changed, 38 insertions(+), 36 deletions(-) diff --git a/src/Neo.CLI/CLI/MainService.Blockchain.cs b/src/Neo.CLI/CLI/MainService.Blockchain.cs index 45f740efc6..f85368ec37 100644 --- a/src/Neo.CLI/CLI/MainService.Blockchain.cs +++ b/src/Neo.CLI/CLI/MainService.Blockchain.cs @@ -53,9 +53,9 @@ private void OnShowBlockCommand(string indexOrHash) Block? block = null; if (uint.TryParse(indexOrHash, out var index)) - block = NativeContract.Ledger.GetBlock(_neoSystem.StoreView, index); + block = NativeContract.Ledger.GetBlock(NeoSystem.StoreView, index); else if (UInt256.TryParse(indexOrHash, out var hash)) - block = NativeContract.Ledger.GetBlock(_neoSystem.StoreView, hash); + block = NativeContract.Ledger.GetBlock(NeoSystem.StoreView, hash); else { ConsoleHelper.Error("Enter a valid block index or hash."); @@ -81,7 +81,7 @@ private void OnShowBlockCommand(string indexOrHash) ConsoleHelper.Info("", " PrevHash: ", $"{block.PrevHash}"); ConsoleHelper.Info("", " NextConsensus: ", $"{block.NextConsensus}"); ConsoleHelper.Info("", " PrimaryIndex: ", $"{block.PrimaryIndex}"); - ConsoleHelper.Info("", " PrimaryPubKey: ", $"{NativeContract.NEO.GetCommittee(_neoSystem.GetSnapshot())[block.PrimaryIndex]}"); + ConsoleHelper.Info("", " PrimaryPubKey: ", $"{NativeContract.NEO.GetCommittee(NeoSystem.GetSnapshot())[block.PrimaryIndex]}"); ConsoleHelper.Info("", " Version: ", $"{block.Version}"); ConsoleHelper.Info("", " Size: ", $"{block.Size} Byte(s)"); ConsoleHelper.Info(); @@ -116,7 +116,7 @@ public void OnShowTransactionCommand(UInt256 hash) { lock (syncRoot) { - var tx = NativeContract.Ledger.GetTransactionState(_neoSystem.StoreView, hash); + var tx = NativeContract.Ledger.GetTransactionState(NeoSystem.StoreView, hash); if (tx is null) { @@ -124,7 +124,7 @@ public void OnShowTransactionCommand(UInt256 hash) return; } - var block = NativeContract.Ledger.GetHeader(_neoSystem.StoreView, tx.BlockIndex); + var block = NativeContract.Ledger.GetHeader(NeoSystem.StoreView, tx.BlockIndex); DateTime transactionDatetime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); transactionDatetime = transactionDatetime.AddMilliseconds(block.Timestamp).ToLocalTime(); @@ -231,13 +231,13 @@ public void OnShowContractCommand(string nameOrHash) ContractState? contract = null; if (UInt160.TryParse(nameOrHash, out var scriptHash)) - contract = NativeContract.ContractManagement.GetContract(_neoSystem.StoreView, scriptHash); + contract = NativeContract.ContractManagement.GetContract(NeoSystem.StoreView, scriptHash); else { var nativeContract = NativeContract.Contracts.SingleOrDefault(s => s.Name.Equals(nameOrHash, StringComparison.InvariantCultureIgnoreCase)); if (nativeContract != null) - contract = NativeContract.ContractManagement.GetContract(_neoSystem.StoreView, nativeContract.Hash); + contract = NativeContract.ContractManagement.GetContract(NeoSystem.StoreView, nativeContract.Hash); } if (contract is null) diff --git a/src/Neo.CLI/CLI/MainService.Contracts.cs b/src/Neo.CLI/CLI/MainService.Contracts.cs index 367b63dcdf..f60faddf04 100644 --- a/src/Neo.CLI/CLI/MainService.Contracts.cs +++ b/src/Neo.CLI/CLI/MainService.Contracts.cs @@ -35,7 +35,7 @@ private void OnDeployCommand(string filePath, string? manifestPath = null, JObje Transaction tx; try { - tx = CurrentWallet.MakeTransaction(NeoSystem.StoreView, script); + tx = CurrentWallet!.MakeTransaction(NeoSystem.StoreView, script); } catch (InvalidOperationException e) { @@ -91,7 +91,7 @@ private void OnUpdateCommand(UInt160 scriptHash, string filePath, string manifes try { byte[] script = LoadUpdateScript(scriptHash, filePath, manifestPath, data, out var nef, out var manifest); - tx = CurrentWallet.MakeTransaction(NeoSystem.StoreView, script, sender, signers); + tx = CurrentWallet!.MakeTransaction(NeoSystem.StoreView, script, sender, signers); } catch (InvalidOperationException e) { @@ -161,7 +161,7 @@ private void OnInvokeCommand(UInt160 scriptHash, string operation, JArray? contr if (NoWallet()) return; try { - tx = CurrentWallet.MakeTransaction(NeoSystem.StoreView, tx.Script, sender, signers, maxGas: (long)gas.Value); + tx = CurrentWallet!.MakeTransaction(NeoSystem.StoreView, tx.Script, sender, signers, maxGas: (long)gas.Value); } catch (InvalidOperationException e) { diff --git a/src/Neo.CLI/CLI/MainService.Wallet.cs b/src/Neo.CLI/CLI/MainService.Wallet.cs index f916ea1bbc..3fdb223dbf 100644 --- a/src/Neo.CLI/CLI/MainService.Wallet.cs +++ b/src/Neo.CLI/CLI/MainService.Wallet.cs @@ -124,7 +124,7 @@ private void OnCreateAddressCommand(ushort count = 1) { Parallel.For(0, count, (i) => { - WalletAccount account = CurrentWallet.CreateAccount(); + WalletAccount account = CurrentWallet!.CreateAccount(); lock (addresses) { addresses.Add(account.Address); @@ -151,7 +151,7 @@ private void OnDeleteAddressCommand(UInt160 address) if (ReadUserInput($"Warning: Irrevocable operation!\nAre you sure to delete account {address.ToAddress(NeoSystem.Settings.AddressVersion)}? (no|yes)").IsYes()) { - if (CurrentWallet.DeleteAccount(address)) + if (CurrentWallet!.DeleteAccount(address)) { if (CurrentWallet is NEP6Wallet wallet) { @@ -172,7 +172,7 @@ private void OnDeleteAddressCommand(UInt160 address) /// Path /// ScriptHash [ConsoleCommand("export key", Category = "Wallet Commands")] - private void OnExportKeyCommand(string path = null, UInt160 scriptHash = null) + private void OnExportKeyCommand(string? path = null, UInt160? scriptHash = null) { if (NoWallet()) return; if (path != null && File.Exists(path)) @@ -186,7 +186,7 @@ private void OnExportKeyCommand(string path = null, UInt160 scriptHash = null) ConsoleHelper.Info("Cancelled"); return; } - if (!CurrentWallet.VerifyPassword(password)) + if (!CurrentWallet!.VerifyPassword(password)) { ConsoleHelper.Error("Incorrect password"); return; @@ -210,7 +210,7 @@ private void OnExportKeyCommand(string path = null, UInt160 scriptHash = null) /// Process "create wallet" command /// [ConsoleCommand("create wallet", Category = "Wallet Commands")] - private void OnCreateWalletCommand(string path, string wifOrFile = null) + private void OnCreateWalletCommand(string path, string? wifOrFile = null) { string password = ReadUserInput("password", true); if (password.Length == 0) @@ -231,7 +231,7 @@ private void OnCreateWalletCommand(string path, string wifOrFile = null) } bool createDefaultAccount = wifOrFile is null; CreateWallet(path, password, createDefaultAccount); - if (!createDefaultAccount) OnImportKeyCommand(wifOrFile); + if (!createDefaultAccount) OnImportKeyCommand(wifOrFile!); } /// @@ -252,7 +252,7 @@ private void OnImportMultisigAddress(ushort m, ECPoint[] publicKeys) } Contract multiSignContract = Contract.CreateMultiSigContract(m, publicKeys); - KeyPair keyPair = CurrentWallet.GetAccounts().FirstOrDefault(p => p.HasKey && publicKeys.Contains(p.GetKey().PublicKey))?.GetKey(); + KeyPair? keyPair = CurrentWallet!.GetAccounts().FirstOrDefault(p => p.HasKey && publicKeys.Contains(p.GetKey().PublicKey))?.GetKey(); CurrentWallet.CreateAccount(multiSignContract, keyPair); if (CurrentWallet is NEP6Wallet wallet) @@ -268,7 +268,7 @@ private void OnImportMultisigAddress(ushort m, ECPoint[] publicKeys) private void OnImportKeyCommand(string wifOrFile) { if (NoWallet()) return; - byte[] prikey = null; + byte[]? prikey = null; try { prikey = Wallet.GetPrivateKeyFromWIF(wifOrFile); @@ -301,7 +301,7 @@ private void OnImportKeyCommand(string wifOrFile) prikey = lines[i].HexToBytes(); else prikey = Wallet.GetPrivateKeyFromWIF(lines[i]); - CurrentWallet.CreateAccount(prikey); + CurrentWallet!.CreateAccount(prikey); Array.Clear(prikey, 0, prikey.Length); percent.Value++; } @@ -309,7 +309,7 @@ private void OnImportKeyCommand(string wifOrFile) } else { - WalletAccount account = CurrentWallet.CreateAccount(prikey); + WalletAccount account = CurrentWallet!.CreateAccount(prikey); Array.Clear(prikey, 0, prikey.Length); ConsoleHelper.Info("Address: ", account.Address); ConsoleHelper.Info(" Pubkey: ", account.GetKey().PublicKey.EncodePoint(true).ToHexString()); @@ -325,7 +325,7 @@ private void OnImportKeyCommand(string wifOrFile) private void OnImportWatchOnlyCommand(string addressOrFile) { if (NoWallet()) return; - UInt160 address = null; + UInt160? address = null; try { address = StringToAddress(addressOrFile, NeoSystem.Settings.AddressVersion); @@ -355,14 +355,14 @@ private void OnImportWatchOnlyCommand(string addressOrFile) for (int i = 0; i < lines.Length; i++) { address = StringToAddress(lines[i], NeoSystem.Settings.AddressVersion); - CurrentWallet.CreateAccount(address); + CurrentWallet!.CreateAccount(address); percent.Value++; } } } else { - WalletAccount account = CurrentWallet.GetAccount(address); + WalletAccount account = CurrentWallet!.GetAccount(address); if (account is not null) { ConsoleHelper.Warning("This address is already in your wallet"); @@ -385,7 +385,7 @@ private void OnListAddressCommand() { if (NoWallet()) return; var snapshot = NeoSystem.StoreView; - foreach (var account in CurrentWallet.GetAccounts()) + foreach (var account in CurrentWallet!.GetAccounts()) { var contract = account.Contract; var type = "Nonstandard"; @@ -420,7 +420,7 @@ private void OnListAssetCommand() { var snapshot = NeoSystem.StoreView; if (NoWallet()) return; - foreach (UInt160 account in CurrentWallet.GetAccounts().Select(p => p.ScriptHash)) + foreach (UInt160 account in CurrentWallet!.GetAccounts().Select(p => p.ScriptHash)) { Console.WriteLine(account.ToAddress(NeoSystem.Settings.AddressVersion)); ConsoleHelper.Info("NEO: ", $"{CurrentWallet.GetBalance(snapshot, NativeContract.NEO.Hash, account)}"); @@ -441,7 +441,7 @@ private void OnListAssetCommand() private void OnListKeyCommand() { if (NoWallet()) return; - foreach (WalletAccount account in CurrentWallet.GetAccounts().Where(p => p.HasKey)) + foreach (WalletAccount account in CurrentWallet!.GetAccounts().Where(p => p.HasKey)) { ConsoleHelper.Info(" Address: ", account.Address); ConsoleHelper.Info("ScriptHash: ", account.ScriptHash.ToString()); @@ -473,7 +473,7 @@ private void OnSignCommand(JObject jsonObjectToSign) ConsoleHelper.Warning("Network mismatch."); return; } - else if (!CurrentWallet.Sign(context)) + else if (!CurrentWallet!.Sign(context)) { ConsoleHelper.Warning("Non-existent private key in wallet."); return; @@ -496,7 +496,7 @@ private void OnSignCommand(JObject jsonObjectToSign) /// Data /// Signer's accounts [ConsoleCommand("send", Category = "Wallet Commands")] - private void OnSendCommand(UInt160 asset, UInt160 to, string amount, UInt160 from = null, string data = null, UInt160[] signerAccounts = null) + private void OnSendCommand(UInt160 asset, UInt160 to, string amount, UInt160? from = null, string? data = null, UInt160[]? signerAccounts = null) { if (NoWallet()) return; string password = ReadUserInput("password", true); @@ -505,7 +505,7 @@ private void OnSendCommand(UInt160 asset, UInt160 to, string amount, UInt160 fro ConsoleHelper.Info("Cancelled"); return; } - if (!CurrentWallet.VerifyPassword(password)) + if (!CurrentWallet!.VerifyPassword(password)) { ConsoleHelper.Error("Incorrect password"); return; @@ -567,8 +567,10 @@ private void OnSendCommand(UInt160 asset, UInt160 to, string amount, UInt160 fro /// Transaction's sender /// Signer's accounts [ConsoleCommand("cancel", Category = "Wallet Commands")] - private void OnCancelCommand(UInt256 txid, UInt160 sender = null, UInt160[] signerAccounts = null) + private void OnCancelCommand(UInt256 txid, UInt160? sender = null, UInt160[]? signerAccounts = null) { + if (NoWallet()) return; + TransactionState state = NativeContract.Ledger.GetTransactionState(NeoSystem.StoreView, txid); if (state != null) { @@ -578,7 +580,7 @@ private void OnCancelCommand(UInt256 txid, UInt160 sender = null, UInt160[] sign var conflict = new TransactionAttribute[] { new Conflicts() { Hash = txid } }; Signer[] signers = Array.Empty(); - if (!NoWallet() && sender != null) + if (sender != null) { if (signerAccounts == null) signerAccounts = new UInt160[1] { sender }; @@ -595,7 +597,7 @@ private void OnCancelCommand(UInt256 txid, UInt160 sender = null, UInt160[] sign signers = signerAccounts.Select(p => new Signer() { Account = p, Scopes = WitnessScope.None }).ToArray(); } - Transaction tx = new Transaction + Transaction tx = new() { Signers = signers, Attributes = conflict, @@ -606,7 +608,7 @@ private void OnCancelCommand(UInt256 txid, UInt160 sender = null, UInt160[] sign { using ScriptBuilder scriptBuilder = new(); scriptBuilder.Emit(OpCode.RET); - tx = CurrentWallet.MakeTransaction(NeoSystem.StoreView, scriptBuilder.ToArray(), sender, signers, conflict); + tx = CurrentWallet!.MakeTransaction(NeoSystem.StoreView, scriptBuilder.ToArray(), sender, signers, conflict); } catch (InvalidOperationException e) { @@ -652,7 +654,7 @@ private void OnShowGasCommand() BigInteger gas = BigInteger.Zero; var snapshot = NeoSystem.StoreView; uint height = NativeContract.Ledger.CurrentIndex(snapshot) + 1; - foreach (UInt160 account in CurrentWallet.GetAccounts().Select(p => p.ScriptHash)) + foreach (UInt160 account in CurrentWallet!.GetAccounts().Select(p => p.ScriptHash)) gas += NativeContract.NEO.UnclaimedGas(snapshot, account, height); ConsoleHelper.Info("Unclaimed gas: ", new BigDecimal(gas, NativeContract.GAS.Decimals).ToString()); } @@ -670,7 +672,7 @@ private void OnChangePasswordCommand() ConsoleHelper.Info("Cancelled"); return; } - if (!CurrentWallet.VerifyPassword(oldPassword)) + if (!CurrentWallet!.VerifyPassword(oldPassword)) { ConsoleHelper.Error("Incorrect password"); return; diff --git a/src/Neo.CLI/CLI/MainService.cs b/src/Neo.CLI/CLI/MainService.cs index b20c7c5825..b3df2b042f 100644 --- a/src/Neo.CLI/CLI/MainService.cs +++ b/src/Neo.CLI/CLI/MainService.cs @@ -60,7 +60,7 @@ private set private NeoSystem? _neoSystem; public NeoSystem NeoSystem { - get => _neoSystem; + get => _neoSystem!; private set => _neoSystem = value; }