core: keep Notary cache always valid and up-to-date

This commit is contained in:
Anna Shaleva 2022-04-19 18:32:34 +03:00
parent 27b0193da0
commit 0f6bf33f86
2 changed files with 20 additions and 29 deletions

View file

@ -32,11 +32,21 @@ func TestNotary_MaxNotValidBeforeDelta(t *testing.T) {
testGetSet(t, c, "MaxNotValidBeforeDelta", 140, int64(c.Chain.GetConfig().ValidatorsCount), int64(c.Chain.GetConfig().MaxValidUntilBlockIncrement/2))
}
func TestNotary_MaxNotValidBeforeDeltaCache(t *testing.T) {
c := newNotaryClient(t)
testGetSetCache(t, c, "MaxNotValidBeforeDelta", 140)
}
func TestNotary_NotaryServiceFeePerKey(t *testing.T) {
c := newNotaryClient(t)
testGetSet(t, c, "NotaryServiceFeePerKey", 1000_0000, 0, 0)
}
func TestNotary_NotaryServiceFeePerKeyCache(t *testing.T) {
c := newNotaryClient(t)
testGetSetCache(t, c, "NotaryServiceFeePerKey", 1000_0000)
}
func TestNotary_Pipeline(t *testing.T) {
notaryCommitteeInvoker := newNotaryClient(t)
e := notaryCommitteeInvoker.Executor

View file

@ -34,10 +34,6 @@ type Notary struct {
}
type NotaryCache struct {
// isValid defies whether cached values were changed during the current
// consensus iteration. If false, these values will be updated after
// blockchain DAO persisting. If true, we can safely use cached values.
isValid bool
maxNotValidBeforeDelta uint32
notaryServiceFeePerKey int64
}
@ -143,7 +139,6 @@ func (n *Notary) Initialize(ic *interop.Context) error {
setIntWithKey(n.ID, ic.DAO, notaryServiceFeeKey, defaultNotaryServiceFeePerKey)
cache := &NotaryCache{
isValid: true,
maxNotValidBeforeDelta: defaultMaxNotValidBeforeDelta,
notaryServiceFeePerKey: defaultNotaryServiceFeePerKey,
}
@ -152,10 +147,10 @@ func (n *Notary) Initialize(ic *interop.Context) error {
}
func (n *Notary) InitializeCache(d *dao.Simple) error {
cache := &NotaryCache{isValid: true}
cache.maxNotValidBeforeDelta = uint32(getIntWithKey(n.ID, d, maxNotValidBeforeDeltaKey))
cache.notaryServiceFeePerKey = getIntWithKey(n.ID, d, notaryServiceFeeKey)
cache := &NotaryCache{
maxNotValidBeforeDelta: uint32(getIntWithKey(n.ID, d, maxNotValidBeforeDeltaKey)),
notaryServiceFeePerKey: getIntWithKey(n.ID, d, notaryServiceFeeKey),
}
d.Store.SetCache(n.ID, cache)
return nil
@ -206,14 +201,6 @@ func (n *Notary) OnPersist(ic *interop.Context) error {
// PostPersist implements Contract interface.
func (n *Notary) PostPersist(ic *interop.Context) error {
cache := ic.DAO.Store.GetRWCache(n.ID).(*NotaryCache)
if cache.isValid {
return nil
}
cache.maxNotValidBeforeDelta = uint32(getIntWithKey(n.ID, ic.DAO, maxNotValidBeforeDeltaKey))
cache.notaryServiceFeePerKey = getIntWithKey(n.ID, ic.DAO, notaryServiceFeeKey)
cache.isValid = true
return nil
}
@ -421,10 +408,7 @@ func (n *Notary) getMaxNotValidBeforeDelta(ic *interop.Context, _ []stackitem.It
// GetMaxNotValidBeforeDelta is an internal representation of Notary getMaxNotValidBeforeDelta method.
func (n *Notary) GetMaxNotValidBeforeDelta(dao *dao.Simple) uint32 {
cache := dao.Store.GetROCache(n.ID).(*NotaryCache)
if cache.isValid {
return cache.maxNotValidBeforeDelta
}
return uint32(getIntWithKey(n.ID, dao, maxNotValidBeforeDeltaKey))
return cache.maxNotValidBeforeDelta
}
// setMaxNotValidBeforeDelta is Notary contract method and sets the maximum NotValidBefore delta.
@ -438,9 +422,9 @@ func (n *Notary) setMaxNotValidBeforeDelta(ic *interop.Context, args []stackitem
if !n.NEO.checkCommittee(ic) {
panic("invalid committee signature")
}
cache := ic.DAO.Store.GetRWCache(n.ID).(*NotaryCache)
setIntWithKey(n.ID, ic.DAO, maxNotValidBeforeDeltaKey, int64(value))
cache.isValid = false
cache := ic.DAO.Store.GetRWCache(n.ID).(*NotaryCache)
cache.maxNotValidBeforeDelta = value
return stackitem.Null{}
}
@ -452,10 +436,7 @@ func (n *Notary) getNotaryServiceFeePerKey(ic *interop.Context, _ []stackitem.It
// GetNotaryServiceFeePerKey is an internal representation of Notary getNotaryServiceFeePerKey method.
func (n *Notary) GetNotaryServiceFeePerKey(dao *dao.Simple) int64 {
cache := dao.Store.GetROCache(n.ID).(*NotaryCache)
if cache.isValid {
return cache.notaryServiceFeePerKey
}
return getIntWithKey(n.ID, dao, notaryServiceFeeKey)
return cache.notaryServiceFeePerKey
}
// setNotaryServiceFeePerKey is Notary contract method and sets a reward per notary request key for notary nodes.
@ -467,9 +448,9 @@ func (n *Notary) setNotaryServiceFeePerKey(ic *interop.Context, args []stackitem
if !n.NEO.checkCommittee(ic) {
panic("invalid committee signature")
}
cache := ic.DAO.Store.GetRWCache(n.ID).(*NotaryCache)
setIntWithKey(n.ID, ic.DAO, notaryServiceFeeKey, int64(value))
cache.isValid = false
cache := ic.DAO.Store.GetRWCache(n.ID).(*NotaryCache)
cache.notaryServiceFeePerKey = value
return stackitem.Null{}
}