diff --git a/internal/testchain/transaction.go b/internal/testchain/transaction.go index 1d0d8570d..30529c73a 100644 --- a/internal/testchain/transaction.go +++ b/internal/testchain/transaction.go @@ -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 } diff --git a/pkg/compiler/compiler_test.go b/pkg/compiler/compiler_test.go index 47e9c2933..0a82eb10b 100644 --- a/pkg/compiler/compiler_test.go +++ b/pkg/compiler/compiler_test.go @@ -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 diff --git a/pkg/compiler/debug_test.go b/pkg/compiler/debug_test.go index 4d0994cf4..447d4490f 100644 --- a/pkg/compiler/debug_test.go +++ b/pkg/compiler/debug_test.go @@ -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"}}) diff --git a/pkg/compiler/function_call_test.go b/pkg/compiler/function_call_test.go index c017fa542..5a265591b 100644 --- a/pkg/compiler/function_call_test.go +++ b/pkg/compiler/function_call_test.go @@ -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)) }) diff --git a/pkg/compiler/syscall_test.go b/pkg/compiler/syscall_test.go index e553e01c4..fe6ee2626 100644 --- a/pkg/compiler/syscall_test.go +++ b/pkg/compiler/syscall_test.go @@ -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() diff --git a/pkg/core/blockchain_test.go b/pkg/core/blockchain_test.go index 89f13c864..8d8b14e28 100644 --- a/pkg/core/blockchain_test.go +++ b/pkg/core/blockchain_test.go @@ -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) diff --git a/pkg/core/helper_test.go b/pkg/core/helper_test.go index 633bd6eb3..3b776966e 100644 --- a/pkg/core/helper_test.go +++ b/pkg/core/helper_test.go @@ -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) diff --git a/pkg/vm/cli/cli.go b/pkg/vm/cli/cli.go index ba2186a74..5b4878804 100644 --- a/pkg/vm/cli/cli.go +++ b/pkg/vm/cli/cli.go @@ -394,7 +394,9 @@ func handleLoadGo(c *ishell.Context) { c.Err(fmt.Errorf("%w: ", 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 diff --git a/pkg/vm/cli/cli_test.go b/pkg/vm/cli/cli_test.go index 3dcb3afd2..f6840c30d 100644 --- a/pkg/vm/cli/cli_test.go +++ b/pkg/vm/cli/cli_test.go @@ -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() diff --git a/scripts/gendump/main.go b/scripts/gendump/main.go index 15712d532..579e2fa02 100644 --- a/scripts/gendump/main.go +++ b/scripts/gendump/main.go @@ -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