diff --git a/cli/smartcontract/generate_test.go b/cli/smartcontract/generate_test.go index 6756adb82..b274e99f3 100644 --- a/cli/smartcontract/generate_test.go +++ b/cli/smartcontract/generate_test.go @@ -316,7 +316,6 @@ func NewReader(invoker Invoker) *ContractReader { return &ContractReader{invoker} } - // Get invokes `+"`get`"+` method of contract. func (c *ContractReader) Get() (*big.Int, error) { return unwrap.BigInt(c.invoker.Call(Hash, "get")) diff --git a/cli/smartcontract/testdata/gas/gas.go b/cli/smartcontract/testdata/gas/gas.go index d3c938c29..d12bbb8ee 100644 --- a/cli/smartcontract/testdata/gas/gas.go +++ b/cli/smartcontract/testdata/gas/gas.go @@ -44,4 +44,3 @@ func New(actor Actor) *Contract { var nep17t = nep17.New(actor, Hash) return &Contract{ContractReader{nep17t.TokenReader, actor}, nep17t.TokenWriter, actor} } - diff --git a/cli/smartcontract/testdata/nameservice/nns.go b/cli/smartcontract/testdata/nameservice/nns.go index efdfcabfe..c279951ea 100644 --- a/cli/smartcontract/testdata/nameservice/nns.go +++ b/cli/smartcontract/testdata/nameservice/nns.go @@ -59,7 +59,6 @@ func New(actor Actor) *Contract { return &Contract{ContractReader{nep11ndt.NonDivisibleReader, actor}, nep11ndt.BaseWriter, actor} } - // Roots invokes `roots` method of contract. func (c *ContractReader) Roots() (uuid.UUID, result.Iterator, error) { return unwrap.SessionIterator(c.invoker.Call(Hash, "roots")) diff --git a/cli/smartcontract/testdata/nex/nex.go b/cli/smartcontract/testdata/nex/nex.go index 4a4a46de4..068fc1987 100644 --- a/cli/smartcontract/testdata/nex/nex.go +++ b/cli/smartcontract/testdata/nex/nex.go @@ -56,7 +56,6 @@ func New(actor Actor) *Contract { return &Contract{ContractReader{nep17t.TokenReader, actor}, nep17t.TokenWriter, actor} } - // Cap invokes `cap` method of contract. func (c *ContractReader) Cap() (*big.Int, error) { return unwrap.BigInt(c.invoker.Call(Hash, "cap")) diff --git a/cli/smartcontract/testdata/nonepiter/iter.go b/cli/smartcontract/testdata/nonepiter/iter.go index e04f6953c..f298ab8de 100644 --- a/cli/smartcontract/testdata/nonepiter/iter.go +++ b/cli/smartcontract/testdata/nonepiter/iter.go @@ -30,7 +30,6 @@ func NewReader(invoker Invoker) *ContractReader { return &ContractReader{invoker} } - // Tokens invokes `tokens` method of contract. func (c *ContractReader) Tokens() (uuid.UUID, result.Iterator, error) { return unwrap.SessionIterator(c.invoker.Call(Hash, "tokens")) diff --git a/cli/smartcontract/testdata/structs/rpcbindings.out b/cli/smartcontract/testdata/structs/rpcbindings.out index 8b165ec20..29af22323 100644 --- a/cli/smartcontract/testdata/structs/rpcbindings.out +++ b/cli/smartcontract/testdata/structs/rpcbindings.out @@ -17,7 +17,6 @@ import ( // Hash contains contract hash. var Hash = util.Uint160{0x33, 0x22, 0x11, 0x0, 0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x0} - // LedgerBlock is a contract-specific ledger.Block type used by its methods. type LedgerBlock struct { Hash util.Uint256 @@ -154,6 +153,7 @@ type StructsInternal struct { Map map[*big.Int]keys.PublicKeys Struct *StructsInternal } + // Invoker is used by ContractReader to call various safe methods. type Invoker interface { Call(contract util.Uint160, operation string, params ...any) (*result.Invoke, error) @@ -169,7 +169,6 @@ func NewReader(invoker Invoker) *ContractReader { return &ContractReader{invoker} } - // Block invokes `block` method of contract. func (c *ContractReader) Block(b *LedgerBlock) (*LedgerBlock, error) { return itemToLedgerBlock(unwrap.Item(c.invoker.Call(Hash, "block", b))) @@ -309,7 +308,6 @@ func (res *LedgerBlock) FromStackItem(item stackitem.Item) error { return fmt.Errorf("field TransactionsLength: %w", err) } - return nil } @@ -448,7 +446,6 @@ func (res *LedgerBlockSR) FromStackItem(item stackitem.Item) error { return fmt.Errorf("field PrevStateRoot: %w", err) } - return nil } @@ -545,7 +542,6 @@ func (res *LedgerTransaction) FromStackItem(item stackitem.Item) error { return fmt.Errorf("field Script: %w", err) } - return nil } @@ -673,7 +669,6 @@ func (res *LedgerTransactionSigner) FromStackItem(item stackitem.Item) error { return fmt.Errorf("field Rules: %w", err) } - return nil } @@ -714,7 +709,6 @@ func (res *LedgerWitnessCondition) FromStackItem(item stackitem.Item) error { return fmt.Errorf("field Value: %w", err) } - return nil } @@ -755,7 +749,6 @@ func (res *LedgerWitnessRule) FromStackItem(item stackitem.Item) error { return fmt.Errorf("field Condition: %w", err) } - return nil } @@ -822,7 +815,6 @@ func (res *ManagementABI) FromStackItem(item stackitem.Item) error { return fmt.Errorf("field Events: %w", err) } - return nil } @@ -891,7 +883,6 @@ func (res *ManagementContract) FromStackItem(item stackitem.Item) error { return fmt.Errorf("field Manifest: %w", err) } - return nil } @@ -954,7 +945,6 @@ func (res *ManagementEvent) FromStackItem(item stackitem.Item) error { return fmt.Errorf("field Params: %w", err) } - return nil } @@ -1005,7 +995,6 @@ func (res *ManagementGroup) FromStackItem(item stackitem.Item) error { return fmt.Errorf("field Signature: %w", err) } - return nil } @@ -1198,7 +1187,6 @@ func (res *ManagementManifest) FromStackItem(item stackitem.Item) error { return fmt.Errorf("field Extra: %w", err) } - return nil } @@ -1279,7 +1267,6 @@ func (res *ManagementMethod) FromStackItem(item stackitem.Item) error { return fmt.Errorf("field Safe: %w", err) } - return nil } @@ -1329,7 +1316,6 @@ func (res *ManagementParameter) FromStackItem(item stackitem.Item) error { return fmt.Errorf("field Type: %w", err) } - return nil } @@ -1402,7 +1388,6 @@ func (res *ManagementPermission) FromStackItem(item stackitem.Item) error { return fmt.Errorf("field Methods: %w", err) } - return nil } @@ -1635,6 +1620,5 @@ func (res *StructsInternal) FromStackItem(item stackitem.Item) error { return fmt.Errorf("field Struct: %w", err) } - return nil } diff --git a/cli/smartcontract/testdata/types/rpcbindings.out b/cli/smartcontract/testdata/types/rpcbindings.out index 94bf63910..ca4f0ce36 100644 --- a/cli/smartcontract/testdata/types/rpcbindings.out +++ b/cli/smartcontract/testdata/types/rpcbindings.out @@ -31,7 +31,6 @@ func NewReader(invoker Invoker) *ContractReader { return &ContractReader{invoker} } - // AAAStrings invokes `aAAStrings` method of contract. func (c *ContractReader) AAAStrings(s [][][]string) ([][][]string, error) { return func (item stackitem.Item, err error) ([][][]string, error) { diff --git a/cli/smartcontract/testdata/verifyrpc/verify.go b/cli/smartcontract/testdata/verifyrpc/verify.go index 4a9af0156..a614c36cf 100644 --- a/cli/smartcontract/testdata/verifyrpc/verify.go +++ b/cli/smartcontract/testdata/verifyrpc/verify.go @@ -30,7 +30,6 @@ func New(actor Actor) *Contract { return &Contract{actor} } - func scriptForVerify() ([]byte, error) { return smartcontract.CreateCallWithAssertScript(Hash, "verify") } diff --git a/pkg/smartcontract/rpcbinding/binding.go b/pkg/smartcontract/rpcbinding/binding.go index 6443f5c64..58ed76240 100644 --- a/pkg/smartcontract/rpcbinding/binding.go +++ b/pkg/smartcontract/rpcbinding/binding.go @@ -12,8 +12,12 @@ import ( "github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest/standard" ) -const srcTmpl = ` -{{- define "SAFEMETHOD" -}} +// The set of constants containing parts of RPC binding template. Each block of code +// including template definition and var/type/method definitions contain new line at the +// start and ends with a new line. On adding new block of code to the template, please, +// ensure that this block has new line at the start and in the end of the block. +const ( + safemethodDefinition = `{{ define "SAFEMETHOD" }} // {{.Name}} {{.Comment}} func (c *ContractReader) {{.Name}}({{range $index, $arg := .Arguments -}} {{- if ne $index 0}}, {{end}} @@ -32,8 +36,7 @@ func (c *ContractReader) {{.Name}}({{range $index, $arg := .Arguments -}} {{- range $arg := .Arguments -}}, {{.Name}}{{end}}) {{- end}} } -{{- if eq .Unwrapper "SessionIterator"}} - +{{ if eq .Unwrapper "SessionIterator" }} // {{.Name}}Expanded is similar to {{.Name}} (uses the same contract // method), but can be useful if the server used doesn't support sessions and // doesn't expand iterators. It creates a script that will get the specified @@ -42,17 +45,16 @@ func (c *ContractReader) {{.Name}}({{range $index, $arg := .Arguments -}} func (c *ContractReader) {{.Name}}Expanded({{range $index, $arg := .Arguments}}{{.Name}} {{.Type}}, {{end}}_numOfIteratorItems int) ([]stackitem.Item, error) { return unwrap.Array(c.invoker.CallAndExpandIterator(Hash, "{{.NameABI}}", _numOfIteratorItems{{range $arg := .Arguments}}, {{.Name}}{{end}})) } -{{- end -}} -{{- end -}} -{{- define "METHOD" -}} -{{- if eq .ReturnType "bool"}}func scriptFor{{.Name}}({{range $index, $arg := .Arguments -}} +{{ end }}{{ end }}` + methodDefinition = `{{ define "METHOD" }}{{ if eq .ReturnType "bool"}} +func scriptFor{{.Name}}({{range $index, $arg := .Arguments -}} {{- if ne $index 0}}, {{end}} {{- .Name}} {{.Type}} {{- end}}) ([]byte, error) { return smartcontract.CreateCallWithAssertScript(Hash, "{{ .NameABI }}"{{- range $index, $arg := .Arguments -}}, {{.Name}}{{end}}) } - -{{end}}// {{.Name}} {{.Comment}} +{{ end }} +// {{.Name}} {{.Comment}} // This transaction is signed and immediately sent to the network. // The values returned are its hash, ValidUntilBlock value and error if any. func (c *Contract) {{.Name}}({{range $index, $arg := .Arguments -}} @@ -97,8 +99,9 @@ func (c *Contract) {{.Name}}Unsigned({{range $index, $arg := .Arguments -}} } return c.actor.MakeUnsignedRun(script, nil){{end}} } -{{- end -}} -// Package {{.PackageName}} contains RPC wrappers for {{.ContractName}} contract. +{{end}}` + + bindingDefinition = `// Package {{.PackageName}} contains RPC wrappers for {{.ContractName}} contract. package {{.PackageName}} import ( @@ -107,16 +110,16 @@ import ( // Hash contains contract hash. var Hash = {{ .Hash }} - -{{range $name, $typ := .NamedTypes}} +{{ range $name, $typ := .NamedTypes }} // {{toTypeName $name}} is a contract-specific {{$name}} type used by its methods. type {{toTypeName $name}} struct { {{- range $m := $typ.Fields}} {{.Field}} {{etTypeToStr .ExtendedType}} {{- end}} } -{{end -}} -{{if .HasReader}}// Invoker is used by ContractReader to call various safe methods. +{{end}} +{{- if .HasReader}} +// Invoker is used by ContractReader to call various safe methods. type Invoker interface { {{if or .IsNep11D .IsNep11ND}} nep11.Invoker {{else -}} @@ -129,9 +132,9 @@ type Invoker interface { {{end -}} {{end -}} } - {{end -}} -{{if .HasWriter}}// Actor is used by Contract to call state-changing methods. +{{- if .HasWriter}} +// Actor is used by Contract to call state-changing methods. type Actor interface { {{- if .HasReader}} Invoker @@ -150,9 +153,9 @@ type Actor interface { SendRun(script []byte) (util.Uint256, uint32, error) {{end -}} } - {{end -}} -{{if .HasReader}}// ContractReader implements safe contract methods. +{{- if .HasReader}} +// ContractReader implements safe contract methods. type ContractReader struct { {{if .IsNep11D}}nep11.DivisibleReader {{end -}} @@ -162,9 +165,9 @@ type ContractReader struct { {{end -}} invoker Invoker } - {{end -}} -{{if .HasWriter}}// Contract implements all contract methods. +{{- if .HasWriter}} +// Contract implements all contract methods. type Contract struct { {{if .HasReader}}ContractReader {{end -}} @@ -176,9 +179,9 @@ type Contract struct { {{end -}} actor Actor } - {{end -}} -{{if .HasReader}}// NewReader creates an instance of ContractReader using Hash and the given Invoker. +{{- if .HasReader}} +// NewReader creates an instance of ContractReader using Hash and the given Invoker. func NewReader(invoker Invoker) *ContractReader { return &ContractReader{ {{- if .IsNep11D}}*nep11.NewDivisibleReader(invoker, Hash), {{end}} @@ -186,9 +189,9 @@ func NewReader(invoker Invoker) *ContractReader { {{- if .IsNep17}}*nep17.NewReader(invoker, Hash), {{end -}} invoker} } - {{end -}} -{{if .HasWriter}}// New creates an instance of Contract using Hash and the given Actor. +{{- if .HasWriter}} +// New creates an instance of Contract using Hash and the given Actor. func New(actor Actor) *Contract { {{if .IsNep11D}}var nep11dt = nep11.NewDivisible(actor, Hash) {{end -}} @@ -207,15 +210,10 @@ func New(actor Actor) *Contract { {{- if .IsNep17}}nep17t.TokenWriter, {{end -}} actor} } - {{end -}} -{{range $m := .SafeMethods}} -{{template "SAFEMETHOD" $m }} -{{end}} -{{- range $m := .Methods}} -{{template "METHOD" $m }} -{{end}} -{{- range $name, $typ := .NamedTypes}} +{{- range $m := .SafeMethods }}{{template "SAFEMETHOD" $m }}{{ end -}} +{{- range $m := .Methods -}}{{template "METHOD" $m }}{{ end -}} +{{- range $name, $typ := .NamedTypes }} // itemTo{{toTypeName $name}} converts stack item into *{{toTypeName $name}}. func itemTo{{toTypeName $name}}(item stackitem.Item, err error) (*{{toTypeName $name}}, error) { if err != nil { @@ -236,8 +234,8 @@ func (res *{{toTypeName $name}}) FromStackItem(item stackitem.Item) error { if len(arr) != {{len $typ.Fields}} { return errors.New("wrong number of structure elements") } - -{{if len .Fields}} var ( +{{if len .Fields}} + var ( index = -1 err error ) @@ -248,10 +246,15 @@ func (res *{{toTypeName $name}}) FromStackItem(item stackitem.Item) error { return fmt.Errorf("field {{.Field}}: %w", err) } {{end}} -{{end}} +{{- end}} return nil } -{{end}}` +{{end -}}` + + srcTmpl = bindingDefinition + + safemethodDefinition + + methodDefinition +) type ( ContractTmpl struct {