Skip to content

Commit

Permalink
Remove more nullables
Browse files Browse the repository at this point in the history
  • Loading branch information
shargon committed Dec 29, 2023
1 parent 06666c6 commit b314f95
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 36 deletions.
14 changes: 7 additions & 7 deletions src/Neo.CLI/CLI/MainService.Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand All @@ -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();
Expand Down Expand Up @@ -116,15 +116,15 @@ 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)
{
ConsoleHelper.Error($"Transaction {hash} doesn't exist.");
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();
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions src/Neo.CLI/CLI/MainService.Contracts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down
52 changes: 27 additions & 25 deletions src/Neo.CLI/CLI/MainService.Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)
{
Expand All @@ -172,7 +172,7 @@ private void OnDeleteAddressCommand(UInt160 address)
/// <param name="path">Path</param>
/// <param name="scriptHash">ScriptHash</param>
[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))
Expand All @@ -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;
Expand All @@ -210,7 +210,7 @@ private void OnExportKeyCommand(string path = null, UInt160 scriptHash = null)
/// Process "create wallet" command
/// </summary>
[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)
Expand All @@ -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!);
}

/// <summary>
Expand All @@ -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)
Expand All @@ -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);
Expand Down Expand Up @@ -301,15 +301,15 @@ 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++;
}
}
}
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());
Expand All @@ -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);
Expand Down Expand Up @@ -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");
Expand All @@ -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";
Expand Down Expand Up @@ -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)}");
Expand All @@ -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());
Expand Down Expand Up @@ -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;
Expand All @@ -496,7 +496,7 @@ private void OnSignCommand(JObject jsonObjectToSign)
/// <param name="data">Data</param>
/// <param name="signerAccounts">Signer's accounts</param>
[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);
Expand All @@ -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;
Expand Down Expand Up @@ -567,8 +567,10 @@ private void OnSendCommand(UInt160 asset, UInt160 to, string amount, UInt160 fro
/// <param name="sender">Transaction's sender</param>
/// <param name="signerAccounts">Signer's accounts</param>
[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)
{
Expand All @@ -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<Signer>();
if (!NoWallet() && sender != null)
if (sender != null)
{
if (signerAccounts == null)
signerAccounts = new UInt160[1] { sender };
Expand All @@ -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,
Expand All @@ -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)
{
Expand Down Expand Up @@ -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());
}
Expand All @@ -670,7 +672,7 @@ private void OnChangePasswordCommand()
ConsoleHelper.Info("Cancelled");
return;
}
if (!CurrentWallet.VerifyPassword(oldPassword))
if (!CurrentWallet!.VerifyPassword(oldPassword))
{
ConsoleHelper.Error("Incorrect password");
return;
Expand Down
2 changes: 1 addition & 1 deletion src/Neo.CLI/CLI/MainService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private set
private NeoSystem? _neoSystem;
public NeoSystem NeoSystem
{
get => _neoSystem;
get => _neoSystem!;
private set => _neoSystem = value;
}

Expand Down

0 comments on commit b314f95

Please sign in to comment.