2020-03-19 15:52:37 +00:00
|
|
|
package native
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2020-04-20 17:38:47 +00:00
|
|
|
"fmt"
|
2020-03-19 15:52:37 +00:00
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop"
|
2020-09-21 14:34:40 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
2020-12-13 18:36:06 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger"
|
2020-03-19 15:52:37 +00:00
|
|
|
)
|
|
|
|
|
2020-07-22 16:03:05 +00:00
|
|
|
// Call calls specified native contract method.
|
2020-08-07 11:37:49 +00:00
|
|
|
func Call(ic *interop.Context) error {
|
|
|
|
name := ic.VM.Estack().Pop().String()
|
2020-07-22 16:03:05 +00:00
|
|
|
var c interop.Contract
|
|
|
|
for _, ctr := range ic.Natives {
|
|
|
|
if ctr.Metadata().Name == name {
|
|
|
|
c = ctr
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if c == nil {
|
|
|
|
return fmt.Errorf("native contract %s not found", name)
|
|
|
|
}
|
2020-08-07 11:37:49 +00:00
|
|
|
h := ic.VM.GetCurrentScriptHash()
|
2020-07-22 16:03:05 +00:00
|
|
|
if !h.Equals(c.Metadata().Hash) {
|
|
|
|
return errors.New("it is not allowed to use Neo.Native.Call directly to call native contracts. System.Contract.Call should be used")
|
|
|
|
}
|
2020-08-07 11:37:49 +00:00
|
|
|
operation := ic.VM.Estack().Pop().String()
|
|
|
|
args := ic.VM.Estack().Pop().Array()
|
2020-07-22 16:03:05 +00:00
|
|
|
m, ok := c.Metadata().Methods[operation]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("method %s not found", operation)
|
|
|
|
}
|
2020-08-07 11:37:49 +00:00
|
|
|
if !ic.VM.Context().GetCallFlags().Has(m.RequiredFlags) {
|
2020-12-11 13:37:13 +00:00
|
|
|
return fmt.Errorf("missing call flags for native %s `%s` operation call: %05b vs %05b", name, operation, ic.VM.Context().GetCallFlags(), m.RequiredFlags)
|
2020-07-22 16:03:05 +00:00
|
|
|
}
|
2020-08-07 11:37:49 +00:00
|
|
|
if !ic.VM.AddGas(m.Price) {
|
2020-07-22 16:03:05 +00:00
|
|
|
return errors.New("gas limit exceeded")
|
|
|
|
}
|
2020-11-19 15:02:21 +00:00
|
|
|
ctx := ic.VM.Context()
|
2020-07-22 16:03:05 +00:00
|
|
|
result := m.Func(ic, args)
|
2020-09-21 14:34:40 +00:00
|
|
|
if m.MD.ReturnType != smartcontract.VoidType {
|
2020-11-19 15:02:21 +00:00
|
|
|
ctx.Estack().PushVal(result)
|
2020-09-21 14:34:40 +00:00
|
|
|
}
|
2020-07-22 16:03:05 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-12-13 18:36:06 +00:00
|
|
|
|
|
|
|
// OnPersist calls OnPersist methods for all native contracts.
|
|
|
|
func OnPersist(ic *interop.Context) error {
|
|
|
|
if ic.Trigger != trigger.OnPersist {
|
|
|
|
return errors.New("onPersist must be trigered by system")
|
|
|
|
}
|
|
|
|
for _, c := range ic.Natives {
|
|
|
|
err := c.OnPersist(ic)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PostPersist calls PostPersist methods for all native contracts.
|
|
|
|
func PostPersist(ic *interop.Context) error {
|
|
|
|
if ic.Trigger != trigger.PostPersist {
|
|
|
|
return errors.New("postPersist must be trigered by system")
|
|
|
|
}
|
|
|
|
for _, c := range ic.Natives {
|
|
|
|
err := c.PostPersist(ic)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|