core: don't reset NEO's registerPrice cache

This commit is contained in:
Anna Shaleva 2022-04-19 19:46:50 +03:00
parent adec635f0e
commit c36448f27e
2 changed files with 12 additions and 21 deletions

View file

@ -44,8 +44,7 @@ type NeoCache struct {
// gasPerBlock represents the history of generated gas per block.
gasPerBlock gasRecord
registerPrice int64
registerPriceChanged bool
registerPrice int64
votesChanged bool
nextValidators keys.PublicKeys
@ -126,7 +125,6 @@ func copyNeoCache(src, dst *NeoCache) {
copy(dst.committee, src.committee)
dst.committeeHash = src.committeeHash
dst.registerPriceChanged = src.registerPriceChanged
dst.registerPrice = src.registerPrice
// Can't omit copying because gasPerBlock is append-only, thus to be able to
@ -242,9 +240,8 @@ func (n *NEO) Initialize(ic *interop.Context) error {
}
cache := &NeoCache{
gasPerVoteCache: make(map[string]big.Int),
votesChanged: true,
registerPriceChanged: true,
gasPerVoteCache: make(map[string]big.Int),
votesChanged: true,
}
// We need cache to be present in DAO before the subsequent call to `mint`.
@ -275,7 +272,6 @@ func (n *NEO) Initialize(ic *interop.Context) error {
setIntWithKey(n.ID, ic.DAO, []byte{prefixRegisterPrice}, DefaultRegisterPrice)
cache.registerPrice = int64(DefaultRegisterPrice)
cache.registerPriceChanged = false
return nil
}
@ -285,9 +281,8 @@ func (n *NEO) Initialize(ic *interop.Context) error {
// called only when deploying native contracts.
func (n *NEO) InitializeCache(bc interop.Ledger, d *dao.Simple) error {
cache := &NeoCache{
gasPerVoteCache: make(map[string]big.Int),
votesChanged: true,
registerPriceChanged: true,
gasPerVoteCache: make(map[string]big.Int),
votesChanged: true,
}
var committee = keysWithVotes{}
@ -300,6 +295,7 @@ func (n *NEO) InitializeCache(bc interop.Ledger, d *dao.Simple) error {
}
cache.gasPerBlock = n.getSortedGASRecordFromDAO(d)
cache.registerPrice = getIntWithKey(n.ID, d, []byte{prefixRegisterPrice})
d.Store.SetCache(n.ID, cache)
return nil
@ -415,12 +411,6 @@ func (n *NEO) PostPersist(ic *interop.Context) error {
}
}
}
if cache.registerPriceChanged {
p := getIntWithKey(n.ID, ic.DAO, []byte{prefixRegisterPrice})
cache.registerPrice = p
cache.registerPriceChanged = false
}
return nil
}
@ -599,10 +589,7 @@ func (n *NEO) getRegisterPrice(ic *interop.Context, _ []stackitem.Item) stackite
func (n *NEO) getRegisterPriceInternal(d *dao.Simple) int64 {
cache := d.Store.GetROCache(n.ID).(*NeoCache)
if !cache.registerPriceChanged {
return cache.registerPrice
}
return getIntWithKey(n.ID, d, []byte{prefixRegisterPrice})
return cache.registerPrice
}
func (n *NEO) setRegisterPrice(ic *interop.Context, args []stackitem.Item) stackitem.Item {
@ -616,7 +603,7 @@ func (n *NEO) setRegisterPrice(ic *interop.Context, args []stackitem.Item) stack
setIntWithKey(n.ID, ic.DAO, []byte{prefixRegisterPrice}, price.Int64())
cache := ic.DAO.Store.GetRWCache(n.ID).(*NeoCache)
cache.registerPriceChanged = true
cache.registerPrice = price.Int64()
return stackitem.Null{}
}

View file

@ -50,6 +50,10 @@ func TestNEO_RegisterPrice(t *testing.T) {
testGetSet(t, newNeoCommitteeClient(t, 100_0000_0000), "RegisterPrice", native.DefaultRegisterPrice, 1, math.MaxInt64)
}
func TestNEO_RegisterPriceCache(t *testing.T) {
testGetSetCache(t, newNeoCommitteeClient(t, 100_0000_0000), "RegisterPrice", native.DefaultRegisterPrice)
}
func TestNEO_Vote(t *testing.T) {
neoCommitteeInvoker := newNeoCommitteeClient(t, 100_0000_0000)
neoValidatorsInvoker := neoCommitteeInvoker.WithSigners(neoCommitteeInvoker.Validator)