diff --git a/.circleci/config.yml b/.circleci/config.yml index 586ea9719..c025f694b 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -3,11 +3,6 @@ orbs: codecov: codecov/codecov@1.0.5 executors: - go1_14: - docker: - - image: circleci/golang:1.14 - environment: - GO111MODULE: "on" go1_15: docker: - image: circleci/golang:1.15 @@ -18,6 +13,9 @@ executors: - image: circleci/golang:1.16 environment: GO111MODULE: "on" + go1_17: + docker: + - image: circleci/golang:1.17 commands: gomod: @@ -34,7 +32,7 @@ commands: jobs: lint: working_directory: /go/src/github.com/nspcc-dev/neo-go - executor: go1_16 + executor: go1_17 steps: - checkout - gomod @@ -44,15 +42,6 @@ jobs: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.40.0 make lint - test_1_14: - working_directory: /go/src/github.com/nspcc-dev/neo-go - executor: go1_14 - steps: - - checkout - - run: git submodule sync - - run: git submodule update --init - - gomod - - run: go test -v -race ./... test_1_15: working_directory: /go/src/github.com/nspcc-dev/neo-go executor: go1_15 @@ -63,9 +52,19 @@ jobs: - gomod - run: go test -v -race ./... - test_cover: + test_1_16: working_directory: /go/src/github.com/nspcc-dev/neo-go executor: go1_16 + steps: + - checkout + - run: git submodule sync + - run: git submodule update --init + - gomod + - run: go test -v -race ./... + + test_cover: + working_directory: /go/src/github.com/nspcc-dev/neo-go + executor: go1_17 environment: CGO_ENABLED: 0 steps: @@ -79,7 +78,7 @@ jobs: build_cli: working_directory: /go/src/github.com/nspcc-dev/neo-go - executor: go1_16 + executor: go1_17 steps: - checkout - gomod @@ -90,7 +89,7 @@ jobs: build_image: working_directory: /go/src/github.com/nspcc-dev/neo-go - executor: go1_16 + executor: go1_17 docker: - image: golang:1-alpine steps: @@ -118,11 +117,11 @@ workflows: filters: tags: only: v/[0-9]+\.[0-9]+\.[0-9]+/ - - test_1_14: + - test_1_15: filters: tags: only: v/[0-9]+\.[0-9]+\.[0-9]+/ - - test_1_15: + - test_1_16: filters: tags: only: v/[0-9]+\.[0-9]+\.[0-9]+/ diff --git a/.github/workflows/publish_to_dockerhub.yml b/.github/workflows/publish_to_dockerhub.yml index b7ea9ea94..b3db4dad7 100644 --- a/.github/workflows/publish_to_dockerhub.yml +++ b/.github/workflows/publish_to_dockerhub.yml @@ -57,7 +57,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v2 with: - go-version: 1.16 + go-version: 1.17 - name: Restore go modules from cache uses: actions/cache@v2 @@ -94,7 +94,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v2 with: - go-version: 1.16 + go-version: 1.17 - name: Restore go modules from cache uses: actions/cache@v2 diff --git a/.github/workflows/run_tests.yml b/.github/workflows/run_tests.yml index 43d4587e3..f77f0541a 100644 --- a/.github/workflows/run_tests.yml +++ b/.github/workflows/run_tests.yml @@ -44,7 +44,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v2 with: - go-version: 1.16 + go-version: 1.17 - name: Restore Go modules from cache uses: actions/cache@v2 @@ -70,7 +70,7 @@ jobs: runs-on: ubuntu-18.04 strategy: matrix: - go_versions: [ '1.14', '1.15', '1.16' ] + go_versions: [ '1.15', '1.16' ] fail-fast: false steps: - uses: actions/checkout@v2 @@ -111,7 +111,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v2 with: - go-version: 1.16 + go-version: 1.17 - name: Restore Go modules from cache uses: actions/cache@v2 @@ -138,7 +138,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v2 with: - go-version: 1.16 + go-version: 1.17 - name: Restore Go modules from cache uses: actions/cache@v2 diff --git a/.travis.yml b/.travis.yml index 77e2b66aa..a4fab5637 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,6 @@ language: go go: - - 1.14.x + - 1.15.x env: - GO111MODULE=on install: diff --git a/README.md b/README.md index 8c45e8032..f91dd9983 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ NeoGo, `:latest` points to the latest release) or build yourself. ### Building -To build NeoGo you need Go 1.14+ and `make`: +To build NeoGo you need Go 1.15+ and `make`: ``` make build diff --git a/cli/contract_test.go b/cli/contract_test.go index 6e865a746..36bd0e811 100644 --- a/cli/contract_test.go +++ b/cli/contract_test.go @@ -28,6 +28,7 @@ import ( ) func TestCalcHash(t *testing.T) { + tmpDir := t.TempDir() e := newExecutor(t, false) nefPath := "./testdata/verify.nef" @@ -58,13 +59,9 @@ func TestCalcHash(t *testing.T) { "--in", "./testdata/verify.nef123", "--manifest", manifestPath)...) }) t.Run("invalid file", func(t *testing.T) { - p, err := ioutil.TempFile("", "neogo.calchash.verify.nef") - require.NoError(t, err) - t.Cleanup(func() { - os.Remove(p.Name()) - }) - require.NoError(t, ioutil.WriteFile(p.Name(), src[:4], os.ModePerm)) - e.RunWithError(t, append(cmd, "--sender", sender.StringLE(), "--in", p.Name(), "--manifest", manifestPath)...) + p := path.Join(tmpDir, "neogo.calchash.verify.nef") + require.NoError(t, ioutil.WriteFile(p, src[:4], os.ModePerm)) + e.RunWithError(t, append(cmd, "--sender", sender.StringLE(), "--in", p, "--manifest", manifestPath)...) }) cmd = append(cmd, "--in", nefPath, "--manifest", manifestPath) @@ -84,12 +81,7 @@ func TestCalcHash(t *testing.T) { } func TestContractInitAndCompile(t *testing.T) { - tmpDir, err := ioutil.TempDir("", "neogo.inittest") - require.NoError(t, err) - t.Cleanup(func() { - os.RemoveAll(tmpDir) - }) - + tmpDir := t.TempDir() e := newExecutor(t, false) t.Run("no path is provided", func(t *testing.T) { @@ -148,12 +140,7 @@ func TestDeployBigContract(t *testing.T) { // For proper nef generation. config.Version = "0.90.0-test" - - tmpDir, err := ioutil.TempDir("", "neogo.test.deployfail") - require.NoError(t, err) - t.Cleanup(func() { - os.RemoveAll(tmpDir) - }) + tmpDir := t.TempDir() nefName := path.Join(tmpDir, "deploy.nef") manifestName := path.Join(tmpDir, "deploy.manifest.json") @@ -174,12 +161,7 @@ func TestContractDeployWithData(t *testing.T) { // For proper nef generation. config.Version = "0.90.0-test" - - tmpDir, err := ioutil.TempDir("", "neogo.test.deployfail") - require.NoError(t, err) - t.Cleanup(func() { - os.RemoveAll(tmpDir) - }) + tmpDir := t.TempDir() nefName := path.Join(tmpDir, "deploy.nef") manifestName := path.Join(tmpDir, "deploy.manifest.json") @@ -232,12 +214,7 @@ func TestContractManifestGroups(t *testing.T) { // For proper nef generation. config.Version = "0.90.0-test" - - tmpDir, err := ioutil.TempDir("", "neogo.test.deployfail") - require.NoError(t, err) - t.Cleanup(func() { - os.RemoveAll(tmpDir) - }) + tmpDir := t.TempDir() w, err := wallet.NewWalletFromFile(testWalletPath) require.NoError(t, err) @@ -273,11 +250,7 @@ func deployVerifyContract(t *testing.T, e *executor) util.Uint160 { } func deployContract(t *testing.T, e *executor, inPath, configPath, wallet, address, pass string) util.Uint160 { - tmpDir, err := ioutil.TempDir(os.TempDir(), "neogo.test.deploycontract*") - require.NoError(t, err) - t.Cleanup(func() { - os.RemoveAll(tmpDir) - }) + tmpDir := t.TempDir() nefName := path.Join(tmpDir, "contract.nef") manifestName := path.Join(tmpDir, "contract.manifest.json") e.Run(t, "neo-go", "contract", "compile", @@ -303,12 +276,7 @@ func TestComlileAndInvokeFunction(t *testing.T) { // For proper nef generation. config.Version = "0.90.0-test" - - tmpDir, err := ioutil.TempDir("", "neogo.test.compileandinvoke") - require.NoError(t, err) - t.Cleanup(func() { - os.RemoveAll(tmpDir) - }) + tmpDir := t.TempDir() nefName := path.Join(tmpDir, "deploy.nef") manifestName := path.Join(tmpDir, "deploy.manifest.json") @@ -556,12 +524,7 @@ func TestContractInspect(t *testing.T) { // For proper nef generation. config.Version = "0.90.0-test" const srcPath = "testdata/deploy/main.go" - - tmpDir, err := ioutil.TempDir("", "neogo.test.contract.inspect") - require.NoError(t, err) - t.Cleanup(func() { - os.RemoveAll(tmpDir) - }) + tmpDir := t.TempDir() nefName := path.Join(tmpDir, "deploy.nef") manifestName := path.Join(tmpDir, "deploy.manifest.json") @@ -588,6 +551,7 @@ func TestContractInspect(t *testing.T) { } func TestCompileExamples(t *testing.T) { + tmpDir := t.TempDir() const examplePath = "../examples" infos, err := ioutil.ReadDir(examplePath) require.NoError(t, err) @@ -608,21 +572,15 @@ func TestCompileExamples(t *testing.T) { require.NoError(t, err) require.False(t, len(infos) == 0, "detected smart contract folder with no contract in it") - outF, err := ioutil.TempFile("", info.Name()+".nef") - require.NoError(t, err) - manifestF, err := ioutil.TempFile("", info.Name()+".manifest.json") - require.NoError(t, err) - t.Cleanup(func() { - os.Remove(outF.Name()) - os.Remove(manifestF.Name()) - }) + outF := path.Join(tmpDir, info.Name()+".nef") + manifestF := path.Join(tmpDir, info.Name()+".manifest.json") cfgName := filterFilename(infos, ".yml") opts := []string{ "neo-go", "contract", "compile", "--in", path.Join(examplePath, info.Name()), - "--out", outF.Name(), - "--manifest", manifestF.Name(), + "--out", outF, + "--manifest", manifestF, "--config", path.Join(examplePath, info.Name(), cfgName), } e.Run(t, opts...) @@ -632,18 +590,12 @@ func TestCompileExamples(t *testing.T) { t.Run("invalid events in manifest", func(t *testing.T) { const dir = "./testdata/" for _, name := range []string{"invalid1", "invalid2", "invalid3"} { - outF, err := ioutil.TempFile("", name+".nef") - require.NoError(t, err) - manifestF, err := ioutil.TempFile("", name+".manifest.json") - require.NoError(t, err) - t.Cleanup(func() { - os.Remove(outF.Name()) - os.Remove(manifestF.Name()) - }) + outF := path.Join(tmpDir, name+".nef") + manifestF := path.Join(tmpDir, name+".manifest.json") e.RunWithError(t, "neo-go", "contract", "compile", "--in", path.Join(dir, name), - "--out", outF.Name(), - "--manifest", manifestF.Name(), + "--out", outF, + "--manifest", manifestF, "--config", path.Join(dir, name, "invalid.yml"), ) } diff --git a/cli/dump_test.go b/cli/dump_test.go index 0356179f5..1318577b6 100644 --- a/cli/dump_test.go +++ b/cli/dump_test.go @@ -12,11 +12,7 @@ import ( ) func TestDBRestore(t *testing.T) { - tmpDir, err := ioutil.TempDir("", "neogo.restoretest") - require.NoError(t, err) - t.Cleanup(func() { - os.RemoveAll(tmpDir) - }) + tmpDir := t.TempDir() chainPath := path.Join(tmpDir, "neogotestchain") cfg, err := config.LoadFile("../config/protocol.unit_testnet.yml") diff --git a/cli/multisig_test.go b/cli/multisig_test.go index d8dcf0282..95d1da0d8 100644 --- a/cli/multisig_test.go +++ b/cli/multisig_test.go @@ -2,7 +2,6 @@ package main import ( "encoding/hex" - "io/ioutil" "math/big" "os" "path" @@ -28,13 +27,9 @@ func TestSignMultisigTx(t *testing.T) { multisigAddr := address.Uint160ToString(multisigHash) // Create 2 wallets participating in multisig. - tmpDir, err := ioutil.TempDir("", "neogo.wallettest") - require.NoError(t, err) + tmpDir := t.TempDir() wallet1Path := path.Join(tmpDir, "multiWallet1.json") wallet2Path := path.Join(tmpDir, "multiWallet2.json") - t.Cleanup(func() { - os.RemoveAll(tmpDir) - }) addAccount := func(w string, wif string) { e.Run(t, "neo-go", "wallet", "init", "--wallet", w) diff --git a/cli/nep11_test.go b/cli/nep11_test.go index b3aa455cd..69a83e1e3 100644 --- a/cli/nep11_test.go +++ b/cli/nep11_test.go @@ -6,7 +6,6 @@ import ( "io" "io/ioutil" "math/big" - "os" "path" "strings" "testing" @@ -31,10 +30,8 @@ const ( func TestNEP11Import(t *testing.T) { e := newExecutor(t, true) - tmpDir, err := ioutil.TempDir("", "neogo.nep11import") - require.NoError(t, err) + tmpDir := t.TempDir() walletPath := path.Join(tmpDir, "walletForImport.json") - t.Cleanup(func() { os.RemoveAll(tmpDir) }) // deploy NFT NeoNameService contract nnsContractHash := deployNNSContract(t, e) @@ -93,12 +90,7 @@ func TestNEP11Import(t *testing.T) { func TestNEP11_OwnerOf_BalanceOf_Transfer(t *testing.T) { e := newExecutor(t, true) - - tmpDir, err := ioutil.TempDir(os.TempDir(), "neogo.test.nftwallet*") - require.NoError(t, err) - t.Cleanup(func() { - os.RemoveAll(tmpDir) - }) + tmpDir := t.TempDir() // copy wallet to temp dir in order not to overwrite the original file bytesRead, err := ioutil.ReadFile(nftOwnerWallet) diff --git a/cli/nep17_test.go b/cli/nep17_test.go index 23a2dd542..68ad55b7f 100644 --- a/cli/nep17_test.go +++ b/cli/nep17_test.go @@ -2,9 +2,7 @@ package main import ( "io" - "io/ioutil" "math/big" - "os" "path" "strconv" "strings" @@ -260,10 +258,7 @@ func TestNEP17MultiTransfer(t *testing.T) { func TestNEP17ImportToken(t *testing.T) { e := newExecutor(t, true) - - tmpDir, err := ioutil.TempDir("", "neogo.nep17import") - require.NoError(t, err) - t.Cleanup(func() { os.RemoveAll(tmpDir) }) + tmpDir := t.TempDir() walletPath := path.Join(tmpDir, "walletForImport.json") neoContractHash, err := e.Chain.GetNativeContractScriptHash(nativenames.Neo) diff --git a/cli/server/dump_test.go b/cli/server/dump_test.go index 6a407fdba..ddaa82e20 100644 --- a/cli/server/dump_test.go +++ b/cli/server/dump_test.go @@ -1,8 +1,6 @@ package server import ( - "io/ioutil" - "os" "path" "testing" @@ -10,12 +8,7 @@ import ( ) func TestGetPath(t *testing.T) { - testPath, err := ioutil.TempDir("./", "") - require.NoError(t, err) - t.Cleanup(func() { - err := os.RemoveAll(testPath) - require.NoError(t, err) - }) + testPath := t.TempDir() actual, err := getPath(testPath, 123) require.NoError(t, err) require.Equal(t, path.Join(testPath, "/BlockStorage_100000/dump-block-1000.json"), actual) diff --git a/cli/server/server_test.go b/cli/server/server_test.go index 4be79c892..bce4d9336 100644 --- a/cli/server/server_test.go +++ b/cli/server/server_test.go @@ -2,8 +2,8 @@ package server import ( "flag" - "io/ioutil" "os" + "path" "testing" "github.com/nspcc-dev/neo-go/pkg/config" @@ -16,6 +16,17 @@ import ( "go.uber.org/zap" ) +// serverTestWD is the default working directory for server tests. +var serverTestWD string + +func init() { + var err error + serverTestWD, err = os.Getwd() + if err != nil { + panic("can't get current working directory") + } +} + func TestGetConfigFromContext(t *testing.T) { set := flag.NewFlagSet("flagSet", flag.ExitOnError) set.String("config-path", "../../config", "") @@ -27,17 +38,14 @@ func TestGetConfigFromContext(t *testing.T) { } func TestHandleLoggingParams(t *testing.T) { - testLog, err := ioutil.TempFile("./", "*.log") - require.NoError(t, err) - t.Cleanup(func() { - require.NoError(t, os.Remove(testLog.Name())) - }) + d := t.TempDir() + testLog := path.Join(d, "file.log") t.Run("default", func(t *testing.T) { set := flag.NewFlagSet("flagSet", flag.ExitOnError) ctx := cli.NewContext(cli.NewApp(), set, nil) cfg := config.ApplicationConfiguration{ - LogPath: testLog.Name(), + LogPath: testLog, } logger, err := handleLoggingParams(ctx, cfg) require.NoError(t, err) @@ -50,7 +58,7 @@ func TestHandleLoggingParams(t *testing.T) { set.Bool("debug", true, "") ctx := cli.NewContext(cli.NewApp(), set, nil) cfg := config.ApplicationConfiguration{ - LogPath: testLog.Name(), + LogPath: testLog, } logger, err := handleLoggingParams(ctx, cfg) require.NoError(t, err) @@ -60,18 +68,13 @@ func TestHandleLoggingParams(t *testing.T) { } func TestInitBCWithMetrics(t *testing.T) { - d, err := ioutil.TempDir("./", "") + d := t.TempDir() + err := os.Chdir(d) require.NoError(t, err) - err = os.Chdir(d) - require.NoError(t, err) - t.Cleanup(func() { - err = os.Chdir("..") - require.NoError(t, err) - os.RemoveAll(d) - }) + t.Cleanup(func() { require.NoError(t, os.Chdir(serverTestWD)) }) set := flag.NewFlagSet("flagSet", flag.ExitOnError) - set.String("config-path", "../../../config", "") + set.String("config-path", path.Join(serverTestWD, "../../config"), "") set.Bool("testnet", true, "") set.Bool("debug", true, "") ctx := cli.NewContext(cli.NewApp(), set, nil) @@ -90,19 +93,15 @@ func TestInitBCWithMetrics(t *testing.T) { } func TestDumpDB(t *testing.T) { + testDump := "file.acc" + t.Run("too low chain", func(t *testing.T) { - d, err := ioutil.TempDir("./", "") + d := t.TempDir() + err := os.Chdir(d) require.NoError(t, err) - err = os.Chdir(d) - require.NoError(t, err) - t.Cleanup(func() { - err = os.Chdir("..") - require.NoError(t, err) - os.RemoveAll(d) - }) - testDump := "file.acc" + t.Cleanup(func() { require.NoError(t, os.Chdir(serverTestWD)) }) set := flag.NewFlagSet("flagSet", flag.ExitOnError) - set.String("config-path", "../../../config", "") + set.String("config-path", path.Join(serverTestWD, "../../config"), "") set.Bool("privnet", true, "") set.Bool("debug", true, "") set.Int("start", 0, "") @@ -114,18 +113,12 @@ func TestDumpDB(t *testing.T) { }) t.Run("positive", func(t *testing.T) { - d, err := ioutil.TempDir("./", "") + d := t.TempDir() + err := os.Chdir(d) require.NoError(t, err) - err = os.Chdir(d) - require.NoError(t, err) - t.Cleanup(func() { - err = os.Chdir("..") - require.NoError(t, err) - os.RemoveAll(d) - }) - testDump := "file.acc" + t.Cleanup(func() { require.NoError(t, os.Chdir(serverTestWD)) }) set := flag.NewFlagSet("flagSet", flag.ExitOnError) - set.String("config-path", "../../../config", "") + set.String("config-path", path.Join(serverTestWD, "../../config"), "") set.Bool("privnet", true, "") set.Bool("debug", true, "") set.Int("start", 0, "") @@ -138,21 +131,16 @@ func TestDumpDB(t *testing.T) { } func TestRestoreDB(t *testing.T) { - d, err := ioutil.TempDir("./", "") - require.NoError(t, err) + d := t.TempDir() testDump := "file1.acc" saveDump := "file2.acc" - err = os.Chdir(d) + err := os.Chdir(d) require.NoError(t, err) - t.Cleanup(func() { - err = os.Chdir("..") - require.NoError(t, err) - os.RemoveAll(d) - }) + t.Cleanup(func() { require.NoError(t, os.Chdir(serverTestWD)) }) //dump first set := flag.NewFlagSet("flagSet", flag.ExitOnError) - set.String("config-path", "../../../config", "") + set.String("config-path", path.Join(serverTestWD, "../../config"), "") set.Bool("privnet", true, "") set.Bool("debug", true, "") set.Int("start", 0, "") diff --git a/cli/smartcontract/smart_contract_test.go b/cli/smartcontract/smart_contract_test.go index 48a111a74..7316dfcb5 100644 --- a/cli/smartcontract/smart_contract_test.go +++ b/cli/smartcontract/smart_contract_test.go @@ -16,15 +16,12 @@ import ( ) func TestInitSmartContract(t *testing.T) { - d, err := ioutil.TempDir("./", "") + d := t.TempDir() + testWD, err := os.Getwd() require.NoError(t, err) err = os.Chdir(d) require.NoError(t, err) - t.Cleanup(func() { - err = os.Chdir("..") - require.NoError(t, err) - os.RemoveAll(d) - }) + t.Cleanup(func() { require.NoError(t, os.Chdir(testWD)) }) contractName := "testContract" set := flag.NewFlagSet("flagSet", flag.ExitOnError) diff --git a/cli/wallet_test.go b/cli/wallet_test.go index 7c6d80791..c5855f341 100644 --- a/cli/wallet_test.go +++ b/cli/wallet_test.go @@ -5,7 +5,6 @@ import ( "encoding/json" "io/ioutil" "math/big" - "os" "path" "strings" "testing" @@ -20,12 +19,7 @@ import ( ) func TestWalletAccountRemove(t *testing.T) { - tmpDir, err := ioutil.TempDir("", "neogo.test.walletinit") - require.NoError(t, err) - t.Cleanup(func() { - os.RemoveAll(tmpDir) - }) - + tmpDir := t.TempDir() e := newExecutor(t, false) walletPath := path.Join(tmpDir, "wallet.json") @@ -52,12 +46,7 @@ func TestWalletAccountRemove(t *testing.T) { } func TestWalletInit(t *testing.T) { - tmpDir, err := ioutil.TempDir("", "neogo.test.walletinit") - require.NoError(t, err) - t.Cleanup(func() { - os.RemoveAll(tmpDir) - }) - + tmpDir := t.TempDir() e := newExecutor(t, false) walletPath := path.Join(tmpDir, "wallet.json") @@ -268,15 +257,11 @@ func TestClaimGas(t *testing.T) { } func TestImportDeployed(t *testing.T) { + tmpDir := t.TempDir() e := newExecutor(t, true) - h := deployVerifyContract(t, e) - tmpDir, err := ioutil.TempDir("", "neogo.importdeployed") - require.NoError(t, err) - t.Cleanup(func() { - os.Remove(tmpDir) - }) walletPath := path.Join(tmpDir, "wallet.json") + e.Run(t, "neo-go", "wallet", "init", "--wallet", walletPath) priv, err := keys.NewPrivateKey() @@ -402,12 +387,7 @@ func TestDumpKeys(t *testing.T) { // Testcase is the wallet of privnet validator. func TestWalletConvert(t *testing.T) { - tmpDir, err := ioutil.TempDir("", "neogo.test.convert") - require.NoError(t, err) - t.Cleanup(func() { - os.RemoveAll(tmpDir) - }) - + tmpDir := t.TempDir() e := newExecutor(t, false) outPath := path.Join(tmpDir, "wallet.json") diff --git a/docs/compiler.md b/docs/compiler.md index 5a10d927e..9dda92d87 100644 --- a/docs/compiler.md +++ b/docs/compiler.md @@ -48,7 +48,7 @@ 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 +export GOROOT=/usr/lib64/go/1.15 ``` ### Compiling diff --git a/go.mod b/go.mod index 76609e8c6..453b6681c 100644 --- a/go.mod +++ b/go.mod @@ -31,4 +31,4 @@ require ( gopkg.in/yaml.v2 v2.4.0 ) -go 1.14 +go 1.15 diff --git a/pkg/core/stateroot_test.go b/pkg/core/stateroot_test.go index a7683da9d..6dd0703a3 100644 --- a/pkg/core/stateroot_test.go +++ b/pkg/core/stateroot_test.go @@ -2,8 +2,6 @@ package core import ( "errors" - "io/ioutil" - "os" "path" "sort" "testing" @@ -79,11 +77,7 @@ func TestStateRoot(t *testing.T) { updateIndex := bc.BlockHeight() transferTokenFromMultisigAccount(t, bc, h, bc.contracts.GAS.Hash, 1_0000_0000) - tmpDir, err := ioutil.TempDir("", "neogo.test.stateroot") - require.NoError(t, err) - t.Cleanup(func() { - os.RemoveAll(tmpDir) - }) + tmpDir := t.TempDir() w := createAndWriteWallet(t, accs[0], path.Join(tmpDir, "w"), "pass") cfg := createStateRootConfig(w.Path(), "pass") srv, err := stateroot.New(cfg, zaptest.NewLogger(t), bc, nil) @@ -151,11 +145,7 @@ func TestStateRootInitNonZeroHeight(t *testing.T) { _, err := persistBlock(bc) require.NoError(t, err) - tmpDir, err := ioutil.TempDir("", "neogo.initsnz") - require.NoError(t, err) - t.Cleanup(func() { - os.RemoveAll(tmpDir) - }) + tmpDir := t.TempDir() w := createAndWriteWallet(t, accs[0], path.Join(tmpDir, "w"), "pass") cfg := createStateRootConfig(w.Path(), "pass") srv, err := stateroot.New(cfg, zaptest.NewLogger(t), bc, nil) @@ -195,12 +185,7 @@ func createStateRootConfig(walletPath, password string) config.StateRoot { } func TestStateRootFull(t *testing.T) { - tmpDir, err := ioutil.TempDir("", "neogo.stateroot4") - require.NoError(t, err) - t.Cleanup(func() { - os.RemoveAll(tmpDir) - }) - + tmpDir := t.TempDir() bc := newTestChain(t) h, pubs, accs := newMajorityMultisigWithGAS(t, 2) diff --git a/pkg/core/storage/badgerdb_store_test.go b/pkg/core/storage/badgerdb_store_test.go index 820c716c8..e00fa4a15 100644 --- a/pkg/core/storage/badgerdb_store_test.go +++ b/pkg/core/storage/badgerdb_store_test.go @@ -1,33 +1,13 @@ package storage import ( - "io/ioutil" - "os" "testing" "github.com/stretchr/testify/require" ) -type tempBadgerDB struct { - *BadgerDBStore - dir string -} - -func (tbdb *tempBadgerDB) Close() error { - err := tbdb.BadgerDBStore.Close() - // Make test fail if failed to cleanup, even though technically it's - // not a BadgerDBStore problem. - osErr := os.RemoveAll(tbdb.dir) - if osErr != nil { - return osErr - } - return err -} - func newBadgerDBForTesting(t *testing.T) Store { - bdbDir, err := ioutil.TempDir(os.TempDir(), "testbadgerdb") - require.Nil(t, err, "failed to setup temporary directory") - + bdbDir := t.TempDir() dbConfig := DBConfiguration{ Type: "badgerdb", BadgerDBOptions: BadgerDBOptions{ @@ -36,9 +16,5 @@ func newBadgerDBForTesting(t *testing.T) Store { } newBadgerStore, err := NewBadgerDBStore(dbConfig.BadgerDBOptions) require.Nil(t, err, "NewBadgerDBStore error") - tbdb := &tempBadgerDB{ - BadgerDBStore: newBadgerStore, - dir: bdbDir, - } - return tbdb + return newBadgerStore } diff --git a/pkg/core/storage/boltdb_store_test.go b/pkg/core/storage/boltdb_store_test.go index 30becef21..9f8fd4caf 100644 --- a/pkg/core/storage/boltdb_store_test.go +++ b/pkg/core/storage/boltdb_store_test.go @@ -1,23 +1,16 @@ package storage import ( - "io/ioutil" - "os" + "path" "testing" "github.com/stretchr/testify/require" ) func newBoltStoreForTesting(t *testing.T) Store { - testFileName := "test_bolt_db" - file, err := ioutil.TempFile("", testFileName) - t.Cleanup(func() { - err := os.RemoveAll(file.Name()) - require.NoError(t, err) - }) - require.NoError(t, err) - require.NoError(t, file.Close()) - boltDBStore, err := NewBoltDBStore(BoltDBOptions{FilePath: file.Name()}) + d := t.TempDir() + testFileName := path.Join(d, "test_bolt_db") + boltDBStore, err := NewBoltDBStore(BoltDBOptions{FilePath: testFileName}) require.NoError(t, err) return boltDBStore } diff --git a/pkg/core/storage/leveldb_store_test.go b/pkg/core/storage/leveldb_store_test.go index b8dcbbe83..5d8672e7a 100644 --- a/pkg/core/storage/leveldb_store_test.go +++ b/pkg/core/storage/leveldb_store_test.go @@ -1,33 +1,13 @@ package storage import ( - "io/ioutil" - "os" "testing" "github.com/stretchr/testify/require" ) -type tempLevelDB struct { - LevelDBStore - dir string -} - -func (tldb *tempLevelDB) Close() error { - err := tldb.LevelDBStore.Close() - // Make test fail if failed to cleanup, even though technically it's - // not a LevelDBStore problem. - osErr := os.RemoveAll(tldb.dir) - if osErr != nil { - return osErr - } - return err -} - func newLevelDBForTesting(t *testing.T) Store { - ldbDir, err := ioutil.TempDir(os.TempDir(), "testleveldb") - require.Nil(t, err, "failed to setup temporary directory") - + ldbDir := t.TempDir() dbConfig := DBConfiguration{ Type: "leveldb", LevelDBOptions: LevelDBOptions{ @@ -36,6 +16,5 @@ func newLevelDBForTesting(t *testing.T) Store { } newLevelStore, err := NewLevelDBStore(dbConfig.LevelDBOptions) require.Nil(t, err, "NewLevelDBStore error") - tldb := &tempLevelDB{LevelDBStore: *newLevelStore, dir: ldbDir} - return tldb + return newLevelStore } diff --git a/pkg/crypto/keys/private_key.go b/pkg/crypto/keys/private_key.go index f27f92145..6cded1914 100644 --- a/pkg/crypto/keys/private_key.go +++ b/pkg/crypto/keys/private_key.go @@ -64,7 +64,7 @@ func NewPrivateKeyFromBytes(b []byte) (*PrivateKey, error) { d = new(big.Int).SetBytes(b) ) - x, y := c.ScalarBaseMult(d.Bytes()) + x, y := c.ScalarBaseMult(b) return &PrivateKey{ ecdsa.PrivateKey{ @@ -85,7 +85,7 @@ func NewPrivateKeyFromASN1(b []byte) (*PrivateKey, error) { if err != nil { return nil, err } - return NewPrivateKeyFromBytes(privkey.D.Bytes()) + return &PrivateKey{*privkey}, nil } // PublicKey derives the public key from the private key. @@ -155,10 +155,9 @@ func (p *PrivateKey) SignHashable(net uint32, hh hash.Hashable) []byte { func getSignatureSlice(curve elliptic.Curve, r, s *big.Int) []byte { params := curve.Params() curveOrderByteSize := params.P.BitLen() / 8 - rBytes, sBytes := r.Bytes(), s.Bytes() signature := make([]byte, curveOrderByteSize*2) - copy(signature[curveOrderByteSize-len(rBytes):], rBytes) - copy(signature[curveOrderByteSize*2-len(sBytes):], sBytes) + _ = r.FillBytes(signature[:curveOrderByteSize]) + _ = s.FillBytes(signature[curveOrderByteSize:]) return signature } @@ -170,9 +169,8 @@ func (p *PrivateKey) String() string { // Bytes returns the underlying bytes of the PrivateKey. func (p *PrivateKey) Bytes() []byte { - bytes := p.D.Bytes() result := make([]byte, 32) - copy(result[32-len(bytes):], bytes) + _ = p.D.FillBytes(result) return result } diff --git a/pkg/crypto/keys/publickey.go b/pkg/crypto/keys/publickey.go index 311a7a207..f39a15b1d 100644 --- a/pkg/crypto/keys/publickey.go +++ b/pkg/crypto/keys/publickey.go @@ -8,7 +8,6 @@ import ( "encoding/json" "errors" "fmt" - gio "io" "math/big" "github.com/btcsuite/btcd/btcec" @@ -143,29 +142,10 @@ func (p *PublicKey) getBytes(compressed bool) []byte { return []byte{0x00} } - var resLen = 1 + coordLen - if !compressed { - resLen += coordLen - } - var res = make([]byte, resLen) - var prefix byte - - xBytes := p.X.Bytes() - copy(res[1+coordLen-len(xBytes):], xBytes) if compressed { - if p.Y.Bit(0) == 0 { - prefix = 0x02 - } else { - prefix = 0x03 - } - } else { - prefix = 0x04 - yBytes := p.Y.Bytes() - copy(res[1+coordLen+coordLen-len(yBytes):], yBytes) + return elliptic.MarshalCompressed(p.Curve, p.X, p.Y) } - res[0] = prefix - - return res + return elliptic.Marshal(p.Curve, p.X, p.Y) } // Bytes returns byte array representation of the public key in compressed @@ -239,8 +219,7 @@ func (p *PublicKey) DecodeBytes(data []byte) error { return b.Err } - b.ReadB() - if b.Err != gio.EOF { + if b.Len() != 0 { return errors.New("extra data") } return nil @@ -270,7 +249,7 @@ func (p *PublicKey) DecodeBinary(r *io.BinReader) { return case 0x02, 0x03: // Compressed public keys - xbytes := make([]byte, 32) + xbytes := make([]byte, coordLen) r.ReadBytes(xbytes) if r.Err != nil { return @@ -283,8 +262,8 @@ func (p *PublicKey) DecodeBinary(r *io.BinReader) { return } case 0x04: - xbytes := make([]byte, 32) - ybytes := make([]byte, 32) + xbytes := make([]byte, coordLen) + ybytes := make([]byte, coordLen) r.ReadBytes(xbytes) r.ReadBytes(ybytes) if r.Err != nil { diff --git a/pkg/crypto/keys/publickey_test.go b/pkg/crypto/keys/publickey_test.go index 30d992c14..13b37ffda 100644 --- a/pkg/crypto/keys/publickey_test.go +++ b/pkg/crypto/keys/publickey_test.go @@ -165,7 +165,7 @@ func TestUnique(t *testing.T) { require.Equal(t, 1, unique.Len()) } -func getPubKey(t *testing.T) *PublicKey { +func getPubKey(t testing.TB) *PublicKey { pubKey, err := NewPublicKeyFromString("031ee4e73a17d8f76dc02532e2620bcb12425b33c0c9f9694cc2caa8226b68cad4") require.NoError(t, err) return pubKey @@ -212,3 +212,37 @@ func TestUnmarshallJSONBadFormat(t *testing.T) { err := json.Unmarshal([]byte(str), actual) require.Error(t, err) } + +func BenchmarkPublicEqual(t *testing.B) { + k11 := getPubKey(t) + k12 := getPubKey(t) + k2, err := NewPublicKeyFromString("03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c") + require.NoError(t, err) + for n := 0; n < t.N; n++ { + _ = k11.Equal(k12) + _ = k11.Equal(k2) + } +} + +func BenchmarkPublicBytes(t *testing.B) { + k := getPubKey(t) + for n := 0; n < t.N; n++ { + _ = k.Bytes() + } +} + +func BenchmarkPublicUncompressedBytes(t *testing.B) { + k := getPubKey(t) + for n := 0; n < t.N; n++ { + _ = k.Bytes() + } +} + +func BenchmarkPublicDecodeBytes(t *testing.B) { + keyBytes, err := hex.DecodeString("03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c") + require.NoError(t, err) + k := new(PublicKey) + for n := 0; n < t.N; n++ { + require.NoError(t, k.DecodeBytes(keyBytes)) + } +} diff --git a/pkg/io/fileWriter_test.go b/pkg/io/fileWriter_test.go index d93296b96..65b0f872e 100644 --- a/pkg/io/fileWriter_test.go +++ b/pkg/io/fileWriter_test.go @@ -1,7 +1,6 @@ package io import ( - "io/ioutil" "os" "path" "testing" @@ -10,34 +9,22 @@ import ( ) func TestMakeDirForFile_HappyPath(t *testing.T) { - tempDir, err := ioutil.TempDir("", "test") - require.NoError(t, err) - - filePath := tempDir + "/testDir/testFile.test" - err = MakeDirForFile(filePath, "test") - t.Cleanup(func() { - removeDir(t, tempDir) - }) + tempDir := t.TempDir() + filePath := path.Join(tempDir, "testDir/testFile.test") + err := MakeDirForFile(filePath, "test") require.NoError(t, err) _, errChDir := os.Create(filePath) require.NoError(t, errChDir) } -func removeDir(t *testing.T, dirName string) { - err := os.RemoveAll(dirName) - require.NoError(t, err) -} - func TestMakeDirForFile_Negative(t *testing.T) { - file, err := ioutil.TempFile("", "test") + tempDir := t.TempDir() + filePath := path.Join(tempDir, "testFile.test") + _, err := os.Create(filePath) require.NoError(t, err) - filePath := file.Name() + "/error" - dir := path.Dir(filePath) + filePath = path.Join(filePath, "error") err = MakeDirForFile(filePath, "test") - t.Cleanup(func() { - removeDir(t, dir) - }) require.Errorf(t, err, "could not create dir for test: mkdir %s : not a directory", filePath) } diff --git a/pkg/vm/cli/cli_test.go b/pkg/vm/cli/cli_test.go index 6dba3b5b2..0ff8b0cd3 100644 --- a/pkg/vm/cli/cli_test.go +++ b/pkg/vm/cli/cli_test.go @@ -162,11 +162,7 @@ func TestLoad(t *testing.T) { return a * b } }` - tmpDir, err := ioutil.TempDir("", "neogo.vmcliloadtest") - require.NoError(t, err) - t.Cleanup(func() { - os.RemoveAll(tmpDir) - }) + tmpDir := t.TempDir() t.Run("loadgo", func(t *testing.T) { filename := path.Join(tmpDir, "vmtestcontract.go") @@ -262,11 +258,7 @@ func TestRunWithDifferentArguments(t *testing.T) { return arg }` - tmpDir, err := ioutil.TempDir("", "neogo.vmcliruntest") - require.NoError(t, err) - t.Cleanup(func() { - os.RemoveAll(tmpDir) - }) + tmpDir := t.TempDir() filename := path.Join(tmpDir, "run_vmtestcontract.go") require.NoError(t, ioutil.WriteFile(filename, []byte(src), os.ModePerm)) diff --git a/pkg/wallet/wallet_test.go b/pkg/wallet/wallet_test.go index ccdf081b4..68c97579b 100644 --- a/pkg/wallet/wallet_test.go +++ b/pkg/wallet/wallet_test.go @@ -2,8 +2,6 @@ package wallet import ( "encoding/json" - "io/ioutil" - "os" "path" "testing" @@ -75,10 +73,8 @@ func TestPath(t *testing.T) { } func TestSave(t *testing.T) { - file, err := ioutil.TempFile("", walletTemplate) - require.NoError(t, err) - wallet, err := NewWallet(file.Name()) - require.NoError(t, err) + wallet := checkWalletConstructor(t) + wallet.AddAccount(&Account{ privateKey: nil, publicKey: nil, @@ -91,9 +87,6 @@ func TestSave(t *testing.T) { Default: false, }) - t.Cleanup(func() { - removeWallet(t, file.Name()) - }) errForSave := wallet.Save() require.NoError(t, errForSave) @@ -130,21 +123,13 @@ func TestJSONMarshallUnmarshal(t *testing.T) { } func checkWalletConstructor(t *testing.T) *Wallet { - file, err := ioutil.TempFile("", walletTemplate) - require.NoError(t, err) - wallet, err := NewWallet(file.Name()) - t.Cleanup(func() { - removeWallet(t, file.Name()) - }) + tmpDir := t.TempDir() + file := path.Join(tmpDir, walletTemplate) + wallet, err := NewWallet(file) require.NoError(t, err) return wallet } -func removeWallet(t *testing.T, walletPath string) { - err := os.RemoveAll(walletPath) - require.NoError(t, err) -} - func TestWallet_AddToken(t *testing.T) { w := checkWalletConstructor(t) tok := NewToken(util.Uint160{1, 2, 3}, "Rubl", "RUB", 2, manifest.NEP17StandardName)