Merge pull request #1099 from nspcc-dev/feature/cache

native: cache GetValidators result
This commit is contained in:
Roman Khimov 2020-06-24 21:52:26 +03:00 committed by GitHub
commit 3d6ff3559e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -4,6 +4,7 @@ import (
"math/big" "math/big"
"sort" "sort"
"strings" "strings"
"sync/atomic"
"github.com/nspcc-dev/neo-go/pkg/core/blockchainer" "github.com/nspcc-dev/neo-go/pkg/core/blockchainer"
"github.com/nspcc-dev/neo-go/pkg/core/dao" "github.com/nspcc-dev/neo-go/pkg/core/dao"
@ -23,6 +24,8 @@ import (
type NEO struct { type NEO struct {
nep5TokenNative nep5TokenNative
GAS *GAS GAS *GAS
validators atomic.Value
} }
// keyWithVotes is a serialized key with votes balance. It's not deserialized // keyWithVotes is a serialized key with votes balance. It's not deserialized
@ -330,6 +333,7 @@ func (n *NEO) ModifyAccountVotes(acc *state.NEOBalanceState, d dao.DAO, value *b
return err return err
} }
} }
n.validators.Store(nil)
return nil return nil
} }
@ -382,9 +386,13 @@ func (n *NEO) getRegisteredValidatorsCall(ic *interop.Context, _ []stackitem.Ite
// GetValidatorsInternal returns a list of current validators. // GetValidatorsInternal returns a list of current validators.
func (n *NEO) GetValidatorsInternal(bc blockchainer.Blockchainer, d dao.DAO) (keys.PublicKeys, error) { func (n *NEO) GetValidatorsInternal(bc blockchainer.Blockchainer, d dao.DAO) (keys.PublicKeys, error) {
if vals := n.validators.Load(); vals != nil {
return vals.(keys.PublicKeys), nil
}
standByValidators := bc.GetStandByValidators() standByValidators := bc.GetStandByValidators()
si := d.GetStorageItem(n.ContractID, validatorsCountKey) si := d.GetStorageItem(n.ContractID, validatorsCountKey)
if si == nil { if si == nil {
n.validators.Store(standByValidators)
return standByValidators, nil return standByValidators, nil
} }
validatorsCount, err := ValidatorsCountFromBytes(si.Value) validatorsCount, err := ValidatorsCountFromBytes(si.Value)
@ -428,6 +436,7 @@ func (n *NEO) GetValidatorsInternal(bc blockchainer.Blockchainer, d dao.DAO) (ke
} }
} }
sort.Sort(result) sort.Sort(result)
n.validators.Store(result)
return result, nil return result, nil
} }