2020-03-31 12:56:10 +00:00
|
|
|
package compiler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"go/ast"
|
|
|
|
"go/types"
|
2022-02-11 12:16:15 +00:00
|
|
|
"sort"
|
2020-03-31 12:56:10 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2020-08-13 07:29:08 +00:00
|
|
|
"unicode"
|
|
|
|
"unicode/utf8"
|
compiler: add ability to generate .abi.json file
A part of integration with NEO Blockchain Toolkit (see #902). To be
able to deploy smart-contract compiled with neo-go compiler via NEO
Express, we have to generate additional .abi.json file. This file
contains the following information:
- hash of the compiled contract
- smart-contract metadata (title, description, version, author,
email, has-storage, has-dynamic-invoke, is-payable)
- smart-contract entry point
- functions
- events
However, this .abi.json file is slightly different from the one,
described in manifest.go, so we have to add auxilaury stractures for
json marshalling. The .abi.json format used by NEO-Express is described
[here](https://github.com/neo-project/neo-devpack-dotnet/blob/master/src/Neo.Compiler.MSIL/FuncExport.cs#L66).
2020-04-28 16:39:01 +00:00
|
|
|
|
2022-04-06 12:27:01 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
compiler: add ability to generate .abi.json file
A part of integration with NEO Blockchain Toolkit (see #902). To be
able to deploy smart-contract compiled with neo-go compiler via NEO
Express, we have to generate additional .abi.json file. This file
contains the following information:
- hash of the compiled contract
- smart-contract metadata (title, description, version, author,
email, has-storage, has-dynamic-invoke, is-payable)
- smart-contract entry point
- functions
- events
However, this .abi.json file is slightly different from the one,
described in manifest.go, so we have to add auxilaury stractures for
json marshalling. The .abi.json format used by NEO-Express is described
[here](https://github.com/neo-project/neo-devpack-dotnet/blob/master/src/Neo.Compiler.MSIL/FuncExport.cs#L66).
2020-04-28 16:39:01 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
2022-02-11 12:16:15 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/binding"
|
2020-06-25 13:10:08 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
|
2021-06-24 15:36:40 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2020-12-09 10:14:21 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2020-03-31 12:56:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// DebugInfo represents smart-contract debug information.
|
|
|
|
type DebugInfo struct {
|
2020-07-24 10:40:54 +00:00
|
|
|
MainPkg string `json:"-"`
|
2022-04-06 12:27:01 +00:00
|
|
|
Hash util.Uint160 `json:"hash"`
|
2020-06-24 05:55:24 +00:00
|
|
|
Documents []string `json:"documents"`
|
|
|
|
Methods []MethodDebugInfo `json:"methods"`
|
2022-12-01 18:05:54 +00:00
|
|
|
// NamedTypes are exported structured types that have some name (even
|
|
|
|
// if the original structure doesn't) and a number of internal fields.
|
|
|
|
NamedTypes map[string]binding.ExtendedType `json:"-"`
|
2023-05-24 08:52:14 +00:00
|
|
|
// Events are the events that contract is allowed to emit and that have to
|
|
|
|
// be presented in the resulting contract manifest and debug info file.
|
|
|
|
Events []EventDebugInfo `json:"events"`
|
|
|
|
// EmittedEvents contains events occurring in code, i.e. events emitted
|
|
|
|
// via runtime.Notify(...) call in the contract code if they have constant
|
|
|
|
// names and doesn't have ellipsis arguments. EmittedEvents are not related
|
|
|
|
// to the debug info and are aimed to serve bindings generation.
|
|
|
|
EmittedEvents map[string][]EmittedEventInfo `json:"-"`
|
2021-06-24 15:36:40 +00:00
|
|
|
// InvokedContracts contains foreign contract invocations.
|
|
|
|
InvokedContracts map[util.Uint160][]string `json:"-"`
|
2022-04-20 18:30:09 +00:00
|
|
|
// StaticVariables contains a list of static variable names and types.
|
2021-05-12 11:54:40 +00:00
|
|
|
StaticVariables []string `json:"static-variables"`
|
2020-03-31 12:56:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MethodDebugInfo represents smart-contract's method debug information.
|
|
|
|
type MethodDebugInfo struct {
|
2020-08-13 07:29:08 +00:00
|
|
|
// ID is the actual name of the method.
|
2020-03-31 12:56:10 +00:00
|
|
|
ID string `json:"id"`
|
2020-08-13 07:29:08 +00:00
|
|
|
// Name is the name of the method with the first letter in a lowercase
|
|
|
|
// together with the namespace it belongs to. We need to keep the first letter
|
|
|
|
// lowercased to match manifest standards.
|
2020-03-31 12:56:10 +00:00
|
|
|
Name DebugMethodName `json:"name"`
|
2022-04-20 18:30:09 +00:00
|
|
|
// IsExported defines whether the method is exported.
|
2020-07-03 09:58:41 +00:00
|
|
|
IsExported bool `json:"-"`
|
2022-04-20 18:30:09 +00:00
|
|
|
// IsFunction defines whether the method has no receiver.
|
2020-09-01 16:06:32 +00:00
|
|
|
IsFunction bool `json:"-"`
|
2020-03-31 12:56:10 +00:00
|
|
|
// Range is the range of smart-contract's opcodes corresponding to the method.
|
|
|
|
Range DebugRange `json:"range"`
|
2022-04-20 18:30:09 +00:00
|
|
|
// Parameters is a list of the method's parameters.
|
2020-03-31 12:56:10 +00:00
|
|
|
Parameters []DebugParam `json:"params"`
|
2022-04-20 18:30:09 +00:00
|
|
|
// ReturnType is the method's return type.
|
2020-12-09 10:14:21 +00:00
|
|
|
ReturnType string `json:"return"`
|
2022-04-20 18:30:09 +00:00
|
|
|
// ReturnTypeReal is the method's return type as specified in Go code.
|
2022-02-11 12:16:15 +00:00
|
|
|
ReturnTypeReal binding.Override `json:"-"`
|
2022-12-01 18:05:54 +00:00
|
|
|
// ReturnTypeExtended is the method's return type with additional data.
|
|
|
|
ReturnTypeExtended *binding.ExtendedType `json:"-"`
|
2022-04-20 18:30:09 +00:00
|
|
|
// ReturnTypeSC is a return type to use in manifest.
|
2020-12-09 10:14:21 +00:00
|
|
|
ReturnTypeSC smartcontract.ParamType `json:"-"`
|
|
|
|
Variables []string `json:"variables"`
|
2020-03-31 12:56:10 +00:00
|
|
|
// SeqPoints is a map between source lines and byte-code instruction offsets.
|
|
|
|
SeqPoints []DebugSeqPoint `json:"sequence-points"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// DebugMethodName is a combination of a namespace and name.
|
|
|
|
type DebugMethodName struct {
|
|
|
|
Namespace string
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
// EventDebugInfo represents smart-contract's event debug information.
|
|
|
|
type EventDebugInfo struct {
|
|
|
|
ID string `json:"id"`
|
2020-08-12 14:39:41 +00:00
|
|
|
// Name is a human-readable event name in a format "{namespace},{name}".
|
2020-03-31 12:56:10 +00:00
|
|
|
Name string `json:"name"`
|
2020-06-26 09:08:59 +00:00
|
|
|
Parameters []DebugParam `json:"params"`
|
2020-03-31 12:56:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DebugSeqPoint represents break-point for debugger.
|
|
|
|
type DebugSeqPoint struct {
|
|
|
|
// Opcode is an opcode's address.
|
|
|
|
Opcode int
|
|
|
|
// Document is an index of file where sequence point occurs.
|
|
|
|
Document int
|
|
|
|
// StartLine is the first line of the break-pointed statement.
|
|
|
|
StartLine int
|
|
|
|
// StartCol is the first column of the break-pointed statement.
|
|
|
|
StartCol int
|
|
|
|
// EndLine is the last line of the break-pointed statement.
|
|
|
|
EndLine int
|
|
|
|
// EndCol is the last column of the break-pointed statement.
|
|
|
|
EndCol int
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// DebugRange represents the method's section in bytecode.
|
2020-03-31 12:56:10 +00:00
|
|
|
type DebugRange struct {
|
|
|
|
Start uint16
|
|
|
|
End uint16
|
|
|
|
}
|
|
|
|
|
2023-05-05 13:57:07 +00:00
|
|
|
// DebugParam represents the variable's name and type.
|
2020-03-31 12:56:10 +00:00
|
|
|
type DebugParam struct {
|
2022-12-01 18:05:54 +00:00
|
|
|
Name string `json:"name"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
RealType binding.Override `json:"-"`
|
|
|
|
ExtendedType *binding.ExtendedType `json:"-"`
|
|
|
|
TypeSC smartcontract.ParamType `json:"-"`
|
compiler: add ability to generate .abi.json file
A part of integration with NEO Blockchain Toolkit (see #902). To be
able to deploy smart-contract compiled with neo-go compiler via NEO
Express, we have to generate additional .abi.json file. This file
contains the following information:
- hash of the compiled contract
- smart-contract metadata (title, description, version, author,
email, has-storage, has-dynamic-invoke, is-payable)
- smart-contract entry point
- functions
- events
However, this .abi.json file is slightly different from the one,
described in manifest.go, so we have to add auxilaury stractures for
json marshalling. The .abi.json format used by NEO-Express is described
[here](https://github.com/neo-project/neo-devpack-dotnet/blob/master/src/Neo.Compiler.MSIL/FuncExport.cs#L66).
2020-04-28 16:39:01 +00:00
|
|
|
}
|
|
|
|
|
2023-05-24 08:52:14 +00:00
|
|
|
// EmittedEventInfo describes information about single emitted event got from
|
|
|
|
// the contract code. It has the map of extended types used as the parameters to
|
|
|
|
// runtime.Notify(...) call (if any) and the parameters info itself.
|
|
|
|
type EmittedEventInfo struct {
|
|
|
|
ExtTypes map[string]binding.ExtendedType
|
|
|
|
Params []DebugParam
|
|
|
|
}
|
|
|
|
|
2020-03-31 13:57:35 +00:00
|
|
|
func (c *codegen) saveSequencePoint(n ast.Node) {
|
2020-08-10 14:42:53 +00:00
|
|
|
name := "init"
|
|
|
|
if c.scope != nil {
|
|
|
|
name = c.scope.name
|
2020-05-07 08:54:35 +00:00
|
|
|
}
|
2020-08-10 14:42:53 +00:00
|
|
|
|
2021-12-02 14:44:53 +00:00
|
|
|
fset := c.buildInfo.config.Fset
|
2020-03-31 13:57:35 +00:00
|
|
|
start := fset.Position(n.Pos())
|
|
|
|
end := fset.Position(n.End())
|
2020-08-10 14:42:53 +00:00
|
|
|
c.sequencePoints[name] = append(c.sequencePoints[name], DebugSeqPoint{
|
2020-03-31 13:57:35 +00:00
|
|
|
Opcode: c.prog.Len(),
|
2020-08-10 10:10:35 +00:00
|
|
|
Document: c.docIndex[start.Filename],
|
2020-03-31 13:57:35 +00:00
|
|
|
StartLine: start.Line,
|
2023-07-27 15:31:34 +00:00
|
|
|
StartCol: start.Column,
|
2020-03-31 13:57:35 +00:00
|
|
|
EndLine: end.Line,
|
2023-07-27 15:31:34 +00:00
|
|
|
EndCol: end.Column,
|
2020-03-31 13:57:35 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-06-24 04:22:33 +00:00
|
|
|
func (c *codegen) emitDebugInfo(contract []byte) *DebugInfo {
|
2020-03-31 12:56:10 +00:00
|
|
|
d := &DebugInfo{
|
2022-04-06 12:27:01 +00:00
|
|
|
Hash: hash.Hash160(contract),
|
2021-12-02 14:44:53 +00:00
|
|
|
MainPkg: c.mainPkg.Name,
|
2021-05-12 11:54:40 +00:00
|
|
|
Events: []EventDebugInfo{},
|
|
|
|
Documents: c.documents,
|
|
|
|
StaticVariables: c.staticVariables,
|
2020-07-24 10:40:54 +00:00
|
|
|
}
|
|
|
|
if c.initEndOffset > 0 {
|
|
|
|
d.Methods = append(d.Methods, MethodDebugInfo{
|
|
|
|
ID: manifest.MethodInit,
|
|
|
|
Name: DebugMethodName{
|
|
|
|
Name: manifest.MethodInit,
|
2021-12-02 14:44:53 +00:00
|
|
|
Namespace: c.mainPkg.Name,
|
2020-07-24 10:40:54 +00:00
|
|
|
},
|
|
|
|
IsExported: true,
|
2020-09-01 16:06:32 +00:00
|
|
|
IsFunction: true,
|
2020-07-24 10:40:54 +00:00
|
|
|
Range: DebugRange{
|
|
|
|
Start: 0,
|
|
|
|
End: uint16(c.initEndOffset),
|
|
|
|
},
|
2020-12-09 10:14:21 +00:00
|
|
|
ReturnType: "Void",
|
|
|
|
ReturnTypeSC: smartcontract.VoidType,
|
|
|
|
SeqPoints: c.sequencePoints["init"],
|
2021-05-12 12:08:22 +00:00
|
|
|
Variables: c.initVariables,
|
2020-07-24 10:40:54 +00:00
|
|
|
})
|
2020-03-31 12:56:10 +00:00
|
|
|
}
|
2020-10-02 10:16:02 +00:00
|
|
|
if c.deployEndOffset >= 0 {
|
|
|
|
d.Methods = append(d.Methods, MethodDebugInfo{
|
|
|
|
ID: manifest.MethodDeploy,
|
|
|
|
Name: DebugMethodName{
|
|
|
|
Name: manifest.MethodDeploy,
|
2021-12-02 14:44:53 +00:00
|
|
|
Namespace: c.mainPkg.Name,
|
2020-10-02 10:16:02 +00:00
|
|
|
},
|
|
|
|
IsExported: true,
|
|
|
|
IsFunction: true,
|
|
|
|
Range: DebugRange{
|
|
|
|
Start: uint16(c.initEndOffset + 1),
|
|
|
|
End: uint16(c.deployEndOffset),
|
|
|
|
},
|
2021-01-28 13:31:50 +00:00
|
|
|
Parameters: []DebugParam{
|
|
|
|
{
|
|
|
|
Name: "data",
|
|
|
|
Type: "Any",
|
|
|
|
TypeSC: smartcontract.AnyType,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "isUpdate",
|
|
|
|
Type: "Boolean",
|
|
|
|
TypeSC: smartcontract.BoolType,
|
|
|
|
},
|
|
|
|
},
|
2020-12-09 10:14:21 +00:00
|
|
|
ReturnType: "Void",
|
|
|
|
ReturnTypeSC: smartcontract.VoidType,
|
|
|
|
SeqPoints: c.sequencePoints[manifest.MethodDeploy],
|
2021-05-12 13:03:47 +00:00
|
|
|
Variables: c.deployVariables,
|
2020-10-02 10:16:02 +00:00
|
|
|
})
|
|
|
|
}
|
2022-02-11 12:16:15 +00:00
|
|
|
|
|
|
|
start := len(d.Methods)
|
2022-12-01 18:05:54 +00:00
|
|
|
d.NamedTypes = make(map[string]binding.ExtendedType)
|
2020-03-31 12:56:10 +00:00
|
|
|
for name, scope := range c.funcs {
|
2023-11-21 14:48:53 +00:00
|
|
|
if scope.rng.Start == scope.rng.End {
|
2020-03-31 12:56:10 +00:00
|
|
|
continue
|
|
|
|
}
|
2023-11-21 14:48:53 +00:00
|
|
|
m := c.methodInfoFromScope(name, scope, d.NamedTypes)
|
2020-03-31 12:56:10 +00:00
|
|
|
d.Methods = append(d.Methods, *m)
|
|
|
|
}
|
2022-02-11 12:16:15 +00:00
|
|
|
sort.Slice(d.Methods[start:], func(i, j int) bool {
|
|
|
|
return d.Methods[start+i].Name.Name < d.Methods[start+j].Name.Name
|
|
|
|
})
|
2020-11-24 13:36:35 +00:00
|
|
|
d.EmittedEvents = c.emittedEvents
|
2021-06-24 15:36:40 +00:00
|
|
|
d.InvokedContracts = c.invokedContracts
|
2020-03-31 12:56:10 +00:00
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2020-04-02 13:36:11 +00:00
|
|
|
func (c *codegen) registerDebugVariable(name string, expr ast.Expr) {
|
2022-12-01 18:05:54 +00:00
|
|
|
_, vt, _, _ := c.scAndVMTypeFromExpr(expr, nil)
|
2020-05-07 08:54:35 +00:00
|
|
|
if c.scope == nil {
|
2021-05-12 11:54:40 +00:00
|
|
|
c.staticVariables = append(c.staticVariables, name+","+vt.String())
|
2020-05-07 08:54:35 +00:00
|
|
|
return
|
|
|
|
}
|
2020-12-09 10:14:21 +00:00
|
|
|
c.scope.variables = append(c.scope.variables, name+","+vt.String())
|
2020-04-02 13:36:11 +00:00
|
|
|
}
|
|
|
|
|
2022-12-01 18:05:54 +00:00
|
|
|
func (c *codegen) methodInfoFromScope(name string, scope *funcScope, exts map[string]binding.ExtendedType) *MethodDebugInfo {
|
2020-03-31 12:56:10 +00:00
|
|
|
ps := scope.decl.Type.Params
|
|
|
|
params := make([]DebugParam, 0, ps.NumFields())
|
2020-04-29 13:32:07 +00:00
|
|
|
for i := range ps.List {
|
2020-03-31 12:56:10 +00:00
|
|
|
for j := range ps.List[i].Names {
|
2022-12-01 18:05:54 +00:00
|
|
|
st, vt, rt, et := c.scAndVMTypeFromExpr(ps.List[i].Type, exts)
|
2020-03-31 12:56:10 +00:00
|
|
|
params = append(params, DebugParam{
|
2022-12-01 18:05:54 +00:00
|
|
|
Name: ps.List[i].Names[j].Name,
|
|
|
|
Type: vt.String(),
|
|
|
|
ExtendedType: et,
|
|
|
|
RealType: rt,
|
|
|
|
TypeSC: st,
|
2020-03-31 12:56:10 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-07-29 14:20:00 +00:00
|
|
|
ss := strings.Split(name, ".")
|
|
|
|
name = ss[len(ss)-1]
|
2020-08-13 07:29:08 +00:00
|
|
|
r, n := utf8.DecodeRuneInString(name)
|
2022-12-01 18:05:54 +00:00
|
|
|
st, vt, rt, et := c.scAndVMReturnTypeFromScope(scope, exts)
|
2022-02-11 12:16:15 +00:00
|
|
|
|
2020-03-31 12:56:10 +00:00
|
|
|
return &MethodDebugInfo{
|
2020-07-07 08:49:14 +00:00
|
|
|
ID: name,
|
|
|
|
Name: DebugMethodName{
|
2020-08-13 07:29:08 +00:00
|
|
|
Name: string(unicode.ToLower(r)) + name[n:],
|
2020-07-07 08:49:14 +00:00
|
|
|
Namespace: scope.pkg.Name(),
|
|
|
|
},
|
2022-12-01 18:05:54 +00:00
|
|
|
IsExported: scope.decl.Name.IsExported(),
|
|
|
|
IsFunction: scope.decl.Recv == nil,
|
|
|
|
Range: scope.rng,
|
|
|
|
Parameters: params,
|
|
|
|
ReturnType: vt,
|
|
|
|
ReturnTypeExtended: et,
|
|
|
|
ReturnTypeReal: rt,
|
|
|
|
ReturnTypeSC: st,
|
|
|
|
SeqPoints: c.sequencePoints[name],
|
|
|
|
Variables: scope.variables,
|
2020-03-31 12:56:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-01 18:05:54 +00:00
|
|
|
func (c *codegen) scAndVMReturnTypeFromScope(scope *funcScope, exts map[string]binding.ExtendedType) (smartcontract.ParamType, string, binding.Override, *binding.ExtendedType) {
|
2020-03-31 12:56:10 +00:00
|
|
|
results := scope.decl.Type.Results
|
|
|
|
switch results.NumFields() {
|
|
|
|
case 0:
|
2022-12-01 18:05:54 +00:00
|
|
|
return smartcontract.VoidType, "Void", binding.Override{}, nil
|
2020-03-31 12:56:10 +00:00
|
|
|
case 1:
|
2022-12-01 18:05:54 +00:00
|
|
|
st, vt, s, et := c.scAndVMTypeFromExpr(results.List[0].Type, exts)
|
|
|
|
return st, vt.String(), s, et
|
2020-03-31 12:56:10 +00:00
|
|
|
default:
|
|
|
|
// multiple return values are not supported in debugger
|
2022-12-01 18:05:54 +00:00
|
|
|
return smartcontract.AnyType, "Any", binding.Override{}, nil
|
2020-03-31 12:56:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-01 18:05:54 +00:00
|
|
|
func scAndVMInteropTypeFromExpr(named *types.Named, isPointer bool) (smartcontract.ParamType, stackitem.Type, binding.Override, *binding.ExtendedType) {
|
2020-12-10 11:08:42 +00:00
|
|
|
name := named.Obj().Name()
|
|
|
|
pkg := named.Obj().Pkg().Name()
|
|
|
|
switch pkg {
|
2022-11-25 07:35:13 +00:00
|
|
|
case "ledger", "management":
|
2022-11-25 10:00:43 +00:00
|
|
|
switch name {
|
|
|
|
case "ParameterType", "SignerScope", "WitnessAction", "WitnessConditionType", "VMState":
|
|
|
|
return smartcontract.IntegerType, stackitem.IntegerT, binding.Override{TypeName: "int"}, nil
|
|
|
|
}
|
2022-12-01 18:05:54 +00:00
|
|
|
// Block, Transaction, Contract.
|
2022-02-11 12:16:15 +00:00
|
|
|
typeName := pkg + "." + name
|
2022-12-01 18:05:54 +00:00
|
|
|
et := &binding.ExtendedType{Base: smartcontract.ArrayType, Name: typeName}
|
2022-02-11 12:16:15 +00:00
|
|
|
if isPointer {
|
|
|
|
typeName = "*" + typeName
|
|
|
|
}
|
|
|
|
return smartcontract.ArrayType, stackitem.ArrayT, binding.Override{
|
|
|
|
Package: named.Obj().Pkg().Path(),
|
|
|
|
TypeName: typeName,
|
2022-12-01 18:05:54 +00:00
|
|
|
}, et
|
2020-12-10 11:08:42 +00:00
|
|
|
case "interop":
|
|
|
|
if name != "Interface" {
|
2022-02-11 12:16:15 +00:00
|
|
|
over := binding.Override{
|
|
|
|
Package: interopPrefix,
|
|
|
|
TypeName: "interop." + name,
|
|
|
|
}
|
2020-12-10 11:08:42 +00:00
|
|
|
switch name {
|
|
|
|
case "Hash160":
|
2022-12-01 18:05:54 +00:00
|
|
|
return smartcontract.Hash160Type, stackitem.ByteArrayT, over, nil
|
2020-12-10 11:08:42 +00:00
|
|
|
case "Hash256":
|
2022-12-01 18:05:54 +00:00
|
|
|
return smartcontract.Hash256Type, stackitem.ByteArrayT, over, nil
|
2020-12-10 11:08:42 +00:00
|
|
|
case "PublicKey":
|
2022-12-01 18:05:54 +00:00
|
|
|
return smartcontract.PublicKeyType, stackitem.ByteArrayT, over, nil
|
2020-12-10 11:08:42 +00:00
|
|
|
case "Signature":
|
2022-12-01 18:05:54 +00:00
|
|
|
return smartcontract.SignatureType, stackitem.ByteArrayT, over, nil
|
2020-12-10 11:08:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-12-01 18:05:54 +00:00
|
|
|
return smartcontract.InteropInterfaceType,
|
|
|
|
stackitem.InteropT,
|
2023-04-03 10:34:24 +00:00
|
|
|
binding.Override{TypeName: "any"},
|
2022-12-01 18:05:54 +00:00
|
|
|
&binding.ExtendedType{Base: smartcontract.InteropInterfaceType, Interface: "iterator"} // Temporarily all interops are iterators.
|
2022-02-11 12:16:15 +00:00
|
|
|
}
|
|
|
|
|
2022-12-01 18:05:54 +00:00
|
|
|
func (c *codegen) scAndVMTypeFromExpr(typ ast.Expr, exts map[string]binding.ExtendedType) (smartcontract.ParamType, stackitem.Type, binding.Override, *binding.ExtendedType) {
|
|
|
|
return c.scAndVMTypeFromType(c.typeOf(typ), exts)
|
2020-12-10 11:08:42 +00:00
|
|
|
}
|
|
|
|
|
2022-12-01 18:05:54 +00:00
|
|
|
func (c *codegen) scAndVMTypeFromType(t types.Type, exts map[string]binding.ExtendedType) (smartcontract.ParamType, stackitem.Type, binding.Override, *binding.ExtendedType) {
|
2022-02-11 12:16:15 +00:00
|
|
|
if t == nil {
|
2023-04-03 10:34:24 +00:00
|
|
|
return smartcontract.AnyType, stackitem.AnyT, binding.Override{TypeName: "any"}, nil
|
2020-05-13 15:09:55 +00:00
|
|
|
}
|
2022-02-11 12:16:15 +00:00
|
|
|
|
|
|
|
var isPtr bool
|
|
|
|
|
|
|
|
named, isNamed := t.(*types.Named)
|
|
|
|
if !isNamed {
|
|
|
|
var ptr *types.Pointer
|
|
|
|
if ptr, isPtr = t.(*types.Pointer); isPtr {
|
|
|
|
named, isNamed = ptr.Elem().(*types.Named)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if isNamed {
|
2020-08-28 08:08:04 +00:00
|
|
|
if isInteropPath(named.String()) {
|
2022-12-01 18:05:54 +00:00
|
|
|
st, vt, over, et := scAndVMInteropTypeFromExpr(named, isPtr)
|
|
|
|
if et != nil && et.Base == smartcontract.ArrayType && exts != nil && exts[et.Name].Name != et.Name {
|
|
|
|
_ = c.genStructExtended(named.Underlying().(*types.Struct), et.Name, exts)
|
|
|
|
}
|
|
|
|
return st, vt, over, et
|
2020-08-28 08:08:04 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-25 10:01:57 +00:00
|
|
|
if ptr, isPtr := t.(*types.Pointer); isPtr {
|
|
|
|
t = ptr.Elem()
|
|
|
|
}
|
2022-02-11 12:16:15 +00:00
|
|
|
var over binding.Override
|
2020-05-13 15:09:55 +00:00
|
|
|
switch t := t.Underlying().(type) {
|
2020-03-31 12:56:10 +00:00
|
|
|
case *types.Basic:
|
|
|
|
info := t.Info()
|
|
|
|
switch {
|
|
|
|
case info&types.IsInteger != 0:
|
2022-02-11 12:16:15 +00:00
|
|
|
over.TypeName = "int"
|
2022-12-01 18:05:54 +00:00
|
|
|
return smartcontract.IntegerType, stackitem.IntegerT, over, nil
|
2020-03-31 12:56:10 +00:00
|
|
|
case info&types.IsBoolean != 0:
|
2022-02-11 12:16:15 +00:00
|
|
|
over.TypeName = "bool"
|
2022-12-01 18:05:54 +00:00
|
|
|
return smartcontract.BoolType, stackitem.BooleanT, over, nil
|
2020-03-31 12:56:10 +00:00
|
|
|
case info&types.IsString != 0:
|
2022-02-11 12:16:15 +00:00
|
|
|
over.TypeName = "string"
|
2022-12-01 18:05:54 +00:00
|
|
|
return smartcontract.StringType, stackitem.ByteArrayT, over, nil
|
2020-03-31 12:56:10 +00:00
|
|
|
default:
|
2023-04-03 10:34:24 +00:00
|
|
|
over.TypeName = "any"
|
2022-12-01 18:05:54 +00:00
|
|
|
return smartcontract.AnyType, stackitem.AnyT, over, nil
|
2020-03-31 12:56:10 +00:00
|
|
|
}
|
|
|
|
case *types.Map:
|
2022-12-01 18:05:54 +00:00
|
|
|
et := &binding.ExtendedType{
|
|
|
|
Base: smartcontract.MapType,
|
|
|
|
}
|
|
|
|
et.Key, _, _, _ = c.scAndVMTypeFromType(t.Key(), exts)
|
|
|
|
vt, _, over, vet := c.scAndVMTypeFromType(t.Elem(), exts)
|
|
|
|
et.Value = vet
|
|
|
|
if et.Value == nil {
|
|
|
|
et.Value = &binding.ExtendedType{Base: vt}
|
|
|
|
}
|
2022-02-11 12:16:15 +00:00
|
|
|
over.TypeName = "map[" + t.Key().String() + "]" + over.TypeName
|
2022-12-01 18:05:54 +00:00
|
|
|
return smartcontract.MapType, stackitem.MapT, over, et
|
2020-03-31 12:56:10 +00:00
|
|
|
case *types.Struct:
|
2023-05-25 15:45:59 +00:00
|
|
|
var extName string
|
2022-02-11 12:16:15 +00:00
|
|
|
if isNamed {
|
|
|
|
over.Package = named.Obj().Pkg().Path()
|
|
|
|
over.TypeName = named.Obj().Pkg().Name() + "." + named.Obj().Name()
|
2022-12-01 18:05:54 +00:00
|
|
|
_ = c.genStructExtended(t, over.TypeName, exts)
|
2023-05-25 15:45:59 +00:00
|
|
|
extName = over.TypeName
|
2022-12-01 18:05:54 +00:00
|
|
|
} else {
|
|
|
|
name := "unnamed"
|
|
|
|
if exts != nil {
|
|
|
|
for exts[name].Name == name {
|
|
|
|
name = name + "X"
|
|
|
|
}
|
|
|
|
_ = c.genStructExtended(t, name, exts)
|
|
|
|
}
|
2023-05-25 15:45:59 +00:00
|
|
|
// For bindings configurator this structure becomes named in fact. Its name
|
|
|
|
// is "unnamed[X...X]".
|
|
|
|
extName = name
|
2022-02-11 12:16:15 +00:00
|
|
|
}
|
2022-12-01 18:05:54 +00:00
|
|
|
return smartcontract.ArrayType, stackitem.StructT, over,
|
|
|
|
&binding.ExtendedType{ // Value-less, refer to exts.
|
|
|
|
Base: smartcontract.ArrayType,
|
2023-05-25 15:45:59 +00:00
|
|
|
Name: extName,
|
2022-12-01 18:05:54 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 12:56:10 +00:00
|
|
|
case *types.Slice:
|
2020-05-13 15:09:55 +00:00
|
|
|
if isByte(t.Elem()) {
|
2022-02-11 12:16:15 +00:00
|
|
|
over.TypeName = "[]byte"
|
2022-12-01 18:05:54 +00:00
|
|
|
return smartcontract.ByteArrayType, stackitem.ByteArrayT, over, nil
|
|
|
|
}
|
|
|
|
et := &binding.ExtendedType{
|
|
|
|
Base: smartcontract.ArrayType,
|
|
|
|
}
|
|
|
|
vt, _, over, vet := c.scAndVMTypeFromType(t.Elem(), exts)
|
|
|
|
et.Value = vet
|
|
|
|
if et.Value == nil {
|
|
|
|
et.Value = &binding.ExtendedType{
|
|
|
|
Base: vt,
|
|
|
|
}
|
2022-02-11 12:16:15 +00:00
|
|
|
}
|
|
|
|
if over.TypeName != "" {
|
|
|
|
over.TypeName = "[]" + over.TypeName
|
2020-03-31 12:56:10 +00:00
|
|
|
}
|
2022-12-01 18:05:54 +00:00
|
|
|
return smartcontract.ArrayType, stackitem.ArrayT, over, et
|
2020-03-31 12:56:10 +00:00
|
|
|
default:
|
2023-04-03 10:34:24 +00:00
|
|
|
over.TypeName = "any"
|
2022-12-01 18:05:54 +00:00
|
|
|
return smartcontract.AnyType, stackitem.AnyT, over, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *codegen) genStructExtended(t *types.Struct, name string, exts map[string]binding.ExtendedType) *binding.ExtendedType {
|
|
|
|
var et *binding.ExtendedType
|
|
|
|
if exts != nil {
|
|
|
|
if exts[name].Name != name {
|
|
|
|
et = &binding.ExtendedType{
|
|
|
|
Base: smartcontract.ArrayType,
|
|
|
|
Name: name,
|
|
|
|
Fields: make([]binding.FieldExtendedType, t.NumFields()),
|
|
|
|
}
|
|
|
|
exts[name] = *et // Prefill to solve recursive structures.
|
|
|
|
for i := range et.Fields {
|
|
|
|
field := t.Field(i)
|
|
|
|
ft, _, _, fet := c.scAndVMTypeFromType(field.Type(), exts)
|
|
|
|
if fet == nil {
|
|
|
|
et.Fields[i].ExtendedType.Base = ft
|
|
|
|
} else {
|
|
|
|
et.Fields[i].ExtendedType = *fet
|
|
|
|
}
|
|
|
|
et.Fields[i].Field = field.Name()
|
|
|
|
}
|
|
|
|
exts[name] = *et // Set real structure data.
|
|
|
|
} else {
|
|
|
|
et = new(binding.ExtendedType)
|
|
|
|
*et = exts[name]
|
|
|
|
}
|
2020-03-31 12:56:10 +00:00
|
|
|
}
|
2022-12-01 18:05:54 +00:00
|
|
|
return et
|
2020-03-31 12:56:10 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// MarshalJSON implements the json.Marshaler interface.
|
2020-03-31 12:56:10 +00:00
|
|
|
func (d *DebugRange) MarshalJSON() ([]byte, error) {
|
|
|
|
return []byte(`"` + strconv.FormatUint(uint64(d.Start), 10) + `-` +
|
|
|
|
strconv.FormatUint(uint64(d.End), 10) + `"`), nil
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// UnmarshalJSON implements the json.Unmarshaler interface.
|
2020-03-31 12:56:10 +00:00
|
|
|
func (d *DebugRange) UnmarshalJSON(data []byte) error {
|
|
|
|
startS, endS, err := parsePairJSON(data, "-")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
start, err := strconv.ParseUint(startS, 10, 16)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
end, err := strconv.ParseUint(endS, 10, 16)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Start = uint16(start)
|
|
|
|
d.End = uint16(end)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// MarshalJSON implements the json.Marshaler interface.
|
2020-03-31 12:56:10 +00:00
|
|
|
func (d *DebugParam) MarshalJSON() ([]byte, error) {
|
|
|
|
return []byte(`"` + d.Name + `,` + d.Type + `"`), nil
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// UnmarshalJSON implements the json.Unmarshaler interface.
|
2020-03-31 12:56:10 +00:00
|
|
|
func (d *DebugParam) UnmarshalJSON(data []byte) error {
|
|
|
|
startS, endS, err := parsePairJSON(data, ",")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Name = startS
|
|
|
|
d.Type = endS
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-12 20:17:03 +00:00
|
|
|
// ToManifestParameter converts DebugParam to manifest.Parameter.
|
2020-12-10 15:56:05 +00:00
|
|
|
func (d *DebugParam) ToManifestParameter() manifest.Parameter {
|
2020-06-25 13:10:08 +00:00
|
|
|
return manifest.Parameter{
|
|
|
|
Name: d.Name,
|
2020-12-09 10:14:21 +00:00
|
|
|
Type: d.TypeSC,
|
2020-12-10 15:56:05 +00:00
|
|
|
}
|
2020-06-25 13:10:08 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 20:17:03 +00:00
|
|
|
// ToManifestMethod converts MethodDebugInfo to manifest.Method.
|
2020-12-10 15:56:05 +00:00
|
|
|
func (m *MethodDebugInfo) ToManifestMethod() manifest.Method {
|
2020-06-25 13:10:08 +00:00
|
|
|
var (
|
|
|
|
result manifest.Method
|
|
|
|
)
|
|
|
|
parameters := make([]manifest.Parameter, len(m.Parameters))
|
|
|
|
for i, p := range m.Parameters {
|
2020-12-10 15:56:05 +00:00
|
|
|
parameters[i] = p.ToManifestParameter()
|
2020-06-25 13:10:08 +00:00
|
|
|
}
|
2020-08-13 07:29:08 +00:00
|
|
|
result.Name = m.Name.Name
|
2020-07-23 13:12:41 +00:00
|
|
|
result.Offset = int(m.Range.Start)
|
2020-06-25 13:10:08 +00:00
|
|
|
result.Parameters = parameters
|
2020-12-09 10:14:21 +00:00
|
|
|
result.ReturnType = m.ReturnTypeSC
|
2020-12-10 15:56:05 +00:00
|
|
|
return result
|
2020-06-25 13:10:08 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// MarshalJSON implements the json.Marshaler interface.
|
2020-03-31 12:56:10 +00:00
|
|
|
func (d *DebugMethodName) MarshalJSON() ([]byte, error) {
|
|
|
|
return []byte(`"` + d.Namespace + `,` + d.Name + `"`), nil
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// UnmarshalJSON implements the json.Unmarshaler interface.
|
2020-03-31 12:56:10 +00:00
|
|
|
func (d *DebugMethodName) UnmarshalJSON(data []byte) error {
|
|
|
|
startS, endS, err := parsePairJSON(data, ",")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Namespace = startS
|
|
|
|
d.Name = endS
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// MarshalJSON implements the json.Marshaler interface.
|
2020-03-31 12:56:10 +00:00
|
|
|
func (d *DebugSeqPoint) MarshalJSON() ([]byte, error) {
|
|
|
|
s := fmt.Sprintf("%d[%d]%d:%d-%d:%d", d.Opcode, d.Document,
|
|
|
|
d.StartLine, d.StartCol, d.EndLine, d.EndCol)
|
|
|
|
return []byte(`"` + s + `"`), nil
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// UnmarshalJSON implements the json.Unmarshaler interface.
|
2020-03-31 12:56:10 +00:00
|
|
|
func (d *DebugSeqPoint) UnmarshalJSON(data []byte) error {
|
|
|
|
_, err := fmt.Sscanf(string(data), `"%d[%d]%d:%d-%d:%d"`,
|
|
|
|
&d.Opcode, &d.Document, &d.StartLine, &d.StartCol, &d.EndLine, &d.EndCol)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func parsePairJSON(data []byte, sep string) (string, string, error) {
|
|
|
|
var s string
|
|
|
|
if err := json.Unmarshal(data, &s); err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
ss := strings.SplitN(s, sep, 2)
|
|
|
|
if len(ss) != 2 {
|
|
|
|
return "", "", errors.New("invalid range format")
|
|
|
|
}
|
|
|
|
return ss[0], ss[1], nil
|
|
|
|
}
|
compiler: add ability to generate .abi.json file
A part of integration with NEO Blockchain Toolkit (see #902). To be
able to deploy smart-contract compiled with neo-go compiler via NEO
Express, we have to generate additional .abi.json file. This file
contains the following information:
- hash of the compiled contract
- smart-contract metadata (title, description, version, author,
email, has-storage, has-dynamic-invoke, is-payable)
- smart-contract entry point
- functions
- events
However, this .abi.json file is slightly different from the one,
described in manifest.go, so we have to add auxilaury stractures for
json marshalling. The .abi.json format used by NEO-Express is described
[here](https://github.com/neo-project/neo-devpack-dotnet/blob/master/src/Neo.Compiler.MSIL/FuncExport.cs#L66).
2020-04-28 16:39:01 +00:00
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// ConvertToManifest converts a contract to the manifest.Manifest struct for debugger.
|
2020-06-09 13:12:58 +00:00
|
|
|
// Note: manifest is taken from the external source, however it can be generated ad-hoc. See #1038.
|
2020-12-10 14:42:12 +00:00
|
|
|
func (di *DebugInfo) ConvertToManifest(o *Options) (*manifest.Manifest, error) {
|
2020-07-03 09:58:41 +00:00
|
|
|
methods := make([]manifest.Method, 0)
|
2020-06-25 13:10:08 +00:00
|
|
|
for _, method := range di.Methods {
|
2020-09-01 16:06:32 +00:00
|
|
|
if method.IsExported && method.IsFunction && method.Name.Namespace == di.MainPkg {
|
2020-12-10 15:56:05 +00:00
|
|
|
mMethod := method.ToManifestMethod()
|
2020-12-10 14:55:52 +00:00
|
|
|
for i := range o.SafeMethods {
|
|
|
|
if mMethod.Name == o.SafeMethods[i] {
|
|
|
|
mMethod.Safe = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-06-25 13:10:08 +00:00
|
|
|
methods = append(methods, mMethod)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-10 14:42:12 +00:00
|
|
|
result := manifest.NewManifest(o.Name)
|
|
|
|
if o.ContractSupportedStandards != nil {
|
|
|
|
result.SupportedStandards = o.ContractSupportedStandards
|
2020-08-11 08:21:54 +00:00
|
|
|
}
|
2023-05-08 18:58:28 +00:00
|
|
|
events := make([]manifest.Event, len(o.ContractEvents))
|
|
|
|
for i, e := range o.ContractEvents {
|
|
|
|
params := make([]manifest.Parameter, len(e.Parameters))
|
|
|
|
for j, p := range e.Parameters {
|
|
|
|
params[j] = p.Parameter
|
|
|
|
}
|
|
|
|
events[i] = manifest.Event{
|
|
|
|
Name: o.ContractEvents[i].Name,
|
|
|
|
Parameters: params,
|
|
|
|
}
|
|
|
|
}
|
2020-06-25 13:10:08 +00:00
|
|
|
result.ABI = manifest.ABI{
|
2020-07-23 13:03:00 +00:00
|
|
|
Methods: methods,
|
2023-05-08 18:58:28 +00:00
|
|
|
Events: events,
|
2020-12-10 14:42:12 +00:00
|
|
|
}
|
|
|
|
if result.ABI.Events == nil {
|
|
|
|
result.ABI.Events = make([]manifest.Event, 0)
|
compiler: add ability to generate .abi.json file
A part of integration with NEO Blockchain Toolkit (see #902). To be
able to deploy smart-contract compiled with neo-go compiler via NEO
Express, we have to generate additional .abi.json file. This file
contains the following information:
- hash of the compiled contract
- smart-contract metadata (title, description, version, author,
email, has-storage, has-dynamic-invoke, is-payable)
- smart-contract entry point
- functions
- events
However, this .abi.json file is slightly different from the one,
described in manifest.go, so we have to add auxilaury stractures for
json marshalling. The .abi.json format used by NEO-Express is described
[here](https://github.com/neo-project/neo-devpack-dotnet/blob/master/src/Neo.Compiler.MSIL/FuncExport.cs#L66).
2020-04-28 16:39:01 +00:00
|
|
|
}
|
2021-06-02 07:50:17 +00:00
|
|
|
result.Permissions = o.Permissions
|
2021-10-16 11:10:17 +00:00
|
|
|
for name, emitName := range o.Overloads {
|
|
|
|
m := result.ABI.GetMethod(name, -1)
|
|
|
|
if m == nil {
|
|
|
|
return nil, fmt.Errorf("overload for method %s was provided but it wasn't found", name)
|
|
|
|
}
|
|
|
|
if result.ABI.GetMethod(emitName, -1) == nil {
|
|
|
|
return nil, fmt.Errorf("overload with target method %s was provided but it wasn't found", emitName)
|
|
|
|
}
|
|
|
|
|
|
|
|
realM := result.ABI.GetMethod(emitName, len(m.Parameters))
|
|
|
|
if realM != nil {
|
|
|
|
return nil, fmt.Errorf("conflict overload for %s: "+
|
|
|
|
"multiple methods with the same number of parameters", name)
|
|
|
|
}
|
|
|
|
m.Name = emitName
|
2022-01-20 18:13:35 +00:00
|
|
|
// Check the resulting name against set of safe methods.
|
|
|
|
for i := range o.SafeMethods {
|
|
|
|
if m.Name == o.SafeMethods[i] {
|
|
|
|
m.Safe = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-10-16 11:10:17 +00:00
|
|
|
}
|
2020-06-25 13:10:08 +00:00
|
|
|
return result, nil
|
compiler: add ability to generate .abi.json file
A part of integration with NEO Blockchain Toolkit (see #902). To be
able to deploy smart-contract compiled with neo-go compiler via NEO
Express, we have to generate additional .abi.json file. This file
contains the following information:
- hash of the compiled contract
- smart-contract metadata (title, description, version, author,
email, has-storage, has-dynamic-invoke, is-payable)
- smart-contract entry point
- functions
- events
However, this .abi.json file is slightly different from the one,
described in manifest.go, so we have to add auxilaury stractures for
json marshalling. The .abi.json format used by NEO-Express is described
[here](https://github.com/neo-project/neo-devpack-dotnet/blob/master/src/Neo.Compiler.MSIL/FuncExport.cs#L66).
2020-04-28 16:39:01 +00:00
|
|
|
}
|