2020-04-08 10:35:39 +00:00
|
|
|
package interop
|
|
|
|
|
|
|
|
import (
|
2020-07-28 13:38:00 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
|
2020-04-08 10:35:39 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/block"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/blockchainer"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/dao"
|
2020-08-14 10:50:52 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop/interopnames"
|
2020-04-08 10:35:39 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
2020-04-13 13:18:28 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto"
|
2020-04-22 20:00:18 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
|
2020-04-08 10:35:39 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger"
|
2020-04-22 20:00:18 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2020-04-08 10:35:39 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
2020-04-22 20:00:18 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
|
2020-06-03 12:55:06 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2020-04-08 10:35:39 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Context represents context in which interops are executed.
|
|
|
|
type Context struct {
|
|
|
|
Chain blockchainer.Blockchainer
|
2020-04-13 13:18:28 +00:00
|
|
|
Container crypto.Verifiable
|
2020-04-22 20:00:18 +00:00
|
|
|
Natives []Contract
|
2020-04-08 10:35:39 +00:00
|
|
|
Trigger trigger.Type
|
|
|
|
Block *block.Block
|
|
|
|
Tx *transaction.Transaction
|
|
|
|
DAO *dao.Cached
|
|
|
|
Notifications []state.NotificationEvent
|
|
|
|
Log *zap.Logger
|
2020-08-07 11:37:49 +00:00
|
|
|
VM *vm.VM
|
2020-07-28 13:38:00 +00:00
|
|
|
Functions [][]Function
|
2020-04-08 10:35:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewContext returns new interop context.
|
2020-04-22 20:00:18 +00:00
|
|
|
func NewContext(trigger trigger.Type, bc blockchainer.Blockchainer, d dao.DAO, natives []Contract, block *block.Block, tx *transaction.Transaction, log *zap.Logger) *Context {
|
2020-04-08 10:35:39 +00:00
|
|
|
dao := dao.NewCached(d)
|
|
|
|
nes := make([]state.NotificationEvent, 0)
|
|
|
|
return &Context{
|
|
|
|
Chain: bc,
|
2020-04-22 20:00:18 +00:00
|
|
|
Natives: natives,
|
2020-04-08 10:35:39 +00:00
|
|
|
Trigger: trigger,
|
|
|
|
Block: block,
|
|
|
|
Tx: tx,
|
|
|
|
DAO: dao,
|
|
|
|
Notifications: nes,
|
|
|
|
Log: log,
|
2020-07-28 13:38:00 +00:00
|
|
|
// Functions is a slice of slices of interops sorted by ID.
|
|
|
|
Functions: [][]Function{},
|
2020-04-08 10:35:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Function binds function name, id with the function itself and price,
|
|
|
|
// it's supposed to be inited once for all interopContexts, so it doesn't use
|
|
|
|
// vm.InteropFuncPrice directly.
|
|
|
|
type Function struct {
|
2020-07-28 10:17:38 +00:00
|
|
|
ID uint32
|
|
|
|
Name string
|
2020-08-07 11:37:49 +00:00
|
|
|
Func func(*Context) error
|
2020-07-28 14:15:23 +00:00
|
|
|
// DisallowCallback is true iff syscall can't be used in a callback.
|
|
|
|
DisallowCallback bool
|
2020-07-28 10:17:38 +00:00
|
|
|
// ParamCount is a number of function parameters.
|
|
|
|
ParamCount int
|
|
|
|
Price int64
|
2020-06-10 14:21:26 +00:00
|
|
|
// RequiredFlags is a set of flags which must be set during script invocations.
|
|
|
|
// Default value is NoneFlag i.e. no flags are required.
|
|
|
|
RequiredFlags smartcontract.CallFlag
|
2020-04-08 10:35:39 +00:00
|
|
|
}
|
2020-04-22 20:00:18 +00:00
|
|
|
|
|
|
|
// Method is a signature for a native method.
|
2020-06-03 12:55:06 +00:00
|
|
|
type Method = func(ic *Context, args []stackitem.Item) stackitem.Item
|
2020-04-22 20:00:18 +00:00
|
|
|
|
|
|
|
// MethodAndPrice is a native-contract method descriptor.
|
|
|
|
type MethodAndPrice struct {
|
|
|
|
Func Method
|
2020-09-21 14:34:40 +00:00
|
|
|
MD *manifest.Method
|
2020-04-22 20:00:18 +00:00
|
|
|
Price int64
|
|
|
|
RequiredFlags smartcontract.CallFlag
|
|
|
|
}
|
|
|
|
|
|
|
|
// Contract is an interface for all native contracts.
|
|
|
|
type Contract interface {
|
|
|
|
Initialize(*Context) error
|
|
|
|
Metadata() *ContractMD
|
|
|
|
}
|
|
|
|
|
|
|
|
// ContractMD represents native contract instance.
|
|
|
|
type ContractMD struct {
|
2020-07-22 16:03:05 +00:00
|
|
|
Manifest manifest.Manifest
|
|
|
|
Name string
|
|
|
|
ContractID int32
|
|
|
|
Script []byte
|
|
|
|
Hash util.Uint160
|
|
|
|
Methods map[string]MethodAndPrice
|
2020-04-22 20:00:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewContractMD returns Contract with the specified list of methods.
|
|
|
|
func NewContractMD(name string) *ContractMD {
|
|
|
|
c := &ContractMD{
|
2020-07-22 16:03:05 +00:00
|
|
|
Name: name,
|
|
|
|
Methods: make(map[string]MethodAndPrice),
|
2020-04-22 20:00:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
w := io.NewBufBinWriter()
|
2020-07-22 16:03:05 +00:00
|
|
|
emit.String(w.BinWriter, c.Name)
|
2020-08-14 10:50:52 +00:00
|
|
|
emit.Syscall(w.BinWriter, interopnames.NeoNativeCall)
|
2020-07-22 16:03:05 +00:00
|
|
|
|
2020-04-22 20:00:18 +00:00
|
|
|
c.Script = w.Bytes()
|
|
|
|
c.Hash = hash.Hash160(c.Script)
|
|
|
|
c.Manifest = *manifest.DefaultManifest(c.Hash)
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddMethod adds new method to a native contract.
|
|
|
|
func (c *ContractMD) AddMethod(md *MethodAndPrice, desc *manifest.Method, safe bool) {
|
|
|
|
c.Manifest.ABI.Methods = append(c.Manifest.ABI.Methods, *desc)
|
2020-09-21 14:34:40 +00:00
|
|
|
md.MD = desc
|
2020-04-22 20:00:18 +00:00
|
|
|
c.Methods[desc.Name] = *md
|
|
|
|
if safe {
|
|
|
|
c.Manifest.SafeMethods.Add(desc.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddEvent adds new event to a native contract.
|
|
|
|
func (c *ContractMD) AddEvent(name string, ps ...manifest.Parameter) {
|
|
|
|
c.Manifest.ABI.Events = append(c.Manifest.ABI.Events, manifest.Event{
|
|
|
|
Name: name,
|
|
|
|
Parameters: ps,
|
|
|
|
})
|
|
|
|
}
|
2020-07-28 13:38:00 +00:00
|
|
|
|
|
|
|
// Sort sorts interop functions by id.
|
|
|
|
func Sort(fs []Function) {
|
|
|
|
sort.Slice(fs, func(i, j int) bool { return fs[i].ID < fs[j].ID })
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFunction returns metadata for interop with the specified id.
|
|
|
|
func (ic *Context) GetFunction(id uint32) *Function {
|
|
|
|
for _, slice := range ic.Functions {
|
|
|
|
n := sort.Search(len(slice), func(i int) bool {
|
|
|
|
return slice[i].ID >= id
|
|
|
|
})
|
|
|
|
if n < len(slice) && slice[n].ID == id {
|
|
|
|
return &slice[n]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SyscallHandler handles syscall with id.
|
2020-08-07 11:37:49 +00:00
|
|
|
func (ic *Context) SyscallHandler(_ *vm.VM, id uint32) error {
|
2020-07-28 13:38:00 +00:00
|
|
|
f := ic.GetFunction(id)
|
|
|
|
if f == nil {
|
|
|
|
return errors.New("syscall not found")
|
|
|
|
}
|
2020-08-07 11:37:49 +00:00
|
|
|
cf := ic.VM.Context().GetCallFlags()
|
2020-07-28 13:38:00 +00:00
|
|
|
if !cf.Has(f.RequiredFlags) {
|
|
|
|
return fmt.Errorf("missing call flags: %05b vs %05b", cf, f.RequiredFlags)
|
|
|
|
}
|
2020-08-07 11:37:49 +00:00
|
|
|
if !ic.VM.AddGas(f.Price) {
|
2020-07-28 13:38:00 +00:00
|
|
|
return errors.New("insufficient amount of gas")
|
|
|
|
}
|
2020-08-07 11:37:49 +00:00
|
|
|
return f.Func(ic)
|
2020-07-28 13:38:00 +00:00
|
|
|
}
|
|
|
|
|
2020-08-07 11:37:49 +00:00
|
|
|
// SpawnVM spawns new VM with the specified gas limit and set context.VM field.
|
2020-07-28 13:38:00 +00:00
|
|
|
func (ic *Context) SpawnVM() *vm.VM {
|
|
|
|
v := vm.NewWithTrigger(ic.Trigger)
|
|
|
|
v.GasLimit = -1
|
|
|
|
v.SyscallHandler = ic.SyscallHandler
|
2020-08-07 11:37:49 +00:00
|
|
|
ic.VM = v
|
2020-07-28 13:38:00 +00:00
|
|
|
return v
|
|
|
|
}
|