From ef23f5de136948ef2a30340149e0559cff0fb86d Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Sun, 3 Mar 2024 10:28:10 +0300 Subject: [PATCH 1/2] Add P2PNotary node role for native RoleManagement contract Close #2895. Signed-off-by: Anna Shaleva --- src/Neo/SmartContract/Native/Role.cs | 7 ++- .../SmartContract/Native/UT_RoleManagement.cs | 56 +++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/src/Neo/SmartContract/Native/Role.cs b/src/Neo/SmartContract/Native/Role.cs index c0954d8fcd..710cc6f390 100644 --- a/src/Neo/SmartContract/Native/Role.cs +++ b/src/Neo/SmartContract/Native/Role.cs @@ -29,6 +29,11 @@ public enum Role : byte /// /// NeoFS Alphabet nodes. /// - NeoFSAlphabetNode = 16 + NeoFSAlphabetNode = 16, + + /// + /// P2P Notary nodes used to process P2P notary requests. + /// + P2PNotary = 32 } } diff --git a/tests/Neo.UnitTests/SmartContract/Native/UT_RoleManagement.cs b/tests/Neo.UnitTests/SmartContract/Native/UT_RoleManagement.cs index a87c630194..dc52e9e41c 100644 --- a/tests/Neo.UnitTests/SmartContract/Native/UT_RoleManagement.cs +++ b/tests/Neo.UnitTests/SmartContract/Native/UT_RoleManagement.cs @@ -18,6 +18,7 @@ using Neo.SmartContract; using Neo.SmartContract.Native; using Neo.UnitTests.Extensions; +using Neo.Wallets; using System; using System.Collections.Generic; using System.Linq; @@ -90,6 +91,61 @@ public void TestSetAndGet() (ret as VM.Types.Array).Count.Should().Be(0); } + [TestMethod] + public void TestDesignateP2PNotary() + { + byte[] privateKey1 = new byte[32]; + var rng1 = System.Security.Cryptography.RandomNumberGenerator.Create(); + rng1.GetBytes(privateKey1); + KeyPair key1 = new KeyPair(privateKey1); + byte[] privateKey2 = new byte[32]; + var rng2 = System.Security.Cryptography.RandomNumberGenerator.Create(); + rng2.GetBytes(privateKey2); + KeyPair key2 = new KeyPair(privateKey2); + ECPoint[] publicKeys = new ECPoint[2]; + publicKeys[0] = key1.PublicKey; + publicKeys[1] = key2.PublicKey; + publicKeys = publicKeys.OrderBy(p => p).ToArray(); + + var snapshot1 = _snapshot.CreateSnapshot(); + UInt160 committeeMultiSigAddr = NativeContract.NEO.GetCommitteeAddress(snapshot1); + List notifications = new List(); + EventHandler ev = (o, e) => notifications.Add(e); + ApplicationEngine.Notify += ev; + var ret = NativeContract.RoleManagement.Call( + snapshot1, + new Nep17NativeContractExtensions.ManualWitness(committeeMultiSigAddr), + new Block { Header = new Header() }, + "designateAsRole", + new ContractParameter(ContractParameterType.Integer) { Value = new BigInteger((int)Role.P2PNotary) }, + new ContractParameter(ContractParameterType.Array) { Value = publicKeys.Select(p => new ContractParameter(ContractParameterType.ByteArray) { Value = p.ToArray() }).ToList() } + ); + snapshot1.Commit(); + ApplicationEngine.Notify -= ev; + notifications.Count.Should().Be(1); + notifications[0].EventName.Should().Be("Designation"); + var snapshot2 = _snapshot.CreateSnapshot(); + ret = NativeContract.RoleManagement.Call( + snapshot2, + "getDesignatedByRole", + new ContractParameter(ContractParameterType.Integer) { Value = new BigInteger((int)Role.P2PNotary) }, + new ContractParameter(ContractParameterType.Integer) { Value = new BigInteger(1u) } + ); + ret.Should().BeOfType(); + (ret as VM.Types.Array).Count.Should().Be(2); + (ret as VM.Types.Array)[0].GetSpan().ToHexString().Should().Be(publicKeys[0].ToArray().ToHexString()); + (ret as VM.Types.Array)[1].GetSpan().ToHexString().Should().Be(publicKeys[1].ToArray().ToHexString()); + + ret = NativeContract.RoleManagement.Call( + snapshot2, + "getDesignatedByRole", + new ContractParameter(ContractParameterType.Integer) { Value = new BigInteger((int)Role.P2PNotary) }, + new ContractParameter(ContractParameterType.Integer) { Value = new BigInteger(0) } + ); + ret.Should().BeOfType(); + (ret as VM.Types.Array).Count.Should().Be(0); + } + private void ApplicationEngine_Notify(object sender, NotifyEventArgs e) { throw new System.NotImplementedException(); From 5710e1a64f8717de60decc314529287adf8de5d7 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Sun, 3 Mar 2024 21:49:47 +0300 Subject: [PATCH 2/2] Squash TestDesignateP2PNotary and TestGetSet of native RoleManagement Extend TestGetSet to check designation of all node roles, including P2PNotary role. TestDesignatedP2PNotary is not needed anymore. Signed-off-by: Anna Shaleva --- .../SmartContract/Native/UT_RoleManagement.cs | 124 ++++++------------ 1 file changed, 40 insertions(+), 84 deletions(-) diff --git a/tests/Neo.UnitTests/SmartContract/Native/UT_RoleManagement.cs b/tests/Neo.UnitTests/SmartContract/Native/UT_RoleManagement.cs index dc52e9e41c..25ca7ee0d6 100644 --- a/tests/Neo.UnitTests/SmartContract/Native/UT_RoleManagement.cs +++ b/tests/Neo.UnitTests/SmartContract/Native/UT_RoleManagement.cs @@ -45,54 +45,6 @@ public void Clean() [TestMethod] public void TestSetAndGet() - { - var snapshot1 = _snapshot.CreateSnapshot(); - UInt160 committeeMultiSigAddr = NativeContract.NEO.GetCommitteeAddress(snapshot1); - ECPoint[] validators = NativeContract.NEO.ComputeNextBlockValidators(snapshot1, TestProtocolSettings.Default); - List notifications = new List(); - EventHandler ev = (o, e) => notifications.Add(e); - ApplicationEngine.Notify += ev; - var ret = NativeContract.RoleManagement.Call( - snapshot1, - new Nep17NativeContractExtensions.ManualWitness(committeeMultiSigAddr), - new Block { Header = new Header() }, - "designateAsRole", - new ContractParameter(ContractParameterType.Integer) { Value = new BigInteger((int)Role.StateValidator) }, - new ContractParameter(ContractParameterType.Array) { Value = validators.Select(p => new ContractParameter(ContractParameterType.ByteArray) { Value = p.ToArray() }).ToList() } - ); - snapshot1.Commit(); - ApplicationEngine.Notify -= ev; - notifications.Count.Should().Be(1); - notifications[0].EventName.Should().Be("Designation"); - var snapshot2 = _snapshot.CreateSnapshot(); - ret = NativeContract.RoleManagement.Call( - snapshot2, - "getDesignatedByRole", - new ContractParameter(ContractParameterType.Integer) { Value = new BigInteger((int)Role.StateValidator) }, - new ContractParameter(ContractParameterType.Integer) { Value = new BigInteger(1u) } - ); - ret.Should().BeOfType(); - (ret as VM.Types.Array).Count.Should().Be(7); - (ret as VM.Types.Array)[0].GetSpan().ToHexString().Should().Be(validators[0].ToArray().ToHexString()); - (ret as VM.Types.Array)[1].GetSpan().ToHexString().Should().Be(validators[1].ToArray().ToHexString()); - (ret as VM.Types.Array)[2].GetSpan().ToHexString().Should().Be(validators[2].ToArray().ToHexString()); - (ret as VM.Types.Array)[3].GetSpan().ToHexString().Should().Be(validators[3].ToArray().ToHexString()); - (ret as VM.Types.Array)[4].GetSpan().ToHexString().Should().Be(validators[4].ToArray().ToHexString()); - (ret as VM.Types.Array)[5].GetSpan().ToHexString().Should().Be(validators[5].ToArray().ToHexString()); - (ret as VM.Types.Array)[6].GetSpan().ToHexString().Should().Be(validators[6].ToArray().ToHexString()); - - ret = NativeContract.RoleManagement.Call( - snapshot2, - "getDesignatedByRole", - new ContractParameter(ContractParameterType.Integer) { Value = new BigInteger((int)Role.StateValidator) }, - new ContractParameter(ContractParameterType.Integer) { Value = new BigInteger(0) } - ); - ret.Should().BeOfType(); - (ret as VM.Types.Array).Count.Should().Be(0); - } - - [TestMethod] - public void TestDesignateP2PNotary() { byte[] privateKey1 = new byte[32]; var rng1 = System.Security.Cryptography.RandomNumberGenerator.Create(); @@ -107,43 +59,47 @@ public void TestDesignateP2PNotary() publicKeys[1] = key2.PublicKey; publicKeys = publicKeys.OrderBy(p => p).ToArray(); - var snapshot1 = _snapshot.CreateSnapshot(); - UInt160 committeeMultiSigAddr = NativeContract.NEO.GetCommitteeAddress(snapshot1); - List notifications = new List(); - EventHandler ev = (o, e) => notifications.Add(e); - ApplicationEngine.Notify += ev; - var ret = NativeContract.RoleManagement.Call( - snapshot1, - new Nep17NativeContractExtensions.ManualWitness(committeeMultiSigAddr), - new Block { Header = new Header() }, - "designateAsRole", - new ContractParameter(ContractParameterType.Integer) { Value = new BigInteger((int)Role.P2PNotary) }, - new ContractParameter(ContractParameterType.Array) { Value = publicKeys.Select(p => new ContractParameter(ContractParameterType.ByteArray) { Value = p.ToArray() }).ToList() } - ); - snapshot1.Commit(); - ApplicationEngine.Notify -= ev; - notifications.Count.Should().Be(1); - notifications[0].EventName.Should().Be("Designation"); - var snapshot2 = _snapshot.CreateSnapshot(); - ret = NativeContract.RoleManagement.Call( - snapshot2, - "getDesignatedByRole", - new ContractParameter(ContractParameterType.Integer) { Value = new BigInteger((int)Role.P2PNotary) }, - new ContractParameter(ContractParameterType.Integer) { Value = new BigInteger(1u) } - ); - ret.Should().BeOfType(); - (ret as VM.Types.Array).Count.Should().Be(2); - (ret as VM.Types.Array)[0].GetSpan().ToHexString().Should().Be(publicKeys[0].ToArray().ToHexString()); - (ret as VM.Types.Array)[1].GetSpan().ToHexString().Should().Be(publicKeys[1].ToArray().ToHexString()); + List roles = new List() { Role.StateValidator, Role.Oracle, Role.NeoFSAlphabetNode, Role.P2PNotary }; + foreach (var role in roles) + { + var snapshot1 = _snapshot.CreateSnapshot(); + UInt160 committeeMultiSigAddr = NativeContract.NEO.GetCommitteeAddress(snapshot1); + List notifications = new List(); + EventHandler ev = (o, e) => notifications.Add(e); + ApplicationEngine.Notify += ev; + var ret = NativeContract.RoleManagement.Call( + snapshot1, + new Nep17NativeContractExtensions.ManualWitness(committeeMultiSigAddr), + new Block { Header = new Header() }, + "designateAsRole", + new ContractParameter(ContractParameterType.Integer) { Value = new BigInteger((int)role) }, + new ContractParameter(ContractParameterType.Array) { Value = publicKeys.Select(p => new ContractParameter(ContractParameterType.ByteArray) { Value = p.ToArray() }).ToList() } + ); + snapshot1.Commit(); + ApplicationEngine.Notify -= ev; + notifications.Count.Should().Be(1); + notifications[0].EventName.Should().Be("Designation"); + var snapshot2 = _snapshot.CreateSnapshot(); + ret = NativeContract.RoleManagement.Call( + snapshot2, + "getDesignatedByRole", + new ContractParameter(ContractParameterType.Integer) { Value = new BigInteger((int)role) }, + new ContractParameter(ContractParameterType.Integer) { Value = new BigInteger(1u) } + ); + ret.Should().BeOfType(); + (ret as VM.Types.Array).Count.Should().Be(2); + (ret as VM.Types.Array)[0].GetSpan().ToHexString().Should().Be(publicKeys[0].ToArray().ToHexString()); + (ret as VM.Types.Array)[1].GetSpan().ToHexString().Should().Be(publicKeys[1].ToArray().ToHexString()); - ret = NativeContract.RoleManagement.Call( - snapshot2, - "getDesignatedByRole", - new ContractParameter(ContractParameterType.Integer) { Value = new BigInteger((int)Role.P2PNotary) }, - new ContractParameter(ContractParameterType.Integer) { Value = new BigInteger(0) } - ); - ret.Should().BeOfType(); - (ret as VM.Types.Array).Count.Should().Be(0); + ret = NativeContract.RoleManagement.Call( + snapshot2, + "getDesignatedByRole", + new ContractParameter(ContractParameterType.Integer) { Value = new BigInteger((int)role) }, + new ContractParameter(ContractParameterType.Integer) { Value = new BigInteger(0) } + ); + ret.Should().BeOfType(); + (ret as VM.Types.Array).Count.Should().Be(0); + } } private void ApplicationEngine_Notify(object sender, NotifyEventArgs e)