Skip to content
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
6b31958
Json BigInteger
shargon Dec 27, 2023
48c2649
Fix Min
shargon Dec 27, 2023
186c2ba
Fix ut
shargon Dec 27, 2023
eab5b25
add ut
shargon Dec 27, 2023
776ab64
Fix exception msg
shargon Dec 27, 2023
9ccb97d
format
shargon Dec 27, 2023
458499b
Fix UT
shargon Dec 27, 2023
edae719
Merge branch 'master' into json-bi
Feb 5, 2024
be973c9
Merge branch 'master' into json-bi
Feb 15, 2024
308d33d
Update src/Neo.Json/JNumber.cs
Feb 15, 2024
19e3111
Update tests/Neo.Json.UnitTests/UT_JNumber.cs
Feb 15, 2024
1214697
Fix format
shargon Feb 15, 2024
58ec583
Merge branch 'master' into json-bi
shargon Feb 15, 2024
efa2c34
Merge branch 'master' into json-bi
vncoelho Feb 16, 2024
bcc5fd0
add hardofork HF_Echidna
Aug 7, 2024
e63f10b
Add entries to `Designation` event (#3397)
shargon Aug 8, 2024
9d16c53
[Neo Core StdLib] Add Base64url (#3453)
Aug 8, 2024
f746f8d
add hardofork HF_Echidna
Aug 7, 2024
8d7f9e8
Add entries to `Designation` event (#3397)
shargon Aug 8, 2024
3fc8077
[Neo Core StdLib] Add Base64url (#3453)
Aug 8, 2024
620d938
Merge branch 'HF_Echidna' of github.com:neo-project/neo into HF_Echidna
Oct 2, 2024
7e31d0c
add hardofork HF_Echidna
Aug 7, 2024
993e3fe
Add entries to `Designation` event (#3397)
shargon Aug 8, 2024
37bf0cb
[Neo Core StdLib] Add Base64url (#3453)
Aug 8, 2024
7dba130
format
Nov 6, 2024
0457ccd
Merge branch 'HF_Echidna' of github.com:neo-project/neo into HF_Echidna
Nov 6, 2024
d7a291e
Merge Master
cschuchardt88 Nov 17, 2024
6e780c0
Fixed typo
cschuchardt88 Nov 17, 2024
02d6ab9
Added back #3397
cschuchardt88 Nov 17, 2024
8fb30df
Fixed tests
cschuchardt88 Nov 17, 2024
f9244eb
fixed global.json
cschuchardt88 Nov 17, 2024
650c9e9
Merge branch 'master' into HF_Echidna
Nov 18, 2024
331541a
Merge branch 'master' into HF_Echidna
Nov 20, 2024
eb96d14
Merge branch 'master' into HF_Echidna
shargon Nov 20, 2024
74498e5
Merge branch 'master' into HF_Echidna
Nov 23, 2024
195abf9
Merge branch 'HF_Echidna' into json-bi
shargon Nov 28, 2024
4b6542b
Add min and max ut
shargon Nov 28, 2024
758933e
Vitor suggestion
shargon Nov 28, 2024
3599c81
Avoid doble casting
shargon Nov 28, 2024
d418916
Add HK checker
shargon Nov 28, 2024
04402c9
Reduce changes
shargon Nov 28, 2024
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
13 changes: 10 additions & 3 deletions src/Neo.Json/JNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// modifications are permitted.

using System.Globalization;
using System.Numerics;
using System.Text.Json;

namespace Neo.Json
Expand Down Expand Up @@ -40,7 +41,13 @@ public class JNumber : JToken
/// <param name="value">The value of the JSON token.</param>
public JNumber(double value = 0)
{
if (!double.IsFinite(value)) throw new FormatException();
if (!double.IsFinite(value))
throw new ArgumentException("value is not finite", nameof(value));
if (value > MAX_SAFE_INTEGER)
throw new ArgumentException("value is higher than MAX_SAFE_INTEGER", nameof(value));
if (value < MIN_SAFE_INTEGER)
throw new ArgumentException("value is lower than MIN_SAFE_INTEGER", nameof(value));

this.Value = value;
}

Expand Down Expand Up @@ -119,9 +126,9 @@ public static implicit operator JNumber(double value)
return new JNumber(value);
}

public static implicit operator JNumber(long value)
public static implicit operator JNumber(BigInteger value)
{
return new JNumber(value);
return new JNumber((long)value);
}

public static bool operator ==(JNumber left, JNumber? right)
Expand Down
6 changes: 6 additions & 0 deletions src/Neo.Json/JToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using System.Numerics;
using System.Text.Json;
using static Neo.Json.Utility;

Expand Down Expand Up @@ -298,6 +299,11 @@ public static implicit operator JToken(double value)
return (JNumber)value;
}

public static implicit operator JToken(BigInteger value)
{
return (JNumber)value;
}

public static implicit operator JToken?(string? value)
{
return (JString?)value;
Expand Down
7 changes: 2 additions & 5 deletions src/Neo/SmartContract/JsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@ public static JToken Serialize(StackItem item)
}
case Integer num:
{
var integer = num.GetInteger();
if (integer > JNumber.MAX_SAFE_INTEGER || integer < JNumber.MIN_SAFE_INTEGER)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, #2498.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

He put it in the JNumber. Different exception, but will fail as well.

Copy link
Member Author

@shargon shargon Dec 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was moved to the constructor, that was the bug, when we used parse we allowed it...

throw new InvalidOperationException();
return (double)integer;
return num.GetInteger();
}
case Boolean boolean:
{
Expand All @@ -66,7 +63,7 @@ public static JToken Serialize(StackItem item)

foreach (var entry in map)
{
if (!(entry.Key is ByteString)) throw new FormatException();
if (entry.Key is not ByteString) throw new FormatException();

var key = entry.Key.GetString();
var value = Serialize(entry.Value);
Expand Down
17 changes: 14 additions & 3 deletions tests/Neo.Json.UnitTests/UT_JNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using System.Numerics;

namespace Neo.Json.UnitTests
{
enum Woo
Expand Down Expand Up @@ -37,20 +39,29 @@ public void SetUp()
public void TestAsBoolean()
{
maxInt.AsBoolean().Should().BeTrue();
minInt.AsBoolean().Should().BeTrue();
zero.AsBoolean().Should().BeFalse();
}

[TestMethod]
public void TestBigInteger()
{
((JNumber)BigInteger.One).AsNumber().Should().Be(1);
((JNumber)BigInteger.Zero).AsNumber().Should().Be(0);
((JNumber)BigInteger.MinusOne).AsNumber().Should().Be(-1);
}
Copy link
Member

@cschuchardt88 cschuchardt88 Dec 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When doing testing, use max and min values please. You will see that using the max or min will fail.... double is to small to hold the data. BigInteger is 32 BYTES and double is 8 BYTES you get overflows. Its like doing

var failed = checked((int)long.MaxValue);

image

image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ping @shargon

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added ut with min and max


[TestMethod]
public void TestAsString()
{
Action action1 = () => new JNumber(double.PositiveInfinity).AsString();
action1.Should().Throw<FormatException>();
action1.Should().Throw<ArgumentException>();

Action action2 = () => new JNumber(double.NegativeInfinity).AsString();
action2.Should().Throw<FormatException>();
action2.Should().Throw<ArgumentException>();

Action action3 = () => new JNumber(double.NaN).AsString();
action3.Should().Throw<FormatException>();
action3.Should().Throw<ArgumentException>();
}

[TestMethod]
Expand Down
12 changes: 6 additions & 6 deletions tests/Neo.UnitTests/SmartContract/UT_JsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void JsonTest_WrongJson()
Assert.ThrowsException<FormatException>(() => JObject.Parse(json));

json = @"{""length"":99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999}";
Assert.ThrowsException<FormatException>(() => JObject.Parse(json));
Assert.ThrowsException<ArgumentException>(() => JObject.Parse(json));
}

[TestMethod]
Expand Down Expand Up @@ -91,10 +91,10 @@ public void JsonTest_Numbers()

Assert.AreEqual("[1,-2,3.5]", parsed.ToString());

json = "[200.500000E+005,200.500000e+5,-1.1234e-100,9.05E+28]";
json = "[200.500000E+005,200.500000e+5,-1.1234e-100,9.05E+8]";
parsed = JObject.Parse(json);

Assert.AreEqual("[20050000,20050000,-1.1234E-100,9.05E+28]", parsed.ToString());
Assert.AreEqual("[20050000,20050000,-1.1234E-100,905000000]", parsed.ToString());

json = "[-]";
Assert.ThrowsException<FormatException>(() => JObject.Parse(json));
Expand Down Expand Up @@ -216,7 +216,7 @@ public void Serialize_EmptyObject()
public void Serialize_Number()
{
var entry = new VM.Types.Array { 1, 9007199254740992 };
Assert.ThrowsException<InvalidOperationException>(() => JsonSerializer.Serialize(entry));
Assert.ThrowsException<ArgumentException>(() => JsonSerializer.Serialize(entry));
}

[TestMethod]
Expand Down Expand Up @@ -303,7 +303,7 @@ public void Serialize_Array_Bool_Str_Num()
public void Deserialize_Array_Bool_Str_Num()
{
ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Application, null, null);
var items = JsonSerializer.Deserialize(engine, JObject.Parse("[true,\"test\",123,9.05E+28]"), ExecutionEngineLimits.Default);
var items = JsonSerializer.Deserialize(engine, JObject.Parse("[true,\"test\",123,1.05E+4]"), ExecutionEngineLimits.Default);

Assert.IsInstanceOfType(items, typeof(VM.Types.Array));
Assert.AreEqual(((VM.Types.Array)items).Count, 4);
Expand All @@ -313,7 +313,7 @@ public void Deserialize_Array_Bool_Str_Num()
Assert.IsTrue(array[0].GetBoolean());
Assert.AreEqual(array[1].GetString(), "test");
Assert.AreEqual(array[2].GetInteger(), 123);
Assert.AreEqual(array[3].GetInteger(), BigInteger.Parse("90500000000000000000000000000"));
Assert.AreEqual(array[3].GetInteger(), BigInteger.Parse("10500"));
}

[TestMethod]
Expand Down