Skip to content

Commit

Permalink
NEO: clear LastGasPerVote when voting for NULL, fix #2894 (#3173)
Browse files Browse the repository at this point in the history
This value won't be used in any way, so save some bytes of storage.

Signed-off-by: Roman Khimov <[email protected]>
Co-authored-by: Shargon <[email protected]>
Co-authored-by: Vitor Nazário Coelho <[email protected]>
  • Loading branch information
3 people authored Mar 7, 2024
1 parent 413cfc4 commit b05501a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Neo/SmartContract/Native/NeoToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,10 @@ private async ContractTask<bool> Vote(ApplicationEngine engine, UInt160 account,
{
validator_new.Votes += state_account.Balance;
}
else
{
state_account.LastGasPerVote = 0;
}
engine.SendNotification(Hash, "Vote",
new VM.Types.Array(engine.ReferenceCounter) { account.ToArray(), from?.ToArray() ?? StackItem.Null, voteTo?.ToArray() ?? StackItem.Null, state_account.Balance });
if (gasDistribution is not null)
Expand Down
3 changes: 3 additions & 0 deletions tests/Neo.UnitTests/SmartContract/Native/UT_NeoToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,13 @@ public void Check_Vote_VoteToNull()
var accountState = snapshot.TryGet(CreateStorageKey(20, from_Account)).GetInteroperable<NeoAccountState>();
accountState.Balance = 100;
snapshot.Add(CreateStorageKey(33, ECCurve.Secp256r1.G.ToArray()), new StorageItem(new CandidateState() { Registered = true }));
snapshot.Add(CreateStorageKey(23, ECCurve.Secp256r1.G.ToArray()), new StorageItem(new BigInteger(100500)));
var ret = Check_Vote(snapshot, from_Account, ECCurve.Secp256r1.G.ToArray(), true, persistingBlock);
ret.Result.Should().BeTrue();
ret.State.Should().BeTrue();
accountState = snapshot.TryGet(CreateStorageKey(20, from_Account)).GetInteroperable<NeoAccountState>();
accountState.VoteTo.Should().Be(ECCurve.Secp256r1.G);
accountState.LastGasPerVote.Should().Be(100500);

//from vote to null account G votes becomes 0
var G_stateValidator = snapshot.GetAndChange(CreateStorageKey(33, ECCurve.Secp256r1.G.ToArray())).GetInteroperable<CandidateState>();
Expand All @@ -211,6 +213,7 @@ public void Check_Vote_VoteToNull()
G_stateValidator.Votes.Should().Be(0);
accountState = snapshot.TryGet(CreateStorageKey(20, from_Account)).GetInteroperable<NeoAccountState>();
accountState.VoteTo.Should().Be(null);
accountState.LastGasPerVote.Should().Be(0);
}

[TestMethod]
Expand Down

0 comments on commit b05501a

Please sign in to comment.