Skip to content

Commit f0cdf1b

Browse files
author
Tural Devrishev
committed
compiler: set SourceURL in NEF file
Close #3543. Signed-off-by: Tural Devrishev <[email protected]>
1 parent 82f975e commit f0cdf1b

File tree

5 files changed

+16
-12
lines changed

5 files changed

+16
-12
lines changed

cli/vm/cli_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1129,7 +1129,7 @@ func TestRunWithHistoricState(t *testing.T) {
11291129
e.checkNextLine(t, "READY: loaded 36 instructions")
11301130
e.checkStack(t, []byte{1})
11311131
e.checkNextLine(t, "READY: loaded 36 instructions")
1132-
e.checkNextLineExact(t, "Error: at instruction 31 (SYSCALL): System.Contract.Call failed: called contract 0f825b050eb8ce9eaa82993e90615025ab798016 not found: key not found\n")
1132+
e.checkNextLineExact(t, "Error: at instruction 31 (SYSCALL): System.Contract.Call failed: called contract fc78291ebacd4dc1d4d6a16399ccb1fac8438d93 not found: key not found\n")
11331133
}
11341134

11351135
func TestEvents(t *testing.T) {

pkg/compiler/codegen.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2508,6 +2508,12 @@ func codeGen(info *buildInfo) (*nef.File, *DebugInfo, error) {
25082508
if c.callTokens != nil {
25092509
f.Tokens = c.callTokens
25102510
}
2511+
if info.options != nil && info.options.SourceURL != "" {
2512+
if len(info.options.SourceURL) > nef.MaxSourceURLLength {
2513+
return nil, nil, errors.New("too long source URL")
2514+
}
2515+
f.Source = info.options.SourceURL
2516+
}
25112517
f.Checksum = f.CalculateChecksum()
25122518
return f, di, vm.IsScriptCorrect(buf, methods)
25132519
}

pkg/compiler/compiler.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package compiler
22

33
import (
44
"encoding/json"
5-
"errors"
65
"fmt"
76
"go/ast"
87
"go/parser"
@@ -267,13 +266,6 @@ func CompileAndSave(src string, o *Options) ([]byte, error) {
267266
if err != nil {
268267
return nil, fmt.Errorf("error while trying to compile smart contract file: %w", err)
269268
}
270-
if o.SourceURL != "" {
271-
if len(o.SourceURL) > nef.MaxSourceURLLength {
272-
return nil, errors.New("too long source URL")
273-
}
274-
f.Source = o.SourceURL
275-
f.Checksum = f.CalculateChecksum()
276-
}
277269
bytes, err := f.Bytes()
278270
if err != nil {
279271
return nil, fmt.Errorf("error while serializing .nef file: %w", err)

pkg/neotest/compile.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,6 @@ func CompileFile(t testing.TB, sender util.Uint160, srcPath string, configPath s
6262
// nef.NewFile() cares about version a lot.
6363
config.Version = "neotest"
6464

65-
ne, di, err := compiler.CompileWithOptions(srcPath, nil, nil)
66-
require.NoError(t, err)
67-
6865
conf, err := smartcontract.ParseContractConfig(configPath)
6966
require.NoError(t, err)
7067

@@ -80,6 +77,8 @@ func CompileFile(t testing.TB, sender util.Uint160, srcPath string, configPath s
8077
o.SafeMethods = conf.SafeMethods
8178
o.Overloads = conf.Overloads
8279
o.SourceURL = conf.SourceURL
80+
ne, di, err := compiler.CompileWithOptions(srcPath, nil, o)
81+
require.NoError(t, err)
8382
m, err := compiler.CreateManifest(di, o)
8483
require.NoError(t, err)
8584

pkg/neotest/compile_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,10 @@ func TestCompileFileCashedIdentifiers(t *testing.T) {
2626
contract2 := CompileFile(t, sender, srcPath, configPath2)
2727
require.NotEqual(t, contract1, contract2)
2828
}
29+
30+
func TestAddSourceURLToNEF(t *testing.T) {
31+
srcPath := "../../internal/basicchain/testdata/test_contract.go"
32+
configPath := "../../internal/basicchain/testdata/test_contract.yml"
33+
ctr := CompileFile(t, util.Uint160{}, srcPath, configPath)
34+
require.NotEqual(t, "", ctr.NEF.Source)
35+
}

0 commit comments

Comments
 (0)