Skip to content

Commit 069b774

Browse files
Add getcommittee and unregister candidate ut (neo-project#1831)
* add unregister candidate and getcommittee ut * fix format Co-authored-by: Shargon <[email protected]>
1 parent 8da3c53 commit 069b774

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed

tests/neo.UnitTests/SmartContract/Native/Tokens/UT_NeoToken.cs

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,107 @@ public void Check_RegisterValidator()
223223
Assert.AreEqual(2, members.Length);
224224
}
225225

226+
[TestMethod]
227+
public void Check_UnregisterCandidate()
228+
{
229+
var snapshot = Blockchain.Singleton.GetSnapshot();
230+
231+
var keyCount = snapshot.Storages.GetChangeSet().Count();
232+
var point = Blockchain.StandbyValidators[0].EncodePoint(true);
233+
234+
//without register
235+
var ret = Check_UnregisterCandidate(snapshot, point);
236+
ret.State.Should().BeTrue();
237+
ret.Result.Should().BeTrue();
238+
snapshot.Storages.GetChangeSet().Count().Should().Be(keyCount);
239+
240+
//register and then unregister
241+
ret = Check_RegisterValidator(snapshot, point);
242+
StorageItem item = snapshot.Storages.GetAndChange(CreateStorageKey(33, point));
243+
ret.State.Should().BeTrue();
244+
ret.Result.Should().BeTrue();
245+
246+
var members = NativeContract.NEO.GetCandidates(snapshot);
247+
Assert.AreEqual(1, members.Length);
248+
snapshot.Storages.GetChangeSet().Count().Should().Be(keyCount + 1);
249+
StorageKey key = CreateStorageKey(33, point);
250+
snapshot.Storages.TryGet(key).Should().NotBeNull();
251+
252+
ret = Check_UnregisterCandidate(snapshot, point);
253+
ret.State.Should().BeTrue();
254+
ret.Result.Should().BeTrue();
255+
snapshot.Storages.GetChangeSet().Count().Should().Be(keyCount);
256+
257+
members = NativeContract.NEO.GetCandidates(snapshot);
258+
Assert.AreEqual(0, members.Length);
259+
snapshot.Storages.TryGet(key).Should().BeNull();
260+
261+
//register with votes, then unregister
262+
ret = Check_RegisterValidator(snapshot, point);
263+
var G_Account = Contract.CreateSignatureContract(ECCurve.Secp256r1.G).ScriptHash.ToArray();
264+
snapshot.Storages.Add(CreateStorageKey(20, G_Account), new StorageItem(new NeoAccountState()));
265+
var accountState = snapshot.Storages.TryGet(CreateStorageKey(20, G_Account)).GetInteroperable<NeoAccountState>();
266+
accountState.Balance = 100;
267+
Check_Vote(snapshot, G_Account, Blockchain.StandbyValidators[0].ToArray(), true);
268+
ret = Check_UnregisterCandidate(snapshot, point);
269+
ret.State.Should().BeTrue();
270+
ret.Result.Should().BeTrue();
271+
snapshot.Storages.TryGet(key).Should().NotBeNull();
272+
StorageItem pointItem = snapshot.Storages.TryGet(key);
273+
CandidateState pointState = pointItem.GetInteroperable<CandidateState>();
274+
pointState.Registered.Should().BeFalse();
275+
pointState.Votes.Should().Be(100);
276+
277+
//vote fail
278+
ret = Check_Vote(snapshot, G_Account, Blockchain.StandbyValidators[0].ToArray(), true);
279+
ret.State.Should().BeTrue();
280+
ret.Result.Should().BeFalse();
281+
accountState.VoteTo.Should().Be(Blockchain.StandbyValidators[0]);
282+
}
283+
284+
[TestMethod]
285+
public void Check_GetCommittee()
286+
{
287+
var snapshot = Blockchain.Singleton.GetSnapshot();
288+
var keyCount = snapshot.Storages.GetChangeSet().Count();
289+
var point = Blockchain.StandbyValidators[0].EncodePoint(true);
290+
291+
//register with votes with 20000000
292+
var G_Account = Contract.CreateSignatureContract(ECCurve.Secp256r1.G).ScriptHash.ToArray();
293+
snapshot.Storages.Add(CreateStorageKey(20, G_Account), new StorageItem(new NeoAccountState()));
294+
var accountState = snapshot.Storages.TryGet(CreateStorageKey(20, G_Account)).GetInteroperable<NeoAccountState>();
295+
accountState.Balance = 20000000;
296+
var ret = Check_RegisterValidator(snapshot, ECCurve.Secp256r1.G.ToArray());
297+
ret.State.Should().BeTrue();
298+
ret.Result.Should().BeTrue();
299+
ret = Check_Vote(snapshot, G_Account, ECCurve.Secp256r1.G.ToArray(), true);
300+
ret.State.Should().BeTrue();
301+
ret.Result.Should().BeTrue();
302+
303+
var committeemembers = NativeContract.NEO.GetCommittee(snapshot);
304+
var defaultCommittee = Blockchain.StandbyCommittee.OrderBy(p => p).ToArray();
305+
committeemembers.GetType().Should().Be(typeof(ECPoint[]));
306+
for (int i = 0; i < ProtocolSettings.Default.CommitteeMembersCount; i++)
307+
{
308+
committeemembers[i].Should().Be(defaultCommittee[i]);
309+
}
310+
311+
//register more candidates,committee member change
312+
for (int i = 0; i < ProtocolSettings.Default.CommitteeMembersCount - 1; i++)
313+
{
314+
Check_RegisterValidator(snapshot, Blockchain.StandbyCommittee[i].ToArray());
315+
var currentCandidates = NativeContract.NEO.GetCandidates(snapshot);
316+
}
317+
committeemembers = NativeContract.NEO.GetCommittee(snapshot);
318+
committeemembers.Length.Should().Be(ProtocolSettings.Default.CommitteeMembersCount);
319+
committeemembers.Contains(ECCurve.Secp256r1.G).Should().BeTrue();
320+
for (int i = 0; i < ProtocolSettings.Default.CommitteeMembersCount - 1; i++)
321+
{
322+
committeemembers.Contains(Blockchain.StandbyCommittee[i]).Should().BeTrue();
323+
}
324+
committeemembers.Contains(Blockchain.StandbyCommittee[ProtocolSettings.Default.CommitteeMembersCount - 1]).Should().BeFalse();
325+
}
326+
226327
[TestMethod]
227328
public void Check_Transfer()
228329
{
@@ -653,5 +754,30 @@ internal static StorageKey CreateStorageKey(byte prefix, byte[] key = null)
653754
key?.CopyTo(storageKey.Key.AsSpan(1));
654755
return storageKey;
655756
}
757+
758+
internal static (bool State, bool Result) Check_UnregisterCandidate(StoreView snapshot, byte[] pubkey)
759+
{
760+
var engine = ApplicationEngine.Create(TriggerType.Application,
761+
new Nep5NativeContractExtensions.ManualWitness(Contract.CreateSignatureRedeemScript(ECPoint.DecodePoint(pubkey, ECCurve.Secp256r1)).ToScriptHash()), snapshot);
762+
763+
engine.LoadScript(NativeContract.NEO.Script);
764+
765+
var script = new ScriptBuilder();
766+
script.EmitPush(pubkey);
767+
script.EmitPush(1);
768+
script.Emit(OpCode.PACK);
769+
script.EmitPush("unregisterCandidate");
770+
engine.LoadScript(script.ToArray());
771+
772+
if (engine.Execute() == VMState.FAULT)
773+
{
774+
return (false, false);
775+
}
776+
777+
var result = engine.ResultStack.Pop();
778+
result.Should().BeOfType(typeof(VM.Types.Boolean));
779+
780+
return (true, result.GetBoolean());
781+
}
656782
}
657783
}

0 commit comments

Comments
 (0)