Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions src/Neo.CLI/CLI/MainService.Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
using Neo.Ledger;
using Neo.Network.P2P;
using Neo.Network.P2P.Payloads;
using Neo.SmartContract;
using Neo.SmartContract.Native;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -193,7 +192,7 @@ private void WriteBlocks(uint start, uint count, string path, bool writeStart)
{
for (uint i = start; i <= end; i++)
{
Block block = NativeContract.Ledger.GetBlock(NeoSystem.StoreView, i);
Block block = NativeContract.Ledger.GetBlock(NeoSystem.StoreView, i)!;
byte[] array = block.ToArray();
fs.Write(BitConverter.GetBytes(array.Length), 0, sizeof(int));
fs.Write(array, 0, array.Length);
Expand All @@ -220,11 +219,7 @@ private async Task ImportBlocksFromFile(bool verifyImport)
blocksToImport.Add(blocksBeingImported.Current);
}
if (blocksToImport.Count == 0) break;
await NeoSystem.Blockchain.Ask<Blockchain.ImportCompleted>(new Blockchain.Import
{
Blocks = blocksToImport,
Verify = verifyImport
});
await NeoSystem.Blockchain.Ask<Blockchain.ImportCompleted>(new Blockchain.Import(blocksToImport, verifyImport));
if (NeoSystem is null) return;
}
}
Expand Down
24 changes: 13 additions & 11 deletions src/Neo.CLI/CLI/MainService.Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,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)!;

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

foreach (var signer in tx.Transaction.Signers)
{
if (signer.Rules.Length == 0)
ConsoleHelper.Info("", " Rules: ", "[]");
else
if (signer.Rules?.Length > 0)
ConsoleHelper.Info("", " Rules: ", $"[{string.Join(", ", signer.Rules.Select(s => $"\"{s.ToJson()}\""))}]");
else
ConsoleHelper.Info("", " Rules: ", "[]");
ConsoleHelper.Info("", " Account: ", $"{signer.Account}");
ConsoleHelper.Info("", " Scopes: ", $"{signer.Scopes}");
if (signer.AllowedContracts.Length == 0)
ConsoleHelper.Info("", " AllowedContracts: ", "[]");
else
if (signer.AllowedContracts?.Length > 0)
ConsoleHelper.Info("", " AllowedContracts: ", $"[{string.Join(", ", signer.AllowedContracts.Select(s => s.ToString()))}]");
if (signer.AllowedGroups.Length == 0)
ConsoleHelper.Info("", " AllowedGroups: ", "[]");
else
ConsoleHelper.Info("", " AllowedContracts: ", "[]");
if (signer.AllowedGroups?.Length > 0)
ConsoleHelper.Info("", " AllowedGroups: ", $"[{string.Join(", ", signer.AllowedGroups.Select(s => s.ToString()))}]");
else
ConsoleHelper.Info("", " AllowedGroups: ", "[]");
ConsoleHelper.Info("", " Size: ", $"{signer.Size} Byte(s)");
ConsoleHelper.Info();
}
Expand Down Expand Up @@ -209,15 +209,17 @@ public void OnShowContractCommand(string nameOrHash)
{
ContractState? contract = null;
NativeContract? nativeContract = null;
var isHash = UInt160.TryParse(nameOrHash, out var scriptHash);
if (isHash)
bool isHash;
if (UInt160.TryParse(nameOrHash, out var scriptHash))
{
isHash = true;
contract = NativeContract.ContractManagement.GetContract(NeoSystem.StoreView, scriptHash);
if (contract is null)
nativeContract = NativeContract.Contracts.SingleOrDefault(s => s.Hash == scriptHash);
}
else
{
isHash = false;
nativeContract = NativeContract.Contracts.SingleOrDefault(s => s.Name.Equals(nameOrHash, StringComparison.InvariantCultureIgnoreCase));
if (nativeContract != null)
contract = NativeContract.ContractManagement.GetContract(NeoSystem.StoreView, nativeContract.Hash);
Expand Down
10 changes: 5 additions & 5 deletions src/Neo.CLI/CLI/MainService.Contracts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private void OnUpdateCommand(UInt160 scriptHash, string filePath, string manifes
ConsoleHelper.Error(GetExceptionMessage(e));
return;
}
ContractState contract = NativeContract.ContractManagement.GetContract(NeoSystem.StoreView, scriptHash);
ContractState? contract = NativeContract.ContractManagement.GetContract(NeoSystem.StoreView, scriptHash);
if (contract == null)
{
ConsoleHelper.Warning($"Can't upgrade, contract hash not exist: {scriptHash}");
Expand Down Expand Up @@ -220,17 +220,17 @@ private void OnInvokeAbiCommand(UInt160 scriptHash, string operation,
// Find the method in the ABI with matching parameter count
var paramCount = args?.Count ?? 0;
var method = contract.Manifest.Abi.GetMethod(operation, paramCount);
if (method == null)
if (method is null)
{
// Try to find any method with that name for a better error message
var anyMethod = contract.Manifest.Abi.GetMethod(operation, -1);
if (anyMethod != null)
if (anyMethod is null)
{
ConsoleHelper.Error($"Method '{operation}' exists but expects {anyMethod.Parameters.Length} parameters, not {paramCount}.");
ConsoleHelper.Error($"Method '{operation}' does not exist in this contract.");
}
else
{
ConsoleHelper.Error($"Method '{operation}' does not exist in this contract.");
ConsoleHelper.Error($"Method '{operation}' exists but expects {anyMethod.Parameters.Length} parameters, not {paramCount}.");
}
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Neo.CLI/CLI/MainService.NEP17.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ private void OnBalanceOfCommand(UInt160 tokenHash, UInt160 address)
[ConsoleCommand("name", Category = "NEP17 Commands")]
private void OnNameCommand(UInt160 tokenHash)
{
ContractState contract = NativeContract.ContractManagement.GetContract(NeoSystem.StoreView, tokenHash);
ContractState? contract = NativeContract.ContractManagement.GetContract(NeoSystem.StoreView, tokenHash);
if (contract == null) Console.WriteLine($"Contract hash not exist: {tokenHash}");
else ConsoleHelper.Info("Result: ", contract.Manifest.Name);
}
Expand Down
12 changes: 10 additions & 2 deletions src/Neo.CLI/CLI/MainService.Network.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ private void OnBroadcastAddressCommand(IPAddress payload, ushort port)
[ConsoleCommand("broadcast block", Category = "Network Commands")]
private void OnBroadcastGetBlocksByHashCommand(UInt256 hash)
{
OnBroadcastCommand(MessageCommand.Block, NativeContract.Ledger.GetBlock(NeoSystem.StoreView, hash));
Block? block = NativeContract.Ledger.GetBlock(NeoSystem.StoreView, hash);
if (block is null)
ConsoleHelper.Error("Block is not found.");
else
OnBroadcastCommand(MessageCommand.Block, block);
}

/// <summary>
Expand All @@ -64,7 +68,11 @@ private void OnBroadcastGetBlocksByHashCommand(UInt256 hash)
[ConsoleCommand("broadcast block", Category = "Network Commands")]
private void OnBroadcastGetBlocksByHeightCommand(uint height)
{
OnBroadcastCommand(MessageCommand.Block, NativeContract.Ledger.GetBlock(NeoSystem.StoreView, height));
Block? block = NativeContract.Ledger.GetBlock(NeoSystem.StoreView, height);
if (block is null)
ConsoleHelper.Error("Block is not found.");
else
OnBroadcastCommand(MessageCommand.Block, block);
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/Neo.CLI/CLI/MainService.Tools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ private static string Base64Fixed(string str)
{
try
{
UInt160 scriptHash;
UInt160? scriptHash;
if (script.StartsWith("0x"))
{
if (!UInt160.TryParse(script, out scriptHash))
Expand All @@ -273,7 +273,7 @@ private static string Base64Fixed(string str)
}
else
{
if (!UInt160.TryParse(script, out UInt160 littleEndScript))
if (!UInt160.TryParse(script, out UInt160? littleEndScript))
{
return null;
}
Expand Down
16 changes: 8 additions & 8 deletions src/Neo.CLI/CLI/MainService.Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,11 @@ private void OnExportKeyCommand(string? path = null, UInt160? scriptHash = null)
}
IEnumerable<KeyPair> keys;
if (scriptHash == null)
keys = CurrentWallet.GetAccounts().Where(p => p.HasKey).Select(p => p.GetKey());
keys = CurrentWallet.GetAccounts().Where(p => p.HasKey).Select(p => p.GetKey()!);
else
{
var account = CurrentWallet.GetAccount(scriptHash);
keys = account?.HasKey != true ? Array.Empty<KeyPair>() : new[] { account.GetKey() };
keys = account?.HasKey != true ? Array.Empty<KeyPair>() : new[] { account.GetKey()! };
}
if (path == null)
foreach (KeyPair key in keys)
Expand Down Expand Up @@ -273,7 +273,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 Down Expand Up @@ -333,7 +333,7 @@ private void OnImportKeyCommand(string wifOrFile)
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());
ConsoleHelper.Info(" Pubkey: ", account.GetKey()!.PublicKey.EncodePoint(true).ToHexString());
}
if (CurrentWallet is NEP6Wallet wallet)
wallet.Save();
Expand Down Expand Up @@ -383,7 +383,7 @@ private void OnImportWatchOnlyCommand(string addressOrFile)
}
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 Down Expand Up @@ -415,7 +415,7 @@ private void OnListAddressCommand()
{
type = "WatchOnly";
}
else if (IsMultiSigContract(contract.Script))
else if (IsMultiSigContract(contract!.Script))
{
type = "MultiSignature";
}
Expand Down Expand Up @@ -467,7 +467,7 @@ private void OnListKeyCommand()
{
ConsoleHelper.Info(" Address: ", account.Address);
ConsoleHelper.Info("ScriptHash: ", account.ScriptHash.ToString());
ConsoleHelper.Info(" PublicKey: ", account.GetKey().PublicKey.EncodePoint(true).ToHexString());
ConsoleHelper.Info(" PublicKey: ", account.GetKey()!.PublicKey.EncodePoint(true).ToHexString());
Console.WriteLine();
}
}
Expand Down Expand Up @@ -594,7 +594,7 @@ private void OnCancelCommand(UInt256 txid, UInt160? sender = null, UInt160[]? si
{
if (NoWallet()) return;

TransactionState state = NativeContract.Ledger.GetTransactionState(NeoSystem.StoreView, txid);
TransactionState? state = NativeContract.Ledger.GetTransactionState(NeoSystem.StoreView, txid);
if (state != null)
{
ConsoleHelper.Error("This tx is already confirmed, can't be cancelled.");
Expand Down
12 changes: 6 additions & 6 deletions src/Neo.CLI/CLI/MainService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ namespace Neo.CLI
{
public partial class MainService : ConsoleServiceBase, IWalletProvider
{
public event EventHandler<Wallet?>? WalletChanged = null;
public event EventHandler<Wallet?>? WalletChanged;

public const long TestModeGas = 20_00000000;

Expand Down Expand Up @@ -142,7 +142,7 @@ public override void RunConsole()

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

if (engine.State == VMState.FAULT)
ConsoleHelper.Error(GetExceptionMessage(engine.FaultException));
ConsoleHelper.Error(GetExceptionMessage(engine.FaultException!));
}

static string GetExceptionMessage(Exception exception)
Expand Down Expand Up @@ -570,7 +570,7 @@ static string GetExceptionMessage(Exception exception)
{
try
{
var addressData = data.GetString();
var addressData = data.GetString()!;
if (UInt160.TryParse(addressData, out var address))
return address;
else
Expand Down
3 changes: 1 addition & 2 deletions src/Neo.CLI/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
using Neo.Network.P2P;
using Neo.Persistence.Providers;
using System;
using System.IO;
using System.Reflection;
using System.Threading;

Expand Down Expand Up @@ -47,7 +46,7 @@ public static Settings Default
{
if (s_default == null)
{
var configFile = ProtocolSettings.FindFile("config.json", Environment.CurrentDirectory);
var configFile = ProtocolSettings.FindFile("config.json", Environment.CurrentDirectory)!;
var config = new ConfigurationBuilder().AddJsonFile(configFile, optional: true).Build();
Initialize(config);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Neo.Cryptography.MPTTrie/Cache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public void Commit()
{
case TrackState.Added:
case TrackState.Changed:
_store.Put(Key(item.Key), item.Value.Node.ToArray());
_store.Put(Key(item.Key), item.Value.Node!.ToArray());
break;
case TrackState.Deleted:
_store.Delete(Key(item.Key));
Expand Down
17 changes: 8 additions & 9 deletions src/Neo.Extensions/Exceptions/TryCatchExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public static TSource TryCatch<TSource>(this TSource obj, Action<TSource?> actio
return obj;
}

public static TSource TryCatch<TSource, TException>(this TSource obj, Action<TSource?> action, Action<TSource?, TException>? onError = default)
where TSource : class?
public static TSource TryCatch<TSource, TException>(this TSource obj, Action<TSource> action, Action<TSource?, TException>? onError = default)
where TSource : notnull
where TException : Exception
{
try
Expand All @@ -45,18 +45,18 @@ public static TSource TryCatch<TSource, TException>(this TSource obj, Action<TSo
return obj;
}

public static TResult? TryCatch<TSource, TException, TResult>(this TSource obj, Func<TSource?, TResult?> func, Func<TSource?, TException, TResult?>? onError = default)
where TSource : class?
public static TResult TryCatch<TSource, TException, TResult>(this TSource obj, Func<TSource, TResult> func, Func<TSource, TException, TResult>? onError = default)
where TSource : notnull
where TException : Exception
where TResult : class?
{
try
{
return func(obj);
}
catch (TException ex)
{
return onError?.Invoke(obj, ex);
if (onError == null) throw;
return onError(obj, ex);
}
}

Expand All @@ -76,10 +76,9 @@ public static TSource TryCatchThrow<TSource, TException>(this TSource obj, Actio
}
}

public static TResult? TryCatchThrow<TSource, TException, TResult>(this TSource obj, Func<TSource?, TResult?> func)
where TSource : class?
public static TResult TryCatchThrow<TSource, TException, TResult>(this TSource obj, Func<TSource, TResult> func)
where TSource : notnull
where TException : Exception
where TResult : class?
{
try
{
Expand Down
6 changes: 2 additions & 4 deletions src/Neo.Extensions/SecureStringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@ namespace Neo.Extensions
{
public static class SecureStringExtensions
{
public static string? GetClearText(this SecureString secureString)
public static string GetClearText(this SecureString secureString)
{
ArgumentNullException.ThrowIfNull(secureString);

var unmanagedStringPtr = IntPtr.Zero;

try
{
unmanagedStringPtr = Marshal.SecureStringToGlobalAllocUnicode(secureString);
return Marshal.PtrToStringUni(unmanagedStringPtr);
return Marshal.PtrToStringUni(unmanagedStringPtr)!;
}
finally
{
Expand Down
Loading