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

Add random number to neo #2477

Merged
merged 65 commits into from
Jun 30, 2021
Merged
Changes from 18 commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
b3680dc
Add Verifiable Random Function and test cases
Jim8y May 16, 2021
6579546
update the code format.
Jim8y May 16, 2021
192e553
Merge branch 'master' into master
shargon May 16, 2021
bcb863a
Remove empty lines
shargon May 17, 2021
a525e77
Remove empty lines
shargon May 17, 2021
7b99fad
fix TODO by throwing exception
Jim8y May 17, 2021
e47e565
Merge branch 'master' of github.com:Liaojinghui/neo
Jim8y May 17, 2021
1391184
add a `nonce` field to the header
Jim8y May 18, 2021
80fff9d
Add comment to GetRandom
Jim8y May 18, 2021
cc1b9f7
update comment
Jim8y May 18, 2021
1b2f912
change pubkey type to ECPoint
Jim8y May 18, 2021
10e7b6d
Add exception
Jim8y May 20, 2021
4ecdc48
Remove VRF from neo core to dBFT plugin
Jim8y May 21, 2021
d8f2bb2
Remove empty line
erikzhang May 22, 2021
d20bd0d
add suppoort to multiple random numbers
Jim8y May 22, 2021
110db72
update test
Jim8y May 22, 2021
0126393
Add extra random check
Jim8y May 22, 2021
bb3e29d
fix mistake
Jim8y May 22, 2021
cd23177
update random algorithm
Jim8y May 22, 2021
d58a3ce
Clean code
shargon May 23, 2021
99fa4b7
Add extra check to next_nonce
Jim8y May 23, 2021
5ed2940
fix error
Jim8y May 23, 2021
f52c2b2
Add more test to test random number
Jim8y May 23, 2021
60086e3
Add extra check to make sure the next_once will be reinitialized when…
Jim8y May 23, 2021
798cafc
Variable name style consistency
Jim8y May 23, 2021
91a8836
Remove `nonce` from the header.
Jim8y May 24, 2021
6a1ba3d
update test
Jim8y May 24, 2021
11ba5b7
fix test error. So weird the error does not exist in my local environ…
Jim8y May 24, 2021
1ba655f
get the nonce_tx at index 0
Jim8y May 27, 2021
9470311
Set next_nonce to null if there is no valid getrandom call in the per…
Jim8y May 27, 2021
728f479
update runtime test
Jim8y May 27, 2021
62eda32
Set the ECCurve.N public since VRFin the dBFT need to use this value.
Jim8y May 30, 2021
a0ee63e
Merge branch 'master' into vrf
shargon May 31, 2021
e62b56b
add nonce to header
Jim8y Jun 8, 2021
303bbbb
nextnonce : ulong to replace byte[]
Jim8y Jun 8, 2021
e5aa633
format
erikzhang Jun 9, 2021
d0c81f7
add nonce to `trimmedblock`
Jim8y Jun 9, 2021
c48adbc
Merge branch 'vrf' of https://github.com/Liaojinghui/neo into vrf
Jim8y Jun 9, 2021
491df4a
update comment
Jim8y Jun 9, 2021
2cec716
add `Murmur128`
Jim8y Jun 9, 2021
73b989c
add more test to `Murmur128`
Jim8y Jun 9, 2021
3087243
fix format
Jim8y Jun 9, 2021
e43c150
`ulong` to `hexstring` in `ToJson`
Jim8y Jun 9, 2021
714bac5
update nonce.
Jim8y Jun 10, 2021
3d3d3ed
update format
Jim8y Jun 10, 2021
4a508ae
update `getrandom`
Jim8y Jun 10, 2021
9359dff
update comment
Jim8y Jun 10, 2021
4360d00
Update src/neo/Network/P2P/Payloads/Header.cs
Jim8y Jun 11, 2021
8a059bb
update unite test
Jim8y Jun 11, 2021
2fab87f
Update src/neo/Network/P2P/Payloads/Header.cs
Jim8y Jun 11, 2021
866ffe4
update test
Jim8y Jun 11, 2021
bcd6b27
Merge branch 'vrf' of https://github.com/Liaojinghui/neo into vrf
Jim8y Jun 11, 2021
874e633
Merge branch 'master' into vrf
Qiao-Jin Jun 23, 2021
f5b3bf1
Update `GetRandom`
Jim8y Jun 24, 2021
1ca4244
update comments
Jim8y Jun 24, 2021
8f89321
update comments and format
Jim8y Jun 24, 2021
654e37b
Improve
erikzhang Jun 25, 2021
5258a1b
More improve
erikzhang Jun 25, 2021
ef41ef2
Fix UT
erikzhang Jun 25, 2021
daa642b
update `nonce` test cases
Jim8y Jun 25, 2021
ac35197
Merge branch 'vrf' of https://github.com/Liaojinghui/neo into vrf
Jim8y Jun 25, 2021
460f0a6
update comments
Jim8y Jun 26, 2021
77c7775
Format UT
shargon Jun 28, 2021
2c6be15
Merge branch 'master' into vrf
shargon Jun 29, 2021
30f33d0
Merge branch 'master' into vrf
erikzhang Jun 30, 2021
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
1 change: 1 addition & 0 deletions src/neo/NeoSystem.cs
Original file line number Diff line number Diff line change
@@ -134,6 +134,7 @@ public NeoSystem(ProtocolSettings settings, string storageEngine = null, string
MerkleRoot = UInt256.Zero,
Timestamp = (new DateTime(2016, 7, 15, 15, 8, 21, DateTimeKind.Utc)).ToTimestampMS(),
Index = 0,
Nonce = 2083236893, // nonce from the Bitcoin genesis block.
vncoelho marked this conversation as resolved.
Show resolved Hide resolved
PrimaryIndex = 0,
NextConsensus = Contract.GetBFTAddress(settings.StandbyValidators),
Witness = new Witness
5 changes: 5 additions & 0 deletions src/neo/Network/P2P/Payloads/Block.cs
Original file line number Diff line number Diff line change
@@ -51,6 +51,11 @@ public sealed class Block : IEquatable<Block>, IInventory
/// </summary>
public uint Index => Header.Index;

/// <summary>
/// The random number of the block from VRF.
/// </summary>
public uint Nonce => Header.Nonce;

/// <summary>
/// The primary index of the consensus node that generated this block.
/// </summary>
14 changes: 14 additions & 0 deletions src/neo/Network/P2P/Payloads/Header.cs
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@ public sealed class Header : IEquatable<Header>, IVerifiable
private UInt256 merkleRoot;
private ulong timestamp;
private uint index;
private uint nonce;
private byte primaryIndex;
private UInt160 nextConsensus;

@@ -73,6 +74,15 @@ public uint Index
set { index = value; _hash = null; }
}

/// <summary>
/// The last four bytes of random number generated from VRF
/// </summary>
public uint Nonce
{
get => nonce;
set { nonce = value; _hash = null; }
}

/// <summary>
/// The primary index of the consensus node that generated this block.
/// </summary>
@@ -110,6 +120,7 @@ public UInt256 Hash
UInt256.Length + // MerkleRoot
sizeof(ulong) + // Timestamp
sizeof(uint) + // Index
sizeof(uint) + // Nonce
sizeof(byte) + // PrimaryIndex
UInt160.Length + // NextConsensus
1 + Witness.Size; // Witness
@@ -144,6 +155,7 @@ void IVerifiable.DeserializeUnsigned(BinaryReader reader)
merkleRoot = reader.ReadSerializable<UInt256>();
timestamp = reader.ReadUInt64();
index = reader.ReadUInt32();
nonce = reader.ReadUInt32();
erikzhang marked this conversation as resolved.
Show resolved Hide resolved
primaryIndex = reader.ReadByte();
nextConsensus = reader.ReadSerializable<UInt160>();
}
@@ -186,6 +198,7 @@ void IVerifiable.SerializeUnsigned(BinaryWriter writer)
writer.Write(merkleRoot);
writer.Write(timestamp);
writer.Write(index);
writer.Write(nonce);
writer.Write(primaryIndex);
writer.Write(nextConsensus);
}
@@ -205,6 +218,7 @@ public JObject ToJson(ProtocolSettings settings)
json["merkleroot"] = merkleRoot.ToString();
json["time"] = timestamp;
json["index"] = index;
json["nonce"] = nonce;
json["primary"] = primaryIndex;
json["nextconsensus"] = nextConsensus.ToAddress(settings.AddressVersion);
json["witnesses"] = new JArray(Witness.ToJson());
18 changes: 18 additions & 0 deletions src/neo/SmartContract/ApplicationEngine.Runtime.cs
Original file line number Diff line number Diff line change
@@ -77,6 +77,13 @@ partial class ApplicationEngine
/// </summary>
public static readonly InteropDescriptor System_Runtime_GetInvocationCounter = Register("System.Runtime.GetInvocationCounter", nameof(GetInvocationCounter), 1 << 4, CallFlags.None);

/// <summary>
/// The <see cref="InteropDescriptor"/> of System.Runtime.GetRandom.
/// Gets the random number generated form the VRF.
erikzhang marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
public static readonly InteropDescriptor System_Runtime_GetRandom = Register("System.Runtime.GetRandom", nameof(GetRandom), 1 << 4, CallFlags.None);
erikzhang marked this conversation as resolved.
Show resolved Hide resolved


/// <summary>
/// The <see cref="InteropDescriptor"/> of System.Runtime.Log.
/// Writes a log.
@@ -228,6 +235,17 @@ protected internal int GetInvocationCounter()
return counter;
}

/// <summary>
/// The implementation of System.Runtime.GetRandom.
/// Gets the random number genrated form the VRF
/// Primary geterates this random number with `prevHash`, the hash of the previous (validators.Length/3 + 1) block
erikzhang marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
/// <returns>The last four bytes of the random number.</returns>
protected internal uint GetRandom()
{
return (uint)random.Next(int.MinValue, int.MaxValue);
}

/// <summary>
/// The implementation of System.Runtime.Log.
/// Writes a log.
5 changes: 5 additions & 0 deletions src/neo/SmartContract/ApplicationEngine.cs
Original file line number Diff line number Diff line change
@@ -115,6 +115,8 @@ public partial class ApplicationEngine : ExecutionEngine
/// </summary>
public IReadOnlyList<NotifyEventArgs> Notifications => notifications ?? (IReadOnlyList<NotifyEventArgs>)Array.Empty<NotifyEventArgs>();

private static Random random;
erikzhang marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Initializes a new instance of the <see cref="ApplicationEngine"/> class.
/// </summary>
@@ -254,6 +256,9 @@ protected override void ContextUnloaded(ExecutionContext context)
/// <returns>The engine instance created.</returns>
public static ApplicationEngine Create(TriggerType trigger, IVerifiable container, DataCache snapshot, Block persistingBlock = null, ProtocolSettings settings = null, long gas = TestModeGas)
{
if (persistingBlock != null && random == null)
random = new Random((int)persistingBlock.Nonce);

return applicationEngineProvider?.Create(trigger, container, snapshot, persistingBlock, settings, gas)
?? new ApplicationEngine(trigger, container, snapshot, persistingBlock, settings, gas);
}
2 changes: 1 addition & 1 deletion tests/neo.UnitTests/Ledger/UT_TrimmedBlock.cs
Original file line number Diff line number Diff line change
@@ -87,7 +87,7 @@ public void TestGetSize()
{
TrimmedBlock tblock = GetTrimmedBlockWithNoTransaction();
tblock.Hashes = new UInt256[] { TestUtils.GetTransaction(UInt160.Zero).Hash };
tblock.Size.Should().Be(138);
tblock.Size.Should().Be(142); // 138 + nonce
}

[TestMethod]
43 changes: 23 additions & 20 deletions tests/neo.UnitTests/Network/P2P/Payloads/UT_Block.cs
Original file line number Diff line number Diff line change
@@ -28,45 +28,46 @@ public void Transactions_Get()
public void Header_Get()
{
UInt256 val256 = UInt256.Zero;
TestUtils.SetupBlockWithValues(uut, val256, out var merkRootVal, out _, out var timestampVal, out var indexVal, out var scriptVal, out _, 0);
TestUtils.SetupBlockWithValues(uut, val256, out var merkRootVal, out _, out var timestampVal, out var indexVal, out var nonceVal, out var scriptVal, out _, 0);

uut.Header.Should().NotBeNull();
uut.Header.PrevHash.Should().Be(val256);
uut.Header.MerkleRoot.Should().Be(merkRootVal);
uut.Header.Timestamp.Should().Be(timestampVal);
uut.Header.Index.Should().Be(indexVal);
uut.Header.Nonce.Should().Be(nonceVal);
uut.Header.Witness.Should().Be(scriptVal);
}

[TestMethod]
public void Size_Get()
{
UInt256 val256 = UInt256.Zero;
TestUtils.SetupBlockWithValues(uut, val256, out var _, out var _, out var _, out var _, out var _, out var _, 0);
TestUtils.SetupBlockWithValues(uut, val256, out var _, out var _, out var _, out var _, out var _, out var _, out var _, 0);
// header 4 + 32 + 32 + 8 + 4 + 1 + 20 + 4
// tx 1
uut.Size.Should().Be(106);
uut.Size.Should().Be(110); // 106 + nonce
}

[TestMethod]
public void Size_Get_1_Transaction()
{
UInt256 val256 = UInt256.Zero;
TestUtils.SetupBlockWithValues(uut, val256, out var _, out var _, out var _, out var _, out var _, out var _, 0);
TestUtils.SetupBlockWithValues(uut, val256, out var _, out var _, out var _, out var _, out var _, out var _, out var _, 0);

uut.Transactions = new[]
{
TestUtils.GetTransaction(UInt160.Zero)
};

uut.Size.Should().Be(159);
uut.Size.Should().Be(163); // 159 + nonce
}

[TestMethod]
public void Size_Get_3_Transaction()
{
UInt256 val256 = UInt256.Zero;
TestUtils.SetupBlockWithValues(uut, val256, out var _, out var _, out var _, out var _, out var _, out var _, 0);
TestUtils.SetupBlockWithValues(uut, val256, out var _, out var _, out var _, out var _, out var _, out var _, out var _, 0);

uut.Transactions = new[]
{
@@ -75,41 +76,42 @@ public void Size_Get_3_Transaction()
TestUtils.GetTransaction(UInt160.Zero)
};

uut.Size.Should().Be(265);
uut.Size.Should().Be(269); // 265 + nonce
}

[TestMethod]
public void Serialize()
{
UInt256 val256 = UInt256.Zero;
TestUtils.SetupBlockWithValues(uut, val256, out var _, out var _, out var _, out var _, out var _, out var _, 1);
TestUtils.SetupBlockWithValues(uut, val256, out var _, out var _, out var _, out var _, out var _, out var _, out var _, 1);

var hex = "0000000000000000000000000000000000000000000000000000000000000000000000006c23be5d32679baa9c5c2aa0d329fd2a2441d7875d0f34d42f58f70428fbbbb9e913ff854c0000000000000000000000000000000000000000000000000000000001000111010000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000001000112010000";
var hex = "0000000000000000000000000000000000000000000000000000000000000000000000006c23be5d32679baa9c5c2aa0d329fd2a2441d7875d0f34d42f58f70428fbbbb9e913ff854c000000000000000000000000000000000000000000000000000000000000000001000111010000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000001000112010000";
uut.ToArray().ToHexString().Should().Be(hex);
}

[TestMethod]
public void Deserialize()
{
UInt256 val256 = UInt256.Zero;
TestUtils.SetupBlockWithValues(new Block(), val256, out _, out var val160, out var timestampVal, out var indexVal, out var scriptVal, out var transactionsVal, 1);
TestUtils.SetupBlockWithValues(new Block(), val256, out _, out var val160, out var timestampVal, out var indexVal, out var nonceVal, out var scriptVal, out var transactionsVal, 1);

var hex = "0000000000000000000000000000000000000000000000000000000000000000000000006c23be5d32679baa9c5c2aa0d329fd2a2441d7875d0f34d42f58f70428fbbbb9e913ff854c0000000000000000000000000000000000000000000000000000000001000111010000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000001000112010000";
var hex = "0000000000000000000000000000000000000000000000000000000000000000000000006c23be5d32679baa9c5c2aa0d329fd2a2441d7875d0f34d42f58f70428fbbbb9e913ff854c000000000000000000000000000000000000000000000000000000000000000001000111010000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000001000112010000";

using MemoryStream ms = new(hex.HexToBytes(), false);
using BinaryReader reader = new(ms);
uut.Deserialize(reader);
UInt256 merkRoot = uut.MerkleRoot;

AssertStandardBlockTestVals(val256, merkRoot, val160, timestampVal, indexVal, scriptVal, transactionsVal);
AssertStandardBlockTestVals(val256, merkRoot, val160, timestampVal, indexVal, nonceVal, scriptVal, transactionsVal);
}

private void AssertStandardBlockTestVals(UInt256 val256, UInt256 merkRoot, UInt160 val160, ulong timestampVal, uint indexVal, Witness scriptVal, Transaction[] transactionsVal, bool testTransactions = true)
private void AssertStandardBlockTestVals(UInt256 val256, UInt256 merkRoot, UInt160 val160, ulong timestampVal, uint indexVal, uint nonceVal, Witness scriptVal, Transaction[] transactionsVal, bool testTransactions = true)
{
uut.PrevHash.Should().Be(val256);
uut.MerkleRoot.Should().Be(merkRoot);
uut.Timestamp.Should().Be(timestampVal);
uut.Index.Should().Be(indexVal);
uut.Index.Should().Be(nonceVal);
uut.NextConsensus.Should().Be(val160);
uut.Witness.InvocationScript.Length.Should().Be(0);
uut.Witness.Size.Should().Be(scriptVal.Size);
@@ -133,8 +135,8 @@ public void Equals_DiffObj()
Block newBlock = new();
UInt256 val256 = UInt256.Zero;
UInt256 prevHash = new(TestUtils.GetByteArray(32, 0x42));
TestUtils.SetupBlockWithValues(newBlock, val256, out _, out _, out _, out uint _, out _, out _, 1);
TestUtils.SetupBlockWithValues(uut, prevHash, out _, out _, out _, out _, out _, out _, 0);
TestUtils.SetupBlockWithValues(newBlock, val256, out _, out _, out _, out uint _, out uint _, out _, out _, 1);
TestUtils.SetupBlockWithValues(uut, prevHash, out _, out _, out _, out _, out _, out _, out _, 0);

uut.Equals(newBlock).Should().BeFalse();
}
@@ -150,8 +152,8 @@ public void Equals_SameHash()
{
Block newBlock = new();
UInt256 prevHash = new(TestUtils.GetByteArray(32, 0x42));
TestUtils.SetupBlockWithValues(newBlock, prevHash, out _, out _, out _, out _, out _, out _, 1);
TestUtils.SetupBlockWithValues(uut, prevHash, out _, out _, out _, out _, out _, out _, 1);
TestUtils.SetupBlockWithValues(newBlock, prevHash, out _, out _, out _, out _, out _, out _, out _, 1);
TestUtils.SetupBlockWithValues(uut, prevHash, out _, out _, out _, out _, out _, out _, out _, 1);

uut.Equals(newBlock).Should().BeTrue();
}
@@ -160,17 +162,18 @@ public void Equals_SameHash()
public void ToJson()
{
UInt256 val256 = UInt256.Zero;
TestUtils.SetupBlockWithValues(uut, val256, out _, out _, out _, out _, out _, out _, 1);
TestUtils.SetupBlockWithValues(uut, val256, out _, out _, out _, out _, out _, out _, out _, 1);

JObject jObj = uut.ToJson(ProtocolSettings.Default);
jObj.Should().NotBeNull();
jObj["hash"].AsString().Should().Be("0x54b3a829333d9bb352eef69942317fe011251370c2212ad0d20d7c2a3974b26e");
jObj["size"].AsNumber().Should().Be(159);
jObj["hash"].AsString().Should().Be("0xed3b9b32bb6ffb5478d10e48cda4fe624b7ed164c10b753414710d6d2883103f");
jObj["size"].AsNumber().Should().Be(163); // 159 + nonce
jObj["version"].AsNumber().Should().Be(0);
jObj["previousblockhash"].AsString().Should().Be("0x0000000000000000000000000000000000000000000000000000000000000000");
jObj["merkleroot"].AsString().Should().Be("0xb9bbfb2804f7582fd4340f5d87d741242afd29d3a02a5c9caa9b67325dbe236c");
jObj["time"].AsNumber().Should().Be(328665601001);
jObj["index"].AsNumber().Should().Be(0);
//jObj["nonce"].AsNumber().Should().Be(0);
jObj["nextconsensus"].AsString().Should().Be("NKuyBkoGdZZSLyPbJEetheRhMjeznFZszf");

JObject scObj = ((JArray)jObj["witnesses"])[0];
Loading