2020-09-21 14:00:33 +00:00
|
|
|
package contract
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop"
|
2020-10-02 09:56:51 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
2020-12-29 10:44:07 +00:00
|
|
|
"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-21 14:00:33 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2020-10-12 11:32:27 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
2020-09-21 14:00:33 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
|
|
|
)
|
|
|
|
|
2021-01-19 08:23:39 +00:00
|
|
|
// LoadToken calls method specified by token id.
|
|
|
|
func LoadToken(ic *interop.Context) func(id int32) error {
|
|
|
|
return func(id int32) error {
|
|
|
|
ctx := ic.VM.Context()
|
2021-02-25 15:14:46 +00:00
|
|
|
if !ctx.GetCallFlags().Has(callflag.ReadStates | callflag.AllowCall) {
|
|
|
|
return errors.New("invalid call flags")
|
|
|
|
}
|
2021-01-19 08:23:39 +00:00
|
|
|
tok := ctx.NEF.Tokens[id]
|
|
|
|
if int(tok.ParamCount) > ctx.Estack().Len() {
|
|
|
|
return errors.New("stack is too small")
|
|
|
|
}
|
|
|
|
args := make([]stackitem.Item, tok.ParamCount)
|
|
|
|
for i := range args {
|
|
|
|
args[i] = ic.VM.Estack().Pop().Item()
|
|
|
|
}
|
|
|
|
cs, err := ic.GetContract(tok.Hash)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("contract not found: %w", err)
|
|
|
|
}
|
|
|
|
return callInternal(ic, cs, tok.Method, tok.CallFlag, tok.HasReturn, args)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-29 10:44:07 +00:00
|
|
|
// Call calls a contract with flags.
|
2020-09-21 14:00:33 +00:00
|
|
|
func Call(ic *interop.Context) error {
|
|
|
|
h := ic.VM.Estack().Pop().Bytes()
|
|
|
|
method := ic.VM.Estack().Pop().String()
|
2020-12-29 10:45:49 +00:00
|
|
|
fs := callflag.CallFlag(int32(ic.VM.Estack().Pop().BigInt().Int64()))
|
|
|
|
if fs&^callflag.All != 0 {
|
2020-09-21 14:00:33 +00:00
|
|
|
return errors.New("call flags out of range")
|
|
|
|
}
|
2020-12-29 10:44:07 +00:00
|
|
|
args := ic.VM.Estack().Pop().Array()
|
2020-09-21 14:00:33 +00:00
|
|
|
u, err := util.Uint160DecodeBytesBE(h)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("invalid contract hash")
|
|
|
|
}
|
2020-12-13 15:26:35 +00:00
|
|
|
cs, err := ic.GetContract(u)
|
2020-09-21 14:00:33 +00:00
|
|
|
if err != nil {
|
2020-12-13 15:26:35 +00:00
|
|
|
return fmt.Errorf("contract not found: %w", err)
|
2020-09-21 14:00:33 +00:00
|
|
|
}
|
2021-01-19 08:23:39 +00:00
|
|
|
if strings.HasPrefix(method, "_") {
|
2020-09-21 14:00:33 +00:00
|
|
|
return errors.New("invalid method name (starts with '_')")
|
|
|
|
}
|
2021-01-26 14:37:34 +00:00
|
|
|
md := cs.Manifest.ABI.GetMethod(method, len(args))
|
2020-12-08 10:27:41 +00:00
|
|
|
if md == nil {
|
|
|
|
return errors.New("method not found")
|
|
|
|
}
|
2020-12-29 10:44:07 +00:00
|
|
|
hasReturn := md.ReturnType != smartcontract.VoidType
|
|
|
|
if !hasReturn {
|
|
|
|
ic.VM.Estack().PushVal(stackitem.Null{})
|
|
|
|
}
|
2021-01-19 08:23:39 +00:00
|
|
|
return callInternal(ic, cs, method, fs, hasReturn, args)
|
|
|
|
}
|
|
|
|
|
|
|
|
func callInternal(ic *interop.Context, cs *state.Contract, name string, f callflag.CallFlag,
|
|
|
|
hasReturn bool, args []stackitem.Item) error {
|
2021-01-26 14:37:34 +00:00
|
|
|
md := cs.Manifest.ABI.GetMethod(name, len(args))
|
2020-12-08 10:27:41 +00:00
|
|
|
if md.Safe {
|
2021-03-16 19:47:49 +00:00
|
|
|
f &^= (callflag.WriteStates | callflag.AllowNotify)
|
2020-12-08 10:27:41 +00:00
|
|
|
} else if ctx := ic.VM.Context(); ctx != nil && ctx.IsDeployed() {
|
2020-12-13 15:26:35 +00:00
|
|
|
curr, err := ic.GetContract(ic.VM.GetCurrentScriptHash())
|
2020-11-26 20:02:00 +00:00
|
|
|
if err == nil {
|
2021-01-19 08:23:39 +00:00
|
|
|
if !curr.Manifest.CanCall(cs.Hash, &cs.Manifest, name) {
|
2020-11-26 20:02:00 +00:00
|
|
|
return errors.New("disallowed method call")
|
|
|
|
}
|
2020-09-21 14:00:33 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-29 10:44:07 +00:00
|
|
|
return callExFromNative(ic, ic.VM.GetCurrentScriptHash(), cs, name, args, f, hasReturn)
|
2020-12-09 12:16:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// callExFromNative calls a contract with flags using provided calling hash.
|
|
|
|
func callExFromNative(ic *interop.Context, caller util.Uint160, cs *state.Contract,
|
2020-12-29 10:44:07 +00:00
|
|
|
name string, args []stackitem.Item, f callflag.CallFlag, hasReturn bool) error {
|
2021-01-26 14:37:34 +00:00
|
|
|
md := cs.Manifest.ABI.GetMethod(name, len(args))
|
2020-10-02 09:56:51 +00:00
|
|
|
if md == nil {
|
|
|
|
return fmt.Errorf("method '%s' not found", name)
|
|
|
|
}
|
2020-09-21 14:00:33 +00:00
|
|
|
|
|
|
|
if len(args) != len(md.Parameters) {
|
|
|
|
return fmt.Errorf("invalid argument count: %d (expected %d)", len(args), len(md.Parameters))
|
|
|
|
}
|
|
|
|
|
2020-11-18 20:10:48 +00:00
|
|
|
ic.VM.Invocations[cs.Hash]++
|
2021-02-15 13:40:44 +00:00
|
|
|
ic.VM.LoadScriptWithCallingHash(caller, cs.NEF.Script, cs.Hash, ic.VM.Context().GetCallFlags()&f, hasReturn, uint16(len(args)))
|
2021-01-19 08:23:39 +00:00
|
|
|
ic.VM.Context().NEF = &cs.NEF
|
2020-12-29 10:44:07 +00:00
|
|
|
for i := len(args) - 1; i >= 0; i-- {
|
|
|
|
ic.VM.Estack().PushVal(args[i])
|
|
|
|
}
|
2021-02-15 13:40:44 +00:00
|
|
|
// use Jump not Call here because context was loaded in LoadScript above.
|
|
|
|
ic.VM.Jump(ic.VM.Context(), md.Offset)
|
2020-12-29 10:44:07 +00:00
|
|
|
if hasReturn {
|
|
|
|
ic.VM.Context().RetCount = 1
|
|
|
|
} else {
|
|
|
|
ic.VM.Context().RetCount = 0
|
|
|
|
}
|
2020-09-21 14:00:33 +00:00
|
|
|
|
2021-01-26 14:37:34 +00:00
|
|
|
md = cs.Manifest.ABI.GetMethod(manifest.MethodInit, 0)
|
2020-09-21 14:00:33 +00:00
|
|
|
if md != nil {
|
|
|
|
ic.VM.Call(ic.VM.Context(), md.Offset)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-12-09 12:16:49 +00:00
|
|
|
|
|
|
|
// ErrNativeCall is returned for failed calls from native.
|
|
|
|
var ErrNativeCall = errors.New("error during call from native")
|
|
|
|
|
|
|
|
// CallFromNative performs synchronous call from native contract.
|
2020-12-29 10:44:07 +00:00
|
|
|
func CallFromNative(ic *interop.Context, caller util.Uint160, cs *state.Contract, method string, args []stackitem.Item, hasReturn bool) error {
|
2020-12-09 12:16:49 +00:00
|
|
|
startSize := ic.VM.Istack().Len()
|
2020-12-29 10:44:07 +00:00
|
|
|
if err := callExFromNative(ic, caller, cs, method, args, callflag.All, hasReturn); err != nil {
|
2020-12-09 12:16:49 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for !ic.VM.HasStopped() && ic.VM.Istack().Len() > startSize {
|
|
|
|
if err := ic.VM.Step(); err != nil {
|
|
|
|
return fmt.Errorf("%w: %v", ErrNativeCall, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ic.VM.State() == vm.FaultState {
|
|
|
|
return ErrNativeCall
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|