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"
|
|
|
|
"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/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-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"
|
|
|
|
)
|
|
|
|
|
2020-12-11 12:22:49 +00:00
|
|
|
const (
|
|
|
|
// DefaultBaseExecFee specifies default multiplier for opcode and syscall prices.
|
|
|
|
DefaultBaseExecFee = 30
|
|
|
|
)
|
|
|
|
|
2020-04-08 10:35:39 +00:00
|
|
|
// 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-12-13 15:26:35 +00:00
|
|
|
getContract func(dao.DAO, util.Uint160) (*state.Contract, error)
|
2020-04-08 10:35:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewContext returns new interop context.
|
2020-12-13 15:26:35 +00:00
|
|
|
func NewContext(trigger trigger.Type, bc blockchainer.Blockchainer, d dao.DAO,
|
|
|
|
getContract func(dao.DAO, util.Uint160) (*state.Contract, error), 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.
|
2020-12-13 15:26:35 +00:00
|
|
|
Functions: [][]Function{},
|
|
|
|
getContract: getContract,
|
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
|
2020-12-13 18:36:06 +00:00
|
|
|
OnPersist(*Context) error
|
|
|
|
PostPersist(*Context) error
|
2020-04-22 20:00:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2020-12-14 11:11:24 +00:00
|
|
|
c.Script, c.Hash = state.CreateNativeContractHash(c.Name)
|
2020-11-18 09:43:51 +00:00
|
|
|
c.Manifest = *manifest.DefaultManifest(name)
|
2020-04-22 20:00:18 +00:00
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddMethod adds new method to a native contract.
|
2020-12-08 10:27:41 +00:00
|
|
|
func (c *ContractMD) AddMethod(md *MethodAndPrice, desc *manifest.Method) {
|
2020-04-22 20:00:18 +00:00
|
|
|
c.Manifest.ABI.Methods = append(c.Manifest.ABI.Methods, *desc)
|
2020-09-21 14:34:40 +00:00
|
|
|
md.MD = desc
|
2020-12-11 08:38:14 +00:00
|
|
|
desc.Safe = md.RequiredFlags&(smartcontract.All^smartcontract.ReadOnly) == 0
|
2020-04-22 20:00:18 +00:00
|
|
|
c.Methods[desc.Name] = *md
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 })
|
|
|
|
}
|
|
|
|
|
2020-12-13 15:26:35 +00:00
|
|
|
// GetContract returns contract by its hash in current interop context.
|
|
|
|
func (ic *Context) GetContract(hash util.Uint160) (*state.Contract, error) {
|
|
|
|
return ic.getContract(ic.DAO, hash)
|
|
|
|
}
|
|
|
|
|
2020-07-28 13:38:00 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2020-12-11 12:22:49 +00:00
|
|
|
// BaseExecFee represents factor to multiply syscall prices with.
|
|
|
|
func (ic *Context) BaseExecFee() int64 {
|
|
|
|
if ic.Chain == nil {
|
|
|
|
return DefaultBaseExecFee
|
|
|
|
}
|
|
|
|
return ic.Chain.GetPolicer().GetBaseExecFee()
|
|
|
|
}
|
|
|
|
|
2020-07-28 13:38:00 +00:00
|
|
|
// 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-12-11 12:22:49 +00:00
|
|
|
if !ic.VM.AddGas(f.Price * ic.BaseExecFee()) {
|
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
|
|
|
|
}
|