Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix undefined layout behaviour, and remove pinning: Uint256 and Uint160 #1387

Merged
merged 17 commits into from
Jan 15, 2020
25 changes: 17 additions & 8 deletions src/neo/UInt160.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
using Neo.IO;
using System;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;

namespace Neo
{
/// <summary>
/// This class stores a 160 bit unsigned int, represented as a 20-byte little-endian byte array
/// It is composed by ulong(64) + ulong(64) + uint(32) = UInt160(160)
/// </summary>
public class UInt160 : UIntBase, IComparable<UInt160>, IEquatable<UInt160>
[StructLayout(LayoutKind.Explicit, Size = 20)]
public class UInt160 : IComparable<UInt160>, IEquatable<UInt160>, ISerializable
{
public const int Length = 20;
public static readonly UInt160 Zero = new UInt160();

private ulong value1;
private ulong value2;
private uint value3;
[FieldOffset(0)] private ulong value1;
[FieldOffset(8)] private ulong value2;
[FieldOffset(16)] private uint value3;

public override int Size => Length;
public int Size => Length;

public UInt160()
{
Expand Down Expand Up @@ -44,7 +48,7 @@ public int CompareTo(UInt160 other)
return value1.CompareTo(other.value1);
}

public override void Deserialize(BinaryReader reader)
public void Deserialize(BinaryReader reader)
{
value1 = reader.ReadUInt64();
value2 = reader.ReadUInt64();
Expand Down Expand Up @@ -80,7 +84,7 @@ public override int GetHashCode()
/// Method Parse receives a big-endian hex string and stores as a UInt160 little-endian 20-bytes array
/// Example: Parse("0xa400ff00ff00ff00ff00ff00ff00ff00ff00ff01") should create UInt160 01ff00ff00ff00ff00ff00ff00ff00ff00ff00a4
/// </summary>
public static new UInt160 Parse(string value)
public static UInt160 Parse(string value)
{
if (value == null)
throw new ArgumentNullException();
Expand All @@ -93,13 +97,18 @@ public override int GetHashCode()
return new UInt160(data);
}

public override void Serialize(BinaryWriter writer)
public void Serialize(BinaryWriter writer)
{
writer.Write(value1);
writer.Write(value2);
writer.Write(value3);
}

public override string ToString()
{
return "0x" + this.ToArray().ToHexString(reverse: true);
}

/// <summary>
/// Method TryParse tries to parse a big-endian hex string and store it as a UInt160 little-endian 20-bytes array
/// Example: TryParse("0xa400ff00ff00ff00ff00ff00ff00ff00ff00ff01", result) should create result UInt160 01ff00ff00ff00ff00ff00ff00ff00ff00ff00a4
Expand Down
27 changes: 18 additions & 9 deletions src/neo/UInt256.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
using Neo.IO;
using System;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;

namespace Neo
{
/// <summary>
/// This class stores a 256 bit unsigned int, represented as a 32-byte little-endian byte array
/// Composed by ulong(64) + ulong(64) + ulong(64) + ulong(64) = UInt256(256)
/// </summary>
public class UInt256 : UIntBase, IComparable<UInt256>, IEquatable<UInt256>
[StructLayout(LayoutKind.Explicit, Size = 32)]
public class UInt256 : IComparable<UInt256>, IEquatable<UInt256>, ISerializable
{
public const int Length = 32;
public static readonly UInt256 Zero = new UInt256();

private ulong value1;
private ulong value2;
private ulong value3;
private ulong value4;
[FieldOffset(0)] private ulong value1;
[FieldOffset(8)] private ulong value2;
[FieldOffset(16)] private ulong value3;
[FieldOffset(24)] private ulong value4;

public override int Size => Length;
public int Size => Length;

public UInt256()
{
Expand Down Expand Up @@ -47,7 +51,7 @@ public int CompareTo(UInt256 other)
return value1.CompareTo(other.value1);
}

public override void Deserialize(BinaryReader reader)
public void Deserialize(BinaryReader reader)
{
value1 = reader.ReadUInt64();
value2 = reader.ReadUInt64();
Expand Down Expand Up @@ -85,7 +89,7 @@ public override int GetHashCode()
/// Method Parse receives a big-endian hex string and stores as a UInt256 little-endian 32-bytes array
/// Example: Parse("0xa400ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff01") should create UInt256 01ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00a4
/// </summary>
public static new UInt256 Parse(string s)
public static UInt256 Parse(string s)
{
if (s == null)
throw new ArgumentNullException();
Expand All @@ -98,14 +102,19 @@ public override int GetHashCode()
return new UInt256(data);
}

public override void Serialize(BinaryWriter writer)
public void Serialize(BinaryWriter writer)
{
writer.Write(value1);
writer.Write(value2);
writer.Write(value3);
writer.Write(value4);
}

public override string ToString()
{
return "0x" + this.ToArray().ToHexString(reverse: true);
}

/// <summary>
/// Method TryParse tries to parse a big-endian hex string and store it as a UInt256 little-endian 32-bytes array
/// Example: TryParse("0xa400ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff01", result) should create result UInt256 01ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00a4
Expand Down
90 changes: 0 additions & 90 deletions src/neo/UIntBase.cs

This file was deleted.

49 changes: 0 additions & 49 deletions tests/neo.UnitTests/UT_UIntBase.cs

This file was deleted.