Skip to content

Commit

Permalink
Add vote ut (neo-project#1822)
Browse files Browse the repository at this point in the history
  • Loading branch information
cloud8little committed Jan 24, 2021
1 parent be32f98 commit 3426b5f
Showing 1 changed file with 87 additions and 1 deletion.
88 changes: 87 additions & 1 deletion tests/neo.UnitTests/SmartContract/Native/Tokens/UT_NeoToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,94 @@ public void Check_Vote()
ret.Result.Should().BeTrue();
ret.State.Should().BeTrue();
accountState.VoteTo.Should().Be(ECCurve.Secp256r1.G);
}

[TestMethod]
public void Check_Vote_Sameaccounts()
{
var snapshot = Blockchain.Singleton.GetSnapshot();
snapshot.PersistingBlock = new Block() { Index = 1000 };

byte[] from = Blockchain.GetConsensusAddress(Blockchain.StandbyValidators).ToArray();
var accountState = snapshot.Storages.TryGet(CreateStorageKey(20, from)).GetInteroperable<NeoAccountState>();
accountState.Balance = 100;
snapshot.Storages.Add(CreateStorageKey(33, ECCurve.Secp256r1.G.ToArray()), new StorageItem(new CandidateState()));
var ret = Check_Vote(snapshot, from, ECCurve.Secp256r1.G.ToArray(), true);
ret.Result.Should().BeTrue();
ret.State.Should().BeTrue();
accountState.VoteTo.Should().Be(ECCurve.Secp256r1.G);

//two account vote for the same account
var stateValidator = snapshot.Storages.GetAndChange(CreateStorageKey(33, ECCurve.Secp256r1.G.ToArray())).GetInteroperable<CandidateState>();
stateValidator.Votes.Should().Be(100);
var G_Account = Contract.CreateSignatureContract(ECCurve.Secp256r1.G).ScriptHash.ToArray();
snapshot.Storages.Add(CreateStorageKey(20, G_Account), new StorageItem(new NeoAccountState { Balance = 200 }));
var secondAccount = snapshot.Storages.TryGet(CreateStorageKey(20, G_Account)).GetInteroperable<NeoAccountState>();
ret = Check_Vote(snapshot, G_Account, ECCurve.Secp256r1.G.ToArray(), true);
ret.Result.Should().BeTrue();
ret.State.Should().BeTrue();
stateValidator.Votes.Should().Be(300);
}

[TestMethod]
public void Check_Vote_ChangeVote()
{
var snapshot = Blockchain.Singleton.GetSnapshot();
snapshot.PersistingBlock = new Block() { Index = 1000 };
//from vote to G
byte[] from = Blockchain.StandbyValidators[0].ToArray();
var from_Account = Contract.CreateSignatureContract(Blockchain.StandbyValidators[0]).ScriptHash.ToArray();
snapshot.Storages.Add(CreateStorageKey(20, from_Account), new StorageItem(new NeoAccountState()));
var accountState = snapshot.Storages.TryGet(CreateStorageKey(20, from_Account)).GetInteroperable<NeoAccountState>();
accountState.Balance = 100;
snapshot.Storages.Add(CreateStorageKey(33, ECCurve.Secp256r1.G.ToArray()), new StorageItem(new CandidateState()));
var ret = Check_Vote(snapshot, from_Account, ECCurve.Secp256r1.G.ToArray(), true);
ret.Result.Should().BeTrue();
ret.State.Should().BeTrue();
accountState.VoteTo.Should().Be(ECCurve.Secp256r1.G);

// TODO: More votes tests
//from change vote to itself
var G_stateValidator = snapshot.Storages.GetAndChange(CreateStorageKey(33, ECCurve.Secp256r1.G.ToArray())).GetInteroperable<CandidateState>();
G_stateValidator.Votes.Should().Be(100);
var G_Account = Contract.CreateSignatureContract(ECCurve.Secp256r1.G).ScriptHash.ToArray();
snapshot.Storages.Add(CreateStorageKey(20, G_Account), new StorageItem(new NeoAccountState { Balance = 200 }));
snapshot.Storages.Add(CreateStorageKey(33, from), new StorageItem(new CandidateState()));
ret = Check_Vote(snapshot, from_Account, from, true);
ret.Result.Should().BeTrue();
ret.State.Should().BeTrue();
G_stateValidator.Votes.Should().Be(0);
var from_stateValidator = snapshot.Storages.GetAndChange(CreateStorageKey(33, from)).GetInteroperable<CandidateState>();
from_stateValidator.Votes.Should().Be(100);
}

[TestMethod]
public void Check_Vote_VoteToNull()
{
var snapshot = Blockchain.Singleton.GetSnapshot();
snapshot.PersistingBlock = new Block() { Index = 1000 };

byte[] from = Blockchain.StandbyValidators[0].ToArray();
var from_Account = Contract.CreateSignatureContract(Blockchain.StandbyValidators[0]).ScriptHash.ToArray();
snapshot.Storages.Add(CreateStorageKey(20, from_Account), new StorageItem(new NeoAccountState()));
var accountState = snapshot.Storages.TryGet(CreateStorageKey(20, from_Account)).GetInteroperable<NeoAccountState>();
accountState.Balance = 100;
snapshot.Storages.Add(CreateStorageKey(33, ECCurve.Secp256r1.G.ToArray()), new StorageItem(new CandidateState()));
var ret = Check_Vote(snapshot, from_Account, ECCurve.Secp256r1.G.ToArray(), true);
ret.Result.Should().BeTrue();
ret.State.Should().BeTrue();
accountState.VoteTo.Should().Be(ECCurve.Secp256r1.G);

//from vote to null account G votes becomes 0
var G_stateValidator = snapshot.Storages.GetAndChange(CreateStorageKey(33, ECCurve.Secp256r1.G.ToArray())).GetInteroperable<CandidateState>();
G_stateValidator.Votes.Should().Be(100);
var G_Account = Contract.CreateSignatureContract(ECCurve.Secp256r1.G).ScriptHash.ToArray();
snapshot.Storages.Add(CreateStorageKey(20, G_Account), new StorageItem(new NeoAccountState { Balance = 200 }));
snapshot.Storages.Add(CreateStorageKey(33, from), new StorageItem(new CandidateState()));
ret = Check_Vote(snapshot, from_Account, null, true);
ret.Result.Should().BeTrue();
ret.State.Should().BeTrue();
G_stateValidator.Votes.Should().Be(0);
accountState.VoteTo.Should().Be(null);
}

[TestMethod]
Expand Down

0 comments on commit 3426b5f

Please sign in to comment.