Skip to content

Commit

Permalink
Add P2PNotary node role for native RoleManagement contract
Browse files Browse the repository at this point in the history
Close neo-project#2895.

Signed-off-by: Anna Shaleva <shaleva.ann@nspcc.ru>
  • Loading branch information
AnnaShaleva committed Mar 3, 2024
1 parent d3c9175 commit ef23f5d
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Neo/SmartContract/Native/Role.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public enum Role : byte
/// <summary>
/// NeoFS Alphabet nodes.
/// </summary>
NeoFSAlphabetNode = 16
NeoFSAlphabetNode = 16,

/// <summary>
/// P2P Notary nodes used to process P2P notary requests.
/// </summary>
P2PNotary = 32
}
}
56 changes: 56 additions & 0 deletions tests/Neo.UnitTests/SmartContract/Native/UT_RoleManagement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<NotifyEventArgs> notifications = new List<NotifyEventArgs>();
EventHandler<NotifyEventArgs> 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<VM.Types.Array>();
(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<VM.Types.Array>();
(ret as VM.Types.Array).Count.Should().Be(0);
}

private void ApplicationEngine_Notify(object sender, NotifyEventArgs e)
{
throw new System.NotImplementedException();
Expand Down

0 comments on commit ef23f5d

Please sign in to comment.