2020-09-18 13:26:36 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"math/big"
|
|
|
|
"testing"
|
|
|
|
|
2020-11-23 11:09:00 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/internal/testchain"
|
2020-09-18 13:26:36 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
|
2020-11-05 16:34:48 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/block"
|
2020-09-18 13:26:36 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop/interopnames"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/native"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
2020-11-18 20:10:48 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
2020-09-18 13:26:36 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
"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"
|
2021-01-13 12:34:10 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/nef"
|
2020-09-18 13:26:36 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2020-11-18 20:59:13 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
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/opcode"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
// getTestContractState returns test contract which uses oracles.
|
|
|
|
func getOracleContractState(h util.Uint160) *state.Contract {
|
|
|
|
w := io.NewBufBinWriter()
|
|
|
|
emit.Int(w.BinWriter, 5)
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(w.BinWriter, opcode.PACK)
|
2020-12-29 10:44:07 +00:00
|
|
|
emit.Int(w.BinWriter, int64(callflag.All))
|
2020-09-18 13:26:36 +00:00
|
|
|
emit.String(w.BinWriter, "request")
|
|
|
|
emit.Bytes(w.BinWriter, h.BytesBE())
|
|
|
|
emit.Syscall(w.BinWriter, interopnames.SystemContractCall)
|
2020-12-29 10:44:07 +00:00
|
|
|
emit.Opcodes(w.BinWriter, opcode.DROP)
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(w.BinWriter, opcode.RET)
|
2020-09-18 13:26:36 +00:00
|
|
|
|
|
|
|
// `handle` method aborts if len(userData) == 2
|
|
|
|
offset := w.Len()
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(w.BinWriter, opcode.OVER)
|
|
|
|
emit.Opcodes(w.BinWriter, opcode.SIZE)
|
2020-09-18 13:26:36 +00:00
|
|
|
emit.Int(w.BinWriter, 2)
|
|
|
|
emit.Instruction(w.BinWriter, opcode.JMPNE, []byte{3})
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(w.BinWriter, opcode.ABORT)
|
2020-09-18 13:26:36 +00:00
|
|
|
emit.Int(w.BinWriter, 4) // url, userData, code, result
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(w.BinWriter, opcode.PACK)
|
2020-09-18 13:26:36 +00:00
|
|
|
emit.Syscall(w.BinWriter, interopnames.SystemBinarySerialize)
|
|
|
|
emit.String(w.BinWriter, "lastOracleResponse")
|
|
|
|
emit.Syscall(w.BinWriter, interopnames.SystemStorageGetContext)
|
|
|
|
emit.Syscall(w.BinWriter, interopnames.SystemStoragePut)
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(w.BinWriter, opcode.RET)
|
2020-09-18 13:26:36 +00:00
|
|
|
|
2020-11-18 09:43:51 +00:00
|
|
|
m := manifest.NewManifest("TestOracle")
|
2020-09-18 13:26:36 +00:00
|
|
|
m.ABI.Methods = []manifest.Method{
|
|
|
|
{
|
|
|
|
Name: "requestURL",
|
|
|
|
Offset: 0,
|
|
|
|
Parameters: []manifest.Parameter{
|
|
|
|
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),
|
|
|
|
},
|
|
|
|
ReturnType: smartcontract.VoidType,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "handle",
|
|
|
|
Offset: offset,
|
|
|
|
Parameters: []manifest.Parameter{
|
|
|
|
manifest.NewParameter("url", smartcontract.StringType),
|
|
|
|
manifest.NewParameter("userData", smartcontract.AnyType),
|
|
|
|
manifest.NewParameter("code", smartcontract.IntegerType),
|
|
|
|
manifest.NewParameter("result", smartcontract.ByteArrayType),
|
|
|
|
},
|
|
|
|
ReturnType: smartcontract.VoidType,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
perm := manifest.NewPermission(manifest.PermissionHash, h)
|
|
|
|
perm.Methods.Add("request")
|
|
|
|
m.Permissions = append(m.Permissions, *perm)
|
|
|
|
|
2020-11-18 20:10:48 +00:00
|
|
|
script := w.Bytes()
|
2021-01-13 12:34:10 +00:00
|
|
|
ne, err := nef.NewFile(script)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-09-18 13:26:36 +00:00
|
|
|
return &state.Contract{
|
2021-01-13 12:34:10 +00:00
|
|
|
NEF: *ne,
|
2020-11-18 20:10:48 +00:00
|
|
|
Hash: hash.Hash160(script),
|
2020-09-18 13:26:36 +00:00
|
|
|
Manifest: *m,
|
|
|
|
ID: 42,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func putOracleRequest(t *testing.T, h util.Uint160, bc *Blockchain,
|
2020-12-17 08:41:28 +00:00
|
|
|
url string, filter *string, cb string, userData []byte, gas int64) util.Uint256 {
|
2020-11-18 20:59:13 +00:00
|
|
|
var filtItem interface{}
|
|
|
|
if filter != nil {
|
|
|
|
filtItem = *filter
|
|
|
|
}
|
2020-11-25 15:10:14 +00:00
|
|
|
res, err := invokeContractMethod(bc, gas+50_000_000+5_000_000, h, "requestURL",
|
2020-12-17 08:41:28 +00:00
|
|
|
url, filtItem, cb, userData, gas)
|
2020-11-25 15:10:14 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
return res.Container
|
2020-09-18 13:26:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestOracle_Request(t *testing.T) {
|
|
|
|
bc := newTestChain(t)
|
|
|
|
defer bc.Close()
|
|
|
|
|
|
|
|
orc := bc.contracts.Oracle
|
|
|
|
cs := getOracleContractState(orc.Hash)
|
2020-12-13 15:26:35 +00:00
|
|
|
require.NoError(t, bc.contracts.Management.PutContractState(bc.dao, cs))
|
2020-09-18 13:26:36 +00:00
|
|
|
|
|
|
|
gasForResponse := int64(2000_1234)
|
2020-11-18 20:59:13 +00:00
|
|
|
var filter = "flt"
|
2020-09-18 13:26:36 +00:00
|
|
|
userData := []byte("custom info")
|
2020-12-17 08:41:28 +00:00
|
|
|
txHash := putOracleRequest(t, cs.Hash, bc, "url", &filter, "handle", userData, gasForResponse)
|
2020-09-18 13:26:36 +00:00
|
|
|
|
2021-02-02 15:46:43 +00:00
|
|
|
req, err := orc.GetRequestInternal(bc.dao, 0)
|
2020-09-18 13:26:36 +00:00
|
|
|
require.NotNil(t, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, txHash, req.OriginalTxID)
|
|
|
|
require.Equal(t, "url", req.URL)
|
2020-11-18 20:59:13 +00:00
|
|
|
require.Equal(t, filter, *req.Filter)
|
2020-11-18 20:10:48 +00:00
|
|
|
require.Equal(t, cs.Hash, req.CallbackContract)
|
2020-09-18 13:26:36 +00:00
|
|
|
require.Equal(t, "handle", req.CallbackMethod)
|
|
|
|
require.Equal(t, uint64(gasForResponse), req.GasForResponse)
|
|
|
|
|
|
|
|
idList, err := orc.GetIDListInternal(bc.dao, "url")
|
|
|
|
require.NoError(t, err)
|
2021-02-02 15:46:43 +00:00
|
|
|
require.Equal(t, &native.IDList{0}, idList)
|
2020-09-18 13:26:36 +00:00
|
|
|
|
|
|
|
// Finish.
|
|
|
|
priv, err := keys.NewPrivateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
pub := priv.PublicKey()
|
|
|
|
|
|
|
|
tx := transaction.New(netmode.UnitTestNet, []byte{}, 0)
|
2020-11-17 12:57:50 +00:00
|
|
|
bl := block.New(netmode.UnitTestNet, bc.config.StateRootInHeader)
|
2020-11-05 16:34:48 +00:00
|
|
|
bl.Index = bc.BlockHeight() + 1
|
2020-09-18 13:26:36 +00:00
|
|
|
setSigner(tx, testchain.CommitteeScriptHash())
|
2020-11-05 16:34:48 +00:00
|
|
|
ic := bc.newInteropContext(trigger.Application, bc.dao, bl, tx)
|
2020-09-29 06:56:19 +00:00
|
|
|
ic.SpawnVM()
|
|
|
|
ic.VM.LoadScript([]byte{byte(opcode.RET)})
|
2020-10-01 15:17:09 +00:00
|
|
|
err = bc.contracts.Designate.DesignateAsRole(ic, native.RoleOracle, keys.PublicKeys{pub})
|
2020-09-18 13:26:36 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-01-22 08:28:13 +00:00
|
|
|
tx = transaction.New(netmode.UnitTestNet, orc.GetOracleResponseScript(), 0)
|
2020-09-18 13:26:36 +00:00
|
|
|
ic.Tx = tx
|
|
|
|
ic.Block = bc.newBlock(tx)
|
|
|
|
|
|
|
|
err = orc.FinishInternal(ic)
|
|
|
|
require.True(t, errors.Is(err, native.ErrResponseNotFound), "got: %v", err)
|
|
|
|
|
|
|
|
resp := &transaction.OracleResponse{
|
2021-02-02 15:46:43 +00:00
|
|
|
ID: 12,
|
2020-09-18 13:26:36 +00:00
|
|
|
Code: transaction.Success,
|
|
|
|
Result: []byte{4, 8, 15, 16, 23, 42},
|
|
|
|
}
|
|
|
|
tx.Attributes = []transaction.Attribute{{
|
|
|
|
Type: transaction.OracleResponseT,
|
|
|
|
Value: resp,
|
|
|
|
}}
|
|
|
|
err = orc.FinishInternal(ic)
|
|
|
|
require.True(t, errors.Is(err, native.ErrRequestNotFound), "got: %v", err)
|
|
|
|
|
|
|
|
// We need to ensure that callback is called thus, executing full script is necessary.
|
2021-02-02 15:46:43 +00:00
|
|
|
resp.ID = 0
|
2020-12-29 10:45:49 +00:00
|
|
|
ic.VM.LoadScriptWithFlags(tx.Script, callflag.All)
|
2020-09-18 13:26:36 +00:00
|
|
|
require.NoError(t, ic.VM.Run())
|
|
|
|
|
|
|
|
si := ic.DAO.GetStorageItem(cs.ID, []byte("lastOracleResponse"))
|
|
|
|
require.NotNil(t, si)
|
|
|
|
item, err := stackitem.DeserializeItem(si.Value)
|
|
|
|
require.NoError(t, err)
|
|
|
|
arr, ok := item.Value().([]stackitem.Item)
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, []byte("url"), arr[0].Value())
|
|
|
|
require.Equal(t, userData, arr[1].Value())
|
|
|
|
require.Equal(t, big.NewInt(int64(resp.Code)), arr[2].Value())
|
|
|
|
require.Equal(t, resp.Result, arr[3].Value())
|
|
|
|
|
|
|
|
// Check that processed request is removed during `postPersist`.
|
2021-02-02 15:46:43 +00:00
|
|
|
_, err = orc.GetRequestInternal(ic.DAO, 0)
|
2020-09-18 13:26:36 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.NoError(t, orc.PostPersist(ic))
|
2021-02-02 15:46:43 +00:00
|
|
|
_, err = orc.GetRequestInternal(ic.DAO, 0)
|
2020-09-18 13:26:36 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
t.Run("ErrorOnFinish", func(t *testing.T) {
|
2021-02-02 15:46:43 +00:00
|
|
|
const reqID = 1
|
2020-09-18 13:26:36 +00:00
|
|
|
|
2020-12-17 08:41:28 +00:00
|
|
|
putOracleRequest(t, cs.Hash, bc, "url", nil, "handle", []byte{1, 2}, gasForResponse)
|
2021-02-02 15:46:43 +00:00
|
|
|
_, err := orc.GetRequestInternal(bc.dao, reqID) // ensure ID is 1
|
2020-09-18 13:26:36 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-01-22 08:28:13 +00:00
|
|
|
tx = transaction.New(netmode.UnitTestNet, orc.GetOracleResponseScript(), 0)
|
2020-09-18 13:26:36 +00:00
|
|
|
tx.Attributes = []transaction.Attribute{{
|
|
|
|
Type: transaction.OracleResponseT,
|
|
|
|
Value: &transaction.OracleResponse{
|
|
|
|
ID: reqID,
|
|
|
|
Code: transaction.Success,
|
|
|
|
Result: []byte{4, 8, 15, 16, 23, 42},
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
ic := bc.newInteropContext(trigger.Application, bc.dao, bc.newBlock(tx), tx)
|
|
|
|
ic.VM = ic.SpawnVM()
|
2020-12-29 10:45:49 +00:00
|
|
|
ic.VM.LoadScriptWithFlags(tx.Script, callflag.All)
|
2020-09-18 13:26:36 +00:00
|
|
|
require.Error(t, ic.VM.Run())
|
|
|
|
|
|
|
|
// Request is cleaned up even if callback failed.
|
|
|
|
require.NoError(t, orc.PostPersist(ic))
|
|
|
|
_, err = orc.GetRequestInternal(ic.DAO, reqID)
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
2020-11-18 20:59:13 +00:00
|
|
|
t.Run("BadRequest", func(t *testing.T) {
|
2020-12-17 08:41:28 +00:00
|
|
|
var doBadRequest = func(t *testing.T, h util.Uint160, url string, filter *string, cb string, userData []byte, gas int64) {
|
|
|
|
txHash := putOracleRequest(t, h, bc, url, filter, cb, userData, gas)
|
2020-11-18 20:59:13 +00:00
|
|
|
aer, err := bc.GetAppExecResults(txHash, trigger.Application)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, len(aer))
|
|
|
|
require.Equal(t, vm.FaultState, aer[0].VMState)
|
|
|
|
}
|
|
|
|
t.Run("non-UTF8 url", func(t *testing.T) {
|
2020-12-17 08:41:28 +00:00
|
|
|
doBadRequest(t, cs.Hash, "\xff", nil, "", []byte{1, 2}, gasForResponse)
|
2020-11-18 20:59:13 +00:00
|
|
|
})
|
|
|
|
t.Run("non-UTF8 filter", func(t *testing.T) {
|
|
|
|
var f = "\xff"
|
2020-12-17 08:41:28 +00:00
|
|
|
doBadRequest(t, cs.Hash, "url", &f, "", []byte{1, 2}, gasForResponse)
|
2020-11-18 20:59:13 +00:00
|
|
|
})
|
|
|
|
t.Run("not enough gas", func(t *testing.T) {
|
2020-12-17 08:41:28 +00:00
|
|
|
doBadRequest(t, cs.Hash, "url", nil, "", nil, 1000)
|
|
|
|
})
|
|
|
|
t.Run("disallowed callback", func(t *testing.T) {
|
|
|
|
doBadRequest(t, cs.Hash, "url", nil, "_deploy", nil, 1000_0000)
|
2020-11-18 20:59:13 +00:00
|
|
|
})
|
|
|
|
})
|
2020-09-18 13:26:36 +00:00
|
|
|
}
|