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
5 changes: 2 additions & 3 deletions src/Neo/Extensions/IO/BinaryReaderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@ public static byte[] ReadFixedBytes(this BinaryReader reader, int size)
while (size > 0)
{
var bytesRead = reader.Read(data, index, size);

if (bytesRead <= 0)
{
throw new FormatException();
throw new FormatException($"BinaryReader.Read returned {bytesRead}");
}

size -= bytesRead;
Expand Down Expand Up @@ -72,7 +71,7 @@ public static ulong ReadVarInt(this BinaryReader reader, ulong max = ulong.MaxVa
value = reader.ReadUInt64();
else
value = fb;
if (value > max) throw new FormatException();
if (value > max) throw new FormatException($"`value`({value}) is out of range (max:{max})");
return value;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Neo/Extensions/MemoryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static class MemoryExtensions
/// <returns>The converted <see cref="ISerializable"/> array.</returns>
public static T[] AsSerializableArray<T>(this ReadOnlyMemory<byte> value, int max = 0x1000000) where T : ISerializable, new()
{
if (value.IsEmpty) throw new FormatException();
if (value.IsEmpty) throw new FormatException("`value` is empty");
MemoryReader reader = new(value);
return reader.ReadSerializableArray<T>(max);
}
Expand All @@ -40,7 +40,7 @@ public static class MemoryExtensions
public static T AsSerializable<T>(this ReadOnlyMemory<byte> value)
where T : ISerializable, new()
{
if (value.IsEmpty) throw new FormatException();
if (value.IsEmpty) throw new FormatException("`value` is empty");
MemoryReader reader = new(value);
return reader.ReadSerializable<T>();
}
Expand All @@ -54,7 +54,7 @@ public static T AsSerializable<T>(this ReadOnlyMemory<byte> value)
public static ISerializable AsSerializable(this ReadOnlyMemory<byte> value, Type type)
{
if (!typeof(ISerializable).GetTypeInfo().IsAssignableFrom(type))
throw new InvalidCastException();
throw new InvalidCastException($"`{type.Name}` is not assignable from `ISerializable`");
var serializable = (ISerializable)Activator.CreateInstance(type);
MemoryReader reader = new(value);
serializable.Deserialize(ref reader);
Expand Down
15 changes: 9 additions & 6 deletions src/Neo/Extensions/SpanExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ public static ReadOnlyMemory<byte> CompressLz4(this Span<byte> data)
public static byte[] DecompressLz4(this ReadOnlySpan<byte> data, int maxOutput)
{
var length = BinaryPrimitives.ReadInt32LittleEndian(data);
if (length < 0 || length > maxOutput) throw new FormatException();
if (length < 0 || length > maxOutput) throw new FormatException($"`length`({length}) is out of range [0, {maxOutput}]");
var result = new byte[length];
if (LZ4Codec.Decode(data[4..], result) != length)
throw new FormatException();

var decoded = LZ4Codec.Decode(data[4..], result);
if (decoded != length)
throw new FormatException($"`length`({length}) does not match the decompressed data length({decoded})");
return result;
}

Expand All @@ -70,10 +72,11 @@ public static byte[] DecompressLz4(this ReadOnlySpan<byte> data, int maxOutput)
public static byte[] DecompressLz4(this Span<byte> data, int maxOutput)
{
var length = BinaryPrimitives.ReadInt32LittleEndian(data);
if (length < 0 || length > maxOutput) throw new FormatException();
if (length < 0 || length > maxOutput) throw new FormatException($"`length`({length}) is out of range [0, {maxOutput}]");
var result = new byte[length];
if (LZ4Codec.Decode(data[4..], result) != length)
throw new FormatException();
var decoded = LZ4Codec.Decode(data[4..], result);
if (decoded != length)
throw new FormatException($"`length`({length}) does not match the decompressed data length({decoded})");
return result;
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/Neo/Network/P2P/Capabilities/NodeCapability.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ protected NodeCapability(NodeCapabilityType type)

void ISerializable.Deserialize(ref MemoryReader reader)
{
if (reader.ReadByte() != (byte)Type)
var readType = reader.ReadByte();
if (readType != (byte)Type)
{
throw new FormatException();
throw new FormatException($"ReadType({readType}) does not match NodeCapabilityType({Type})");
}

DeserializeWithoutType(ref reader);
Expand Down
2 changes: 1 addition & 1 deletion src/Neo/Network/P2P/Payloads/AddrPayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void ISerializable.Deserialize(ref MemoryReader reader)
{
AddressList = reader.ReadSerializableArray<NetworkAddressWithTime>(MaxCountToSend);
if (AddressList.Length == 0)
throw new FormatException();
throw new FormatException("`AddressList` in AddrPayload is empty");
}

void ISerializable.Serialize(BinaryWriter writer)
Expand Down
2 changes: 1 addition & 1 deletion src/Neo/Network/P2P/Payloads/Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ private static Transaction[] DeserializeTransactions(ref MemoryReader reader, in
{
var tx = reader.ReadSerializable<Transaction>();
if (!hashset.Add(tx.Hash))
throw new FormatException();
throw new FormatException($"TxHash({tx.Hash}) in Block is duplicate");
txs[i] = tx;
hashes[i] = tx.Hash;
}
Expand Down
7 changes: 4 additions & 3 deletions src/Neo/Network/P2P/Payloads/Conditions/AndCondition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public override int GetHashCode()
protected override void DeserializeWithoutType(ref MemoryReader reader, int maxNestDepth)
{
Expressions = DeserializeConditions(ref reader, maxNestDepth);
if (Expressions.Length == 0) throw new FormatException();
if (Expressions.Length == 0) throw new FormatException("`Expressions` in AndCondition is empty");
}

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

public override JObject ToJson()
Expand Down
7 changes: 4 additions & 3 deletions src/Neo/Network/P2P/Payloads/Conditions/OrCondition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public override int GetHashCode()
protected override void DeserializeWithoutType(ref MemoryReader reader, int maxNestDepth)
{
Expressions = DeserializeConditions(ref reader, maxNestDepth);
if (Expressions.Length == 0) throw new FormatException();
if (Expressions.Length == 0) throw new FormatException("`Expressions` in OrCondition is empty");
}

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

public override JObject ToJson()
Expand Down
14 changes: 9 additions & 5 deletions src/Neo/Network/P2P/Payloads/Conditions/WitnessCondition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ public abstract class WitnessCondition : IInteroperable, ISerializable

void ISerializable.Deserialize(ref MemoryReader reader)
{
if (reader.ReadByte() != (byte)Type) throw new FormatException();
var readType = reader.ReadByte();
if (readType != (byte)Type)
throw new FormatException($"Read type({readType}) does not match WitnessConditionType({Type})");
DeserializeWithoutType(ref reader, MaxNestingDepth);
}

Expand All @@ -66,10 +68,11 @@ protected static WitnessCondition[] DeserializeConditions(ref MemoryReader reade
/// <returns>The deserialized <see cref="WitnessCondition"/>.</returns>
public static WitnessCondition DeserializeFrom(ref MemoryReader reader, int maxNestDepth)
{
if (maxNestDepth <= 0) throw new FormatException();
if (maxNestDepth <= 0)
throw new FormatException($"`maxNestDepth`({maxNestDepth}) in WitnessCondition is out of range (min:1)");
WitnessConditionType type = (WitnessConditionType)reader.ReadByte();
if (ReflectionCache<WitnessConditionType>.CreateInstance(type) is not WitnessCondition condition)
throw new FormatException();
throw new FormatException($"Invalid WitnessConditionType({type})");
condition.DeserializeWithoutType(ref reader, maxNestDepth);
return condition;
}
Expand Down Expand Up @@ -110,10 +113,11 @@ void ISerializable.Serialize(BinaryWriter writer)
/// <returns>The converted <see cref="WitnessCondition"/>.</returns>
public static WitnessCondition FromJson(JObject json, int maxNestDepth)
{
if (maxNestDepth <= 0) throw new FormatException();
if (maxNestDepth <= 0)
throw new FormatException($"`maxNestDepth`({maxNestDepth}) in WitnessCondition is out of range (min:1)");
WitnessConditionType type = Enum.Parse<WitnessConditionType>(json["type"].GetString());
if (ReflectionCache<WitnessConditionType>.CreateInstance(type) is not WitnessCondition condition)
throw new FormatException("Invalid WitnessConditionType.");
throw new FormatException($"Invalid WitnessConditionType({type})");
condition.ParseJson(json, maxNestDepth);
return condition;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Neo/Network/P2P/Payloads/FilterLoadPayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void ISerializable.Deserialize(ref MemoryReader reader)
{
Filter = reader.ReadVarMemory(36000);
K = reader.ReadByte();
if (K > 50) throw new FormatException();
if (K > 50) throw new FormatException($"`K`({K}) is out of range [0, 50]");
Tweak = reader.ReadUInt32();
}

Expand Down
4 changes: 2 additions & 2 deletions src/Neo/Network/P2P/Payloads/Header.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,15 @@ public void Deserialize(ref MemoryReader reader)
{
((IVerifiable)this).DeserializeUnsigned(ref reader);
Witness[] witnesses = reader.ReadSerializableArray<Witness>(1);
if (witnesses.Length != 1) throw new FormatException();
if (witnesses.Length != 1) throw new FormatException($"Expected 1 witness in Header, got {witnesses.Length}.");
Witness = witnesses[0];
}

void IVerifiable.DeserializeUnsigned(ref MemoryReader reader)
{
_hash = null;
version = reader.ReadUInt32();
if (version > 0) throw new FormatException();
if (version > 0) throw new FormatException($"`version`({version}) in Header must be 0");
prevHash = reader.ReadSerializable<UInt256>();
merkleRoot = reader.ReadSerializable<UInt256>();
timestamp = reader.ReadUInt64();
Expand Down
2 changes: 1 addition & 1 deletion src/Neo/Network/P2P/Payloads/HeadersPayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static HeadersPayload Create(params Header[] headers)
void ISerializable.Deserialize(ref MemoryReader reader)
{
Headers = reader.ReadSerializableArray<Header>(MaxHeadersCount);
if (Headers.Length == 0) throw new FormatException();
if (Headers.Length == 0) throw new FormatException("`Headers` in HeadersPayload is empty");
}

void ISerializable.Serialize(BinaryWriter writer)
Expand Down
2 changes: 1 addition & 1 deletion src/Neo/Network/P2P/Payloads/InvPayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ void ISerializable.Deserialize(ref MemoryReader reader)
{
Type = (InventoryType)reader.ReadByte();
if (!Enum.IsDefined(typeof(InventoryType), Type))
throw new FormatException();
throw new FormatException($"`Type`({Type}) is not defined in InventoryType");
Hashes = reader.ReadSerializableArray<UInt256>(MaxHashesCount);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Neo/Network/P2P/Payloads/NetworkAddressWithTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void ISerializable.Deserialize(ref MemoryReader reader)
// taken into account but still preserved to be able to share through the network.
var capabilities = Capabilities.Where(c => c is not UnknownCapability);
if (capabilities.Select(p => p.Type).Distinct().Count() != capabilities.Count())
throw new FormatException();
throw new FormatException("Duplicating capabilities are included");
}

void ISerializable.Serialize(BinaryWriter writer)
Expand Down
2 changes: 1 addition & 1 deletion src/Neo/Network/P2P/Payloads/OracleResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ protected override void DeserializeWithoutType(ref MemoryReader reader)

Result = reader.ReadVarMemory(MaxResultSize);
if (Code != OracleResponseCode.Success && Result.Length > 0)
throw new FormatException();
throw new FormatException($"Result is not empty({Result.Length}) for non-success response code({Code})");
}

protected override void SerializeWithoutType(BinaryWriter writer)
Expand Down
4 changes: 2 additions & 2 deletions src/Neo/Network/P2P/Payloads/Signer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ public void Deserialize(ref MemoryReader reader)
Account = reader.ReadSerializable<UInt160>();
Scopes = (WitnessScope)reader.ReadByte();
if ((Scopes & ~(WitnessScope.CalledByEntry | WitnessScope.CustomContracts | WitnessScope.CustomGroups | WitnessScope.WitnessRules | WitnessScope.Global)) != 0)
throw new FormatException();
throw new FormatException($"`Scopes`({Scopes}) in Signer is not valid");
if (Scopes.HasFlag(WitnessScope.Global) && Scopes != WitnessScope.Global)
throw new FormatException();
throw new FormatException($"`Scopes`({Scopes}) in Signer is not valid");
AllowedContracts = Scopes.HasFlag(WitnessScope.CustomContracts)
? reader.ReadSerializableArray<UInt160>(MaxSubitems) : [];
AllowedGroups = Scopes.HasFlag(WitnessScope.CustomGroups)
Expand Down
11 changes: 6 additions & 5 deletions src/Neo/Network/P2P/Payloads/Transaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ void ISerializable.Deserialize(ref MemoryReader reader)
int startPosition = reader.Position;
DeserializeUnsigned(ref reader);
Witnesses = reader.ReadSerializableArray<Witness>(Signers.Length);
if (Witnesses.Length != Signers.Length) throw new FormatException();
if (Witnesses.Length != Signers.Length)
throw new FormatException($"Witnesses.Length({Witnesses.Length}) != Signers.Length({Signers.Length})");
_size = reader.Position - startPosition;
}

Expand All @@ -210,7 +211,7 @@ private static TransactionAttribute[] DeserializeAttributes(ref MemoryReader rea
{
TransactionAttribute attribute = TransactionAttribute.DeserializeFrom(ref reader);
if (!attribute.AllowMultiple && !hashset.Add(attribute.Type))
throw new FormatException();
throw new FormatException($"`{attribute.Type}` in Transaction is duplicate");
attributes[i] = attribute;
}
return attributes;
Expand All @@ -219,13 +220,13 @@ private static TransactionAttribute[] DeserializeAttributes(ref MemoryReader rea
private static Signer[] DeserializeSigners(ref MemoryReader reader, int maxCount)
{
int count = (int)reader.ReadVarInt((ulong)maxCount);
if (count == 0) throw new FormatException();
if (count == 0) throw new FormatException("Signers in Transaction is empty");
Signer[] signers = new Signer[count];
HashSet<UInt160> hashset = new();
for (int i = 0; i < count; i++)
{
Signer signer = reader.ReadSerializable<Signer>();
if (!hashset.Add(signer.Account)) throw new FormatException();
if (!hashset.Add(signer.Account)) throw new FormatException($"`{signer.Account}` in Transaction is duplicate");
signers[i] = signer;
}
return signers;
Expand All @@ -250,7 +251,7 @@ public void DeserializeUnsigned(ref MemoryReader reader)
Signers = DeserializeSigners(ref reader, MaxTransactionAttributes);
Attributes = DeserializeAttributes(ref reader, MaxTransactionAttributes - Signers.Length);
Script = reader.ReadVarMemory(ushort.MaxValue);
if (Script.Length == 0) throw new FormatException();
if (Script.Length == 0) throw new FormatException("Script in Transaction is empty");
}

public bool Equals(Transaction other)
Expand Down
2 changes: 1 addition & 1 deletion src/Neo/Network/P2P/Payloads/VersionPayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ void ISerializable.Deserialize(ref MemoryReader reader)
Capabilities[x] = NodeCapability.DeserializeFrom(ref reader);
var capabilities = Capabilities.Where(c => c is not UnknownCapability);
if (capabilities.Select(p => p.Type).Distinct().Count() != capabilities.Count())
throw new FormatException();
throw new FormatException("Duplicating capabilities are included");

AllowCompression = !capabilities.Any(u => u is DisableCompressionCapability);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Neo/SmartContract/BinarySerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ public static StackItem Deserialize(ref MemoryReader reader, uint maxSize, uint
}
break;
default:
throw new FormatException();
throw new FormatException($"Invalid StackItemType({type})");
}
if (deserialized.Count > maxItems)
throw new FormatException();
throw new FormatException($"Deserialized count({deserialized.Count}) is out of range (max:{maxItems})");
}

var stackTemp = new Stack<StackItem>();
Expand Down Expand Up @@ -206,7 +206,7 @@ public static void Serialize(BinaryWriter writer, StackItem item, long maxSize,
while (unserialized.Count > 0)
{
if (--maxItems < 0)
throw new FormatException();
throw new FormatException("Too many items to serialize");
item = unserialized.Pop();
writer.Write((byte)item.Type);
switch (item)
Expand Down
2 changes: 1 addition & 1 deletion src/Neo/SmartContract/ContractParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void SetValue(string text)
{
case ContractParameterType.Signature:
byte[] signature = text.HexToBytes();
if (signature.Length != 64) throw new FormatException();
if (signature.Length != 64) throw new FormatException($"Signature length({signature.Length}) is not 64");
Value = signature;
break;
case ContractParameterType.Boolean:
Expand Down
Loading
Loading