Skip to content

Commit 7cd74e6

Browse files
authored
Merge branch 'dev' into nep-25
2 parents 14a9176 + 5cc2cfd commit 7cd74e6

31 files changed

+87
-70
lines changed

src/Neo/Extensions/IO/BinaryReaderExtensions.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,9 @@ public static byte[] ReadFixedBytes(this BinaryReader reader, int size)
3030
while (size > 0)
3131
{
3232
var bytesRead = reader.Read(data, index, size);
33-
3433
if (bytesRead <= 0)
3534
{
36-
throw new FormatException();
35+
throw new FormatException($"BinaryReader.Read returned {bytesRead}");
3736
}
3837

3938
size -= bytesRead;
@@ -72,7 +71,7 @@ public static ulong ReadVarInt(this BinaryReader reader, ulong max = ulong.MaxVa
7271
value = reader.ReadUInt64();
7372
else
7473
value = fb;
75-
if (value > max) throw new FormatException();
74+
if (value > max) throw new FormatException($"`value`({value}) is out of range (max:{max})");
7675
return value;
7776
}
7877
}

src/Neo/Extensions/MemoryExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static class MemoryExtensions
2626
/// <returns>The converted <see cref="ISerializable"/> array.</returns>
2727
public static T[] AsSerializableArray<T>(this ReadOnlyMemory<byte> value, int max = 0x1000000) where T : ISerializable, new()
2828
{
29-
if (value.IsEmpty) throw new FormatException();
29+
if (value.IsEmpty) throw new FormatException("`value` is empty");
3030
MemoryReader reader = new(value);
3131
return reader.ReadSerializableArray<T>(max);
3232
}
@@ -40,7 +40,7 @@ public static class MemoryExtensions
4040
public static T AsSerializable<T>(this ReadOnlyMemory<byte> value)
4141
where T : ISerializable, new()
4242
{
43-
if (value.IsEmpty) throw new FormatException();
43+
if (value.IsEmpty) throw new FormatException("`value` is empty");
4444
MemoryReader reader = new(value);
4545
return reader.ReadSerializable<T>();
4646
}
@@ -54,7 +54,7 @@ public static T AsSerializable<T>(this ReadOnlyMemory<byte> value)
5454
public static ISerializable AsSerializable(this ReadOnlyMemory<byte> value, Type type)
5555
{
5656
if (!typeof(ISerializable).GetTypeInfo().IsAssignableFrom(type))
57-
throw new InvalidCastException();
57+
throw new InvalidCastException($"`{type.Name}` is not assignable from `ISerializable`");
5858
var serializable = (ISerializable)Activator.CreateInstance(type);
5959
MemoryReader reader = new(value);
6060
serializable.Deserialize(ref reader);

src/Neo/Extensions/SpanExtensions.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,12 @@ public static ReadOnlyMemory<byte> CompressLz4(this Span<byte> data)
5454
public static byte[] DecompressLz4(this ReadOnlySpan<byte> data, int maxOutput)
5555
{
5656
var length = BinaryPrimitives.ReadInt32LittleEndian(data);
57-
if (length < 0 || length > maxOutput) throw new FormatException();
57+
if (length < 0 || length > maxOutput) throw new FormatException($"`length`({length}) is out of range [0, {maxOutput}]");
5858
var result = new byte[length];
59-
if (LZ4Codec.Decode(data[4..], result) != length)
60-
throw new FormatException();
59+
60+
var decoded = LZ4Codec.Decode(data[4..], result);
61+
if (decoded != length)
62+
throw new FormatException($"`length`({length}) does not match the decompressed data length({decoded})");
6163
return result;
6264
}
6365

@@ -70,10 +72,11 @@ public static byte[] DecompressLz4(this ReadOnlySpan<byte> data, int maxOutput)
7072
public static byte[] DecompressLz4(this Span<byte> data, int maxOutput)
7173
{
7274
var length = BinaryPrimitives.ReadInt32LittleEndian(data);
73-
if (length < 0 || length > maxOutput) throw new FormatException();
75+
if (length < 0 || length > maxOutput) throw new FormatException($"`length`({length}) is out of range [0, {maxOutput}]");
7476
var result = new byte[length];
75-
if (LZ4Codec.Decode(data[4..], result) != length)
76-
throw new FormatException();
77+
var decoded = LZ4Codec.Decode(data[4..], result);
78+
if (decoded != length)
79+
throw new FormatException($"`length`({length}) does not match the decompressed data length({decoded})");
7780
return result;
7881
}
7982
}

src/Neo/Network/P2P/Capabilities/NodeCapability.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ protected NodeCapability(NodeCapabilityType type)
4343

4444
void ISerializable.Deserialize(ref MemoryReader reader)
4545
{
46-
if (reader.ReadByte() != (byte)Type)
46+
var readType = reader.ReadByte();
47+
if (readType != (byte)Type)
4748
{
48-
throw new FormatException();
49+
throw new FormatException($"ReadType({readType}) does not match NodeCapabilityType({Type})");
4950
}
5051

5152
DeserializeWithoutType(ref reader);

src/Neo/Network/P2P/Payloads/AddrPayload.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ void ISerializable.Deserialize(ref MemoryReader reader)
5050
{
5151
AddressList = reader.ReadSerializableArray<NetworkAddressWithTime>(MaxCountToSend);
5252
if (AddressList.Length == 0)
53-
throw new FormatException();
53+
throw new FormatException("`AddressList` in AddrPayload is empty");
5454
}
5555

5656
void ISerializable.Serialize(BinaryWriter writer)

src/Neo/Network/P2P/Payloads/Block.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ private static Transaction[] DeserializeTransactions(ref MemoryReader reader, in
114114
{
115115
var tx = reader.ReadSerializable<Transaction>();
116116
if (!hashset.Add(tx.Hash))
117-
throw new FormatException();
117+
throw new FormatException($"TxHash({tx.Hash}) in Block is duplicate");
118118
txs[i] = tx;
119119
hashes[i] = tx.Hash;
120120
}

src/Neo/Network/P2P/Payloads/Conditions/AndCondition.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public override int GetHashCode()
6262
protected override void DeserializeWithoutType(ref MemoryReader reader, int maxNestDepth)
6363
{
6464
Expressions = DeserializeConditions(ref reader, maxNestDepth);
65-
if (Expressions.Length == 0) throw new FormatException();
65+
if (Expressions.Length == 0) throw new FormatException("`Expressions` in AndCondition is empty");
6666
}
6767

6868
public override bool Match(ApplicationEngine engine)
@@ -78,9 +78,10 @@ protected override void SerializeWithoutType(BinaryWriter writer)
7878
private protected override void ParseJson(JObject json, int maxNestDepth)
7979
{
8080
JArray expressions = (JArray)json["expressions"];
81-
if (expressions.Count > MaxSubitems) throw new FormatException();
81+
if (expressions.Count > MaxSubitems)
82+
throw new FormatException($"`expressions`({expressions.Count}) in AndCondition is out of range (max:{MaxSubitems})");
8283
Expressions = expressions.Select(p => FromJson((JObject)p, maxNestDepth - 1)).ToArray();
83-
if (Expressions.Length == 0) throw new FormatException();
84+
if (Expressions.Length == 0) throw new FormatException("`Expressions` in AndCondition is empty");
8485
}
8586

8687
public override JObject ToJson()

src/Neo/Network/P2P/Payloads/Conditions/OrCondition.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public override int GetHashCode()
6262
protected override void DeserializeWithoutType(ref MemoryReader reader, int maxNestDepth)
6363
{
6464
Expressions = DeserializeConditions(ref reader, maxNestDepth);
65-
if (Expressions.Length == 0) throw new FormatException();
65+
if (Expressions.Length == 0) throw new FormatException("`Expressions` in OrCondition is empty");
6666
}
6767

6868
public override bool Match(ApplicationEngine engine)
@@ -78,9 +78,10 @@ protected override void SerializeWithoutType(BinaryWriter writer)
7878
private protected override void ParseJson(JObject json, int maxNestDepth)
7979
{
8080
JArray expressions = (JArray)json["expressions"];
81-
if (expressions.Count > MaxSubitems) throw new FormatException();
81+
if (expressions.Count > MaxSubitems)
82+
throw new FormatException($"`expressions`({expressions.Count}) in OrCondition is out of range (max:{MaxSubitems})");
8283
Expressions = expressions.Select(p => FromJson((JObject)p, maxNestDepth - 1)).ToArray();
83-
if (Expressions.Length == 0) throw new FormatException();
84+
if (Expressions.Length == 0) throw new FormatException("`Expressions` in OrCondition is empty");
8485
}
8586

8687
public override JObject ToJson()

src/Neo/Network/P2P/Payloads/Conditions/WitnessCondition.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ public abstract class WitnessCondition : IInteroperable, ISerializable
4040

4141
void ISerializable.Deserialize(ref MemoryReader reader)
4242
{
43-
if (reader.ReadByte() != (byte)Type) throw new FormatException();
43+
var readType = reader.ReadByte();
44+
if (readType != (byte)Type)
45+
throw new FormatException($"Read type({readType}) does not match WitnessConditionType({Type})");
4446
DeserializeWithoutType(ref reader, MaxNestingDepth);
4547
}
4648

@@ -66,10 +68,11 @@ protected static WitnessCondition[] DeserializeConditions(ref MemoryReader reade
6668
/// <returns>The deserialized <see cref="WitnessCondition"/>.</returns>
6769
public static WitnessCondition DeserializeFrom(ref MemoryReader reader, int maxNestDepth)
6870
{
69-
if (maxNestDepth <= 0) throw new FormatException();
71+
if (maxNestDepth <= 0)
72+
throw new FormatException($"`maxNestDepth`({maxNestDepth}) in WitnessCondition is out of range (min:1)");
7073
WitnessConditionType type = (WitnessConditionType)reader.ReadByte();
7174
if (ReflectionCache<WitnessConditionType>.CreateInstance(type) is not WitnessCondition condition)
72-
throw new FormatException();
75+
throw new FormatException($"Invalid WitnessConditionType({type})");
7376
condition.DeserializeWithoutType(ref reader, maxNestDepth);
7477
return condition;
7578
}
@@ -110,10 +113,11 @@ void ISerializable.Serialize(BinaryWriter writer)
110113
/// <returns>The converted <see cref="WitnessCondition"/>.</returns>
111114
public static WitnessCondition FromJson(JObject json, int maxNestDepth)
112115
{
113-
if (maxNestDepth <= 0) throw new FormatException();
116+
if (maxNestDepth <= 0)
117+
throw new FormatException($"`maxNestDepth`({maxNestDepth}) in WitnessCondition is out of range (min:1)");
114118
WitnessConditionType type = Enum.Parse<WitnessConditionType>(json["type"].GetString());
115119
if (ReflectionCache<WitnessConditionType>.CreateInstance(type) is not WitnessCondition condition)
116-
throw new FormatException("Invalid WitnessConditionType.");
120+
throw new FormatException($"Invalid WitnessConditionType({type})");
117121
condition.ParseJson(json, maxNestDepth);
118122
return condition;
119123
}

src/Neo/Network/P2P/Payloads/FilterLoadPayload.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ void ISerializable.Deserialize(ref MemoryReader reader)
6060
{
6161
Filter = reader.ReadVarMemory(36000);
6262
K = reader.ReadByte();
63-
if (K > 50) throw new FormatException();
63+
if (K > 50) throw new FormatException($"`K`({K}) is out of range [0, 50]");
6464
Tweak = reader.ReadUInt32();
6565
}
6666

0 commit comments

Comments
 (0)