Skip to content

Commit

Permalink
Squash TestDesignateP2PNotary and TestGetSet of native RoleManagement
Browse files Browse the repository at this point in the history
Extend TestGetSet to check designation of all node roles, including
P2PNotary role. TestDesignatedP2PNotary is not needed anymore.

Signed-off-by: Anna Shaleva <[email protected]>
  • Loading branch information
AnnaShaleva committed Mar 3, 2024
1 parent ef23f5d commit 5710e1a
Showing 1 changed file with 40 additions and 84 deletions.
124 changes: 40 additions & 84 deletions tests/Neo.UnitTests/SmartContract/Native/UT_RoleManagement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<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.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<VM.Types.Array>();
(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<VM.Types.Array>();
(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();
Expand All @@ -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<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());
List<Role> roles = new List<Role>() { Role.StateValidator, Role.Oracle, Role.NeoFSAlphabetNode, Role.P2PNotary };
foreach (var role in roles)
{
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) },
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<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);
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<VM.Types.Array>();
(ret as VM.Types.Array).Count.Should().Be(0);
}
}

private void ApplicationEngine_Notify(object sender, NotifyEventArgs e)
Expand Down

0 comments on commit 5710e1a

Please sign in to comment.