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"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop/interopnames"
|
2020-12-13 18:25:04 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/native/nativenames"
|
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"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
|
|
|
"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
|
|
|
|
// newRequests contains new requests created during current block.
|
|
|
|
newRequests map[uint64]*state.OracleRequest
|
2020-09-18 13:26:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2021-02-03 19:01:20 +00:00
|
|
|
oracleContractID = -7
|
2020-09-18 13:26:36 +00:00
|
|
|
maxURLLength = 256
|
|
|
|
maxFilterLength = 128
|
|
|
|
maxCallbackLength = 32
|
|
|
|
maxUserDataLength = 512
|
2020-10-21 10:02:46 +00:00
|
|
|
// maxRequestsCount is the maximum number of requests per URL
|
|
|
|
maxRequestsCount = 256
|
2020-09-18 13:26:36 +00:00
|
|
|
|
|
|
|
oracleRequestPrice = 5000_0000
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
prefixIDList = []byte{6}
|
|
|
|
prefixRequest = []byte{7}
|
|
|
|
prefixRequestID = []byte{9}
|
|
|
|
)
|
|
|
|
|
|
|
|
// Various validation errors.
|
|
|
|
var (
|
|
|
|
ErrBigArgument = errors.New("some of the arguments are invalid")
|
|
|
|
ErrInvalidWitness = errors.New("witness check failed")
|
|
|
|
ErrNotEnoughGas = errors.New("gas limit exceeded")
|
|
|
|
ErrRequestNotFound = errors.New("oracle request not found")
|
|
|
|
ErrResponseNotFound = errors.New("oracle response not found")
|
|
|
|
)
|
|
|
|
|
|
|
|
func newOracle() *Oracle {
|
2021-01-15 21:17:31 +00:00
|
|
|
o := &Oracle{ContractMD: *interop.NewContractMD(nativenames.Oracle, oracleContractID)}
|
2020-09-18 13:26:36 +00:00
|
|
|
|
2021-01-22 08:28:13 +00:00
|
|
|
w := io.NewBufBinWriter()
|
2020-09-25 14:39:11 +00:00
|
|
|
emit.Opcodes(w.BinWriter, opcode.NEWARRAY0)
|
2021-01-22 08:28:13 +00:00
|
|
|
emit.Int(w.BinWriter, int64(callflag.All))
|
|
|
|
emit.String(w.BinWriter, "finish")
|
|
|
|
emit.Bytes(w.BinWriter, o.Hash.BytesBE())
|
|
|
|
emit.Syscall(w.BinWriter, interopnames.SystemContractCall)
|
|
|
|
o.oracleScript = w.Bytes()
|
|
|
|
|
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-02-25 15:04:46 +00:00
|
|
|
md := newMethodAndPrice(o.request, oracleRequestPrice, 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)
|
2020-12-29 10:45:49 +00:00
|
|
|
md = newMethodAndPrice(o.verify, 100_0000, 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))
|
|
|
|
|
2020-09-18 13:26:36 +00:00
|
|
|
return o
|
|
|
|
}
|
|
|
|
|
2021-01-22 08:28:13 +00:00
|
|
|
// GetOracleResponseScript returns script for transaction with oracle response.
|
|
|
|
func (o *Oracle) GetOracleResponseScript() []byte {
|
|
|
|
b := make([]byte, len(o.oracleScript))
|
|
|
|
copy(b, o.oracleScript)
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2020-12-13 18:36:06 +00:00
|
|
|
// OnPersist implements Contract interface.
|
|
|
|
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 {
|
|
|
|
var nodes keys.PublicKeys
|
|
|
|
var reward []big.Int
|
|
|
|
single := new(big.Int).SetUint64(oracleRequestPrice)
|
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)
|
2020-09-18 13:26:36 +00:00
|
|
|
if err := o.getSerializableFromDAO(ic.DAO, reqKey, req); err != nil {
|
2020-12-01 08:18:59 +00:00
|
|
|
continue
|
2020-09-18 13:26:36 +00:00
|
|
|
}
|
2021-02-09 09:26:25 +00:00
|
|
|
if err := ic.DAO.DeleteStorageItem(o.ID, reqKey); err != nil {
|
2020-09-18 13:26:36 +00:00
|
|
|
return err
|
|
|
|
}
|
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)
|
|
|
|
if err := o.getSerializableFromDAO(ic.DAO, idKey, idList); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !idList.Remove(resp.ID) {
|
|
|
|
return errors.New("response ID wasn't found")
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
if len(*idList) == 0 {
|
2021-02-09 09:26:25 +00:00
|
|
|
err = ic.DAO.DeleteStorageItem(o.ID, idKey)
|
2020-09-18 13:26:36 +00:00
|
|
|
} else {
|
|
|
|
si := &state.StorageItem{Value: idList.Bytes()}
|
2021-02-09 09:26:25 +00:00
|
|
|
err = ic.DAO.PutStorageItem(o.ID, idKey, si)
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize initializes Oracle contract.
|
|
|
|
func (o *Oracle) Initialize(ic *interop.Context) error {
|
2021-02-09 09:26:25 +00:00
|
|
|
return setIntWithKey(o.ID, ic.DAO, prefixRequestID, 0)
|
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{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FinishInternal processes oracle response.
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-11-05 19:51:08 +00:00
|
|
|
ic.Notifications = append(ic.Notifications, state.NotificationEvent{
|
|
|
|
ScriptHash: o.Hash,
|
|
|
|
Name: "OracleResponse",
|
|
|
|
Item: stackitem.NewArray([]stackitem.Item{
|
|
|
|
stackitem.Make(resp.ID),
|
|
|
|
stackitem.Make(req.OriginalTxID.BytesBE()),
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
|
2020-09-18 13:26:36 +00:00
|
|
|
r := io.NewBinReaderFromBuf(req.UserData)
|
|
|
|
userData := stackitem.DecodeBinaryStackItem(r)
|
2020-12-09 12:16:49 +00:00
|
|
|
args := []stackitem.Item{
|
2020-09-18 13:26:36 +00:00
|
|
|
stackitem.Make(req.URL),
|
|
|
|
stackitem.Make(userData),
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
if err := o.RequestInternal(ic, url, filter, cb, userData, gas); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return stackitem.Null{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RequestInternal processes 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 {
|
|
|
|
if len(url) > maxURLLength || (filter != nil && len(*filter) > maxFilterLength) || len(cb) > maxCallbackLength || gas.Uint64() < 1000_0000 {
|
2020-09-18 13:26:36 +00:00
|
|
|
return ErrBigArgument
|
|
|
|
}
|
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-02-02 15:46:43 +00:00
|
|
|
itemID := bigint.FromBytes(si.Value)
|
|
|
|
id := itemID.Uint64()
|
|
|
|
itemID.Add(itemID, intOne)
|
|
|
|
si.Value = bigint.ToPreallocatedBytes(itemID, si.Value)
|
2021-02-09 09:26:25 +00:00
|
|
|
if err := ic.DAO.PutStorageItem(o.ID, prefixRequestID, si); err != nil {
|
2020-09-18 13:26:36 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should be executed from 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
|
|
|
|
}
|
|
|
|
|
|
|
|
w := io.NewBufBinWriter()
|
|
|
|
stackitem.EncodeBinaryStackItem(userData, w.BinWriter)
|
|
|
|
if w.Err != nil {
|
|
|
|
return w.Err
|
|
|
|
}
|
|
|
|
data := w.Bytes()
|
|
|
|
if len(data) > maxUserDataLength {
|
|
|
|
return ErrBigArgument
|
|
|
|
}
|
|
|
|
|
2020-11-18 20:59:13 +00:00
|
|
|
var filterNotif stackitem.Item
|
|
|
|
if filter != nil {
|
|
|
|
filterNotif = stackitem.Make(*filter)
|
|
|
|
} else {
|
|
|
|
filterNotif = stackitem.Null{}
|
|
|
|
}
|
2020-11-05 19:51:08 +00:00
|
|
|
ic.Notifications = append(ic.Notifications, state.NotificationEvent{
|
|
|
|
ScriptHash: o.Hash,
|
|
|
|
Name: "OracleRequest",
|
|
|
|
Item: stackitem.NewArray([]stackitem.Item{
|
|
|
|
stackitem.Make(id),
|
|
|
|
stackitem.Make(ic.VM.GetCallingScriptHash().BytesBE()),
|
|
|
|
stackitem.Make(url),
|
2020-11-18 20:59:13 +00:00
|
|
|
filterNotif,
|
2020-11-05 19:51:08 +00:00
|
|
|
}),
|
|
|
|
})
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutRequestInternal puts oracle request with the specified id to d.
|
2020-10-07 12:06:10 +00:00
|
|
|
func (o *Oracle) PutRequestInternal(id uint64, req *state.OracleRequest, d dao.DAO) error {
|
2020-09-18 13:26:36 +00:00
|
|
|
reqItem := &state.StorageItem{Value: req.Bytes()}
|
|
|
|
reqKey := makeRequestKey(id)
|
2021-02-09 09:26:25 +00:00
|
|
|
if err := d.PutStorageItem(o.ID, reqKey, reqItem); 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)
|
|
|
|
if err := o.getSerializableFromDAO(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)
|
2020-09-24 13:33:40 +00:00
|
|
|
si := &state.StorageItem{Value: lst.Bytes()}
|
2021-02-09 09:26:25 +00:00
|
|
|
return d.PutStorageItem(o.ID, key, si)
|
2020-09-18 13:26:36 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 12:50:52 +00:00
|
|
|
// GetScriptHash returns script hash or oracle nodes.
|
2020-11-05 16:34:48 +00:00
|
|
|
func (o *Oracle) GetScriptHash(d dao.DAO) (util.Uint160, error) {
|
2021-01-18 12:52:51 +00:00
|
|
|
return o.Desig.GetLastDesignatedHash(d, RoleOracle)
|
2020-09-24 12:50:52 +00:00
|
|
|
}
|
|
|
|
|
2020-10-01 15:17:09 +00:00
|
|
|
// GetOracleNodes returns public keys of oracle nodes.
|
|
|
|
func (o *Oracle) GetOracleNodes(d dao.DAO) (keys.PublicKeys, error) {
|
2020-11-05 16:34:48 +00:00
|
|
|
nodes, _, err := o.Desig.GetDesignatedByRole(d, RoleOracle, math.MaxUint32)
|
|
|
|
return nodes, err
|
2020-09-18 13:26:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetRequestInternal returns request by ID and key under which it is stored.
|
2020-10-07 12:06:10 +00:00
|
|
|
func (o *Oracle) GetRequestInternal(d dao.DAO, 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)
|
2020-09-18 13:26:36 +00:00
|
|
|
return req, o.getSerializableFromDAO(d, key, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetIDListInternal returns request by ID and key under which it is stored.
|
|
|
|
func (o *Oracle) GetIDListInternal(d dao.DAO, url string) (*IDList, error) {
|
|
|
|
key := makeIDListKey(url)
|
|
|
|
idList := new(IDList)
|
|
|
|
return idList, o.getSerializableFromDAO(d, key, idList)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Oracle) verify(ic *interop.Context, _ []stackitem.Item) stackitem.Item {
|
|
|
|
return stackitem.NewBool(ic.Tx.HasAttribute(transaction.OracleResponseT))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Oracle) getOriginalTxID(d dao.DAO, tx *transaction.Transaction) util.Uint256 {
|
|
|
|
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.
|
|
|
|
func (o *Oracle) getRequests(d dao.DAO) (map[uint64]*state.OracleRequest, error) {
|
2021-02-09 09:26:25 +00:00
|
|
|
m, err := d.GetStorageItemsWithPrefix(o.ID, prefixRequest)
|
2020-09-28 11:58:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
reqs := make(map[uint64]*state.OracleRequest, len(m))
|
|
|
|
for k, si := range m {
|
|
|
|
if len(k) != 8 {
|
|
|
|
return nil, errors.New("invalid request ID")
|
|
|
|
}
|
|
|
|
r := io.NewBinReaderFromBuf(si.Value)
|
|
|
|
req := new(state.OracleRequest)
|
|
|
|
req.DecodeBinary(r)
|
|
|
|
if r.Err != nil {
|
|
|
|
return nil, r.Err
|
|
|
|
}
|
2021-02-03 14:02:36 +00:00
|
|
|
id := binary.BigEndian.Uint64([]byte(k))
|
2020-09-28 11:58:04 +00:00
|
|
|
reqs[id] = req
|
|
|
|
}
|
|
|
|
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()...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Oracle) getSerializableFromDAO(d dao.DAO, key []byte, item io.Serializable) error {
|
2021-02-09 09:26:25 +00:00
|
|
|
return getSerializableFromDAO(o.ID, d, key, item)
|
2020-09-24 12:46:31 +00:00
|
|
|
}
|
2020-09-28 11:58:04 +00:00
|
|
|
|
|
|
|
// updateCache updates cached Oracle values if they've been changed
|
|
|
|
func (o *Oracle) updateCache(d dao.DAO) error {
|
|
|
|
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
|
|
|
|
}
|