forked from TrueCloudLab/neoneo-go
core: keep Oracle cache always valid and up-to-date
This commit is contained in:
parent
0f6bf33f86
commit
78b584053d
2 changed files with 11 additions and 17 deletions
|
@ -28,10 +28,14 @@ func newOracleClient(t *testing.T) *neotest.ContractInvoker {
|
||||||
return newNativeClient(t, nativenames.Oracle)
|
return newNativeClient(t, nativenames.Oracle)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetSetPrice(t *testing.T) {
|
func TestOracle_GetSetPrice(t *testing.T) {
|
||||||
testGetSet(t, newOracleClient(t), "Price", native.DefaultOracleRequestPrice, 1, math.MaxInt64)
|
testGetSet(t, newOracleClient(t), "Price", native.DefaultOracleRequestPrice, 1, math.MaxInt64)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestOracle_GetSetPriceCache(t *testing.T) {
|
||||||
|
testGetSetCache(t, newOracleClient(t), "Price", native.DefaultOracleRequestPrice)
|
||||||
|
}
|
||||||
|
|
||||||
func putOracleRequest(t *testing.T, oracleInvoker *neotest.ContractInvoker,
|
func putOracleRequest(t *testing.T, oracleInvoker *neotest.ContractInvoker,
|
||||||
url string, filter *string, cb string, userData []byte, gas int64, errStr ...string) {
|
url string, filter *string, cb string, userData []byte, gas int64, errStr ...string) {
|
||||||
var filtItem interface{}
|
var filtItem interface{}
|
||||||
|
|
|
@ -47,8 +47,7 @@ type Oracle struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type OracleCache struct {
|
type OracleCache struct {
|
||||||
requestPrice int64
|
requestPrice int64
|
||||||
requestPriceChanged bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -159,11 +158,6 @@ func (o *Oracle) OnPersist(ic *interop.Context) error {
|
||||||
// PostPersist represents `postPersist` method.
|
// PostPersist represents `postPersist` method.
|
||||||
func (o *Oracle) PostPersist(ic *interop.Context) error {
|
func (o *Oracle) PostPersist(ic *interop.Context) error {
|
||||||
p := o.getPriceInternal(ic.DAO)
|
p := o.getPriceInternal(ic.DAO)
|
||||||
cache := ic.DAO.Store.GetRWCache(o.ID).(*OracleCache)
|
|
||||||
if cache.requestPriceChanged {
|
|
||||||
cache.requestPrice = p
|
|
||||||
cache.requestPriceChanged = false
|
|
||||||
}
|
|
||||||
|
|
||||||
var nodes keys.PublicKeys
|
var nodes keys.PublicKeys
|
||||||
var reward []big.Int
|
var reward []big.Int
|
||||||
|
@ -238,9 +232,9 @@ func (o *Oracle) Initialize(ic *interop.Context) error {
|
||||||
setIntWithKey(o.ID, ic.DAO, prefixRequestID, 0)
|
setIntWithKey(o.ID, ic.DAO, prefixRequestID, 0)
|
||||||
setIntWithKey(o.ID, ic.DAO, prefixRequestPrice, DefaultOracleRequestPrice)
|
setIntWithKey(o.ID, ic.DAO, prefixRequestPrice, DefaultOracleRequestPrice)
|
||||||
|
|
||||||
cache := &OracleCache{}
|
cache := &OracleCache{
|
||||||
cache.requestPrice = int64(DefaultOracleRequestPrice)
|
requestPrice: int64(DefaultOracleRequestPrice),
|
||||||
cache.requestPriceChanged = false
|
}
|
||||||
ic.DAO.Store.SetCache(o.ID, cache)
|
ic.DAO.Store.SetCache(o.ID, cache)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -248,7 +242,6 @@ func (o *Oracle) Initialize(ic *interop.Context) error {
|
||||||
func (o *Oracle) InitializeCache(d *dao.Simple) {
|
func (o *Oracle) InitializeCache(d *dao.Simple) {
|
||||||
cache := &OracleCache{}
|
cache := &OracleCache{}
|
||||||
cache.requestPrice = getIntWithKey(o.ID, d, prefixRequestPrice)
|
cache.requestPrice = getIntWithKey(o.ID, d, prefixRequestPrice)
|
||||||
cache.requestPriceChanged = false
|
|
||||||
d.Store.SetCache(o.ID, cache)
|
d.Store.SetCache(o.ID, cache)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -467,10 +460,7 @@ func (o *Oracle) getPrice(ic *interop.Context, _ []stackitem.Item) stackitem.Ite
|
||||||
|
|
||||||
func (o *Oracle) getPriceInternal(d *dao.Simple) int64 {
|
func (o *Oracle) getPriceInternal(d *dao.Simple) int64 {
|
||||||
cache := d.Store.GetROCache(o.ID).(*OracleCache)
|
cache := d.Store.GetROCache(o.ID).(*OracleCache)
|
||||||
if !cache.requestPriceChanged {
|
return cache.requestPrice
|
||||||
return cache.requestPrice
|
|
||||||
}
|
|
||||||
return getIntWithKey(o.ID, d, prefixRequestPrice)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *Oracle) setPrice(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
func (o *Oracle) setPrice(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
||||||
|
@ -483,7 +473,7 @@ func (o *Oracle) setPrice(ic *interop.Context, args []stackitem.Item) stackitem.
|
||||||
}
|
}
|
||||||
setIntWithKey(o.ID, ic.DAO, prefixRequestPrice, price.Int64())
|
setIntWithKey(o.ID, ic.DAO, prefixRequestPrice, price.Int64())
|
||||||
cache := ic.DAO.Store.GetRWCache(o.ID).(*OracleCache)
|
cache := ic.DAO.Store.GetRWCache(o.ID).(*OracleCache)
|
||||||
cache.requestPriceChanged = true
|
cache.requestPrice = price.Int64()
|
||||||
return stackitem.Null{}
|
return stackitem.Null{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue