Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion src/Neo.CLI/config.fs.mainnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"HF_Aspidochelone": 3000000,
"HF_Basilisk": 4500000,
"HF_Cockatrice": 5800000,
"HF_Domovoi": 5800000
"HF_Domovoi": 5800000,
"HF_Echidna": 5800001
},
"StandbyCommittee": [
"026fa34ec057d74c2fdf1a18e336d0bd597ea401a0b2ad57340d5c220d09f44086",
Expand Down
3 changes: 2 additions & 1 deletion src/Neo.CLI/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"HF_Aspidochelone": 1730000,
"HF_Basilisk": 4120000,
"HF_Cockatrice": 5450000,
"HF_Domovoi": 5570000
"HF_Domovoi": 5570000,
"HF_Echidna": 5570001
},
"InitialGasDistribution": 5200000000000000,
"ValidatorsCount": 7,
Expand Down
3 changes: 2 additions & 1 deletion src/Neo.CLI/config.mainnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"HF_Aspidochelone": 1730000,
"HF_Basilisk": 4120000,
"HF_Cockatrice": 5450000,
"HF_Domovoi": 5570000
"HF_Domovoi": 5570000,
"HF_Echidna": 5570001
},
"InitialGasDistribution": 5200000000000000,
"ValidatorsCount": 7,
Expand Down
3 changes: 2 additions & 1 deletion src/Neo.CLI/config.testnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"HF_Aspidochelone": 210000,
"HF_Basilisk": 2680000,
"HF_Cockatrice": 3967000,
"HF_Domovoi": 4144000
"HF_Domovoi": 4144000,
"HF_Echidna": 4144001
},
"InitialGasDistribution": 5200000000000000,
"ValidatorsCount": 7,
Expand Down
32 changes: 30 additions & 2 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 @@ -38,9 +39,20 @@ public class JNumber : JToken
/// Initializes a new instance of the <see cref="JNumber"/> class with the specified value.
/// </summary>
/// <param name="value">The value of the JSON token.</param>
public JNumber(double value = 0)
/// <param name="checkMinMax">True if we want to ensure that the value is in the limits.</param>
public JNumber(double value = 0, bool checkMinMax = true)
{
if (!double.IsFinite(value)) throw new FormatException();
if (checkMinMax)
{
if (value > MAX_SAFE_INTEGER)
Copy link
Member

Choose a reason for hiding this comment

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

The value is double. It shouldn't be limited.

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));
}

if (!double.IsFinite(value))
throw new ArgumentException("value is not finite", nameof(value));

Value = value;
}

Expand Down Expand Up @@ -125,6 +137,22 @@ public static implicit operator JNumber(long value)
return new JNumber(value);
}

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

/// <summary>
/// Check if two JNumber are equal.
/// </summary>
/// <param name="left">Non null <see cref="JNumber"></see> value </param>
/// <param name="right">Nullable <see cref="JNumber"></see> value</param>
/// <returns>bool</returns>
/// <remarks>
/// If the left is null, throw an <exception cref="NullReferenceException"></exception>.
/// If the right is null, return false.
/// If the left and right are the same object, return true.
/// </remarks>
public static bool operator ==(JNumber left, JNumber? right)
{
if (right is null) return false;
Expand Down
8 changes: 7 additions & 1 deletion 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 @@ -295,10 +296,15 @@ public static implicit operator JToken(JToken?[] value)

public static implicit operator JToken(bool value)
{
return (JBoolean)value;
return new JBoolean(value);
}

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

public static implicit operator JToken(BigInteger value)
{
return (JNumber)value;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Neo/Hardfork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public enum Hardfork : byte
HF_Aspidochelone,
HF_Basilisk,
HF_Cockatrice,
HF_Domovoi
HF_Domovoi,
HF_Echidna
}
}
2 changes: 2 additions & 0 deletions src/Neo/Neo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="8.0.1" />
<PackageReference Include="System.IO.Hashing" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
14 changes: 6 additions & 8 deletions src/Neo/SmartContract/JsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@ public static class JsonSerializer
/// Serializes a <see cref="StackItem"/> to a <see cref="JToken"/>.
/// </summary>
/// <param name="item">The <see cref="StackItem"/> to serialize.</param>
/// <param name="hardforkChecker">Hardfork checker</param>
/// <returns>The serialized object.</returns>
public static JToken Serialize(StackItem item)
public static JToken Serialize(StackItem item, Func<Hardfork, bool> hardforkChecker = null)
Copy link
Member Author

Choose a reason for hiding this comment

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

@neo-project/core This methods seems that was only used in UT...

{
switch (item)
{
case Array array:
{
return array.Select(p => Serialize(p)).ToArray();
return array.Select(p => Serialize(p, hardforkChecker)).ToArray();
}
case ByteString _:
case Buffer _:
Expand All @@ -51,10 +52,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 new JNumber((long)num.GetInteger(), hardforkChecker == null || hardforkChecker(Hardfork.HF_Echidna));
}
case Boolean boolean:
{
Expand All @@ -66,10 +64,10 @@ 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);
var value = Serialize(entry.Value, hardforkChecker);

ret[key] = value;
}
Expand Down
24 changes: 22 additions & 2 deletions src/Neo/SmartContract/Native/RoleManagement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,16 @@ public sealed class RoleManagement : NativeContract
{
[ContractEvent(0, name: "Designation",
"Role", ContractParameterType.Integer,
"BlockIndex", ContractParameterType.Integer)]
"BlockIndex", ContractParameterType.Integer,
Hardfork.HF_Echidna)]

[ContractEvent(Hardfork.HF_Echidna, 0, name: "Designation",
"Role", ContractParameterType.Integer,
"BlockIndex", ContractParameterType.Integer,
"Old", ContractParameterType.Array,
"New", ContractParameterType.Array
)]

internal RoleManagement() : base() { }

/// <summary>
Expand All @@ -51,6 +60,7 @@ public ECPoint[] GetDesignatedByRole(DataCache snapshot, Role role, uint index)
}

[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.States | CallFlags.AllowNotify)]
[Obsolete]
private void DesignateAsRole(ApplicationEngine engine, Role role, ECPoint[] nodes)
{
if (nodes.Length == 0 || nodes.Length > 32)
Expand All @@ -69,7 +79,17 @@ private void DesignateAsRole(ApplicationEngine engine, Role role, ECPoint[] node
list.AddRange(nodes);
list.Sort();
engine.SnapshotCache.Add(key, new StorageItem(list));
engine.SendNotification(Hash, "Designation", new VM.Types.Array(engine.ReferenceCounter, new StackItem[] { (int)role, engine.PersistingBlock.Index }));
if (engine.IsHardforkEnabled(Hardfork.HF_Echidna))
{
var oldNodes = new VM.Types.Array(engine.ReferenceCounter, GetDesignatedByRole(engine.Snapshot, role, index - 1).Select(u => (ByteString)u.EncodePoint(true)));
var newNodes = new VM.Types.Array(engine.ReferenceCounter, nodes.Select(u => (ByteString)u.EncodePoint(true)));

engine.SendNotification(Hash, "Designation", new VM.Types.Array(engine.ReferenceCounter, [(int)role, engine.PersistingBlock.Index, oldNodes, newNodes]));
}
else
{
engine.SendNotification(Hash, "Designation", new VM.Types.Array(engine.ReferenceCounter, [(int)role, engine.PersistingBlock.Index]));
}
}

private class NodeList : InteroperableList<ECPoint>
Expand Down
23 changes: 23 additions & 0 deletions src/Neo/SmartContract/Native/StdLib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#pragma warning disable IDE0051

using Microsoft.IdentityModel.Tokens;
using Neo.Cryptography;
using Neo.Json;
using Neo.VM.Types;
Expand Down Expand Up @@ -131,6 +132,28 @@ public static byte[] Base64Decode([MaxLength(MaxInputLength)] string s)
return Convert.FromBase64String(s);
}

/// <summary>
/// Encodes a byte array into a base64Url string.
/// </summary>
/// <param name="data">The base64Url to be encoded.</param>
/// <returns>The encoded base64Url string.</returns>
[ContractMethod(Hardfork.HF_Echidna, CpuFee = 1 << 5)]
public static string Base64UrlEncode([MaxLength(MaxInputLength)] string data)
{
return Base64UrlEncoder.Encode(data);
}

/// <summary>
/// Decodes a byte array from a base64Url string.
/// </summary>
/// <param name="s">The base64Url string.</param>
/// <returns>The decoded base64Url string.</returns>
[ContractMethod(Hardfork.HF_Echidna, CpuFee = 1 << 5)]
public static string Base64UrlDecode([MaxLength(MaxInputLength)] string s)
{
return Base64UrlEncoder.Decode(s);
}

/// <summary>
/// Encodes a byte array into a base58 <see cref="string"/>.
/// </summary>
Expand Down
33 changes: 30 additions & 3 deletions tests/Neo.Json.UnitTests/UT_JNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,47 @@ 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);
((JNumber)JNumber.MAX_SAFE_INTEGER).AsNumber().Should().Be(JNumber.MAX_SAFE_INTEGER);
((JNumber)JNumber.MIN_SAFE_INTEGER).AsNumber().Should().Be(JNumber.MIN_SAFE_INTEGER);
}

[TestMethod]
public void TestNullEqual()
{
JNumber nullJNumber = null;

Assert.IsFalse(maxInt.Equals(null));
Assert.IsFalse(maxInt == null);
Assert.IsFalse(minInt.Equals(null));
Assert.IsFalse(minInt == null);
Assert.IsFalse(zero == null);

Assert.ThrowsException<NullReferenceException>(() => nullJNumber == maxInt);
Assert.ThrowsException<NullReferenceException>(() => nullJNumber == minInt);
Assert.ThrowsException<NullReferenceException>(() => nullJNumber == zero);
}

[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
Loading