Skip to content

Commit

Permalink
native: clear LastGasPerVote when voting for NULL
Browse files Browse the repository at this point in the history
Port neo-project/neo#3173.

Close #3345

Signed-off-by: Ekaterina Pavlova <[email protected]>
  • Loading branch information
AliceInHunterland committed Mar 13, 2024
1 parent b12ef70 commit 5456d9c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/core/native/native_neo.go
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,9 @@ func (n *NEO) VoteInternal(ic *interop.Context, h util.Uint160, pub *keys.Public
}
ic.DAO.PutStorageItem(n.ID, key, acc.Bytes(ic.DAO.GetItemCtx()))

if pub == nil {
acc.LastGasPerVote = *big.NewInt(0)
}
ic.AddNotification(n.Hash, "Vote", stackitem.NewArray([]stackitem.Item{
stackitem.NewByteArray(h.BytesBE()),
keyToStackItem(oldVote),
Expand Down
41 changes: 41 additions & 0 deletions pkg/core/native/native_test/neo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,47 @@ func TestNEO_CandidateEvents(t *testing.T) {
require.Equal(t, 0, len(aer.Events))
}

func TestNEO_VoteForNull(t *testing.T) {
neoCommitteeInvoker := newNeoCommitteeClient(t, 100_0000_0000)
neoValidatorsInvoker := neoCommitteeInvoker.WithSigners(neoCommitteeInvoker.Validator)
e := neoCommitteeInvoker.Executor
voter := e.NewAccount(t, 100_0000_0000)
candidate := e.NewAccount(t)
cfg := e.Chain.GetConfig()
committeeSize := cfg.GetCommitteeSize(0)
validatorsCount := cfg.GetNumOfCNs(0)
freq := validatorsCount + committeeSize
getAccountState := func(t *testing.T, account util.Uint160) *state.NEOBalance {
stack, err := neoCommitteeInvoker.TestInvoke(t, "getAccountState", account)
require.NoError(t, err)
as := new(state.NEOBalance)
err = as.FromStackItem(stack.Pop().Item())
require.NoError(t, err)
return as
}
advanceChain := func(t *testing.T) {
for i := 0; i < freq; i++ {
neoCommitteeInvoker.AddNewBlock(t)
}
}
txes := make([]*transaction.Transaction, 0, committeeSize*4-2)
transferTx := neoValidatorsInvoker.PrepareInvoke(t, "transfer", e.Validator.ScriptHash(), candidate.(neotest.SingleSigner).Account().PrivateKey().GetScriptHash(), int64(committeeSize+1)*1000000, nil)
txes = append(txes, transferTx)
registerTx := neoValidatorsInvoker.WithSigners(candidate).PrepareInvoke(t, "registerCandidate", candidate.(neotest.SingleSigner).Account().PublicKey().Bytes())
txes = append(txes, registerTx)
voteTx := neoValidatorsInvoker.WithSigners(voter).PrepareInvoke(t, "vote", voter.(neotest.SingleSigner).Account().PrivateKey().GetScriptHash(), nil)
txes = append(txes, voteTx)
advanceChain(t)
for _, tx := range txes {
e.CheckHalt(t, tx.Hash(), stackitem.Make(true))
}
stateBefore := getAccountState(t, voter.ScriptHash())
require.NotEqual(t, big.NewInt(0), stateBefore.LastGasPerVote)

stateAfter := getAccountState(t, voter.ScriptHash())
require.Equal(t, big.NewInt(0), stateAfter.LastGasPerVote)
}

func TestNEO_Vote(t *testing.T) {
neoCommitteeInvoker := newNeoCommitteeClient(t, 100_0000_0000)
neoValidatorsInvoker := neoCommitteeInvoker.WithSigners(neoCommitteeInvoker.Validator)
Expand Down

0 comments on commit 5456d9c

Please sign in to comment.