Skip to content

Commit

Permalink
Merge pull request #3594 from nspcc-dev/unclaimed-gas
Browse files Browse the repository at this point in the history
vm: fix unclaimedGas calculation
  • Loading branch information
AnnaShaleva authored Oct 4, 2024
2 parents b1068b1 + 02727b1 commit c960a7e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
11 changes: 7 additions & 4 deletions pkg/core/native/native_neo.go
Original file line number Diff line number Diff line change
Expand Up @@ -750,18 +750,21 @@ func makeVoterKey(pub []byte, prealloc ...[]byte) []byte {
// CalculateBonus calculates amount of gas generated for holding value NEO from start to end block
// and having voted for active committee member.
func (n *NEO) CalculateBonus(ic *interop.Context, acc util.Uint160, end uint32) (*big.Int, error) {
if ic.Block == nil || end != ic.Block.Index {
return nil, errors.New("can't calculate bonus of height unequal (BlockHeight + 1)")
}
key := makeAccountKey(acc)
si := ic.DAO.GetStorageItem(n.ID, key)
if si == nil {
return nil, storage.ErrKeyNotFound
return big.NewInt(0), nil
}
st, err := state.NEOBalanceFromBytes(si)
if err != nil {
return nil, err
}
if st.Balance.Sign() == 0 {
return big.NewInt(0), nil
}
if ic.Block == nil || end != ic.Block.Index {
return nil, errors.New("can't calculate bonus of height unequal (BlockHeight + 1)")
}
return n.calculateBonus(ic.DAO, st, end)
}

Expand Down
29 changes: 29 additions & 0 deletions pkg/core/native/native_test/neo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,35 @@ func TestNEO_CalculateBonus(t *testing.T) {
})
}

func TestNEO_UnclaimedGas(t *testing.T) {
neoValidatorsInvoker := newNeoValidatorsClient(t)
e := neoValidatorsInvoker.Executor

acc := neoValidatorsInvoker.WithSigners(e.NewAccount(t))
accH := acc.Signers[0].ScriptHash()

t.Run("non-existing account", func(t *testing.T) {
// non-existing account, should return zero unclaimed GAS.
acc.Invoke(t, 0, "unclaimedGas", util.Uint160{}, 1)
})

t.Run("non-zero balance", func(t *testing.T) {
amount := 100
defaultGASPerBlock := 5
neoHolderRewardRatio := 10
rewardDistance := 10
neoValidatorsInvoker.Invoke(t, true, "transfer", e.Validator.ScriptHash(), accH, amount, nil)

for range rewardDistance - 1 {
e.AddNewBlock(t)
}
expectedGas := int64(amount * neoHolderRewardRatio / 100 * defaultGASPerBlock * rewardDistance)
acc.Invoke(t, expectedGas, "unclaimedGas", accH, e.Chain.BlockHeight()+1)

acc.InvokeFail(t, "can't calculate bonus of height unequal (BlockHeight + 1)", "unclaimedGas", accH, e.Chain.BlockHeight())
})
}

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

0 comments on commit c960a7e

Please sign in to comment.