Merge pull request #1142 from nspcc-dev/neo3/compiler/unexported_methods

compiler, cli: update manifest.json format
This commit is contained in:
Roman Khimov 2020-07-07 16:41:36 +03:00 committed by GitHub
commit b823a516f1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 212 additions and 96 deletions

View file

@ -17,6 +17,9 @@ import (
"github.com/urfave/cli" "github.com/urfave/cli"
) )
// validUntilBlockIncrement is the number of extra blocks to add to an exported transaction
const validUntilBlockIncrement = 50
var ( var (
neoToken = wallet.NewToken(client.NeoContractHash, "NEO", "neo", 0) neoToken = wallet.NewToken(client.NeoContractHash, "NEO", "neo", 0)
gasToken = wallet.NewToken(client.GasContractHash, "GAS", "gas", 8) gasToken = wallet.NewToken(client.GasContractHash, "GAS", "gas", 8)
@ -366,6 +369,8 @@ func transferNEP5(ctx *cli.Context) error {
} }
if outFile := ctx.String("out"); outFile != "" { if outFile := ctx.String("out"); outFile != "" {
// avoid fast transaction expiration
tx.ValidUntilBlock += validUntilBlockIncrement
priv := acc.PrivateKey() priv := acc.PrivateKey()
pub := priv.PublicKey() pub := priv.PublicKey()
sign := priv.Sign(tx.GetSignedPart()) sign := priv.Sign(tx.GetSignedPart())

View file

@ -14,10 +14,7 @@ func Main(operation string, args []interface{}) bool {
// Log owner upon Verification trigger // Log owner upon Verification trigger
if trigger == runtime.Verification() { if trigger == runtime.Verification() {
if runtime.CheckWitness(owner) { return CheckWitness()
runtime.Log("Verified Owner")
}
return true
} }
// Discerns between log and notify for this test // Discerns between log and notify for this test
@ -30,15 +27,33 @@ func Main(operation string, args []interface{}) bool {
func handleOperation(operation string, args []interface{}) bool { func handleOperation(operation string, args []interface{}) bool {
if operation == "log" { if operation == "log" {
message := args[0].(string) return Log(args)
runtime.Log(message)
return true
} }
if operation == "notify" { if operation == "notify" {
runtime.Notify(args[0]) return Notify(args)
return true
} }
return false return false
} }
// CheckWitness checks owner's witness
func CheckWitness() bool {
if runtime.CheckWitness(owner) {
runtime.Log("Verified Owner")
}
return true
}
// Log logs given message
func Log(args []interface{}) bool {
message := args[0].(string)
runtime.Log(message)
return true
}
// Notify notifies about given message
func Notify(args []interface{}) bool {
runtime.Notify(args[0])
return true
}

View file

@ -7,51 +7,72 @@ import (
// Main is a very useful function. // Main is a very useful function.
func Main(operation string, args []interface{}) interface{} { func Main(operation string, args []interface{}) interface{} {
ctx := storage.GetContext()
// Puts value at key
if operation == "put" { if operation == "put" {
if checkArgs(args, 2) { return Put(args)
key := args[0].([]byte)
value := args[1].([]byte)
storage.Put(ctx, key, value)
return key
}
} }
// Returns the value at passed key
if operation == "get" { if operation == "get" {
if checkArgs(args, 1) { return Get(args)
key := args[0].([]byte)
return storage.Get(ctx, key)
}
} }
// Deletes the value at passed key
if operation == "delete" { if operation == "delete" {
key := args[0].([]byte) return Delete(args)
storage.Delete(ctx, key)
return true
} }
// Returns an array of key-value pairs with key that matched the passed value
if operation == "find" { if operation == "find" {
if checkArgs(args, 1) { return Find(args)
value := args[0].([]byte)
iter := storage.Find(ctx, value)
result := []string{}
for iterator.Next(iter) {
val := iterator.Value(iter)
key := iterator.Key(iter)
result = append(result, key.(string)+":"+val.(string))
}
return result
}
} }
return false return false
} }
// Put puts value at key.
func Put(args []interface{}) interface{} {
ctx := storage.GetContext()
if checkArgs(args, 2) {
key := args[0].([]byte)
value := args[1].([]byte)
storage.Put(ctx, key, value)
return key
}
return false
}
// Get returns the value at passed key.
func Get(args []interface{}) interface{} {
ctx := storage.GetContext()
if checkArgs(args, 1) {
key := args[0].([]byte)
return storage.Get(ctx, key)
}
return false
}
// Delete deletes the value at passed key.
func Delete(args []interface{}) interface{} {
ctx := storage.GetContext()
key := args[0].([]byte)
storage.Delete(ctx, key)
return true
}
// Find returns an array of key-value pairs with key that matched the passed value.
func Find(args []interface{}) interface{} {
ctx := storage.GetContext()
if checkArgs(args, 1) {
value := args[0].([]byte)
iter := storage.Find(ctx, value)
result := []string{}
for iterator.Next(iter) {
val := iterator.Value(iter)
key := iterator.Key(iter)
result = append(result, key.(string)+":"+val.(string))
}
return result
}
return false
}
func checkArgs(args []interface{}, length int) bool { func checkArgs(args []interface{}, length int) bool {
if len(args) == length { if len(args) == length {
return true return true

View file

@ -38,13 +38,14 @@ func (t Token) GetSupply(ctx storage.Context) interface{} {
return getIntFromDB(ctx, []byte(t.CirculationKey)) return getIntFromDB(ctx, []byte(t.CirculationKey))
} }
// BalanceOf gets the token balance of a specific address // TBalanceOf gets the token balance of a specific address
func (t Token) BalanceOf(ctx storage.Context, hodler []byte) interface{} { // TODO: https://github.com/nspcc-dev/neo-go/issues/1150
return getIntFromDB(ctx, hodler) func (t Token) TBalanceOf(ctx storage.Context, holder []byte) interface{} {
return getIntFromDB(ctx, holder)
} }
// Transfer token from one user to another // TTransfer token from one user to another
func (t Token) Transfer(ctx storage.Context, from []byte, to []byte, amount int) bool { func (t Token) TTransfer(ctx storage.Context, from []byte, to []byte, amount int) bool {
amountFrom := t.CanTransfer(ctx, from, to, amount) amountFrom := t.CanTransfer(ctx, from, to, amount)
if amountFrom == -1 { if amountFrom == -1 {
return false return false
@ -104,8 +105,8 @@ func IsUsableAddress(addr []byte) bool {
return false return false
} }
// Mint initial supply of tokens. // TMint initial supply of tokens.
func (t Token) Mint(ctx storage.Context, to []byte) bool { func (t Token) TMint(ctx storage.Context, to []byte) bool {
if !IsUsableAddress(t.Owner) { if !IsUsableAddress(t.Owner) {
return false return false
} }

View file

@ -2,7 +2,6 @@ package tokencontract
import ( import (
"github.com/nspcc-dev/neo-go/examples/token/nep5" "github.com/nspcc-dev/neo-go/examples/token/nep5"
"github.com/nspcc-dev/neo-go/pkg/interop/storage" "github.com/nspcc-dev/neo-go/pkg/interop/storage"
"github.com/nspcc-dev/neo-go/pkg/interop/util" "github.com/nspcc-dev/neo-go/pkg/interop/util"
) )
@ -12,10 +11,10 @@ const (
multiplier = 100000000 multiplier = 100000000
) )
var owner = util.FromAddress("NPAsqZkx9WhNd4P72uhZxBhLinSuNkxfB8") var owner = util.FromAddress("NMipL5VsNoLUBUJKPKLhxaEbPQVCZnyJyB")
// CreateToken initializes the Token Interface for the Smart Contract to operate with // createToken initializes the Token Interface for the Smart Contract to operate with
func CreateToken() nep5.Token { func createToken() nep5.Token {
return nep5.Token{ return nep5.Token{
Name: "Awesome NEO Token", Name: "Awesome NEO Token",
Symbol: "ANT", Symbol: "ANT",
@ -28,47 +27,91 @@ func CreateToken() nep5.Token {
// Main function = contract entry // Main function = contract entry
func Main(operation string, args []interface{}) interface{} { func Main(operation string, args []interface{}) interface{} {
token := CreateToken()
if operation == "name" { if operation == "name" {
return token.Name return Name()
} }
if operation == "symbol" { if operation == "symbol" {
return token.Symbol return Symbol()
} }
if operation == "decimals" { if operation == "decimals" {
return token.Decimals return Decimals()
} }
// The following operations need ctx
ctx := storage.GetContext()
if operation == "totalSupply" { if operation == "totalSupply" {
return token.GetSupply(ctx) return TotalSupply()
} }
if operation == "balanceOf" { if operation == "balanceOf" {
hodler := args[0].([]byte) hodler := args[0].([]byte)
return token.BalanceOf(ctx, hodler) return BalanceOf(hodler)
} }
if operation == "transfer" && CheckArgs(args, 3) {
if operation == "transfer" && checkArgs(args, 3) {
from := args[0].([]byte) from := args[0].([]byte)
to := args[1].([]byte) to := args[1].([]byte)
amount := args[2].(int) amount := args[2].(int)
return token.Transfer(ctx, from, to, amount) return Transfer(from, to, amount)
} }
if operation == "mint" && CheckArgs(args, 1) {
if operation == "mint" && checkArgs(args, 1) {
addr := args[0].([]byte) addr := args[0].([]byte)
return token.Mint(ctx, addr) return Mint(addr)
} }
return true return true
} }
// CheckArgs checks args array against a length indicator // checkArgs checks args array against a length indicator
func CheckArgs(args []interface{}, length int) bool { func checkArgs(args []interface{}, length int) bool {
if len(args) == length { if len(args) == length {
return true return true
} }
return false return false
} }
// Name returns the token name
func Name() string {
t := createToken()
return t.Name
}
// Symbol returns the token symbol
func Symbol() string {
t := createToken()
return t.Symbol
}
// Decimals returns the token decimals
func Decimals() int {
t := createToken()
return t.Decimals
}
// TotalSupply returns the token total supply value
func TotalSupply() interface{} {
t := createToken()
ctx := storage.GetContext()
return t.GetSupply(ctx)
}
// BalanceOf returns the amount of token on the specified address
func BalanceOf(holder []byte) interface{} {
t := createToken()
ctx := storage.GetContext()
return t.TBalanceOf(ctx, holder)
}
// Transfer token from one user to another
func Transfer(from []byte, to []byte, amount int) bool {
t := createToken()
ctx := storage.GetContext()
return t.TTransfer(ctx, from, to, amount)
}
// Mint initial supply of tokens
func Mint(to []byte) bool {
t := createToken()
ctx := storage.GetContext()
return t.TMint(ctx, to)
}

View file

@ -272,7 +272,7 @@ func (c *codegen) convertGlobals(f ast.Node) {
}) })
} }
func (c *codegen) convertFuncDecl(file ast.Node, decl *ast.FuncDecl) { func (c *codegen) convertFuncDecl(file ast.Node, decl *ast.FuncDecl, pkg *types.Package) {
var ( var (
f *funcScope f *funcScope
ok, isLambda bool ok, isLambda bool
@ -290,6 +290,7 @@ func (c *codegen) convertFuncDecl(file ast.Node, decl *ast.FuncDecl) {
c.setLabel(f.label) c.setLabel(f.label)
} else { } else {
f = c.newFunc(decl) f = c.newFunc(decl)
f.pkg = pkg
} }
f.rng.Start = uint16(c.prog.Len()) f.rng.Start = uint16(c.prog.Len())
@ -348,7 +349,7 @@ func (c *codegen) convertFuncDecl(file ast.Node, decl *ast.FuncDecl) {
if !isLambda { if !isLambda {
for _, f := range c.lambda { for _, f := range c.lambda {
c.convertFuncDecl(file, f.decl) c.convertFuncDecl(file, f.decl, pkg)
} }
c.lambda = make(map[string]*funcScope) c.lambda = make(map[string]*funcScope)
} }
@ -1433,7 +1434,7 @@ func (c *codegen) compile(info *buildInfo, pkg *loader.PackageInfo) error {
c.traverseGlobals(mainFile) c.traverseGlobals(mainFile)
// convert the entry point first. // convert the entry point first.
c.convertFuncDecl(mainFile, main) c.convertFuncDecl(mainFile, main, pkg.Pkg)
// sort map keys to generate code deterministically. // sort map keys to generate code deterministically.
keys := make([]*types.Package, 0, len(info.program.AllPackages)) keys := make([]*types.Package, 0, len(info.program.AllPackages))
@ -1454,7 +1455,7 @@ func (c *codegen) compile(info *buildInfo, pkg *loader.PackageInfo) error {
// Don't convert the function if it's not used. This will save a lot // Don't convert the function if it's not used. This will save a lot
// of bytecode space. // of bytecode space.
if n.Name.Name != mainIdent && funUsage.funcUsed(n.Name.Name) { if n.Name.Name != mainIdent && funUsage.funcUsed(n.Name.Name) {
c.convertFuncDecl(f, n) c.convertFuncDecl(f, n, k)
} }
} }
} }

View file

@ -28,6 +28,8 @@ type MethodDebugInfo struct {
ID string `json:"id"` ID string `json:"id"`
// Name is the name of the method together with the namespace it belongs to. // Name is the name of the method together with the namespace it belongs to.
Name DebugMethodName `json:"name"` Name DebugMethodName `json:"name"`
// IsExported defines whether method is exported.
IsExported bool `json:"-"`
// Range is the range of smart-contract's opcodes corresponding to the method. // Range is the range of smart-contract's opcodes corresponding to the method.
Range DebugRange `json:"range"` Range DebugRange `json:"range"`
// Parameters is a list of method's parameters. // Parameters is a list of method's parameters.
@ -134,8 +136,12 @@ func (c *codegen) methodInfoFromScope(name string, scope *funcScope) *MethodDebu
} }
} }
return &MethodDebugInfo{ return &MethodDebugInfo{
ID: name, ID: name,
Name: DebugMethodName{Name: name}, Name: DebugMethodName{
Name: name,
Namespace: scope.pkg.Name(),
},
IsExported: scope.decl.Name.IsExported(),
Range: scope.rng, Range: scope.rng,
Parameters: params, Parameters: params,
ReturnType: c.scReturnTypeFromScope(scope), ReturnType: c.scReturnTypeFromScope(scope),
@ -263,7 +269,7 @@ func (m *MethodDebugInfo) ToManifestMethod() (manifest.Method, error) {
if err != nil { if err != nil {
return result, err return result, err
} }
result.Name = m.Name.Name result.Name = strings.ToLower(string(m.Name.Name[0])) + m.Name.Name[1:]
result.Parameters = parameters result.Parameters = parameters
result.ReturnType = returnType result.ReturnType = returnType
return result, nil return result, nil
@ -335,8 +341,9 @@ func parsePairJSON(data []byte, sep string) (string, string, error) {
// Note: manifest is taken from the external source, however it can be generated ad-hoc. See #1038. // Note: manifest is taken from the external source, however it can be generated ad-hoc. See #1038.
func (di *DebugInfo) convertToManifest(fs smartcontract.PropertyState) (*manifest.Manifest, error) { func (di *DebugInfo) convertToManifest(fs smartcontract.PropertyState) (*manifest.Manifest, error) {
var ( var (
entryPoint manifest.Method entryPoint manifest.Method
err error mainNamespace string
err error
) )
for _, method := range di.Methods { for _, method := range di.Methods {
if method.Name.Name == mainIdent { if method.Name.Name == mainIdent {
@ -344,15 +351,16 @@ func (di *DebugInfo) convertToManifest(fs smartcontract.PropertyState) (*manifes
if err != nil { if err != nil {
return nil, err return nil, err
} }
mainNamespace = method.Name.Namespace
break break
} }
} }
if entryPoint.Name == "" { if entryPoint.Name == "" {
return nil, errors.New("no Main method was found") return nil, errors.New("no Main method was found")
} }
methods := make([]manifest.Method, 0, len(di.Methods)-1) methods := make([]manifest.Method, 0)
for _, method := range di.Methods { for _, method := range di.Methods {
if method.Name.Name != mainIdent { if method.Name.Name != mainIdent && method.IsExported && method.Name.Namespace == mainNamespace {
mMethod, err := method.ToManifestMethod() mMethod, err := method.ToManifestMethod()
if err != nil { if err != nil {
return nil, err return nil, err

View file

@ -18,27 +18,30 @@ func TestCodeGen_DebugInfo(t *testing.T) {
func Main(op string) bool { func Main(op string) bool {
var s string var s string
_ = s _ = s
res := methodInt(op) res := MethodInt(op)
_ = methodString() _ = MethodString()
_ = methodByteArray() _ = MethodByteArray()
_ = methodArray() _ = MethodArray()
_ = methodStruct() _ = MethodStruct()
_ = MethodConcat("a", "b", "c")
_ = unexportedMethod()
return res == 42 return res == 42
} }
func methodInt(a string) int { func MethodInt(a string) int {
if a == "get42" { if a == "get42" {
return 42 return 42
} }
return 3 return 3
} }
func methodConcat(a, b string, c string) string{ func MethodConcat(a, b string, c string) string{
return a + b + c return a + b + c
} }
func methodString() string { return "" } func MethodString() string { return "" }
func methodByteArray() []byte { return nil } func MethodByteArray() []byte { return nil }
func methodArray() []bool { return nil } func MethodArray() []bool { return nil }
func methodStruct() struct{} { return struct{}{} } func MethodStruct() struct{} { return struct{}{} }
func unexportedMethod() int { return 1 }
` `
info, err := getBuildInfo(src) info, err := getBuildInfo(src)
@ -58,11 +61,12 @@ func methodStruct() struct{} { return struct{}{} }
t.Run("return types", func(t *testing.T) { t.Run("return types", func(t *testing.T) {
returnTypes := map[string]string{ returnTypes := map[string]string{
"methodInt": "Integer", "MethodInt": "Integer",
"methodConcat": "String", "MethodConcat": "String",
"methodString": "String", "methodByteArray": "ByteArray", "MethodString": "String", "MethodByteArray": "ByteArray",
"methodArray": "Array", "methodStruct": "Struct", "MethodArray": "Array", "MethodStruct": "Struct",
"Main": "Boolean", "Main": "Boolean",
"unexportedMethod": "Integer",
} }
for i := range d.Methods { for i := range d.Methods {
name := d.Methods[i].Name.Name name := d.Methods[i].Name.Name
@ -84,11 +88,11 @@ func methodStruct() struct{} { return struct{}{} }
t.Run("param types", func(t *testing.T) { t.Run("param types", func(t *testing.T) {
paramTypes := map[string][]DebugParam{ paramTypes := map[string][]DebugParam{
"methodInt": {{ "MethodInt": {{
Name: "a", Name: "a",
Type: "String", Type: "String",
}}, }},
"methodConcat": { "MethodConcat": {
{ {
Name: "a", Name: "a",
Type: "String", Type: "String",
@ -129,7 +133,7 @@ func methodStruct() struct{} { return struct{}{} }
ABI: manifest.ABI{ ABI: manifest.ABI{
Hash: hash.Hash160(buf), Hash: hash.Hash160(buf),
EntryPoint: manifest.Method{ EntryPoint: manifest.Method{
Name: "Main", Name: "main",
Parameters: []manifest.Parameter{ Parameters: []manifest.Parameter{
{ {
Name: "op", Name: "op",
@ -169,6 +173,24 @@ func methodStruct() struct{} { return struct{}{} }
Parameters: []manifest.Parameter{}, Parameters: []manifest.Parameter{},
ReturnType: smartcontract.ArrayType, ReturnType: smartcontract.ArrayType,
}, },
{
Name: "methodConcat",
Parameters: []manifest.Parameter{
{
Name: "a",
Type: smartcontract.StringType,
},
{
Name: "b",
Type: smartcontract.StringType,
},
{
Name: "c",
Type: smartcontract.StringType,
},
},
ReturnType: smartcontract.StringType,
},
}, },
Events: []manifest.Event{}, Events: []manifest.Event{},
}, },