Skip to content

Commit ef23f5d

Browse files
committed
Add P2PNotary node role for native RoleManagement contract
Close #2895. Signed-off-by: Anna Shaleva <shaleva.ann@nspcc.ru>
1 parent d3c9175 commit ef23f5d

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

src/Neo/SmartContract/Native/Role.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ public enum Role : byte
2929
/// <summary>
3030
/// NeoFS Alphabet nodes.
3131
/// </summary>
32-
NeoFSAlphabetNode = 16
32+
NeoFSAlphabetNode = 16,
33+
34+
/// <summary>
35+
/// P2P Notary nodes used to process P2P notary requests.
36+
/// </summary>
37+
P2PNotary = 32
3338
}
3439
}

tests/Neo.UnitTests/SmartContract/Native/UT_RoleManagement.cs

+56
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using Neo.SmartContract;
1919
using Neo.SmartContract.Native;
2020
using Neo.UnitTests.Extensions;
21+
using Neo.Wallets;
2122
using System;
2223
using System.Collections.Generic;
2324
using System.Linq;
@@ -90,6 +91,61 @@ public void TestSetAndGet()
9091
(ret as VM.Types.Array).Count.Should().Be(0);
9192
}
9293

94+
[TestMethod]
95+
public void TestDesignateP2PNotary()
96+
{
97+
byte[] privateKey1 = new byte[32];
98+
var rng1 = System.Security.Cryptography.RandomNumberGenerator.Create();
99+
rng1.GetBytes(privateKey1);
100+
KeyPair key1 = new KeyPair(privateKey1);
101+
byte[] privateKey2 = new byte[32];
102+
var rng2 = System.Security.Cryptography.RandomNumberGenerator.Create();
103+
rng2.GetBytes(privateKey2);
104+
KeyPair key2 = new KeyPair(privateKey2);
105+
ECPoint[] publicKeys = new ECPoint[2];
106+
publicKeys[0] = key1.PublicKey;
107+
publicKeys[1] = key2.PublicKey;
108+
publicKeys = publicKeys.OrderBy(p => p).ToArray();
109+
110+
var snapshot1 = _snapshot.CreateSnapshot();
111+
UInt160 committeeMultiSigAddr = NativeContract.NEO.GetCommitteeAddress(snapshot1);
112+
List<NotifyEventArgs> notifications = new List<NotifyEventArgs>();
113+
EventHandler<NotifyEventArgs> ev = (o, e) => notifications.Add(e);
114+
ApplicationEngine.Notify += ev;
115+
var ret = NativeContract.RoleManagement.Call(
116+
snapshot1,
117+
new Nep17NativeContractExtensions.ManualWitness(committeeMultiSigAddr),
118+
new Block { Header = new Header() },
119+
"designateAsRole",
120+
new ContractParameter(ContractParameterType.Integer) { Value = new BigInteger((int)Role.P2PNotary) },
121+
new ContractParameter(ContractParameterType.Array) { Value = publicKeys.Select(p => new ContractParameter(ContractParameterType.ByteArray) { Value = p.ToArray() }).ToList() }
122+
);
123+
snapshot1.Commit();
124+
ApplicationEngine.Notify -= ev;
125+
notifications.Count.Should().Be(1);
126+
notifications[0].EventName.Should().Be("Designation");
127+
var snapshot2 = _snapshot.CreateSnapshot();
128+
ret = NativeContract.RoleManagement.Call(
129+
snapshot2,
130+
"getDesignatedByRole",
131+
new ContractParameter(ContractParameterType.Integer) { Value = new BigInteger((int)Role.P2PNotary) },
132+
new ContractParameter(ContractParameterType.Integer) { Value = new BigInteger(1u) }
133+
);
134+
ret.Should().BeOfType<VM.Types.Array>();
135+
(ret as VM.Types.Array).Count.Should().Be(2);
136+
(ret as VM.Types.Array)[0].GetSpan().ToHexString().Should().Be(publicKeys[0].ToArray().ToHexString());
137+
(ret as VM.Types.Array)[1].GetSpan().ToHexString().Should().Be(publicKeys[1].ToArray().ToHexString());
138+
139+
ret = NativeContract.RoleManagement.Call(
140+
snapshot2,
141+
"getDesignatedByRole",
142+
new ContractParameter(ContractParameterType.Integer) { Value = new BigInteger((int)Role.P2PNotary) },
143+
new ContractParameter(ContractParameterType.Integer) { Value = new BigInteger(0) }
144+
);
145+
ret.Should().BeOfType<VM.Types.Array>();
146+
(ret as VM.Types.Array).Count.Should().Be(0);
147+
}
148+
93149
private void ApplicationEngine_Notify(object sender, NotifyEventArgs e)
94150
{
95151
throw new System.NotImplementedException();

0 commit comments

Comments
 (0)