30526046e7
This was done in https://github.com/neo-project/neo/pull/1693 for native calls. `OnPersist` script still uses `DROP` though as value is pushed via `CheckReturn` logic for regular calls.
89 lines
2.4 KiB
Go
89 lines
2.4 KiB
Go
package contract
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop"
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
|
"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/stackitem"
|
|
)
|
|
|
|
// Call calls a contract.
|
|
func Call(ic *interop.Context) error {
|
|
h := ic.VM.Estack().Pop().Bytes()
|
|
method := ic.VM.Estack().Pop().String()
|
|
args := ic.VM.Estack().Pop().Array()
|
|
return callExInternal(ic, h, method, args, smartcontract.All)
|
|
}
|
|
|
|
// CallEx calls a contract with flags.
|
|
func CallEx(ic *interop.Context) error {
|
|
h := ic.VM.Estack().Pop().Bytes()
|
|
method := ic.VM.Estack().Pop().String()
|
|
args := ic.VM.Estack().Pop().Array()
|
|
flags := smartcontract.CallFlag(int32(ic.VM.Estack().Pop().BigInt().Int64()))
|
|
if flags&^smartcontract.All != 0 {
|
|
return errors.New("call flags out of range")
|
|
}
|
|
return callExInternal(ic, h, method, args, flags)
|
|
}
|
|
|
|
func callExInternal(ic *interop.Context, h []byte, name string, args []stackitem.Item, f smartcontract.CallFlag) error {
|
|
u, err := util.Uint160DecodeBytesBE(h)
|
|
if err != nil {
|
|
return errors.New("invalid contract hash")
|
|
}
|
|
cs, err := ic.DAO.GetContractState(u)
|
|
if err != nil {
|
|
return errors.New("contract not found")
|
|
}
|
|
if strings.HasPrefix(name, "_") {
|
|
return errors.New("invalid method name (starts with '_')")
|
|
}
|
|
md := cs.Manifest.ABI.GetMethod(name)
|
|
if md == nil {
|
|
return fmt.Errorf("method '%s' not found", name)
|
|
}
|
|
curr, err := ic.DAO.GetContractState(ic.VM.GetCurrentScriptHash())
|
|
if err == nil {
|
|
if !curr.Manifest.CanCall(&cs.Manifest, name) {
|
|
return errors.New("disallowed method call")
|
|
}
|
|
}
|
|
|
|
if len(args) != len(md.Parameters) {
|
|
return fmt.Errorf("invalid argument count: %d (expected %d)", len(args), len(md.Parameters))
|
|
}
|
|
|
|
ic.Invocations[u]++
|
|
ic.VM.LoadScriptWithHash(cs.Script, u, ic.VM.Context().GetCallFlags()&f)
|
|
var isNative bool
|
|
for i := range ic.Natives {
|
|
if ic.Natives[i].Metadata().Hash.Equals(u) {
|
|
isNative = true
|
|
break
|
|
}
|
|
}
|
|
if isNative {
|
|
ic.VM.Estack().PushVal(args)
|
|
ic.VM.Estack().PushVal(name)
|
|
} else {
|
|
for i := len(args) - 1; i >= 0; i-- {
|
|
ic.VM.Estack().PushVal(args[i])
|
|
}
|
|
// use Jump not Call here because context was loaded in LoadScript above.
|
|
ic.VM.Jump(ic.VM.Context(), md.Offset)
|
|
}
|
|
ic.VM.Context().CheckReturn = true
|
|
|
|
md = cs.Manifest.ABI.GetMethod(manifest.MethodInit)
|
|
if md != nil {
|
|
ic.VM.Call(ic.VM.Context(), md.Offset)
|
|
}
|
|
|
|
return nil
|
|
}
|