|
18 | 18 | using Neo.SmartContract;
|
19 | 19 | using Neo.SmartContract.Native;
|
20 | 20 | using Neo.UnitTests.Extensions;
|
| 21 | +using Neo.Wallets; |
21 | 22 | using System;
|
22 | 23 | using System.Collections.Generic;
|
23 | 24 | using System.Linq;
|
@@ -90,6 +91,61 @@ public void TestSetAndGet()
|
90 | 91 | (ret as VM.Types.Array).Count.Should().Be(0);
|
91 | 92 | }
|
92 | 93 |
|
| 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 | + |
93 | 149 | private void ApplicationEngine_Notify(object sender, NotifyEventArgs e)
|
94 | 150 | {
|
95 | 151 | throw new System.NotImplementedException();
|
|
0 commit comments