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)) 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) { func TestNotary_NotaryServiceFeePerKey(t *testing.T) {
c := newNotaryClient(t) c := newNotaryClient(t)
testGetSet(t, c, "NotaryServiceFeePerKey", 1000_0000, 0, 0) 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) { func TestNotary_Pipeline(t *testing.T) {
notaryCommitteeInvoker := newNotaryClient(t) notaryCommitteeInvoker := newNotaryClient(t)
e := notaryCommitteeInvoker.Executor e := notaryCommitteeInvoker.Executor

View file

@ -34,10 +34,6 @@ type Notary struct {
} }
type NotaryCache 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 maxNotValidBeforeDelta uint32
notaryServiceFeePerKey int64 notaryServiceFeePerKey int64
} }
@ -143,7 +139,6 @@ func (n *Notary) Initialize(ic *interop.Context) error {
setIntWithKey(n.ID, ic.DAO, notaryServiceFeeKey, defaultNotaryServiceFeePerKey) setIntWithKey(n.ID, ic.DAO, notaryServiceFeeKey, defaultNotaryServiceFeePerKey)
cache := &NotaryCache{ cache := &NotaryCache{
isValid: true,
maxNotValidBeforeDelta: defaultMaxNotValidBeforeDelta, maxNotValidBeforeDelta: defaultMaxNotValidBeforeDelta,
notaryServiceFeePerKey: defaultNotaryServiceFeePerKey, notaryServiceFeePerKey: defaultNotaryServiceFeePerKey,
} }
@ -152,10 +147,10 @@ func (n *Notary) Initialize(ic *interop.Context) error {
} }
func (n *Notary) InitializeCache(d *dao.Simple) error { func (n *Notary) InitializeCache(d *dao.Simple) error {
cache := &NotaryCache{isValid: true} cache := &NotaryCache{
maxNotValidBeforeDelta: uint32(getIntWithKey(n.ID, d, maxNotValidBeforeDeltaKey)),
cache.maxNotValidBeforeDelta = uint32(getIntWithKey(n.ID, d, maxNotValidBeforeDeltaKey)) notaryServiceFeePerKey: getIntWithKey(n.ID, d, notaryServiceFeeKey),
cache.notaryServiceFeePerKey = getIntWithKey(n.ID, d, notaryServiceFeeKey) }
d.Store.SetCache(n.ID, cache) d.Store.SetCache(n.ID, cache)
return nil return nil
@ -206,14 +201,6 @@ func (n *Notary) OnPersist(ic *interop.Context) error {
// PostPersist implements Contract interface. // PostPersist implements Contract interface.
func (n *Notary) PostPersist(ic *interop.Context) error { 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 return nil
} }
@ -421,11 +408,8 @@ func (n *Notary) getMaxNotValidBeforeDelta(ic *interop.Context, _ []stackitem.It
// GetMaxNotValidBeforeDelta is an internal representation of Notary getMaxNotValidBeforeDelta method. // GetMaxNotValidBeforeDelta is an internal representation of Notary getMaxNotValidBeforeDelta method.
func (n *Notary) GetMaxNotValidBeforeDelta(dao *dao.Simple) uint32 { func (n *Notary) GetMaxNotValidBeforeDelta(dao *dao.Simple) uint32 {
cache := dao.Store.GetROCache(n.ID).(*NotaryCache) cache := dao.Store.GetROCache(n.ID).(*NotaryCache)
if cache.isValid {
return cache.maxNotValidBeforeDelta return cache.maxNotValidBeforeDelta
} }
return uint32(getIntWithKey(n.ID, dao, maxNotValidBeforeDeltaKey))
}
// setMaxNotValidBeforeDelta is Notary contract method and sets the maximum NotValidBefore delta. // setMaxNotValidBeforeDelta is Notary contract method and sets the maximum NotValidBefore delta.
func (n *Notary) setMaxNotValidBeforeDelta(ic *interop.Context, args []stackitem.Item) stackitem.Item { func (n *Notary) setMaxNotValidBeforeDelta(ic *interop.Context, args []stackitem.Item) stackitem.Item {
@ -438,9 +422,9 @@ func (n *Notary) setMaxNotValidBeforeDelta(ic *interop.Context, args []stackitem
if !n.NEO.checkCommittee(ic) { if !n.NEO.checkCommittee(ic) {
panic("invalid committee signature") panic("invalid committee signature")
} }
cache := ic.DAO.Store.GetRWCache(n.ID).(*NotaryCache)
setIntWithKey(n.ID, ic.DAO, maxNotValidBeforeDeltaKey, int64(value)) 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{} return stackitem.Null{}
} }
@ -452,11 +436,8 @@ func (n *Notary) getNotaryServiceFeePerKey(ic *interop.Context, _ []stackitem.It
// GetNotaryServiceFeePerKey is an internal representation of Notary getNotaryServiceFeePerKey method. // GetNotaryServiceFeePerKey is an internal representation of Notary getNotaryServiceFeePerKey method.
func (n *Notary) GetNotaryServiceFeePerKey(dao *dao.Simple) int64 { func (n *Notary) GetNotaryServiceFeePerKey(dao *dao.Simple) int64 {
cache := dao.Store.GetROCache(n.ID).(*NotaryCache) cache := dao.Store.GetROCache(n.ID).(*NotaryCache)
if cache.isValid {
return cache.notaryServiceFeePerKey return cache.notaryServiceFeePerKey
} }
return getIntWithKey(n.ID, dao, notaryServiceFeeKey)
}
// setNotaryServiceFeePerKey is Notary contract method and sets a reward per notary request key for notary nodes. // setNotaryServiceFeePerKey is Notary contract method and sets a reward per notary request key for notary nodes.
func (n *Notary) setNotaryServiceFeePerKey(ic *interop.Context, args []stackitem.Item) stackitem.Item { func (n *Notary) setNotaryServiceFeePerKey(ic *interop.Context, args []stackitem.Item) stackitem.Item {
@ -467,9 +448,9 @@ func (n *Notary) setNotaryServiceFeePerKey(ic *interop.Context, args []stackitem
if !n.NEO.checkCommittee(ic) { if !n.NEO.checkCommittee(ic) {
panic("invalid committee signature") panic("invalid committee signature")
} }
cache := ic.DAO.Store.GetRWCache(n.ID).(*NotaryCache)
setIntWithKey(n.ID, ic.DAO, notaryServiceFeeKey, int64(value)) 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{} return stackitem.Null{}
} }