compiler: provide .go filename to Compile

First argument contains filename, thus we use '.go' suffix to
distinguish between directories and files.
Contract name should be provided in options.

Signed-off-by: Evgeniy Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgeniy Stratonikov 2021-12-02 16:36:29 +03:00
parent 8af9c870b1
commit e7a0ecb349
10 changed files with 35 additions and 28 deletions

View file

@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
gio "io"
"strings"
"github.com/nspcc-dev/neo-go/cli/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/compiler"
@ -50,18 +51,14 @@ func NewTransferFromOwner(bc blockchainer.Blockchainer, contractHash, to util.Ui
return tx, SignTx(bc, tx)
}
// NewDeployTx returns new deployment tx for contract with name with Go code read from r.
// NewDeployTx returns new deployment for contract with source from r and name equal to
// filename without '.go' suffix.
func NewDeployTx(bc blockchainer.Blockchainer, name string, sender util.Uint160, r gio.Reader, confFile *string) (*transaction.Transaction, util.Uint160, []byte, error) {
// nef.NewFile() cares about version a lot.
config.Version = "0.90.0-test"
ne, di, err := compiler.CompileWithOptions(name, r, nil)
if err != nil {
return nil, util.Uint160{}, nil, err
}
o := &compiler.Options{
Name: name,
Name: strings.TrimSuffix(name, ".go"),
NoStandardCheck: true,
NoEventsCheck: true,
}
@ -79,6 +76,12 @@ func NewDeployTx(bc blockchainer.Blockchainer, name string, sender util.Uint160,
}
o.SafeMethods = conf.SafeMethods
}
ne, di, err := compiler.CompileWithOptions(name, r, o)
if err != nil {
return nil, util.Uint160{}, nil, err
}
m, err := compiler.CreateManifest(di, o)
if err != nil {
return nil, util.Uint160{}, nil, fmt.Errorf("failed to create manifest: %w", err)
@ -100,7 +103,7 @@ func NewDeployTx(bc blockchainer.Blockchainer, name string, sender util.Uint160,
tx := transaction.New(buf.Bytes(), 100*native.GASFactor)
tx.Signers = []transaction.Signer{{Account: sender}}
h := state.CreateContractHash(tx.Sender(), ne.Checksum, name)
h := state.CreateContractHash(tx.Sender(), ne.Checksum, m.Name)
return tx, h, ne.Script, nil
}

View file

@ -93,7 +93,7 @@ func compileFile(src string) error {
func TestOnPayableChecks(t *testing.T) {
compileAndCheck := func(t *testing.T, src string) error {
_, di, err := compiler.CompileWithOptions("payable", strings.NewReader(src), nil)
_, di, err := compiler.CompileWithOptions("payable.go", strings.NewReader(src), nil)
require.NoError(t, err)
_, err = compiler.CreateManifest(di, &compiler.Options{})
return err
@ -129,7 +129,8 @@ func TestSafeMethodWarnings(t *testing.T) {
src := `package payable
func Main() int { return 1 }`
_, di, err := compiler.CompileWithOptions("eventTest", strings.NewReader(src), nil)
_, di, err := compiler.CompileWithOptions("eventTest.go", strings.NewReader(src),
&compiler.Options{Name: "eventTest"})
require.NoError(t, err)
_, err = compiler.CreateManifest(di, &compiler.Options{SafeMethods: []string{"main"}})
@ -144,7 +145,7 @@ func TestEventWarnings(t *testing.T) {
import "github.com/nspcc-dev/neo-go/pkg/interop/runtime"
func Main() { runtime.Notify("Event", 1) }`
_, di, err := compiler.CompileWithOptions("eventTest", strings.NewReader(src), nil)
_, di, err := compiler.CompileWithOptions("eventTest.go", strings.NewReader(src), nil)
require.NoError(t, err)
t.Run("event it missing from config", func(t *testing.T) {
@ -188,7 +189,7 @@ func TestEventWarnings(t *testing.T) {
return notify.Value
}`
_, di, err := compiler.CompileWithOptions("eventTest", strings.NewReader(src), nil)
_, di, err := compiler.CompileWithOptions("eventTest.go", strings.NewReader(src), &compiler.Options{Name: "eventTest"})
require.NoError(t, err)
_, err = compiler.CreateManifest(di, &compiler.Options{NoEventsCheck: true})
@ -202,7 +203,8 @@ func TestEventWarnings(t *testing.T) {
return 42
}`
_, di, err := compiler.CompileWithOptions("eventTest", strings.NewReader(src), nil)
_, di, err := compiler.CompileWithOptions("eventTest.go",
strings.NewReader(src), &compiler.Options{Name: "eventTest"})
require.NoError(t, err)
_, err = compiler.CreateManifest(di, &compiler.Options{})
@ -224,12 +226,12 @@ func TestNotifyInVerify(t *testing.T) {
for _, name := range []string{"Notify", "Log"} {
t.Run(name, func(t *testing.T) {
src := fmt.Sprintf(srcTmpl, name)
_, _, err := compiler.CompileWithOptions("eventTest", strings.NewReader(src),
_, _, err := compiler.CompileWithOptions("eventTest.go", strings.NewReader(src),
&compiler.Options{ContractEvents: []manifest.Event{{Name: "Event"}}})
require.Error(t, err)
t.Run("suppress", func(t *testing.T) {
_, _, err := compiler.CompileWithOptions("eventTest", strings.NewReader(src),
_, _, err := compiler.CompileWithOptions("eventTest.go", strings.NewReader(src),
&compiler.Options{NoEventsCheck: true})
require.NoError(t, err)
})
@ -258,7 +260,7 @@ func TestInvokedContractsPermissons(t *testing.T) {
return 0
}`
_, di, err := compiler.CompileWithOptions("permissionTest", strings.NewReader(src), &compiler.Options{})
_, di, err := compiler.CompileWithOptions("permissionTest.go", strings.NewReader(src), nil)
require.NoError(t, err)
var nh util.Uint160
@ -302,7 +304,7 @@ func TestInvokedContractsPermissons(t *testing.T) {
contract.Call(runh.RuntimeHash(), "method4", contract.All)
}`, hashStr)
_, di, err := compiler.CompileWithOptions("permissionTest", strings.NewReader(src), &compiler.Options{})
_, di, err := compiler.CompileWithOptions("permissionTest.go", strings.NewReader(src), nil)
require.NoError(t, err)
var h util.Uint160

View file

@ -365,7 +365,7 @@ func TestManifestOverload(t *testing.T) {
return 4
}`
_, di, err := CompileWithOptions("foo", strings.NewReader(src), nil)
_, di, err := CompileWithOptions("foo.go", strings.NewReader(src), nil)
require.NoError(t, err)
m, err := di.ConvertToManifest(&Options{Overloads: map[string]string{"add3Aux": "add3"}})

View file

@ -302,7 +302,7 @@ func TestJumpOptimize(t *testing.T) {
func Main() int {
return Get3()
}`
b, di, err := compiler.CompileWithOptions("", strings.NewReader(src), nil)
b, di, err := compiler.CompileWithOptions("file.go", strings.NewReader(src), nil)
require.NoError(t, err)
require.Equal(t, 6, len(di.Methods))
for _, mi := range di.Methods {
@ -333,7 +333,7 @@ func TestUnusedFunctions(t *testing.T) {
return nestedcall.X
}`
b, err := compiler.Compile("foo", strings.NewReader(src))
b, err := compiler.Compile("foo.go", strings.NewReader(src))
require.NoError(t, err)
require.Equal(t, 3, len(b)) // PUSHINT8 (42) + RET
eval(t, src, big.NewInt(42))
@ -346,7 +346,7 @@ func TestUnusedFunctions(t *testing.T) {
return inner.N()
}`
_, err := compiler.Compile("foo", strings.NewReader(src))
_, err := compiler.Compile("foo.go", strings.NewReader(src))
require.NoError(t, err)
eval(t, src, big.NewInt(65))
})
@ -359,7 +359,7 @@ func TestUnusedFunctions(t *testing.T) {
return t.Method()
}`
_, err := compiler.Compile("foo", strings.NewReader(src))
_, err := compiler.Compile("foo.go", strings.NewReader(src))
require.NoError(t, err)
eval(t, src, big.NewInt(2231))
})

View file

@ -139,7 +139,7 @@ func runSyscallTestCase(t *testing.T, ic *interop.Context, goName string, tc sys
}
ss := strings.Split(goName, ".")
src := fmt.Sprintf(srcTmpl, ss[0], goName, strings.Join(tc.params, ", "))
b, _, err := compiler.CompileWithOptions("foo", strings.NewReader(src), nil)
b, _, err := compiler.CompileWithOptions("foo.go", strings.NewReader(src), nil)
require.NoError(t, err)
v := ic.SpawnVM()

View file

@ -1257,7 +1257,7 @@ func TestIsTxStillRelevant(t *testing.T) {
currentHeight := contract.Call(addr, "currentIndex", contract.ReadStates)
return currentHeight.(int) < %d
}`, bc.BlockHeight()+2) // deploy + next block
txDeploy, h, _, err := testchain.NewDeployTx(bc, "TestVerify", neoOwner, strings.NewReader(src), nil)
txDeploy, h, _, err := testchain.NewDeployTx(bc, "TestVerify.go", neoOwner, strings.NewReader(src), nil)
require.NoError(t, err)
txDeploy.ValidUntilBlock = bc.BlockHeight() + 1
addSigners(neoOwner, txDeploy)

View file

@ -187,7 +187,7 @@ func TestBug1728(t *testing.T) {
func _deploy(_ interface{}, isUpdate bool) {
runtime.Log("Deploy")
}`
nf, di, err := compiler.CompileWithOptions("foo", strings.NewReader(src), nil)
nf, di, err := compiler.CompileWithOptions("foo.go", strings.NewReader(src), nil)
require.NoError(t, err)
m, err := di.ConvertToManifest(&compiler.Options{Name: "TestContract"})
require.NoError(t, err)

View file

@ -394,7 +394,9 @@ func handleLoadGo(c *ishell.Context) {
c.Err(fmt.Errorf("%w: <file>", ErrMissingParameter))
return
}
b, di, err := compiler.CompileWithOptions(c.Args[0], nil, nil)
name := strings.TrimSuffix(c.Args[0], ".go")
b, di, err := compiler.CompileWithOptions(c.Args[0], nil, &compiler.Options{Name: name})
if err != nil {
c.Err(err)
return

View file

@ -243,7 +243,7 @@ func TestLoad(t *testing.T) {
t.Run("loadnef", func(t *testing.T) {
config.Version = "0.92.0-test"
nefFile, di, err := compiler.CompileWithOptions("test", strings.NewReader(src), nil)
nefFile, di, err := compiler.CompileWithOptions("test.go", strings.NewReader(src), nil)
require.NoError(t, err)
filename := filepath.Join(tmpDir, "vmtestcontract.nef")
rawNef, err := nefFile.Bytes()

View file

@ -75,7 +75,7 @@ func main() {
handleError("can't tranfser GAS", err)
lastBlock = addBlock(bc, lastBlock, valScript, txMoveNeo, txMoveGas)
tx, contractHash, _, err := testchain.NewDeployTx(bc, "DumpContract", h, strings.NewReader(contract), nil)
tx, contractHash, _, err := testchain.NewDeployTx(bc, "DumpContract.go", h, strings.NewReader(contract), nil)
handleError("can't create deploy tx", err)
tx.NetworkFee = 10_000_000
tx.ValidUntilBlock = bc.BlockHeight() + 1