2020-09-18 13:26:36 +00:00
|
|
|
package native
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
2020-10-21 10:02:46 +00:00
|
|
|
"fmt"
|
2020-11-05 16:34:48 +00:00
|
|
|
"math"
|
2020-09-18 13:26:36 +00:00
|
|
|
"math/big"
|
2020-12-17 08:41:28 +00:00
|
|
|
"strings"
|
2020-09-28 11:58:04 +00:00
|
|
|
"sync/atomic"
|
2020-09-18 13:26:36 +00:00
|
|
|
|
2020-09-28 11:58:04 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/blockchainer/services"
|
2020-09-18 13:26:36 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/dao"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop/contract"
|
2020-12-13 18:25:04 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/native/nativenames"
|
2021-03-23 10:37:30 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/native/noderoles"
|
2020-09-18 13:26:36 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/storage"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2021-02-02 15:46:43 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/bigint"
|
2020-09-18 13:26:36 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
2020-12-29 10:45:49 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
|
2020-09-18 13:26:36 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2021-07-18 13:32:10 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util/slice"
|
2020-09-18 13:26:36 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Oracle represents Oracle native contract.
|
|
|
|
type Oracle struct {
|
|
|
|
interop.ContractMD
|
|
|
|
GAS *GAS
|
|
|
|
NEO *NEO
|
2020-09-24 12:46:31 +00:00
|
|
|
|
2021-01-22 08:28:13 +00:00
|
|
|
Desig *Designate
|
|
|
|
oracleScript []byte
|
2020-09-28 11:58:04 +00:00
|
|
|
|
|
|
|
// Module is an oracle module capable of talking with the external world.
|
|
|
|
Module atomic.Value
|
2022-04-20 18:30:09 +00:00
|
|
|
// newRequests contains new requests created during the current block.
|
2020-09-28 11:58:04 +00:00
|
|
|
newRequests map[uint64]*state.OracleRequest
|
2020-09-18 13:26:36 +00:00
|
|
|
}
|
|
|
|
|
2022-04-12 14:29:11 +00:00
|
|
|
type OracleCache struct {
|
2022-04-19 15:36:40 +00:00
|
|
|
requestPrice int64
|
2022-04-12 14:29:11 +00:00
|
|
|
}
|
|
|
|
|
2020-09-18 13:26:36 +00:00
|
|
|
const (
|
2021-02-15 15:43:10 +00:00
|
|
|
oracleContractID = -9
|
2020-09-18 13:26:36 +00:00
|
|
|
maxURLLength = 256
|
|
|
|
maxFilterLength = 128
|
|
|
|
maxCallbackLength = 32
|
|
|
|
maxUserDataLength = 512
|
2021-05-12 20:17:03 +00:00
|
|
|
// maxRequestsCount is the maximum number of requests per URL.
|
2020-10-21 10:02:46 +00:00
|
|
|
maxRequestsCount = 256
|
2020-09-18 13:26:36 +00:00
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// DefaultOracleRequestPrice is the default amount GAS needed for an oracle request.
|
2021-03-05 11:38:16 +00:00
|
|
|
DefaultOracleRequestPrice = 5000_0000
|
2021-04-06 19:22:34 +00:00
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// MinimumResponseGas is the minimum response fee permitted for a request.
|
2021-04-06 19:22:34 +00:00
|
|
|
MinimumResponseGas = 10_000_000
|
2020-09-18 13:26:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-03-05 11:38:16 +00:00
|
|
|
prefixRequestPrice = []byte{5}
|
|
|
|
prefixIDList = []byte{6}
|
|
|
|
prefixRequest = []byte{7}
|
|
|
|
prefixRequestID = []byte{9}
|
2020-09-18 13:26:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Various validation errors.
|
|
|
|
var (
|
|
|
|
ErrBigArgument = errors.New("some of the arguments are invalid")
|
|
|
|
ErrInvalidWitness = errors.New("witness check failed")
|
2021-04-06 19:22:34 +00:00
|
|
|
ErrLowResponseGas = errors.New("not enough gas for response")
|
2020-09-18 13:26:36 +00:00
|
|
|
ErrNotEnoughGas = errors.New("gas limit exceeded")
|
|
|
|
ErrRequestNotFound = errors.New("oracle request not found")
|
|
|
|
ErrResponseNotFound = errors.New("oracle response not found")
|
|
|
|
)
|
|
|
|
|
2022-04-15 14:48:58 +00:00
|
|
|
var (
|
2022-04-20 14:47:48 +00:00
|
|
|
_ interop.Contract = (*Oracle)(nil)
|
|
|
|
_ dao.NativeContractCache = (*OracleCache)(nil)
|
2022-04-15 14:48:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Copy implements NativeContractCache interface.
|
2022-04-20 14:47:48 +00:00
|
|
|
func (c *OracleCache) Copy() dao.NativeContractCache {
|
2022-04-15 14:48:58 +00:00
|
|
|
cp := &OracleCache{}
|
|
|
|
copyOracleCache(c, cp)
|
|
|
|
return cp
|
|
|
|
}
|
|
|
|
|
|
|
|
func copyOracleCache(src, dst *OracleCache) {
|
2022-04-19 09:26:46 +00:00
|
|
|
*dst = *src
|
2022-04-15 14:48:58 +00:00
|
|
|
}
|
|
|
|
|
2020-09-18 13:26:36 +00:00
|
|
|
func newOracle() *Oracle {
|
2021-01-15 21:17:31 +00:00
|
|
|
o := &Oracle{ContractMD: *interop.NewContractMD(nativenames.Oracle, oracleContractID)}
|
2021-02-15 13:40:44 +00:00
|
|
|
defer o.UpdateHash()
|
2020-09-18 13:26:36 +00:00
|
|
|
|
2022-03-30 12:53:35 +00:00
|
|
|
o.oracleScript = CreateOracleResponseScript(o.Hash)
|
2021-01-22 08:28:13 +00:00
|
|
|
|
2020-09-18 13:26:36 +00:00
|
|
|
desc := newDescriptor("request", smartcontract.VoidType,
|
|
|
|
manifest.NewParameter("url", smartcontract.StringType),
|
|
|
|
manifest.NewParameter("filter", smartcontract.StringType),
|
|
|
|
manifest.NewParameter("callback", smartcontract.StringType),
|
|
|
|
manifest.NewParameter("userData", smartcontract.AnyType),
|
|
|
|
manifest.NewParameter("gasForResponse", smartcontract.IntegerType))
|
2021-03-05 10:30:16 +00:00
|
|
|
md := newMethodAndPrice(o.request, 0, callflag.States|callflag.AllowNotify)
|
2020-12-08 10:27:41 +00:00
|
|
|
o.AddMethod(md, desc)
|
2020-09-18 13:26:36 +00:00
|
|
|
|
|
|
|
desc = newDescriptor("finish", smartcontract.VoidType)
|
2021-02-25 15:04:46 +00:00
|
|
|
md = newMethodAndPrice(o.finish, 0, callflag.States|callflag.AllowCall|callflag.AllowNotify)
|
2020-12-08 10:27:41 +00:00
|
|
|
o.AddMethod(md, desc)
|
2020-09-18 13:26:36 +00:00
|
|
|
|
|
|
|
desc = newDescriptor("verify", smartcontract.BoolType)
|
2021-03-05 10:30:16 +00:00
|
|
|
md = newMethodAndPrice(o.verify, 1<<15, callflag.NoneFlag)
|
2020-12-08 10:27:41 +00:00
|
|
|
o.AddMethod(md, desc)
|
2020-09-18 13:26:36 +00:00
|
|
|
|
2020-11-05 19:51:08 +00:00
|
|
|
o.AddEvent("OracleRequest", manifest.NewParameter("Id", smartcontract.IntegerType),
|
|
|
|
manifest.NewParameter("RequestContract", smartcontract.Hash160Type),
|
|
|
|
manifest.NewParameter("Url", smartcontract.StringType),
|
|
|
|
manifest.NewParameter("Filter", smartcontract.StringType))
|
|
|
|
o.AddEvent("OracleResponse", manifest.NewParameter("Id", smartcontract.IntegerType),
|
|
|
|
manifest.NewParameter("OriginalTx", smartcontract.Hash256Type))
|
|
|
|
|
2021-03-05 11:38:16 +00:00
|
|
|
desc = newDescriptor("getPrice", smartcontract.IntegerType)
|
|
|
|
md = newMethodAndPrice(o.getPrice, 1<<15, callflag.ReadStates)
|
|
|
|
o.AddMethod(md, desc)
|
|
|
|
|
|
|
|
desc = newDescriptor("setPrice", smartcontract.VoidType,
|
|
|
|
manifest.NewParameter("price", smartcontract.IntegerType))
|
|
|
|
md = newMethodAndPrice(o.setPrice, 1<<15, callflag.States)
|
|
|
|
o.AddMethod(md, desc)
|
|
|
|
|
2020-09-18 13:26:36 +00:00
|
|
|
return o
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// GetOracleResponseScript returns a script for the transaction with an oracle response.
|
2021-01-22 08:28:13 +00:00
|
|
|
func (o *Oracle) GetOracleResponseScript() []byte {
|
2021-07-18 13:32:10 +00:00
|
|
|
return slice.Copy(o.oracleScript)
|
2021-01-22 08:28:13 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// OnPersist implements the Contract interface.
|
2020-12-13 18:36:06 +00:00
|
|
|
func (o *Oracle) OnPersist(ic *interop.Context) error {
|
2020-09-28 11:58:04 +00:00
|
|
|
var err error
|
|
|
|
if o.newRequests == nil {
|
|
|
|
o.newRequests, err = o.getRequests(ic.DAO)
|
|
|
|
}
|
|
|
|
return err
|
2020-12-13 18:36:06 +00:00
|
|
|
}
|
|
|
|
|
2020-09-18 13:26:36 +00:00
|
|
|
// PostPersist represents `postPersist` method.
|
|
|
|
func (o *Oracle) PostPersist(ic *interop.Context) error {
|
2021-03-05 11:38:16 +00:00
|
|
|
p := o.getPriceInternal(ic.DAO)
|
|
|
|
|
2020-09-18 13:26:36 +00:00
|
|
|
var nodes keys.PublicKeys
|
|
|
|
var reward []big.Int
|
2021-03-05 11:38:16 +00:00
|
|
|
single := big.NewInt(p)
|
2020-09-28 11:58:04 +00:00
|
|
|
var removedIDs []uint64
|
|
|
|
|
|
|
|
orc, _ := o.Module.Load().(services.Oracle)
|
2020-09-18 13:26:36 +00:00
|
|
|
for _, tx := range ic.Block.Transactions {
|
|
|
|
resp := getResponse(tx)
|
|
|
|
if resp == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
reqKey := makeRequestKey(resp.ID)
|
2020-10-07 12:06:10 +00:00
|
|
|
req := new(state.OracleRequest)
|
2021-07-17 15:37:33 +00:00
|
|
|
if err := o.getConvertibleFromDAO(ic.DAO, reqKey, req); err != nil {
|
2020-12-01 08:18:59 +00:00
|
|
|
continue
|
2020-09-18 13:26:36 +00:00
|
|
|
}
|
2022-02-16 14:48:15 +00:00
|
|
|
ic.DAO.DeleteStorageItem(o.ID, reqKey)
|
2020-09-28 11:58:04 +00:00
|
|
|
if orc != nil {
|
|
|
|
removedIDs = append(removedIDs, resp.ID)
|
|
|
|
}
|
2020-09-18 13:26:36 +00:00
|
|
|
|
|
|
|
idKey := makeIDListKey(req.URL)
|
|
|
|
idList := new(IDList)
|
2021-07-17 15:37:33 +00:00
|
|
|
if err := o.getConvertibleFromDAO(ic.DAO, idKey, idList); err != nil {
|
2020-09-18 13:26:36 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !idList.Remove(resp.ID) {
|
|
|
|
return errors.New("response ID wasn't found")
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
if len(*idList) == 0 {
|
2022-02-16 14:48:15 +00:00
|
|
|
ic.DAO.DeleteStorageItem(o.ID, idKey)
|
2020-09-18 13:26:36 +00:00
|
|
|
} else {
|
2021-07-17 15:37:33 +00:00
|
|
|
err = putConvertibleToDAO(o.ID, ic.DAO, idKey, idList)
|
2020-09-18 13:26:36 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if nodes == nil {
|
2020-10-01 15:17:09 +00:00
|
|
|
nodes, err = o.GetOracleNodes(ic.DAO)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-18 13:26:36 +00:00
|
|
|
reward = make([]big.Int, len(nodes))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(reward) > 0 {
|
|
|
|
index := resp.ID % uint64(len(nodes))
|
|
|
|
reward[index].Add(&reward[index], single)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for i := range reward {
|
2020-11-30 10:05:42 +00:00
|
|
|
o.GAS.mint(ic, nodes[i].GetScriptHash(), &reward[i], false)
|
2020-09-18 13:26:36 +00:00
|
|
|
}
|
2020-09-28 11:58:04 +00:00
|
|
|
|
|
|
|
if len(removedIDs) != 0 && orc != nil {
|
|
|
|
orc.RemoveRequests(removedIDs)
|
|
|
|
}
|
|
|
|
return o.updateCache(ic.DAO)
|
2020-09-18 13:26:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Metadata returns contract metadata.
|
|
|
|
func (o *Oracle) Metadata() *interop.ContractMD {
|
|
|
|
return &o.ContractMD
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Initialize initializes an Oracle contract.
|
2020-09-18 13:26:36 +00:00
|
|
|
func (o *Oracle) Initialize(ic *interop.Context) error {
|
2022-02-16 14:48:15 +00:00
|
|
|
setIntWithKey(o.ID, ic.DAO, prefixRequestID, 0)
|
|
|
|
setIntWithKey(o.ID, ic.DAO, prefixRequestPrice, DefaultOracleRequestPrice)
|
2022-04-12 14:29:11 +00:00
|
|
|
|
2022-04-19 15:36:40 +00:00
|
|
|
cache := &OracleCache{
|
|
|
|
requestPrice: int64(DefaultOracleRequestPrice),
|
|
|
|
}
|
2022-04-20 14:47:48 +00:00
|
|
|
ic.DAO.SetCache(o.ID, cache)
|
2021-03-05 11:38:16 +00:00
|
|
|
return nil
|
2020-09-18 13:26:36 +00:00
|
|
|
}
|
|
|
|
|
2022-04-12 14:29:11 +00:00
|
|
|
func (o *Oracle) InitializeCache(d *dao.Simple) {
|
|
|
|
cache := &OracleCache{}
|
2022-04-19 09:26:46 +00:00
|
|
|
cache.requestPrice = getIntWithKey(o.ID, d, prefixRequestPrice)
|
2022-04-20 14:47:48 +00:00
|
|
|
d.SetCache(o.ID, cache)
|
2022-04-12 14:29:11 +00:00
|
|
|
}
|
|
|
|
|
2020-09-18 13:26:36 +00:00
|
|
|
func getResponse(tx *transaction.Transaction) *transaction.OracleResponse {
|
|
|
|
for i := range tx.Attributes {
|
|
|
|
if tx.Attributes[i].Type == transaction.OracleResponseT {
|
|
|
|
return tx.Attributes[i].Value.(*transaction.OracleResponse)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Oracle) finish(ic *interop.Context, _ []stackitem.Item) stackitem.Item {
|
|
|
|
err := o.FinishInternal(ic)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return stackitem.Null{}
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// FinishInternal processes an oracle response.
|
2020-09-18 13:26:36 +00:00
|
|
|
func (o *Oracle) FinishInternal(ic *interop.Context) error {
|
|
|
|
resp := getResponse(ic.Tx)
|
|
|
|
if resp == nil {
|
|
|
|
return ErrResponseNotFound
|
|
|
|
}
|
|
|
|
req, err := o.GetRequestInternal(ic.DAO, resp.ID)
|
|
|
|
if err != nil {
|
|
|
|
return ErrRequestNotFound
|
|
|
|
}
|
2022-05-16 08:19:15 +00:00
|
|
|
ic.AddNotification(o.Hash, "OracleResponse", stackitem.NewArray([]stackitem.Item{
|
|
|
|
stackitem.Make(resp.ID),
|
|
|
|
stackitem.Make(req.OriginalTxID.BytesBE()),
|
|
|
|
}))
|
2020-09-18 13:26:36 +00:00
|
|
|
|
2021-11-15 16:29:26 +00:00
|
|
|
origTx, _, err := ic.DAO.GetTransaction(req.OriginalTxID)
|
|
|
|
if err != nil {
|
|
|
|
return ErrRequestNotFound
|
|
|
|
}
|
|
|
|
ic.UseSigners(origTx.Signers)
|
|
|
|
defer ic.UseSigners(nil)
|
2020-11-05 19:51:08 +00:00
|
|
|
|
2021-07-16 10:49:14 +00:00
|
|
|
userData, err := stackitem.Deserialize(req.UserData)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-09 12:16:49 +00:00
|
|
|
args := []stackitem.Item{
|
2020-09-18 13:26:36 +00:00
|
|
|
stackitem.Make(req.URL),
|
2021-07-16 10:49:14 +00:00
|
|
|
userData,
|
2020-09-18 13:26:36 +00:00
|
|
|
stackitem.Make(resp.Code),
|
|
|
|
stackitem.Make(resp.Result),
|
2020-12-09 12:16:49 +00:00
|
|
|
}
|
2020-12-13 15:26:35 +00:00
|
|
|
cs, err := ic.GetContract(req.CallbackContract)
|
2020-12-09 12:16:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-29 10:44:07 +00:00
|
|
|
return contract.CallFromNative(ic, o.Hash, cs, req.CallbackMethod, args, false)
|
2020-09-18 13:26:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Oracle) request(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
|
|
|
url, err := stackitem.ToString(args[0])
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-11-18 20:59:13 +00:00
|
|
|
var filter *string
|
|
|
|
_, ok := args[1].(stackitem.Null)
|
|
|
|
if !ok {
|
|
|
|
// Check UTF-8 validity.
|
|
|
|
str, err := stackitem.ToString(args[1])
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
filter = &str
|
2020-09-18 13:26:36 +00:00
|
|
|
}
|
|
|
|
cb, err := stackitem.ToString(args[2])
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
userData := args[3]
|
|
|
|
gas, err := args[4].TryInteger()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-03-05 11:38:16 +00:00
|
|
|
if !ic.VM.AddGas(o.getPriceInternal(ic.DAO)) {
|
|
|
|
panic("insufficient gas")
|
|
|
|
}
|
2020-09-18 13:26:36 +00:00
|
|
|
if err := o.RequestInternal(ic, url, filter, cb, userData, gas); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return stackitem.Null{}
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// RequestInternal processes an oracle request.
|
2020-11-18 20:59:13 +00:00
|
|
|
func (o *Oracle) RequestInternal(ic *interop.Context, url string, filter *string, cb string, userData stackitem.Item, gas *big.Int) error {
|
2021-07-19 12:17:45 +00:00
|
|
|
if len(url) > maxURLLength || (filter != nil && len(*filter) > maxFilterLength) || len(cb) > maxCallbackLength || !gas.IsInt64() {
|
2020-09-18 13:26:36 +00:00
|
|
|
return ErrBigArgument
|
|
|
|
}
|
2021-07-19 12:17:45 +00:00
|
|
|
if gas.Int64() < MinimumResponseGas {
|
2021-04-06 19:22:34 +00:00
|
|
|
return ErrLowResponseGas
|
|
|
|
}
|
2020-12-17 08:41:28 +00:00
|
|
|
if strings.HasPrefix(cb, "_") {
|
|
|
|
return errors.New("disallowed callback method (starts with '_')")
|
|
|
|
}
|
2020-09-18 13:26:36 +00:00
|
|
|
|
|
|
|
if !ic.VM.AddGas(gas.Int64()) {
|
|
|
|
return ErrNotEnoughGas
|
|
|
|
}
|
2020-11-19 15:01:42 +00:00
|
|
|
callingHash := ic.VM.GetCallingScriptHash()
|
2020-11-30 10:05:42 +00:00
|
|
|
o.GAS.mint(ic, o.Hash, gas, false)
|
2021-02-09 09:26:25 +00:00
|
|
|
si := ic.DAO.GetStorageItem(o.ID, prefixRequestID)
|
2021-03-05 14:06:54 +00:00
|
|
|
itemID := bigint.FromBytes(si)
|
2021-02-02 15:46:43 +00:00
|
|
|
id := itemID.Uint64()
|
|
|
|
itemID.Add(itemID, intOne)
|
2021-03-05 14:06:54 +00:00
|
|
|
si = bigint.ToPreallocatedBytes(itemID, si)
|
2022-02-16 14:48:15 +00:00
|
|
|
ic.DAO.PutStorageItem(o.ID, prefixRequestID, si)
|
2020-09-18 13:26:36 +00:00
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Should be executed from the contract.
|
2020-12-13 15:26:35 +00:00
|
|
|
_, err := ic.GetContract(ic.VM.GetCallingScriptHash())
|
2020-09-18 13:26:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-31 17:10:20 +00:00
|
|
|
data, err := ic.DAO.GetItemCtx().Serialize(userData, false)
|
2021-07-16 13:00:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-09-18 13:26:36 +00:00
|
|
|
}
|
|
|
|
if len(data) > maxUserDataLength {
|
|
|
|
return ErrBigArgument
|
|
|
|
}
|
2022-05-31 17:10:20 +00:00
|
|
|
data = slice.Copy(data) // Serialization context will be used in PutRequestInternal again.
|
2020-09-18 13:26:36 +00:00
|
|
|
|
2020-11-18 20:59:13 +00:00
|
|
|
var filterNotif stackitem.Item
|
|
|
|
if filter != nil {
|
|
|
|
filterNotif = stackitem.Make(*filter)
|
|
|
|
} else {
|
|
|
|
filterNotif = stackitem.Null{}
|
|
|
|
}
|
2022-05-16 08:19:15 +00:00
|
|
|
ic.AddNotification(o.Hash, "OracleRequest", stackitem.NewArray([]stackitem.Item{
|
|
|
|
stackitem.Make(id),
|
|
|
|
stackitem.Make(ic.VM.GetCallingScriptHash().BytesBE()),
|
|
|
|
stackitem.Make(url),
|
|
|
|
filterNotif,
|
|
|
|
}))
|
2020-10-07 12:06:10 +00:00
|
|
|
req := &state.OracleRequest{
|
2020-09-18 13:26:36 +00:00
|
|
|
OriginalTxID: o.getOriginalTxID(ic.DAO, ic.Tx),
|
|
|
|
GasForResponse: gas.Uint64(),
|
|
|
|
URL: url,
|
2020-11-18 20:59:13 +00:00
|
|
|
Filter: filter,
|
2020-11-19 15:01:42 +00:00
|
|
|
CallbackContract: callingHash,
|
2020-09-18 13:26:36 +00:00
|
|
|
CallbackMethod: cb,
|
|
|
|
UserData: data,
|
|
|
|
}
|
2020-09-24 13:33:40 +00:00
|
|
|
return o.PutRequestInternal(id, req, ic.DAO)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// PutRequestInternal puts the oracle request with the specified id to d.
|
2022-02-16 15:04:47 +00:00
|
|
|
func (o *Oracle) PutRequestInternal(id uint64, req *state.OracleRequest, d *dao.Simple) error {
|
2020-09-18 13:26:36 +00:00
|
|
|
reqKey := makeRequestKey(id)
|
2021-07-17 15:37:33 +00:00
|
|
|
if err := putConvertibleToDAO(o.ID, d, reqKey, req); err != nil {
|
2020-09-18 13:26:36 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-09-28 11:58:04 +00:00
|
|
|
o.newRequests[id] = req
|
2020-09-18 13:26:36 +00:00
|
|
|
|
|
|
|
// Add request ID to the id list.
|
|
|
|
lst := new(IDList)
|
2020-09-24 13:33:40 +00:00
|
|
|
key := makeIDListKey(req.URL)
|
2021-07-17 15:37:33 +00:00
|
|
|
if err := o.getConvertibleFromDAO(d, key, lst); err != nil && !errors.Is(err, storage.ErrKeyNotFound) {
|
2020-09-18 13:26:36 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-10-21 10:02:46 +00:00
|
|
|
if len(*lst) >= maxRequestsCount {
|
|
|
|
return fmt.Errorf("there are too many pending requests for %s url", req.URL)
|
|
|
|
}
|
2020-09-18 13:26:36 +00:00
|
|
|
*lst = append(*lst, id)
|
2021-07-17 15:37:33 +00:00
|
|
|
return putConvertibleToDAO(o.ID, d, key, lst)
|
2020-09-18 13:26:36 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// GetScriptHash returns script hash of oracle nodes.
|
2022-02-16 15:04:47 +00:00
|
|
|
func (o *Oracle) GetScriptHash(d *dao.Simple) (util.Uint160, error) {
|
2021-03-23 10:37:30 +00:00
|
|
|
return o.Desig.GetLastDesignatedHash(d, noderoles.Oracle)
|
2020-09-24 12:50:52 +00:00
|
|
|
}
|
|
|
|
|
2020-10-01 15:17:09 +00:00
|
|
|
// GetOracleNodes returns public keys of oracle nodes.
|
2022-02-16 15:04:47 +00:00
|
|
|
func (o *Oracle) GetOracleNodes(d *dao.Simple) (keys.PublicKeys, error) {
|
2021-03-23 10:37:30 +00:00
|
|
|
nodes, _, err := o.Desig.GetDesignatedByRole(d, noderoles.Oracle, math.MaxUint32)
|
2020-11-05 16:34:48 +00:00
|
|
|
return nodes, err
|
2020-09-18 13:26:36 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// GetRequestInternal returns the request by ID and key under which it is stored.
|
2022-02-16 15:04:47 +00:00
|
|
|
func (o *Oracle) GetRequestInternal(d *dao.Simple, id uint64) (*state.OracleRequest, error) {
|
2020-09-18 13:26:36 +00:00
|
|
|
key := makeRequestKey(id)
|
2020-10-07 12:06:10 +00:00
|
|
|
req := new(state.OracleRequest)
|
2021-07-17 15:37:33 +00:00
|
|
|
return req, o.getConvertibleFromDAO(d, key, req)
|
2020-09-18 13:26:36 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// GetIDListInternal returns the request by ID and key under which it is stored.
|
2022-02-16 15:04:47 +00:00
|
|
|
func (o *Oracle) GetIDListInternal(d *dao.Simple, url string) (*IDList, error) {
|
2020-09-18 13:26:36 +00:00
|
|
|
key := makeIDListKey(url)
|
|
|
|
idList := new(IDList)
|
2021-07-17 15:37:33 +00:00
|
|
|
return idList, o.getConvertibleFromDAO(d, key, idList)
|
2020-09-18 13:26:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Oracle) verify(ic *interop.Context, _ []stackitem.Item) stackitem.Item {
|
|
|
|
return stackitem.NewBool(ic.Tx.HasAttribute(transaction.OracleResponseT))
|
|
|
|
}
|
|
|
|
|
2021-03-05 11:38:16 +00:00
|
|
|
func (o *Oracle) getPrice(ic *interop.Context, _ []stackitem.Item) stackitem.Item {
|
|
|
|
return stackitem.NewBigInteger(big.NewInt(o.getPriceInternal(ic.DAO)))
|
|
|
|
}
|
|
|
|
|
2022-02-16 15:04:47 +00:00
|
|
|
func (o *Oracle) getPriceInternal(d *dao.Simple) int64 {
|
2022-04-20 14:47:48 +00:00
|
|
|
cache := d.GetROCache(o.ID).(*OracleCache)
|
2022-04-19 15:36:40 +00:00
|
|
|
return cache.requestPrice
|
2021-03-05 11:38:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Oracle) setPrice(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
|
|
|
price := toBigInt(args[0])
|
|
|
|
if price.Sign() <= 0 || !price.IsInt64() {
|
|
|
|
panic("invalid register price")
|
|
|
|
}
|
|
|
|
if !o.NEO.checkCommittee(ic) {
|
|
|
|
panic("invalid committee signature")
|
|
|
|
}
|
2022-02-16 14:48:15 +00:00
|
|
|
setIntWithKey(o.ID, ic.DAO, prefixRequestPrice, price.Int64())
|
2022-04-20 14:47:48 +00:00
|
|
|
cache := ic.DAO.GetRWCache(o.ID).(*OracleCache)
|
2022-04-19 15:36:40 +00:00
|
|
|
cache.requestPrice = price.Int64()
|
2021-03-05 11:38:16 +00:00
|
|
|
return stackitem.Null{}
|
|
|
|
}
|
|
|
|
|
2022-02-16 15:04:47 +00:00
|
|
|
func (o *Oracle) getOriginalTxID(d *dao.Simple, tx *transaction.Transaction) util.Uint256 {
|
2020-09-18 13:26:36 +00:00
|
|
|
for i := range tx.Attributes {
|
|
|
|
if tx.Attributes[i].Type == transaction.OracleResponseT {
|
|
|
|
id := tx.Attributes[i].Value.(*transaction.OracleResponse).ID
|
|
|
|
req, _ := o.GetRequestInternal(d, id)
|
|
|
|
return req.OriginalTxID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tx.Hash()
|
|
|
|
}
|
|
|
|
|
2020-09-28 11:58:04 +00:00
|
|
|
// getRequests returns all requests which have not been finished yet.
|
2022-02-16 15:04:47 +00:00
|
|
|
func (o *Oracle) getRequests(d *dao.Simple) (map[uint64]*state.OracleRequest, error) {
|
2022-03-31 15:14:11 +00:00
|
|
|
var reqs = make(map[uint64]*state.OracleRequest)
|
|
|
|
var err error
|
|
|
|
d.Seek(o.ID, storage.SeekRange{Prefix: prefixRequest}, func(k, v []byte) bool {
|
|
|
|
if len(k) != 8 {
|
|
|
|
err = errors.New("invalid request ID")
|
|
|
|
return false
|
2020-09-28 11:58:04 +00:00
|
|
|
}
|
|
|
|
req := new(state.OracleRequest)
|
2022-03-31 15:14:11 +00:00
|
|
|
err = stackitem.DeserializeConvertible(v, req)
|
2021-07-17 15:37:33 +00:00
|
|
|
if err != nil {
|
2022-03-31 15:14:11 +00:00
|
|
|
return false
|
2020-09-28 11:58:04 +00:00
|
|
|
}
|
2022-03-31 15:14:11 +00:00
|
|
|
id := binary.BigEndian.Uint64(k)
|
2020-09-28 11:58:04 +00:00
|
|
|
reqs[id] = req
|
2022-03-31 15:14:11 +00:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-09-28 11:58:04 +00:00
|
|
|
}
|
|
|
|
return reqs, nil
|
|
|
|
}
|
|
|
|
|
2020-09-18 13:26:36 +00:00
|
|
|
func makeRequestKey(id uint64) []byte {
|
|
|
|
k := make([]byte, 9)
|
|
|
|
k[0] = prefixRequest[0]
|
2021-02-03 14:02:36 +00:00
|
|
|
binary.BigEndian.PutUint64(k[1:], id)
|
2020-09-18 13:26:36 +00:00
|
|
|
return k
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeIDListKey(url string) []byte {
|
|
|
|
return append(prefixIDList, hash.Hash160([]byte(url)).BytesBE()...)
|
|
|
|
}
|
|
|
|
|
2022-02-16 15:04:47 +00:00
|
|
|
func (o *Oracle) getConvertibleFromDAO(d *dao.Simple, key []byte, item stackitem.Convertible) error {
|
2021-07-17 15:37:33 +00:00
|
|
|
return getConvertibleFromDAO(o.ID, d, key, item)
|
2020-09-24 12:46:31 +00:00
|
|
|
}
|
2020-09-28 11:58:04 +00:00
|
|
|
|
2021-05-12 20:17:03 +00:00
|
|
|
// updateCache updates cached Oracle values if they've been changed.
|
2022-02-16 15:04:47 +00:00
|
|
|
func (o *Oracle) updateCache(d *dao.Simple) error {
|
2020-09-28 11:58:04 +00:00
|
|
|
orc, _ := o.Module.Load().(services.Oracle)
|
|
|
|
if orc == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
reqs := o.newRequests
|
|
|
|
o.newRequests = make(map[uint64]*state.OracleRequest)
|
|
|
|
for id := range reqs {
|
|
|
|
key := makeRequestKey(id)
|
2021-02-09 09:26:25 +00:00
|
|
|
if si := d.GetStorageItem(o.ID, key); si == nil { // tx has failed
|
2020-09-28 11:58:04 +00:00
|
|
|
delete(reqs, id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
orc.AddRequests(reqs)
|
|
|
|
return nil
|
|
|
|
}
|
2022-03-30 12:53:35 +00:00
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// CreateOracleResponseScript returns a script that is used to create the native Oracle
|
2022-03-30 12:53:35 +00:00
|
|
|
// response.
|
|
|
|
func CreateOracleResponseScript(nativeOracleHash util.Uint160) []byte {
|
|
|
|
w := io.NewBufBinWriter()
|
|
|
|
emit.AppCall(w.BinWriter, nativeOracleHash, "finish", callflag.All)
|
|
|
|
if w.Err != nil {
|
|
|
|
panic(fmt.Errorf("failed to create Oracle response script: %w", w.Err))
|
|
|
|
}
|
|
|
|
return w.Bytes()
|
|
|
|
}
|