2020-04-08 10:35:39 +00:00
|
|
|
package interop
|
|
|
|
|
|
|
|
import (
|
2020-07-28 13:38:00 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
2021-02-15 13:40:44 +00:00
|
|
|
"strings"
|
2020-07-28 13:38:00 +00:00
|
|
|
|
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"
|
2021-02-15 13:40:44 +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"
|
2021-03-25 12:22:16 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
2021-02-15 13:40:44 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
2020-12-29 10:45:49 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
|
2020-04-22 20:00:18 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
|
2021-01-13 12:34:10 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/nef"
|
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"
|
2021-02-15 13:40:44 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
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
|
2021-03-25 12:22:16 +00:00
|
|
|
Container hash.Hashable
|
|
|
|
Network uint32
|
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,
|
2021-03-25 12:22:16 +00:00
|
|
|
Network: uint32(bc.GetConfig().Magic),
|
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 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.
|
2020-12-29 10:45:49 +00:00
|
|
|
RequiredFlags callflag.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
|
2021-03-05 10:30:16 +00:00
|
|
|
CPUFee int64
|
2021-03-05 11:53:46 +00:00
|
|
|
StorageFee int64
|
2021-02-15 13:40:44 +00:00
|
|
|
SyscallOffset int
|
2020-12-29 10:45:49 +00:00
|
|
|
RequiredFlags callflag.CallFlag
|
2020-04-22 20:00:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2021-02-09 09:29:41 +00:00
|
|
|
state.NativeContract
|
|
|
|
Name string
|
2021-02-15 13:40:44 +00:00
|
|
|
Methods []MethodAndPrice
|
2020-04-22 20:00:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewContractMD returns Contract with the specified list of methods.
|
2021-01-15 21:17:31 +00:00
|
|
|
func NewContractMD(name string, id int32) *ContractMD {
|
2021-02-15 13:40:44 +00:00
|
|
|
c := &ContractMD{Name: name}
|
2020-04-22 20:00:18 +00:00
|
|
|
|
2021-02-09 09:29:41 +00:00
|
|
|
c.ID = id
|
|
|
|
|
2021-01-13 12:34:10 +00:00
|
|
|
// NEF is now stored in contract state and affects state dump.
|
|
|
|
// Therefore values are taken from C# node.
|
2021-01-19 08:08:34 +00:00
|
|
|
c.NEF.Header.Compiler = "neo-core-v3.0"
|
2021-01-13 12:34:10 +00:00
|
|
|
c.NEF.Header.Magic = nef.Magic
|
2021-02-15 13:40:44 +00:00
|
|
|
c.Hash = state.CreateContractHash(util.Uint160{}, 0, 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
|
|
|
|
}
|
|
|
|
|
2021-02-15 13:40:44 +00:00
|
|
|
// UpdateHash creates native contract script and updates hash.
|
|
|
|
func (c *ContractMD) UpdateHash() {
|
|
|
|
w := io.NewBufBinWriter()
|
|
|
|
for i := range c.Methods {
|
|
|
|
offset := w.Len()
|
|
|
|
c.Methods[i].MD.Offset = offset
|
|
|
|
c.Manifest.ABI.Methods[i].Offset = offset
|
|
|
|
emit.Int(w.BinWriter, 0)
|
|
|
|
c.Methods[i].SyscallOffset = w.Len()
|
|
|
|
emit.Syscall(w.BinWriter, interopnames.SystemContractCallNative)
|
|
|
|
emit.Opcodes(w.BinWriter, opcode.RET)
|
|
|
|
}
|
|
|
|
if w.Err != nil {
|
|
|
|
panic(fmt.Errorf("can't create native contract script: %w", w.Err))
|
|
|
|
}
|
|
|
|
|
|
|
|
c.NEF.Script = w.Bytes()
|
|
|
|
c.NEF.Checksum = c.NEF.CalculateChecksum()
|
|
|
|
}
|
|
|
|
|
2020-04-22 20:00:18 +00:00
|
|
|
// 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-09-21 14:34:40 +00:00
|
|
|
md.MD = desc
|
2020-12-29 10:45:49 +00:00
|
|
|
desc.Safe = md.RequiredFlags&(callflag.All^callflag.ReadOnly) == 0
|
2021-01-28 13:55:03 +00:00
|
|
|
|
|
|
|
index := sort.Search(len(c.Manifest.ABI.Methods), func(i int) bool {
|
|
|
|
md := c.Manifest.ABI.Methods[i]
|
|
|
|
if md.Name != desc.Name {
|
|
|
|
return md.Name >= desc.Name
|
|
|
|
}
|
|
|
|
return len(md.Parameters) > len(desc.Parameters)
|
|
|
|
})
|
|
|
|
c.Manifest.ABI.Methods = append(c.Manifest.ABI.Methods, manifest.Method{})
|
|
|
|
copy(c.Manifest.ABI.Methods[index+1:], c.Manifest.ABI.Methods[index:])
|
|
|
|
c.Manifest.ABI.Methods[index] = *desc
|
|
|
|
|
2021-02-15 13:40:44 +00:00
|
|
|
// Cache follows the same order.
|
|
|
|
c.Methods = append(c.Methods, MethodAndPrice{})
|
|
|
|
copy(c.Methods[index+1:], c.Methods[index:])
|
|
|
|
c.Methods[index] = *md
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetMethodByOffset returns with the provided offset.
|
|
|
|
// Offset is offset of `System.Contract.CallNative` syscall.
|
|
|
|
func (c *ContractMD) GetMethodByOffset(offset int) (MethodAndPrice, bool) {
|
|
|
|
for k := range c.Methods {
|
|
|
|
if c.Methods[k].SyscallOffset == offset {
|
|
|
|
return c.Methods[k], true
|
|
|
|
}
|
2021-01-28 12:36:37 +00:00
|
|
|
}
|
2021-02-15 13:40:44 +00:00
|
|
|
return MethodAndPrice{}, false
|
2021-01-28 12:36:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetMethod returns method `name` with specified number of parameters.
|
|
|
|
func (c *ContractMD) GetMethod(name string, paramCount int) (MethodAndPrice, bool) {
|
2021-02-15 13:40:44 +00:00
|
|
|
index := sort.Search(len(c.Methods), func(i int) bool {
|
|
|
|
md := c.Methods[i]
|
|
|
|
res := strings.Compare(name, md.MD.Name)
|
|
|
|
switch res {
|
|
|
|
case -1, 1:
|
|
|
|
return res == -1
|
|
|
|
default:
|
|
|
|
return paramCount <= len(md.MD.Parameters)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if index < len(c.Methods) {
|
|
|
|
md := c.Methods[index]
|
|
|
|
if md.MD.Name == name && (paramCount == -1 || len(md.MD.Parameters) == paramCount) {
|
|
|
|
return md, true
|
|
|
|
}
|
2021-01-28 12:36:37 +00:00
|
|
|
}
|
2021-02-15 13:40:44 +00:00
|
|
|
return MethodAndPrice{}, false
|
2020-04-22 20:00:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2021-03-11 11:39:51 +00:00
|
|
|
// IsActive returns true iff the contract was deployed by the specified height.
|
|
|
|
func (c *ContractMD) IsActive(height uint32) bool {
|
|
|
|
history := c.UpdateHistory
|
|
|
|
return len(history) != 0 && history[0] <= height
|
|
|
|
}
|
|
|
|
|
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 {
|
2021-02-05 08:25:22 +00:00
|
|
|
if ic.Chain == nil || (ic.Block != nil && ic.Block.Index == 0) {
|
2020-12-11 12:22:49 +00:00
|
|
|
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
|
|
|
|
}
|