From bf909f529f0b1e1bd226d8857baf0b822e998a0e Mon Sep 17 00:00:00 2001 From: kourin Date: Wed, 21 Sep 2022 17:54:22 +0300 Subject: [PATCH] Remove io/ioutil package due to deprecation (#723) * Remove io/ioutil package due to deprecation * Fix lint error * Fix permissions of WriteFile to default --- blockchain/storage/leveldb/leveldb_test.go | 3 +-- chain/chain.go | 4 ++-- chain/chain_bindata.go | 3 +-- chain/chain_test.go | 4 ++-- command/common.go | 4 ++-- command/helper/helper.go | 10 ++++------ command/loadbot/generator/generator.go | 7 +++---- command/server/config/config.go | 7 +++---- consensus/ibft/fork/helper.go | 6 ++---- consensus/ibft/fork/helper_test.go | 8 ++++---- consensus/ibft/fork/storewrapper_test.go | 4 ++-- e2e/framework/helper.go | 6 ++---- helper/keystore/keystore.go | 5 ++--- jsonrpc/jsonrpc.go | 4 ++-- network/e2e_testing.go | 3 +-- secrets/config.go | 6 +++--- secrets/local/local.go | 5 ++--- secrets/local/local_test.go | 5 ++--- state/runtime/precompiled/testing.go | 4 ++-- tests/evm_test.go | 12 +++++------- tests/state_test.go | 7 +++---- tests/testing.go | 3 +-- 22 files changed, 51 insertions(+), 69 deletions(-) diff --git a/blockchain/storage/leveldb/leveldb_test.go b/blockchain/storage/leveldb/leveldb_test.go index 62d7f95c58..e3ea4aef86 100644 --- a/blockchain/storage/leveldb/leveldb_test.go +++ b/blockchain/storage/leveldb/leveldb_test.go @@ -1,7 +1,6 @@ package leveldb import ( - "io/ioutil" "os" "testing" @@ -12,7 +11,7 @@ import ( func newStorage(t *testing.T) (storage.Storage, func()) { t.Helper() - path, err := ioutil.TempDir("/tmp", "minimal_storage") + path, err := os.MkdirTemp("/tmp", "minimal_storage") if err != nil { t.Fatal(err) } diff --git a/chain/chain.go b/chain/chain.go index d02f42080d..3a76a8ea3f 100644 --- a/chain/chain.go +++ b/chain/chain.go @@ -4,8 +4,8 @@ import ( "encoding/binary" "encoding/json" "fmt" - "io/ioutil" "math/big" + "os" "github.com/0xPolygon/polygon-edge/helper/hex" "github.com/0xPolygon/polygon-edge/types" @@ -355,7 +355,7 @@ func ImportFromName(chain string) (*Chain, error) { // ImportFromFile imports a chain from a filepath func ImportFromFile(filename string) (*Chain, error) { - data, err := ioutil.ReadFile(filename) + data, err := os.ReadFile(filename) if err != nil { return nil, err } diff --git a/chain/chain_bindata.go b/chain/chain_bindata.go index 709eec204c..c8e119f595 100644 --- a/chain/chain_bindata.go +++ b/chain/chain_bindata.go @@ -11,7 +11,6 @@ import ( "compress/gzip" "fmt" "io" - "io/ioutil" "os" "path/filepath" "strings" @@ -283,7 +282,7 @@ func RestoreAsset(dir, name string) error { if err != nil { return err } - err = ioutil.WriteFile(_filePath(dir, name), data, info.Mode()) + err = os.WriteFile(_filePath(dir, name), data, info.Mode()) if err != nil { return err } diff --git a/chain/chain_test.go b/chain/chain_test.go index c8c913644a..50edc01b69 100644 --- a/chain/chain_test.go +++ b/chain/chain_test.go @@ -2,8 +2,8 @@ package chain import ( "encoding/json" - "io/ioutil" "math/big" + "os" "reflect" "strings" "testing" @@ -155,7 +155,7 @@ func TestGenesisX(t *testing.T) { func TestChainFolder(t *testing.T) { // it should be able to parse all the chains in the ./chains folder - files, err := ioutil.ReadDir("./chains") + files, err := os.ReadDir("./chains") if err != nil { t.Fatal(err) } diff --git a/command/common.go b/command/common.go index 4fa80d3404..796bb693c2 100644 --- a/command/common.go +++ b/command/common.go @@ -2,7 +2,7 @@ package command import ( "errors" - "io/ioutil" + "os" "strings" "github.com/0xPolygon/polygon-edge/crypto" @@ -59,7 +59,7 @@ func GetValidatorsFromPrefixPath( prefix string, validatorType validators.ValidatorType, ) (validators.Validators, error) { - files, err := ioutil.ReadDir(".") + files, err := os.ReadDir(".") if err != nil { return nil, err } diff --git a/command/helper/helper.go b/command/helper/helper.go index daf4f4aa16..81ae985db0 100644 --- a/command/helper/helper.go +++ b/command/helper/helper.go @@ -4,23 +4,22 @@ import ( "encoding/json" "errors" "fmt" - "io/ioutil" "net" "net/url" + "os" "time" "github.com/0xPolygon/polygon-edge/chain" "github.com/0xPolygon/polygon-edge/command" ibftOp "github.com/0xPolygon/polygon-edge/consensus/ibft/proto" + "github.com/0xPolygon/polygon-edge/helper/common" "github.com/0xPolygon/polygon-edge/server" "github.com/0xPolygon/polygon-edge/server/proto" txpoolOp "github.com/0xPolygon/polygon-edge/txpool/proto" + "github.com/ryanuber/columnize" "github.com/spf13/cobra" "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" - - "github.com/0xPolygon/polygon-edge/helper/common" - "github.com/ryanuber/columnize" ) type ClientCloseResult struct { @@ -235,8 +234,7 @@ func WriteGenesisConfigToDisk(genesisConfig *chain.Chain, genesisPath string) er return fmt.Errorf("failed to generate genesis: %w", err) } - //nolint:gosec - if err := ioutil.WriteFile(genesisPath, data, 0644); err != nil { + if err := os.WriteFile(genesisPath, data, os.ModePerm); err != nil { return fmt.Errorf("failed to write genesis: %w", err) } diff --git a/command/loadbot/generator/generator.go b/command/loadbot/generator/generator.go index 232e0f348d..d02f0bf3e8 100644 --- a/command/loadbot/generator/generator.go +++ b/command/loadbot/generator/generator.go @@ -3,12 +3,11 @@ package generator import ( "crypto/ecdsa" "encoding/json" - "io/ioutil" "math/big" - - "github.com/umbracle/ethgo" + "os" "github.com/0xPolygon/polygon-edge/types" + "github.com/umbracle/ethgo" "github.com/umbracle/ethgo/abi" ) @@ -75,7 +74,7 @@ type GeneratorParams struct { // ReadContractArtifact reads the contract bytecode from the specified path func ReadContractArtifact(configPath string) (*ContractArtifact, error) { - rawData, readErr := ioutil.ReadFile(configPath) + rawData, readErr := os.ReadFile(configPath) if readErr != nil { return nil, readErr } diff --git a/command/server/config/config.go b/command/server/config/config.go index bee6f834e8..7ce099e8e6 100644 --- a/command/server/config/config.go +++ b/command/server/config/config.go @@ -3,13 +3,12 @@ package config import ( "encoding/json" "fmt" - "io/ioutil" + "os" "strings" "github.com/0xPolygon/polygon-edge/network" - "gopkg.in/yaml.v3" - "github.com/hashicorp/hcl" + "gopkg.in/yaml.v3" ) // Config defines the server configuration params @@ -119,7 +118,7 @@ func DefaultConfig() *Config { // // Supported file types: .json, .hcl, .yaml, .yml func ReadConfigFile(path string) (*Config, error) { - data, err := ioutil.ReadFile(path) + data, err := os.ReadFile(path) if err != nil { return nil, err } diff --git a/consensus/ibft/fork/helper.go b/consensus/ibft/fork/helper.go index 6f5c95ecd5..253512619b 100644 --- a/consensus/ibft/fork/helper.go +++ b/consensus/ibft/fork/helper.go @@ -2,7 +2,6 @@ package fork import ( "encoding/json" - "io/ioutil" "os" "github.com/0xPolygon/polygon-edge/validators/store/snapshot" @@ -35,7 +34,7 @@ func readDataStore(path string, obj interface{}) error { return nil } - data, err := ioutil.ReadFile(path) + data, err := os.ReadFile(path) if err != nil { return err } @@ -54,8 +53,7 @@ func writeDataStore(path string, obj interface{}) error { return err } - //nolint: gosec - if err := ioutil.WriteFile(path, data, 0755); err != nil { + if err := os.WriteFile(path, data, os.ModePerm); err != nil { return err } diff --git a/consensus/ibft/fork/helper_test.go b/consensus/ibft/fork/helper_test.go index eebc9a9d35..3596b74eb6 100644 --- a/consensus/ibft/fork/helper_test.go +++ b/consensus/ibft/fork/helper_test.go @@ -67,7 +67,7 @@ func Test_loadSnapshotMetadata(t *testing.T) { dirPath := createTestTempDirectory(t) filePath := path.Join(dirPath, "test.dat") - assert.NoError(t, os.WriteFile(filePath, fileData, 0600)) + assert.NoError(t, os.WriteFile(filePath, fileData, os.ModePerm)) res, err := loadSnapshotMetadata(filePath) @@ -124,7 +124,7 @@ func Test_loadSnapshots(t *testing.T) { dirPath := createTestTempDirectory(t) filePath := path.Join(dirPath, "test.dat") - assert.NoError(t, os.WriteFile(filePath, fileData, 0600)) + assert.NoError(t, os.WriteFile(filePath, fileData, os.ModePerm)) res, err := loadSnapshots(filePath) @@ -166,7 +166,7 @@ func Test_readDataStore(t *testing.T) { assert.NoError( t, - os.WriteFile(filePath, []byte("hello: world"), 0600), + os.WriteFile(filePath, []byte("hello: world"), os.ModePerm), ) data := map[string]interface{}{} @@ -186,7 +186,7 @@ func Test_readDataStore(t *testing.T) { assert.NoError( t, - os.WriteFile(filePath, []byte(sampleJSON), 0600), + os.WriteFile(filePath, []byte(sampleJSON), os.ModePerm), ) data := map[string]interface{}{} diff --git a/consensus/ibft/fork/storewrapper_test.go b/consensus/ibft/fork/storewrapper_test.go index 5ab7e04e8f..225ae7124e 100644 --- a/consensus/ibft/fork/storewrapper_test.go +++ b/consensus/ibft/fork/storewrapper_test.go @@ -112,12 +112,12 @@ func TestSnapshotValidatorStoreWrapper(t *testing.T) { assert.NoError( t, - os.WriteFile(path.Join(dirPath, snapshotMetadataFilename), []byte(test.storedSnapshotMetadata), 0600), + os.WriteFile(path.Join(dirPath, snapshotMetadataFilename), []byte(test.storedSnapshotMetadata), os.ModePerm), ) assert.NoError( t, - os.WriteFile(path.Join(dirPath, snapshotSnapshotsFilename), []byte(test.storedSnapshots), 0600), + os.WriteFile(path.Join(dirPath, snapshotSnapshotsFilename), []byte(test.storedSnapshots), os.ModePerm), ) store, err := NewSnapshotValidatorStoreWrapper( diff --git a/e2e/framework/helper.go b/e2e/framework/helper.go index f598954752..c8cebe319b 100644 --- a/e2e/framework/helper.go +++ b/e2e/framework/helper.go @@ -5,7 +5,6 @@ import ( "crypto/ecdsa" "errors" "fmt" - "io/ioutil" "math/big" "net" "os" @@ -14,8 +13,6 @@ import ( "testing" "time" - "github.com/umbracle/ethgo" - "github.com/0xPolygon/polygon-edge/contracts/abis" "github.com/0xPolygon/polygon-edge/contracts/staking" "github.com/0xPolygon/polygon-edge/crypto" @@ -25,6 +22,7 @@ import ( txpoolProto "github.com/0xPolygon/polygon-edge/txpool/proto" "github.com/0xPolygon/polygon-edge/types" "github.com/stretchr/testify/assert" + "github.com/umbracle/ethgo" "github.com/umbracle/ethgo/jsonrpc" "golang.org/x/crypto/sha3" empty "google.golang.org/protobuf/types/known/emptypb" @@ -382,7 +380,7 @@ func MethodSigWithParams(nameWithParams string) []byte { // tempDir returns directory path in tmp with random directory name func tempDir() (string, error) { - return ioutil.TempDir("/tmp", "polygon-edge-e2e-") + return os.MkdirTemp("/tmp", "polygon-edge-e2e-") } func ToLocalIPv4LibP2pAddr(port int, nodeID string) string { diff --git a/helper/keystore/keystore.go b/helper/keystore/keystore.go index 859e3917fc..09ffb8dfd7 100644 --- a/helper/keystore/keystore.go +++ b/helper/keystore/keystore.go @@ -3,7 +3,6 @@ package keystore import ( "encoding/hex" "fmt" - "io/ioutil" "os" ) @@ -20,7 +19,7 @@ func CreateIfNotExists(path string, create createFn) ([]byte, error) { var keyBuff []byte if !os.IsNotExist(err) { // Key exists - keyBuff, err = ioutil.ReadFile(path) + keyBuff, err = os.ReadFile(path) if err != nil { return nil, fmt.Errorf("unable to read private key from disk (%s), %w", path, err) } @@ -36,7 +35,7 @@ func CreateIfNotExists(path string, create createFn) ([]byte, error) { // Encode it to a readable format (Base64) and write to disk keyBuff = []byte(hex.EncodeToString(keyBuff)) - if err = ioutil.WriteFile(path, keyBuff, 0600); err != nil { + if err = os.WriteFile(path, keyBuff, os.ModePerm); err != nil { return nil, fmt.Errorf("unable to write private key to disk (%s), %w", path, err) } diff --git a/jsonrpc/jsonrpc.go b/jsonrpc/jsonrpc.go index 28ad7ff5fc..de938ddfca 100644 --- a/jsonrpc/jsonrpc.go +++ b/jsonrpc/jsonrpc.go @@ -2,7 +2,7 @@ package jsonrpc import ( "fmt" - "io/ioutil" + "io" "net" "net/http" "sync" @@ -273,7 +273,7 @@ func (j *JSONRPC) handle(w http.ResponseWriter, req *http.Request) { return } - data, err := ioutil.ReadAll(req.Body) + data, err := io.ReadAll(req.Body) if err != nil { _, _ = w.Write([]byte(err.Error())) diff --git a/network/e2e_testing.go b/network/e2e_testing.go index 2947f43909..c02afb1742 100644 --- a/network/e2e_testing.go +++ b/network/e2e_testing.go @@ -4,7 +4,6 @@ import ( "context" "errors" "fmt" - "io/ioutil" "os" "sync" "testing" @@ -371,7 +370,7 @@ func MeshJoin(servers ...*Server) []error { func GenerateTestLibp2pKey(t *testing.T) (crypto.PrivKey, string) { t.Helper() - dir, err := ioutil.TempDir(os.TempDir(), "") + dir, err := os.MkdirTemp(os.TempDir(), "") assert.NoError(t, err) // Instantiate the correct folder structure diff --git a/secrets/config.go b/secrets/config.go index f9ad0ae411..badc96dcc2 100644 --- a/secrets/config.go +++ b/secrets/config.go @@ -2,7 +2,7 @@ package secrets import ( "encoding/json" - "io/ioutil" + "os" ) // SecretsManagerConfig is the configuration that gets @@ -20,12 +20,12 @@ type SecretsManagerConfig struct { func (c *SecretsManagerConfig) WriteConfig(path string) error { jsonBytes, _ := json.MarshalIndent(c, "", " ") - return ioutil.WriteFile(path, jsonBytes, 0600) + return os.WriteFile(path, jsonBytes, os.ModePerm) } // ReadConfig reads the SecretsManagerConfig from the specified path func ReadConfig(path string) (*SecretsManagerConfig, error) { - configFile, readErr := ioutil.ReadFile(path) + configFile, readErr := os.ReadFile(path) if readErr != nil { return nil, readErr } diff --git a/secrets/local/local.go b/secrets/local/local.go index ffb4c3d4aa..caf7db899b 100644 --- a/secrets/local/local.go +++ b/secrets/local/local.go @@ -3,7 +3,6 @@ package local import ( "errors" "fmt" - "io/ioutil" "os" "path/filepath" "sync" @@ -106,7 +105,7 @@ func (l *LocalSecretsManager) GetSecret(name string) ([]byte, error) { } // Read the secret from disk - secret, err := ioutil.ReadFile(secretPath) + secret, err := os.ReadFile(secretPath) if err != nil { return nil, fmt.Errorf( "unable to read secret from disk (%s), %w", @@ -134,7 +133,7 @@ func (l *LocalSecretsManager) SetSecret(name string, value []byte) error { } // Write the secret to disk - if err := ioutil.WriteFile(secretPath, value, 0600); err != nil { + if err := os.WriteFile(secretPath, value, os.ModePerm); err != nil { return fmt.Errorf( "unable to write secret to disk (%s), %w", secretPath, diff --git a/secrets/local/local_test.go b/secrets/local/local_test.go index aced7fafcf..adb2137ba0 100644 --- a/secrets/local/local_test.go +++ b/secrets/local/local_test.go @@ -2,7 +2,6 @@ package local import ( "encoding/hex" - "io/ioutil" "os" "testing" @@ -16,7 +15,7 @@ import ( func TestLocalSecretsManagerFactory(t *testing.T) { // Set up the expected folder structure - workingDirectory, tempErr := ioutil.TempDir("/tmp", "local-secrets-manager") + workingDirectory, tempErr := os.MkdirTemp("/tmp", "local-secrets-manager") if tempErr != nil { t.Fatalf("Unable to instantiate local secrets manager directories, %v", tempErr) } @@ -73,7 +72,7 @@ func getLocalSecretsManager(t *testing.T) secrets.SecretsManager { t.Helper() // Set up the expected folder structure - workingDirectory, tempErr := ioutil.TempDir("/tmp", "local-secrets-manager") + workingDirectory, tempErr := os.MkdirTemp("/tmp", "local-secrets-manager") if tempErr != nil { t.Fatalf("Unable to instantiate local secrets manager directories, %v", tempErr) } diff --git a/state/runtime/precompiled/testing.go b/state/runtime/precompiled/testing.go index 8bd9c22314..dff884125a 100644 --- a/state/runtime/precompiled/testing.go +++ b/state/runtime/precompiled/testing.go @@ -3,7 +3,7 @@ package precompiled import ( "encoding/json" "fmt" - "io/ioutil" + "os" "path/filepath" "testing" @@ -33,7 +33,7 @@ func ReadTestCase(t *testing.T, path string, f func(t *testing.T, c *TestCase)) t.Helper() t.Parallel() - data, err := ioutil.ReadFile(filepath.Join("./fixtures", path)) + data, err := os.ReadFile(filepath.Join("./fixtures", path)) if err != nil { t.Fatal(err) } diff --git a/tests/evm_test.go b/tests/evm_test.go index d79c3063fe..eaab30ec1e 100644 --- a/tests/evm_test.go +++ b/tests/evm_test.go @@ -2,23 +2,21 @@ package tests import ( "encoding/json" - "io/ioutil" "math/big" + "os" "strings" "testing" - "github.com/hashicorp/go-hclog" - "github.com/umbracle/fastrlp" - "github.com/0xPolygon/polygon-edge/chain" + "github.com/0xPolygon/polygon-edge/crypto" "github.com/0xPolygon/polygon-edge/helper/hex" "github.com/0xPolygon/polygon-edge/helper/keccak" "github.com/0xPolygon/polygon-edge/state" "github.com/0xPolygon/polygon-edge/state/runtime" "github.com/0xPolygon/polygon-edge/state/runtime/evm" "github.com/0xPolygon/polygon-edge/types" - - "github.com/0xPolygon/polygon-edge/crypto" + "github.com/hashicorp/go-hclog" + "github.com/umbracle/fastrlp" ) var mainnetChainConfig = chain.Params{ @@ -167,7 +165,7 @@ func TestEVM(t *testing.T) { return } - data, err := ioutil.ReadFile(file) + data, err := os.ReadFile(file) if err != nil { t.Fatal(err) } diff --git a/tests/state_test.go b/tests/state_test.go index 4cc239a261..4d5191261e 100644 --- a/tests/state_test.go +++ b/tests/state_test.go @@ -3,19 +3,18 @@ package tests import ( "bytes" "encoding/json" - "io/ioutil" "math/big" + "os" "strings" "testing" - "github.com/hashicorp/go-hclog" - "github.com/0xPolygon/polygon-edge/chain" "github.com/0xPolygon/polygon-edge/helper/hex" "github.com/0xPolygon/polygon-edge/state" "github.com/0xPolygon/polygon-edge/state/runtime/evm" "github.com/0xPolygon/polygon-edge/state/runtime/precompiled" "github.com/0xPolygon/polygon-edge/types" + "github.com/hashicorp/go-hclog" ) var ( @@ -149,7 +148,7 @@ func TestState(t *testing.T) { continue } - data, err := ioutil.ReadFile(file) + data, err := os.ReadFile(file) if err != nil { t.Fatal(err) } diff --git a/tests/testing.go b/tests/testing.go index e40a96b0bb..efe97c34d4 100644 --- a/tests/testing.go +++ b/tests/testing.go @@ -3,7 +3,6 @@ package tests import ( "encoding/json" "fmt" - "io/ioutil" "math/big" "os" "path/filepath" @@ -488,7 +487,7 @@ func listFolders(paths ...string) ([]string, error) { for _, p := range paths { path := filepath.Join(TESTS, p) - files, err := ioutil.ReadDir(path) + files, err := os.ReadDir(path) if err != nil { return nil, err }