Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cli/smartcontract/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ func TestGenerateRPCBindings_Errors(t *testing.T) {
}

t.Run("event", func(t *testing.T) {
check(t, "invalid6", false, "error during generation: named type `SomeStruct` has two fields with identical resulting binding name `Field`")
check(t, "invalid6", false, "error during generation: named type `invalid6.SomeStruct` has two fields with identical resulting binding name `Field`")
})
t.Run("autogen event", func(t *testing.T) {
check(t, "invalid7", true, "error during generation: named type `invalid7.SomeStruct` has two fields with identical resulting binding name `Field`")
Expand Down
23 changes: 11 additions & 12 deletions cli/smartcontract/smart_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"github.com/nspcc-dev/neo-go/pkg/rpcclient/invoker"
"github.com/nspcc-dev/neo-go/pkg/rpcclient/management"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/binding"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/nef"
"github.com/nspcc-dev/neo-go/pkg/util"
Expand Down Expand Up @@ -419,15 +418,13 @@ func initSmartContract(ctx *cli.Context) error {
SourceURL: "http://example.com/",
SupportedStandards: []string{},
SafeMethods: []string{},
Events: []compiler.HybridEvent{
Events: []manifest.Event{
{
Name: "Hello world!",
Parameters: []compiler.HybridParameter{
Parameters: []manifest.Parameter{
{
Parameter: manifest.Parameter{
Name: "args",
Type: smartcontract.ArrayType,
},
Name: "args",
Type: smartcontract.ArrayType,
},
},
},
Expand Down Expand Up @@ -517,7 +514,10 @@ func contractCompile(ctx *cli.Context) error {
NoEventsCheck: ctx.Bool("no-events"),
NoPermissionsCheck: ctx.Bool("no-permissions"),

GuessEventTypes: ctx.Bool("guess-eventtypes"),
CollectedNamedTypes: make(map[string]manifest.ExtendedType),
}
if ctx.Bool("guess-eventtypes") {
o.GuessedNamedTypes = make(map[string]manifest.ExtendedType)
}

if len(confFile) != 0 {
Expand All @@ -528,7 +528,6 @@ func contractCompile(ctx *cli.Context) error {
o.Name = conf.Name
o.SourceURL = conf.SourceURL
o.ContractEvents = conf.Events
o.DeclaredNamedTypes = conf.NamedTypes
o.ContractSupportedStandards = conf.SupportedStandards
o.Permissions = make([]manifest.Permission, len(conf.Permissions))
for i := range conf.Permissions {
Expand Down Expand Up @@ -760,10 +759,10 @@ type ProjectConfig struct {
SourceURL string
SafeMethods []string
SupportedStandards []string
Events []compiler.HybridEvent
Events []manifest.Event
Permissions []permission
Overloads map[string]string `yaml:"overloads,omitempty"`
NamedTypes map[string]binding.ExtendedType `yaml:"namedtypes,omitempty"`
Overloads map[string]string `yaml:"overloads,omitempty"`
NamedTypes map[string]manifest.ExtendedType `yaml:"namedtypes,omitempty"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likely it's no longer needed here, manifest has it all.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do I need to write an UnmarshalYAML method for ProjectConfig or mark the NamedTypes field as deprecated?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just drop them and then let's see how it goes.

}

func inspect(ctx *cli.Context) error {
Expand Down
10 changes: 3 additions & 7 deletions cli/smartcontract/testdata/rpcbindings/invalid5/invalid.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ events:
extendedtype:
base: Array
name: invalid5.NamedStruct
namedtypes:
invalid5.NamedStruct:
base: Array
name: invalid5.NamedStruct
fields:
- field: SomeInt
base: Integer
fields:
- field: SomeInt
base: Integer
9 changes: 0 additions & 9 deletions cli/smartcontract/testdata/rpcbindings/invalid6/invalid.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,3 @@ events:
extendedtype:
base: Struct
name: SomeStruct
namedtypes:
SomeStruct:
base: Struct
name: SomeStruct
fields:
- field: Field
base: Integer
- field: field
base: Integer
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ events:
extendedtype:
base: Struct
name: crazyStruct
fields:
- field: I
base: Integer
- field: B
base: Boolean
Comment on lines 26 to +33
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you include fields in the definition of named extended type? It shouldn't be like that, all named types should be specified in the namedtypes section, and extended type here should just point to a specific named type.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We no longer store namedtypes in the config, so I don't quite understand where the author should specify the definition of named type.

- name: "SomeArray"
parameters:
- name: a
Expand All @@ -43,18 +48,6 @@ events:
extendedtype:
base: Struct
name: simpleStruct
namedtypes:
crazyStruct:
base: Struct
name: crazyStruct
fields:
- field: I
base: Integer
- field: B
base: Boolean
simpleStruct:
base: Struct
name: simpleStruct
fields:
- field: i
base: Integer
fields:
- field: i
base: Integer
105 changes: 0 additions & 105 deletions cli/smartcontract/testdata/verify.bindings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -125,111 +125,6 @@ overrides:
syscall4.name: string
toBlockSR: '*github.com/nspcc-dev/neo-go/pkg/interop/native/ledger.BlockSR'
verify: bool
namedtypes:
ledger.Block:
base: Array
name: ledger.Block
fields:
- field: Hash
base: Hash256
- field: Version
base: Integer
- field: PrevHash
base: Hash256
- field: MerkleRoot
base: Hash256
- field: Timestamp
base: Integer
- field: Nonce
base: Integer
- field: Index
base: Integer
- field: NextConsensus
base: Hash160
- field: TransactionsLength
base: Integer
ledger.BlockSR:
base: Array
name: ledger.BlockSR
fields:
- field: Hash
base: Hash256
- field: Version
base: Integer
- field: PrevHash
base: Hash256
- field: MerkleRoot
base: Hash256
- field: Timestamp
base: Integer
- field: Nonce
base: Integer
- field: Index
base: Integer
- field: NextConsensus
base: Hash160
- field: TransactionsLength
base: Integer
- field: PrevStateRoot
base: Hash256
ledger.Transaction:
base: Array
name: ledger.Transaction
fields:
- field: Hash
base: Hash256
- field: Version
base: Integer
- field: Nonce
base: Integer
- field: Sender
base: Hash160
- field: SysFee
base: Integer
- field: NetFee
base: Integer
- field: ValidUntilBlock
base: Integer
- field: Script
base: ByteArray
ledger.TransactionSigner:
base: Array
name: ledger.TransactionSigner
fields:
- field: Account
base: Hash160
- field: Scopes
base: Integer
- field: AllowedContracts
base: Array
value:
base: Hash160
- field: AllowedGroups
base: Array
value:
base: PublicKey
- field: Rules
base: Array
value:
base: Array
name: ledger.WitnessRule
ledger.WitnessCondition:
base: Array
name: ledger.WitnessCondition
fields:
- field: Type
base: Integer
- field: Value
base: Any
ledger.WitnessRule:
base: Array
name: ledger.WitnessRule
fields:
- field: Action
base: Integer
- field: Condition
base: Array
name: ledger.WitnessCondition
types:
call.args:
base: Array
Expand Down
1 change: 0 additions & 1 deletion internal/testchain/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ func NewDeployTx(bc Ledger, name string, sender util.Uint160, r gio.Reader, conf
o.Name = conf.Name
o.SourceURL = conf.SourceURL
o.ContractEvents = conf.Events
o.DeclaredNamedTypes = conf.NamedTypes
o.ContractSupportedStandards = conf.SupportedStandards
o.Permissions = make([]manifest.Permission, len(conf.Permissions))
for i := range conf.Permissions {
Expand Down
66 changes: 65 additions & 1 deletion pkg/compiler/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -671,8 +671,11 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor {
if id.Name != "_" {
if !isMapKeyCheck {
if len(t.Values) == 0 {
c.emitDefault(c.typeOf(t.Type))
typ := c.typeOf(t.Type)
c.collectNamedTypes(typ)
c.emitDefault(typ)
} else if i == 0 || !multiRet {
c.collectNamedTypes(c.typeOf(t.Values[i]))
ast.Walk(c, t.Values[i])
}
}
Expand Down Expand Up @@ -725,6 +728,7 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor {
}
if !isAssignOp && !isMapKeyCheck {
for i := range n.Rhs {
c.collectNamedTypes(c.typeOf(n.Rhs[i]))
ast.Walk(c, n.Rhs[i])
}
}
Expand Down Expand Up @@ -972,6 +976,20 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor {
t := c.typeOf(n)
switch typ := t.Underlying().(type) {
case *types.Struct:
named, isNamed := t.(*types.Named)
if c.buildInfo.options != nil {
if !isNamed {
name := "unnamed"
if c.buildInfo.options.CollectedNamedTypes != nil {
for c.buildInfo.options.CollectedNamedTypes[name].Name == name {
name = name + "X"
}
_ = c.genStructExtended(typ, name, c.buildInfo.options.CollectedNamedTypes)
}
} else if !isInteropPath(named.String()) {
_ = c.genStructExtended(typ, named.Obj().Pkg().Name()+"."+named.Obj().Name(), c.buildInfo.options.CollectedNamedTypes)
}
}
c.convertStruct(n, false)
case *types.Map:
c.convertMap(n)
Expand Down Expand Up @@ -2161,6 +2179,52 @@ func (c *codegen) checkGetMapValueWithOKFlag(expr ast.Expr) bool {
return ok
}

func (c *codegen) collectNamedTypes(t types.Type) {
if c.buildInfo.options == nil {
return
}
var (
seen = make(map[types.Type]bool)
walk func(types.Type)
)
walk = func(tt types.Type) {
if tt == nil || seen[tt] {
return
}
seen[tt] = true

switch x := tt.(type) {
case *types.Pointer:
walk(x.Elem())
case *types.Slice:
walk(x.Elem())
case *types.Map:
walk(x.Key())
walk(x.Elem())
case *types.Struct:
name := "unnamed"
if c.buildInfo.options.CollectedNamedTypes != nil {
for c.buildInfo.options.CollectedNamedTypes[name].Name == name {
name = name + "X"
}
_ = c.genStructExtended(x, name, c.buildInfo.options.CollectedNamedTypes)
}
for i := range x.NumFields() {
walk(x.Field(i).Type())
}
case *types.Named:
if typ, ok := x.Underlying().(*types.Struct); ok && !isInteropPath(x.String()) {
_ = c.genStructExtended(typ, x.Obj().Pkg().Path()+"."+x.Obj().Name(), c.buildInfo.options.CollectedNamedTypes)
for i := range typ.NumFields() {
walk(typ.Field(i).Type())
}
}
}
}

walk(t)
}

func (c *codegen) emitGetMapValueWithOKFlag(expr ast.Expr) {
var (
idxExpr = expr.(*ast.IndexExpr)
Expand Down
Loading
Loading