Skip to content

Commit

Permalink
native: fix nspcc-dev#2844 roman's feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhangTao1596 committed Feb 1, 2023
1 parent 93a9e55 commit decd660
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 27 deletions.
2 changes: 1 addition & 1 deletion cli/wallet/wallet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ func TestWalletClaimGas(t *testing.T) {

balanceBefore := e.Chain.GetUtilityTokenBalance(h)
claimHeight := e.Chain.BlockHeight() + 1
cl, err := e.Chain.CalculateClaimable(h)
cl, err := e.Chain.CalculateClaimable(h, claimHeight)
require.NoError(t, err)
require.True(t, cl.Sign() > 0)

Expand Down
3 changes: 2 additions & 1 deletion pkg/core/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,13 @@ func benchmarkGasPerVote(t *testing.B, ps storage.Store, nRewardRecords int, rew

// Advance chain one more time to avoid same start/end rewarding bounds.
e.GenerateNewBlocks(t, rewardDistance)
end := bc.BlockHeight()

t.ResetTimer()
t.ReportAllocs()
t.StartTimer()
for i := 0; i < t.N; i++ {
_, err := bc.CalculateClaimable(to)
_, err := bc.CalculateClaimable(to, end)
require.NoError(t, err)
}
t.StopTimer()
Expand Down
4 changes: 2 additions & 2 deletions pkg/core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2239,9 +2239,9 @@ unsubloop:

// CalculateClaimable calculates the amount of GAS generated by owning specified
// amount of NEO between specified blocks.
func (bc *Blockchain) CalculateClaimable(acc util.Uint160) (*big.Int, error) {
func (bc *Blockchain) CalculateClaimable(acc util.Uint160, endHeight uint32) (*big.Int, error) {
ic := bc.newInteropContext(trigger.Application, bc.dao, nil, nil)
return bc.contracts.NEO.CalculateBonus(ic, acc)
return bc.contracts.NEO.CalculateBonus(ic, acc, endHeight)
}

// FeePerByte returns transaction network fee per byte.
Expand Down
2 changes: 1 addition & 1 deletion pkg/core/blockchain_neotest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ func TestBlockchain_GetClaimable(t *testing.T) {
e.GenerateNewBlocks(t, 10)

t.Run("first generation period", func(t *testing.T) {
amount, err := bc.CalculateClaimable(acc.ScriptHash())
amount, err := bc.CalculateClaimable(acc.ScriptHash(), 1)
require.NoError(t, err)
require.EqualValues(t, big.NewInt(5*native.GASFactor/10), amount)
})
Expand Down
45 changes: 25 additions & 20 deletions pkg/core/native/native_neo.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,15 +423,8 @@ func (n *NEO) PostPersist(ic *interop.Context) error {
tmp.Div(tmp, cs[i].Votes)

key = makeVoterKey([]byte(cs[i].Key), key)

var r *big.Int
if g, ok := cache.gasPerVoteCache[cs[i].Key]; ok {
r = &g
} else {
reward := n.getLatestGASPerVote(ic.DAO, key)
r = &reward
}
tmp.Add(tmp, r)
r := n.getLatestGASPerVote(ic.DAO, key)
tmp.Add(tmp, &r)

if !isCacheRW {
cache = ic.DAO.GetRWCache(n.ID).(*NeoCache)
Expand All @@ -447,11 +440,20 @@ func (n *NEO) PostPersist(ic *interop.Context) error {
}

func (n *NEO) getLatestGASPerVote(d *dao.Simple, key []byte) big.Int {
var g big.Int
cacheKey := string(key[1:])
cache := d.GetROCache(n.ID).(*NeoCache)
if g, ok := cache.gasPerVoteCache[cacheKey]; ok {
return g
}
item := d.GetStorageItem(n.ID, key)
if item == nil {
return *big.NewInt(0)
g = *big.NewInt(0)
} else {
g = *bigint.FromBytes(item)
}
return *bigint.FromBytes(item)
cache.gasPerVoteCache[cacheKey] = g
return g
}

func (n *NEO) increaseBalance(ic *interop.Context, h util.Uint160, si *state.StorageItem, amount *big.Int, checkBal *big.Int) (func(), error) {
Expand Down Expand Up @@ -505,7 +507,7 @@ func (n *NEO) distributeGas(ic *interop.Context, acc *state.NEOBalance) (*big.In
if ic.Block == nil || ic.Block.Index == 0 || ic.Block.Index == acc.BalanceHeight {
return nil, nil
}
gen, err := n.calculateBonus(ic, acc)
gen, err := n.calculateBonus(ic.DAO, acc, ic.Block.Index)
if err != nil {
return nil, err
}
Expand All @@ -520,7 +522,8 @@ func (n *NEO) distributeGas(ic *interop.Context, acc *state.NEOBalance) (*big.In

func (n *NEO) unclaimedGas(ic *interop.Context, args []stackitem.Item) stackitem.Item {
u := toUint160(args[0])
gen, err := n.CalculateBonus(ic, u)
end := uint32(toBigInt(args[1]).Int64())
gen, err := n.CalculateBonus(ic, u, end)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -648,7 +651,10 @@ 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) (*big.Int, error) {
func (n *NEO) CalculateBonus(ic *interop.Context, acc util.Uint160, end uint32) (*big.Int, error) {
if 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 {
Expand All @@ -658,19 +664,18 @@ func (n *NEO) CalculateBonus(ic *interop.Context, acc util.Uint160) (*big.Int, e
if err != nil {
return nil, err
}
return n.calculateBonus(ic, st)
return n.calculateBonus(ic.DAO, st, end)
}

func (n *NEO) calculateBonus(ic *interop.Context, acc *state.NEOBalance) (*big.Int, error) {
end := ic.BlockHeight() + 1
r, err := n.CalculateNEOHolderReward(ic.DAO, &acc.Balance, acc.BalanceHeight, end)
func (n *NEO) calculateBonus(d *dao.Simple, acc *state.NEOBalance, end uint32) (*big.Int, error) {
r, err := n.CalculateNEOHolderReward(d, &acc.Balance, acc.BalanceHeight, end)
if err != nil || acc.VoteTo == nil {
return r, err
}

var key = makeVoterKey(acc.VoteTo.Bytes())
var reward = n.getLatestGASPerVote(ic.DAO, key)
var tmp = (&reward).Sub(&reward, &acc.LastGasPerVote)
var reward = n.getLatestGASPerVote(d, key)
var tmp = big.NewInt(0).Sub(&reward, &acc.LastGasPerVote)
tmp.Mul(tmp, &acc.Balance)
tmp.Div(tmp, bigVoterRewardFactor)
tmp.Add(tmp, r)
Expand Down
4 changes: 2 additions & 2 deletions pkg/services/rpcsrv/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ type (
Ledger interface {
AddBlock(block *block.Block) error
BlockHeight() uint32
CalculateClaimable(h util.Uint160) (*big.Int, error)
CalculateClaimable(h util.Uint160, endHeight uint32) (*big.Int, error)
CurrentBlockHash() util.Uint256
FeePerByte() int64
ForEachNEP11Transfer(acc util.Uint160, newestTimestamp uint64, f func(*state.NEP11Transfer) (bool, error)) error
Expand Down Expand Up @@ -1785,7 +1785,7 @@ func (s *Server) getUnclaimedGas(ps params.Params) (interface{}, *neorpc.Error)
Address: u,
}, nil
}
gas, err := s.chain.CalculateClaimable(u) // +1 as in C#, for the next block.
gas, err := s.chain.CalculateClaimable(u, s.chain.BlockHeight()+1) // +1 as in C#, for the next block.
if err != nil {
return nil, neorpc.NewRPCError("Can't calculate claimable", err.Error())
}
Expand Down

0 comments on commit decd660

Please sign in to comment.