Skip to content
Open
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
29 changes: 29 additions & 0 deletions Substrate.NetApi.Test/TypeConverters/PrimitiveTypesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,19 @@ public void PrimU128Test()
Assert.AreEqual(value, primImplicit);
}

/// <summary>
/// From real use case here : https://mythos.subscan.io/block/2826170?tab=event&event=2826170-189
/// </summary>
[Test]
[TestCase("310789037729451024594500450253905211276")]
public void PrimU128_FromMaxBigIntegerValue_Test(string valueStr)
{
var value = BigInteger.Parse(valueStr);

var prim = new U128(value);
Assert.That(prim.Value, Is.EqualTo(value));
}

[Test]
public void PrimU128CreateTest()
{
Expand Down Expand Up @@ -213,6 +226,22 @@ public void PrimU128_WithNegativeNumber_ShouldFail()
Assert.Throws<InvalidOperationException>(() => new U128(number));
}

[Test]
public void PrimU256_MaxValue()
{
BigInteger u256Max = BigInteger.Pow(2, 256) - 1;
var u256 = new U256(u256Max);

Assert.That(u256.Value, Is.EqualTo(u256Max));
}

[Test]
public void PrimU256_BasicNumberTest()
{
var u256 = new U256(new BigInteger(10));
Assert.That(u256.Value, Is.EqualTo(new BigInteger(10)));
}

[Test]
public void PrimU256Test()
{
Expand Down
11 changes: 9 additions & 2 deletions Substrate.NetApi/Model/Types/Primitive/U128.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,20 @@ public override void Create(BigInteger value)

var byteArray = value.ToByteArray();

if (byteArray.Length > TypeSize)
if (byteArray.Length > TypeSize + 1)
{
throw new NotSupportedException($"Wrong byte array size for {TypeName()}, max. {TypeSize} bytes!");
}

var bytes = new byte[TypeSize];
byteArray.CopyTo(bytes, 0);
if (byteArray.Length == TypeSize + 1)
{
Array.Copy(byteArray, 0, bytes, 0, TypeSize);
}
else
{
byteArray.CopyTo(bytes, 0);
}
Bytes = bytes;
Value = value;
}
Expand Down
12 changes: 9 additions & 3 deletions Substrate.NetApi/Model/Types/Primitive/U256.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,20 @@ public override void Create(BigInteger value)
throw new InvalidOperationException($"Unable to create a {nameof(U256)} instance while value is negative");

var byteArray = value.ToByteArray();

if (byteArray.Length > TypeSize)
if (byteArray.Length > TypeSize + 1)
{
throw new NotSupportedException($"Wrong byte array size for {TypeName()}, max. {TypeSize} bytes!");
}

var bytes = new byte[TypeSize];
byteArray.CopyTo(bytes, 0);
if(byteArray.Length == TypeSize + 1) {
Array.Copy(byteArray, 0, bytes, 0, TypeSize);
}
else {
byteArray.CopyTo(bytes, 0);
}

Bytes = bytes;
Value = value;
}
Expand Down
Loading