Merge pull request #1690 from nspcc-dev/usability-improvements

Compiler/CLI usability improvements
This commit is contained in:
Roman Khimov 2021-01-28 14:44:53 +03:00 committed by GitHub
commit 02d1411c0e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 108 deletions

View file

@ -187,6 +187,9 @@ func TestComlileAndInvokeFunction(t *testing.T) {
e.Run(t, "neo-go", "contract", "testinvokescript",
"--rpc-endpoint", "http://"+e.RPC.Addr,
"--in", nefName, "--", util.Uint160{1, 2, 3}.StringLE())
e.Run(t, "neo-go", "contract", "testinvokescript",
"--rpc-endpoint", "http://"+e.RPC.Addr,
"--in", nefName, "--", address.Uint160ToString(util.Uint160{1, 2, 3}))
e.In.WriteString("one\r")
e.Run(t, "neo-go", "contract", "deploy",

View file

@ -38,7 +38,7 @@ import (
var (
errNoInput = errors.New("no input file was found, specify an input file with the '--in or -i' flag")
errNoConfFile = errors.New("no config file was found, specify a config file with the '--config' or '-c' flag")
errNoManifestFile = errors.New("no manifest file was found, specify a manifest file with the '--manifest' flag")
errNoManifestFile = errors.New("no manifest file was found, specify manifest file with '--manifest' or '-m' flag")
errNoMethod = errors.New("no method specified for function invocation command")
errNoWallet = errors.New("no wallet parameter found, specify it with the '--wallet or -w' flag")
errNoScriptHash = errors.New("no smart contract hash was provided, specify one as the first argument")
@ -107,7 +107,7 @@ func NewCommands() []cli.Command {
Usage: "Input file for the smart contract (*.nef)",
},
cli.StringFlag{
Name: "manifest",
Name: "manifest, m",
Usage: "Manifest input file (*.manifest.json)",
},
walletFlag,
@ -170,10 +170,7 @@ func NewCommands() []cli.Command {
Name: "deploy",
Usage: "deploy a smart contract (.nef with description)",
Description: `Deploys given contract into the chain. The gas parameter is for additional
gas to be added as a network fee to prioritize the transaction. It may also
be required to add that to satisfy chain's policy regarding transaction size
and the minimum size fee (so if transaction send fails, try adding 0.001 GAS
to it).
gas to be added as a network fee to prioritize the transaction.
`,
Action: contractDeploy,
Flags: deployFlags,
@ -269,8 +266,8 @@ func NewCommands() []cli.Command {
Signers represent a set of Uint160 hashes with witness scopes and are used
to verify hashes in System.Runtime.CheckWitness syscall. First signer is treated
as a sender. To specify signers use signer[:scope] syntax where
* 'signer' is hex-encoded 160 bit (20 byte) LE value of signer's address,
which could have '0x' prefix.
* 'signer' is a signer's address (as Neo address or hex-encoded 160 bit (20 byte)
LE value with or without '0x' prefix).
* 'scope' is a comma-separated set of cosigner's scopes, which could be:
- 'None' - default witness scope which may be used for the sender
to only pay fee for the transaction.
@ -289,9 +286,9 @@ func NewCommands() []cli.Command {
neo-go RPC server only. C# implementation does not support scopes capability.
Examples:
* '0000000009070e030d0f0e020d0c06050e030c02'
* 'NNQk4QXsxvsrr3GSozoWBUxEmfag7B6hz5'
* 'NVquyZHoPirw6zAEPvY1ZezxM493zMWQqs:Global'
* '0x0000000009070e030d0f0e020d0c06050e030c02'
* '0x0000000009070e030d0f0e020d0c06050e030c02:Global'
* '0000000009070e030d0f0e020d0c06050e030c02:CalledByEntry,CustomGroups'
`,
Action: testInvokeFunction,
@ -891,7 +888,11 @@ func parseCosigner(c string) (transaction.Signer, error) {
if len(s) == 2*util.Uint160Size+2 && s[0:2] == "0x" {
s = s[2:]
}
res.Account, err = util.Uint160DecodeStringLE(s)
if len(s) == util.Uint160Size*2 {
res.Account, err = util.Uint160DecodeStringLE(s)
} else {
res.Account, err = address.StringToUint160(s)
}
if err != nil {
return res, err
}

View file

@ -42,20 +42,30 @@ It should return no value and accept single bool argument which will be true on
## Quick start
### Go setup
The compiler uses Go parser internally and depends on regular Go compiler
presence, so make sure you have it installed and set up. On some distributions
this requires you to set proper `GOROOT` environment variable, like
```
export GOROOT=/usr/lib64/go/1.14
```
### Compiling
```
./bin/neo-go contract compile -i mycontract.go
./bin/neo-go contract compile -i contract.go
```
By default the filename will be the name of your .go file with the .nef extension, the file will be located in the same directory where your Go contract is. If you want another location for your compiled contract:
```
./bin/neo-go contract compile -i mycontract.go --out /Users/foo/bar/contract.nef
./bin/neo-go contract compile -i contract.go --out /Users/foo/bar/contract.nef
```
If you contract is split across multiple files, you must provide a path
to the directory where package files are contained instead of a single Go file:
to the directory where package files are contained instead of a single Go file
(`out.nef` will be used as the default output file in this case):
```
./bin/neo-go contract compile -i ./path/to/contract
```
@ -64,7 +74,7 @@ to the directory where package files are contained instead of a single Go file:
You can dump the opcodes generated by the compiler with the following command:
```
./bin/neo-go contract inspect -i mycontract.go -c
./bin/neo-go contract inspect -i contract.go -c
```
This will result in something like this:
@ -133,7 +143,7 @@ Toolkit](https://github.com/neo-project/neo-blockchain-toolkit/). To do that
you need to generate debug information using `--debug` option, like this:
```
$ ./bin/neo-go contract compile -i contract.go -o contract.nef --debug contract.debug.json
$ ./bin/neo-go contract compile -i contract.go -c contract.yml -m contract.manifest.json -o contract.nef --debug contract.debug.json
```
This file can then be used by debugger and set up to work just like for any
@ -141,10 +151,18 @@ other supported language.
### Deploying
Deploying a contract to blockchain with neo-go requires a configuration file
with contract's metadata in YAML format, like the following:
Deploying a contract to blockchain with neo-go requires both NEF and JSON
manifest generated by the compiler from configuration file provided in YAML
format. To create contract manifest pass YAML file with `-c` parameter and
specify manifest output file with `-m`:
```
./bin/neo-go contract compile -i contract.go -c config.yml -m contract.manifest.json
```
Example YAML file contents:
```
name: Contract
safemethods: []
supportedstandards: []
events:
- name: info
@ -153,10 +171,10 @@ events:
type: ByteString
```
It's passed to the `deploy` command via `-c` option:
Then the manifest can be passed to the `deploy` command via `-m` option:
```
$ ./bin/neo-go contract deploy -i contract.nef -c contract.yml -r http://localhost:20331 -w wallet.json -g 0.001
$ ./bin/neo-go contract deploy -i contract.nef -m contract.manifest.json -r http://localhost:20331 -w wallet.json
```
Deployment works via an RPC server, an address of which is passed via `-r`
@ -201,93 +219,9 @@ $ ./bin/neo-go contract invokefunction -r http://localhost:20331 -w my_wallet.js
## Smart contract examples
Some examples are provided in the [examples directory](../examples).
### Check if the invoker of the contract is the owning address
```Golang
package mycontract
import (
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
"github.com/nspcc-dev/neo-go/pkg/interop/util"
)
var owner = util.FromAddress("AJX1jGfj3qPBbpAKjY527nPbnrnvSx9nCg")
func Main() bool {
isOwner := runtime.CheckWitness(owner)
if isOwner {
runtime.Log("invoker is the owner")
return true
}
return false
}
```
### Simple token
```Golang
package mytoken
import (
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
)
var owner = util.FromAddress("AJX1jGfj3qPBbpAKjY527nPbnrnvSx9nCg")
type Token struct {
Name string
Symbol string
TotalSupply int
Owner []byte
}
func (t Token) AddToCirculation(amount int) bool {
ctx := storage.Context()
var inCirc int
val := storage.Get(ctx, "in_circ")
if val != nil {
inCirc = val.(int)
}
inCirc += amount
storage.Put(ctx, "in_circ", inCirc)
return true
}
func newToken() Token {
return Token{
Name: "your awesome NEO token",
Symbol: "YANT",
TotalSupply: 1000,
Owner: owner,
}
}
func Main(operation string, args []interface{}) bool {
token := newToken()
trigger := runtime.GetTrigger()
if trigger == runtime.Verification {
isOwner := runtime.CheckWitness(token.Owner)
if isOwner {
return true
}
return false
}
if trigger == runtime.Application {
if operation == "mintTokens" {
token.AddToCirculation(100)
}
}
return true
}
```
Some examples are provided in the [examples directory](../examples). For more
sophisticated real-world contracts written in Go check out [NeoFS
contracts](https://github.com/nspcc-dev/neofs-contract/).
## How to report compiler bugs
1. Make a proper testcase (example testcases can be found in the tests folder)