Skip to content

Commit 064fb92

Browse files
Wi1l-B0tJim8yCopilot
authored
[Optimization]: add exception message to ArgumentException (#3862)
* Style: add exception message to ArgumentException * Update src/Neo/SmartContract/ApplicationEngine.Crypto.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Jimmy <jinghui@wayne.edu> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 93ac5e5 commit 064fb92

File tree

12 files changed

+74
-41
lines changed

12 files changed

+74
-41
lines changed

Diff for: src/Neo.ConsoleService/CommandToken.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,9 @@ public static void Trim(List<CommandToken> args)
204204
return "";
205205
}
206206

207-
throw new ArgumentException();
207+
throw new ArgumentException("Unmatched quote");
208208
}
209-
case CommandSpaceToken _: throw new ArgumentException();
209+
case CommandSpaceToken _: throw new ArgumentException("Unmatched space");
210210
case CommandStringToken str:
211211
{
212212
args.RemoveAt(0);

Diff for: src/Neo/SmartContract/ApplicationEngine.Crypto.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,11 @@ protected internal bool CheckSig(byte[] pubkey, byte[] signature)
6464
/// <returns><see langword="true"/> if the signatures are valid; otherwise, <see langword="false"/>.</returns>
6565
protected internal bool CheckMultisig(byte[][] pubkeys, byte[][] signatures)
6666
{
67-
byte[] message = ScriptContainer.GetSignData(ProtocolSettings.Network);
67+
var message = ScriptContainer.GetSignData(ProtocolSettings.Network);
6868
int m = signatures.Length, n = pubkeys.Length;
69-
if (n == 0 || m == 0 || m > n) throw new ArgumentException();
69+
if (n == 0) throw new ArgumentException("The pubkeys.Length cannot be zero.");
70+
if (m == 0) throw new ArgumentException("The signatures.Length cannot be zero.");
71+
if (m > n) throw new ArgumentException($"The signatures.Length({m}) cannot be greater than the pubkeys.Length({n}).");
7072
AddFee(CheckSigPrice * n * ExecFeeFactor);
7173
try
7274
{

Diff for: src/Neo/SmartContract/ContractParameter.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public void SetValue(string text)
127127
Value = text;
128128
break;
129129
default:
130-
throw new ArgumentException();
130+
throw new ArgumentException($"The ContractParameterType '{Type}' is not supported.");
131131
}
132132
}
133133

Diff for: src/Neo/SmartContract/Native/ContractManagement.cs

+6-3
Original file line numberDiff line numberDiff line change
@@ -264,16 +264,19 @@ private ContractTask Update(ApplicationEngine engine, byte[] nefFile, byte[] man
264264
[ContractMethod(RequiredCallFlags = CallFlags.All)]
265265
private ContractTask Update(ApplicationEngine engine, byte[] nefFile, byte[] manifest, StackItem data)
266266
{
267-
if (nefFile is null && manifest is null) throw new ArgumentException();
267+
if (nefFile is null && manifest is null)
268+
throw new ArgumentException("The nefFile and manifest cannot be null at the same time.");
268269

269270
engine.AddFee(engine.StoragePrice * ((nefFile?.Length ?? 0) + (manifest?.Length ?? 0)));
270271

271272
var contractState = engine.SnapshotCache.GetAndChange(CreateStorageKey(Prefix_Contract, engine.CallingScriptHash))
272273
?? throw new InvalidOperationException($"Updating Contract Does Not Exist: {engine.CallingScriptHash}");
273274

274275
using var sealInterop = contractState.GetInteroperable(out ContractState contract, false);
275-
if (contract is null) throw new InvalidOperationException($"Updating Contract Does Not Exist: {engine.CallingScriptHash}");
276-
if (contract.UpdateCounter == ushort.MaxValue) throw new InvalidOperationException($"The contract reached the maximum number of updates.");
276+
if (contract is null)
277+
throw new InvalidOperationException($"Updating Contract Does Not Exist: {engine.CallingScriptHash}");
278+
if (contract.UpdateCounter == ushort.MaxValue)
279+
throw new InvalidOperationException($"The contract reached the maximum number of updates.");
277280

278281
if (nefFile != null)
279282
{

Diff for: src/Neo/SmartContract/Native/OracleContract.cs

+28-9
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,9 @@ internal override async ContractTask PostPersistAsync(ApplicationEngine engine)
175175
if (list.Count == 0) engine.SnapshotCache.Delete(key);
176176

177177
//Mint GAS for oracle nodes
178-
nodes ??= RoleManagement.GetDesignatedByRole(engine.SnapshotCache, Role.Oracle, engine.PersistingBlock.Index).Select(p => (Contract.CreateSignatureRedeemScript(p).ToScriptHash(), BigInteger.Zero)).ToArray();
178+
nodes ??= RoleManagement.GetDesignatedByRole(engine.SnapshotCache, Role.Oracle, engine.PersistingBlock.Index)
179+
.Select(p => (Contract.CreateSignatureRedeemScript(p).ToScriptHash(), BigInteger.Zero))
180+
.ToArray();
179181
if (nodes.Length > 0)
180182
{
181183
int index = (int)(response.Id % (ulong)nodes.Length);
@@ -193,14 +195,26 @@ internal override async ContractTask PostPersistAsync(ApplicationEngine engine)
193195
}
194196

195197
[ContractMethod(RequiredCallFlags = CallFlags.States | CallFlags.AllowNotify)]
196-
private async ContractTask Request(ApplicationEngine engine, string url, string filter, string callback, StackItem userData, long gasForResponse /* In the unit of datoshi, 1 datoshi = 1e-8 GAS */)
198+
private async ContractTask Request(ApplicationEngine engine, string url, string filter, string callback,
199+
StackItem userData, long gasForResponse /* In the unit of datoshi, 1 datoshi = 1e-8 GAS */)
197200
{
198-
//Check the arguments
199-
if (url.GetStrictUtf8ByteCount() > MaxUrlLength
200-
|| (filter != null && filter.GetStrictUtf8ByteCount() > MaxFilterLength)
201-
|| callback.GetStrictUtf8ByteCount() > MaxCallbackLength || callback.StartsWith('_')
202-
|| gasForResponse < 0_10000000)
203-
throw new ArgumentException();
201+
var urlSize = url.GetStrictUtf8ByteCount();
202+
if (urlSize > MaxUrlLength)
203+
throw new ArgumentException($"The url bytes size({urlSize}) cannot be greater than {MaxUrlLength}.");
204+
205+
var filterSize = filter.GetStrictUtf8ByteCount();
206+
if (filterSize > MaxFilterLength)
207+
throw new ArgumentException($"The filter bytes size({filterSize}) cannot be greater than {MaxFilterLength}.");
208+
209+
var callbackSize = callback is null ? 0 : callback.GetStrictUtf8ByteCount();
210+
if (callbackSize > MaxCallbackLength)
211+
throw new ArgumentException($"The callback bytes size({callbackSize}) cannot be greater than {MaxCallbackLength}.");
212+
213+
if (callback.StartsWith('_'))
214+
throw new ArgumentException($"The callback cannot start with '_'.");
215+
216+
if (gasForResponse < 0_10000000)
217+
throw new ArgumentException($"The gasForResponse({gasForResponse}) must be greater than or equal to 0.1 datoshi.");
204218

205219
engine.AddFee(GetPrice(engine.SnapshotCache));
206220

@@ -237,7 +251,12 @@ private async ContractTask Request(ApplicationEngine engine, string url, string
237251
throw new InvalidOperationException("There are too many pending responses for this url");
238252
list.Add(id);
239253

240-
engine.SendNotification(Hash, "OracleRequest", new Array(engine.ReferenceCounter) { id, engine.CallingScriptHash.ToArray(), url, filter ?? StackItem.Null });
254+
engine.SendNotification(Hash, "OracleRequest", new Array(engine.ReferenceCounter) {
255+
id,
256+
engine.CallingScriptHash.ToArray(),
257+
url,
258+
filter ?? StackItem.Null
259+
});
241260
}
242261

243262
[ContractMethod(CpuFee = 1 << 15)]

Diff for: src/Neo/Wallets/Helper.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ public static UInt160 ToScriptHash(this string address, byte version)
7575

7676
internal static byte[] XOR(byte[] x, byte[] y)
7777
{
78-
if (x.Length != y.Length) throw new ArgumentException();
78+
if (x.Length != y.Length)
79+
throw new ArgumentException($"The x.Length({x.Length}) and y.Length({y.Length}) must be equal.");
7980
var r = new byte[x.Length];
8081
for (var i = 0; i < r.Length; i++)
8182
r[i] = (byte)(x[i] ^ y[i]);

Diff for: src/Plugins/SQLiteWallet/SQLiteWallet.cs

+14-8
Original file line numberDiff line numberDiff line change
@@ -403,11 +403,14 @@ public override bool VerifyPassword(string password)
403403

404404
private static byte[] Encrypt(byte[] data, byte[] key, byte[] iv)
405405
{
406-
ArgumentNullException.ThrowIfNull(data);
407-
ArgumentNullException.ThrowIfNull(key);
408-
ArgumentNullException.ThrowIfNull(iv);
406+
ArgumentNullException.ThrowIfNull(data, nameof(data));
407+
ArgumentNullException.ThrowIfNull(key, nameof(key));
408+
ArgumentNullException.ThrowIfNull(iv, nameof(iv));
409+
410+
if (data.Length % 16 != 0) throw new ArgumentException($"The data.Length({data.Length}) must be a multiple of 16.");
411+
if (key.Length != 32) throw new ArgumentException($"The key.Length({key.Length}) must be 32.");
412+
if (iv.Length != 16) throw new ArgumentException($"The iv.Length({iv.Length}) must be 16.");
409413

410-
if (data.Length % 16 != 0 || key.Length != 32 || iv.Length != 16) throw new ArgumentException();
411414
using var aes = Aes.Create();
412415
aes.Padding = PaddingMode.None;
413416
using var encryptor = aes.CreateEncryptor(key, iv);
@@ -416,11 +419,14 @@ private static byte[] Encrypt(byte[] data, byte[] key, byte[] iv)
416419

417420
private static byte[] Decrypt(byte[] data, byte[] key, byte[] iv)
418421
{
419-
ArgumentNullException.ThrowIfNull(data);
420-
ArgumentNullException.ThrowIfNull(key);
421-
ArgumentNullException.ThrowIfNull(iv);
422+
ArgumentNullException.ThrowIfNull(data, nameof(data));
423+
ArgumentNullException.ThrowIfNull(key, nameof(key));
424+
ArgumentNullException.ThrowIfNull(iv, nameof(iv));
425+
426+
if (data.Length % 16 != 0) throw new ArgumentException($"The data.Length({data.Length}) must be a multiple of 16.");
427+
if (key.Length != 32) throw new ArgumentException($"The key.Length({key.Length}) must be 32.");
428+
if (iv.Length != 16) throw new ArgumentException($"The iv.Length({iv.Length}) must be 16.");
422429

423-
if (data.Length % 16 != 0 || key.Length != 32 || iv.Length != 16) throw new ArgumentException();
424430
using var aes = Aes.Create();
425431
aes.Padding = PaddingMode.None;
426432
using var decryptor = aes.CreateDecryptor(key, iv);

Diff for: src/Plugins/TokensTracker/Trackers/NEP-11/Nep11BalanceKey.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ public class Nep11BalanceKey : IComparable<Nep11BalanceKey>, IEquatable<Nep11Bal
3030

3131
public Nep11BalanceKey(UInt160 userScriptHash, UInt160 assetScriptHash, ByteString tokenId)
3232
{
33-
if (userScriptHash == null || assetScriptHash == null || tokenId == null)
34-
throw new ArgumentNullException();
33+
ArgumentNullException.ThrowIfNull(userScriptHash, nameof(userScriptHash));
34+
ArgumentNullException.ThrowIfNull(assetScriptHash, nameof(assetScriptHash));
35+
ArgumentNullException.ThrowIfNull(tokenId, nameof(tokenId));
36+
3537
UserScriptHash = userScriptHash;
3638
AssetScriptHash = assetScriptHash;
3739
Token = tokenId;

Diff for: src/Plugins/TokensTracker/Trackers/NEP-17/Nep17BalanceKey.cs

+4-5
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,13 @@ public class Nep17BalanceKey : IComparable<Nep17BalanceKey>, IEquatable<Nep17Bal
2323

2424
public int Size => UInt160.Length + UInt160.Length;
2525

26-
public Nep17BalanceKey() : this(new UInt160(), new UInt160())
27-
{
28-
}
26+
public Nep17BalanceKey() : this(new UInt160(), new UInt160()) { }
2927

3028
public Nep17BalanceKey(UInt160 userScriptHash, UInt160 assetScriptHash)
3129
{
32-
if (userScriptHash == null || assetScriptHash == null)
33-
throw new ArgumentNullException();
30+
ArgumentNullException.ThrowIfNull(userScriptHash, nameof(userScriptHash));
31+
ArgumentNullException.ThrowIfNull(assetScriptHash, nameof(assetScriptHash));
32+
3433
UserScriptHash = userScriptHash;
3534
AssetScriptHash = assetScriptHash;
3635
}

Diff for: src/Plugins/TokensTracker/Trackers/TokenTransferKey.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ public class TokenTransferKey : ISerializable
2626

2727
public TokenTransferKey(UInt160 userScriptHash, ulong timestamp, UInt160 assetScriptHash, uint xferIndex)
2828
{
29-
if (userScriptHash is null || assetScriptHash is null)
30-
throw new ArgumentNullException();
29+
ArgumentNullException.ThrowIfNull(userScriptHash, nameof(userScriptHash));
30+
ArgumentNullException.ThrowIfNull(assetScriptHash, nameof(assetScriptHash));
31+
3132
UserScriptHash = userScriptHash;
3233
TimestampMS = timestamp;
3334
AssetScriptHash = assetScriptHash;

Diff for: tests/Neo.UnitTests/Cryptography/UT_MerkleTree.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ namespace Neo.UnitTests.Cryptography
2121
[TestClass]
2222
public class UT_MerkleTree
2323
{
24-
public UInt256 GetByteArrayHash(byte[] byteArray)
24+
public UInt256 GetByteArrayHash(byte[] bytes)
2525
{
26-
if (byteArray == null || byteArray.Length == 0) throw new ArgumentNullException();
27-
var hash = new UInt256(Crypto.Hash256(byteArray));
26+
ArgumentNullException.ThrowIfNull(bytes, nameof(bytes));
27+
28+
var hash = new UInt256(Crypto.Hash256(bytes));
2829
return hash;
2930
}
3031

Diff for: tests/Neo.UnitTests/Ledger/UT_PoolItem.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,8 @@ public static Transaction GenerateTx(long networkFee, int size, byte[] overrideS
153153
Assert.AreEqual(0, tx.Signers.Length);
154154

155155
int diff = size - tx.Size;
156-
if (diff < 0) throw new ArgumentException();
157-
if (diff > 0)
158-
tx.Witnesses[0].VerificationScript = new byte[diff];
156+
if (diff < 0) throw new ArgumentException($"The size({size}) cannot be less than the Transaction.Size({tx.Size}).");
157+
if (diff > 0) tx.Witnesses[0].VerificationScript = new byte[diff];
159158
return tx;
160159
}
161160
}

0 commit comments

Comments
 (0)