From ae578ba9983811d69b1ad228069b45f82c3628ed Mon Sep 17 00:00:00 2001 From: Gheis Mohammadi Date: Sun, 15 Oct 2023 10:54:55 +0800 Subject: [PATCH] update deprecated ioutil, improve local accounts (#4527) --- accounts/abi/bind/auth.go | 5 ++-- accounts/keystore/account_cache_test.go | 9 +++--- accounts/keystore/file_cache.go | 30 ++++++++++++------- accounts/keystore/key.go | 3 +- accounts/keystore/keystore_test.go | 3 +- accounts/keystore/passphrase.go | 3 +- accounts/keystore/passphrase_test.go | 4 +-- accounts/keystore/plain_test.go | 3 +- cmd/harmony/config.go | 9 +++--- cmd/harmony/config_test.go | 3 +- cmd/harmony/main.go | 38 ++++++++++++------------- core/genesis_util.go | 4 +-- core/tx_pool_test.go | 3 +- core/vm/instructions_test.go | 6 ++-- eth/rpc/gzip.go | 3 +- eth/rpc/http.go | 3 +- eth/rpc/method_filter.go | 3 +- eth/rpc/server_test.go | 6 ++-- hmy/tracer.go | 3 +- hmy/tracers/internal/tracers/assets.go | 3 +- internal/blsgen/kms.go | 4 +-- internal/blsgen/kms_test.go | 3 +- internal/blsgen/lib.go | 5 ++-- internal/blsgen/passphrase.go | 6 ++-- internal/blsgen/passphrase_test.go | 3 +- internal/blsgen/utils_test.go | 3 +- internal/utils/passphrase.go | 3 +- internal/utils/testing/tempfile.go | 3 +- node/node.go | 3 +- staking/effective/calculate_test.go | 4 +-- webhooks/yaml.go | 7 +++-- 31 files changed, 88 insertions(+), 100 deletions(-) diff --git a/accounts/abi/bind/auth.go b/accounts/abi/bind/auth.go index ee29295d4d..4f89bf595b 100644 --- a/accounts/abi/bind/auth.go +++ b/accounts/abi/bind/auth.go @@ -20,7 +20,6 @@ import ( "crypto/ecdsa" "errors" "io" - "io/ioutil" "math/big" "github.com/ethereum/go-ethereum/common" @@ -44,7 +43,7 @@ var ErrNotAuthorized = errors.New("not authorized to sign this account") // Deprecated: Use NewTransactorWithChainID instead. func NewTransactor(keyin io.Reader, passphrase string) (*TransactOpts, error) { log.Warn("WARNING: NewTransactor has been deprecated in favour of NewTransactorWithChainID") - json, err := ioutil.ReadAll(keyin) + json, err := io.ReadAll(keyin) if err != nil { return nil, err } @@ -103,7 +102,7 @@ func NewKeyedTransactor(key *ecdsa.PrivateKey) *TransactOpts { // NewTransactorWithChainID is a utility method to easily create a transaction signer from // an encrypted json key stream and the associated passphrase. func NewTransactorWithChainID(keyin io.Reader, passphrase string, chainID *big.Int) (*TransactOpts, error) { - json, err := ioutil.ReadAll(keyin) + json, err := io.ReadAll(keyin) if err != nil { return nil, err } diff --git a/accounts/keystore/account_cache_test.go b/accounts/keystore/account_cache_test.go index 1ced434c52..d6ecefe88e 100644 --- a/accounts/keystore/account_cache_test.go +++ b/accounts/keystore/account_cache_test.go @@ -18,7 +18,6 @@ package keystore import ( "fmt" - "io/ioutil" "math/rand" "os" "path/filepath" @@ -133,11 +132,11 @@ func TestUpdatedKeyfileContents(t *testing.T) { return } - // needed so that modTime of `file` is different to its current value after ioutil.WriteFile + // needed so that modTime of `file` is different to its current value after io.WriteFile time.Sleep(1000 * time.Millisecond) // Now replace file contents with crap - if err := ioutil.WriteFile(file, []byte("foo"), 0644); err != nil { + if err := os.WriteFile(file, []byte("foo"), 0644); err != nil { t.Fatal(err) return } @@ -150,9 +149,9 @@ func TestUpdatedKeyfileContents(t *testing.T) { // forceCopyFile is like cp.CopyFile, but doesn't complain if the destination exists. func forceCopyFile(dst, src string) error { - data, err := ioutil.ReadFile(src) + data, err := os.ReadFile(src) if err != nil { return err } - return ioutil.WriteFile(dst, data, 0644) + return os.WriteFile(dst, data, 0644) } diff --git a/accounts/keystore/file_cache.go b/accounts/keystore/file_cache.go index bd22a9f519..5bf6cc4955 100644 --- a/accounts/keystore/file_cache.go +++ b/accounts/keystore/file_cache.go @@ -17,7 +17,7 @@ package keystore import ( - "io/ioutil" + "io/fs" "os" "path/filepath" "strings" @@ -42,7 +42,7 @@ func (fc *fileCache) scan(keyDir string) (mapset.Set, mapset.Set, mapset.Set, er t0 := time.Now() // List all the failes from the keystore folder - files, err := ioutil.ReadDir(keyDir) + files, err := os.ReadDir(keyDir) if err != nil { return nil, nil, nil, err } @@ -63,15 +63,19 @@ func (fc *fileCache) scan(keyDir string) (mapset.Set, mapset.Set, mapset.Set, er utils.Logger().Debug().Str("path", path).Msg("Ignoring file on account scan") continue } - // Gather the set of all and fresly modified files + // Gather the set of all and freshly modified files all.Add(path) - modified := fi.ModTime() - if modified.After(fc.lastMod) { - mods.Add(path) - } - if modified.After(newLastMod) { - newLastMod = modified + if info, err := fi.Info(); err != nil { + continue + } else { + modified := info.ModTime() + if modified.After(fc.lastMod) { + mods.Add(path) + } + if modified.After(newLastMod) { + newLastMod = modified + } } } t2 := time.Now() @@ -94,14 +98,18 @@ func (fc *fileCache) scan(keyDir string) (mapset.Set, mapset.Set, mapset.Set, er } // nonKeyFile ignores editor backups, hidden files and folders/symlinks. -func nonKeyFile(fi os.FileInfo) bool { +func nonKeyFile(fi fs.DirEntry) bool { // Skip editor backups and UNIX-style hidden files. if strings.HasSuffix(fi.Name(), "~") || strings.HasPrefix(fi.Name(), ".") { return true } // Skip misc special files, directories (yes, symlinks too). - if fi.IsDir() || fi.Mode()&os.ModeType != 0 { + if info, err := fi.Info(); err != nil { return true + } else { + if fi.IsDir() || info.Mode()&os.ModeType != 0 { + return true + } } return false } diff --git a/accounts/keystore/key.go b/accounts/keystore/key.go index 14e0018e20..c632acf2e5 100644 --- a/accounts/keystore/key.go +++ b/accounts/keystore/key.go @@ -23,7 +23,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "os" "path/filepath" "strings" @@ -195,7 +194,7 @@ func writeTemporaryKeyFile(file string, content []byte) (string, error) { } // Atomic write: create a temporary hidden file first // then move it into place. TempFile assigns mode 0600. - f, err := ioutil.TempFile(filepath.Dir(file), "."+filepath.Base(file)+".tmp") + f, err := os.CreateTemp(filepath.Dir(file), "."+filepath.Base(file)+".tmp") if err != nil { return "", err } diff --git a/accounts/keystore/keystore_test.go b/accounts/keystore/keystore_test.go index fa5392209c..420332a150 100644 --- a/accounts/keystore/keystore_test.go +++ b/accounts/keystore/keystore_test.go @@ -17,7 +17,6 @@ package keystore import ( - "io/ioutil" "os" "runtime" "strings" @@ -213,7 +212,7 @@ func TestSignRace(t *testing.T) { } func tmpKeyStore(t *testing.T, encrypted bool) (string, *KeyStore) { - d, err := ioutil.TempDir("", "eth-keystore-test") + d, err := os.MkdirTemp("", "eth-keystore-test") if err != nil { t.Fatal(err) } diff --git a/accounts/keystore/passphrase.go b/accounts/keystore/passphrase.go index 221f82da46..ffeddd7846 100644 --- a/accounts/keystore/passphrase.go +++ b/accounts/keystore/passphrase.go @@ -34,7 +34,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "os" "path/filepath" @@ -82,7 +81,7 @@ type keyStorePassphrase struct { func (ks keyStorePassphrase) GetKey(addr common.Address, filename, auth string) (*Key, error) { // Load the key from the keystore and decrypt its contents - keyjson, err := ioutil.ReadFile(filename) + keyjson, err := os.ReadFile(filename) if err != nil { return nil, err } diff --git a/accounts/keystore/passphrase_test.go b/accounts/keystore/passphrase_test.go index 630682cebd..b94fce8edc 100644 --- a/accounts/keystore/passphrase_test.go +++ b/accounts/keystore/passphrase_test.go @@ -17,7 +17,7 @@ package keystore import ( - "io/ioutil" + "os" "testing" "github.com/ethereum/go-ethereum/common" @@ -30,7 +30,7 @@ const ( // Tests that a json key file can be decrypted and encrypted in multiple rounds. func TestKeyEncryptDecrypt(t *testing.T) { - keyjson, err := ioutil.ReadFile("testdata/very-light-scrypt.json") + keyjson, err := os.ReadFile("testdata/very-light-scrypt.json") if err != nil { t.Fatal(err) } diff --git a/accounts/keystore/plain_test.go b/accounts/keystore/plain_test.go index 2cccb8e3bd..2ab50a9bf8 100644 --- a/accounts/keystore/plain_test.go +++ b/accounts/keystore/plain_test.go @@ -20,7 +20,6 @@ import ( "crypto/rand" "encoding/hex" "fmt" - "io/ioutil" "os" "path/filepath" "reflect" @@ -32,7 +31,7 @@ import ( ) func tmpKeyStoreIface(t *testing.T, encrypted bool) (dir string, ks keyStore) { - d, err := ioutil.TempDir("", "geth-keystore-test") + d, err := os.MkdirTemp("", "geth-keystore-test") if err != nil { t.Fatal(err) } diff --git a/cmd/harmony/config.go b/cmd/harmony/config.go index 30c72c2e25..5a41f22da8 100644 --- a/cmd/harmony/config.go +++ b/cmd/harmony/config.go @@ -3,7 +3,6 @@ package main import ( "errors" "fmt" - "io/ioutil" "os" "strings" "time" @@ -221,7 +220,7 @@ func promptConfigUpdate() bool { } func loadHarmonyConfig(file string) (harmonyconfig.HarmonyConfig, string, error) { - b, err := ioutil.ReadFile(file) + b, err := os.ReadFile(file) if err != nil { return harmonyconfig.HarmonyConfig{}, "", err } @@ -234,12 +233,12 @@ func loadHarmonyConfig(file string) (harmonyconfig.HarmonyConfig, string, error) } func updateConfigFile(file string) error { - configBytes, err := ioutil.ReadFile(file) + configBytes, err := os.ReadFile(file) if err != nil { return err } backup := fmt.Sprintf("%s.backup", file) - if err := ioutil.WriteFile(backup, configBytes, 0664); err != nil { + if err := os.WriteFile(backup, configBytes, 0664); err != nil { return err } fmt.Printf("Original config backed up to %s\n", fmt.Sprintf("%s.backup", file)) @@ -259,5 +258,5 @@ func writeHarmonyConfigToFile(config harmonyconfig.HarmonyConfig, file string) e if err != nil { return err } - return ioutil.WriteFile(file, b, 0644) + return os.WriteFile(file, b, 0644) } diff --git a/cmd/harmony/config_test.go b/cmd/harmony/config_test.go index d19f5d9858..2711041de8 100644 --- a/cmd/harmony/config_test.go +++ b/cmd/harmony/config_test.go @@ -2,7 +2,6 @@ package main import ( "fmt" - "io/ioutil" "os" "path/filepath" "reflect" @@ -120,7 +119,7 @@ Version = "1.0.4" os.RemoveAll(testDir) os.MkdirAll(testDir, 0777) file := filepath.Join(testDir, "test.config") - err := ioutil.WriteFile(file, []byte(testConfig), 0644) + err := os.WriteFile(file, []byte(testConfig), 0644) if err != nil { t.Fatal(err) } diff --git a/cmd/harmony/main.go b/cmd/harmony/main.go index a820abf149..ea2d62f6e2 100644 --- a/cmd/harmony/main.go +++ b/cmd/harmony/main.go @@ -2,7 +2,6 @@ package main import ( "fmt" - "io/ioutil" "math/big" "math/rand" _ "net/http/pprof" @@ -1034,7 +1033,7 @@ func setupBlacklist(hc harmonyconfig.HarmonyConfig) (map[ethCommon.Address]struc rosetta_common.InitRosettaFile(hc.TxPool.RosettaFixFile) utils.Logger().Debug().Msgf("Using blacklist file at `%s`", hc.TxPool.BlacklistFile) - dat, err := ioutil.ReadFile(hc.TxPool.BlacklistFile) + dat, err := os.ReadFile(hc.TxPool.BlacklistFile) if err != nil { return nil, err } @@ -1085,7 +1084,7 @@ func parseAllowedTxs(data []byte) (map[ethCommon.Address]core.AllowedTxData, err func setupAllowedTxs(hc harmonyconfig.HarmonyConfig) (map[ethCommon.Address]core.AllowedTxData, error) { utils.Logger().Debug().Msgf("Using AllowedTxs file at `%s`", hc.TxPool.AllowedTxsFile) - data, err := ioutil.ReadFile(hc.TxPool.AllowedTxsFile) + data, err := os.ReadFile(hc.TxPool.AllowedTxsFile) if err != nil { return nil, err } @@ -1097,7 +1096,7 @@ func setupLocalAccounts(hc harmonyconfig.HarmonyConfig, blacklist map[ethCommon. // check if file exist var fileData string if _, err := os.Stat(file); err == nil { - b, err := ioutil.ReadFile(file) + b, err := os.ReadFile(file) if err != nil { return nil, err } @@ -1113,22 +1112,23 @@ func setupLocalAccounts(hc harmonyconfig.HarmonyConfig, blacklist map[ethCommon. localAccounts := make(map[ethCommon.Address]struct{}) lines := strings.Split(fileData, "\n") for _, line := range lines { - if len(line) != 0 { // the file may have trailing empty string line - trimmedLine := strings.TrimSpace(line) - if strings.HasPrefix(trimmedLine, "#") { //check the line is not commented - continue - } - addr, err := common.Bech32ToAddress(trimmedLine) - if err != nil { - return nil, err - } - // skip the blacklisted addresses - if _, exists := blacklist[addr]; exists { - utils.Logger().Warn().Msgf("local account with address %s is blacklisted", addr.String()) - continue - } - localAccounts[addr] = struct{}{} + if len(line) == 0 { // the file may have trailing empty string line + continue + } + addrPart := strings.TrimSpace(strings.Split(string(line), "#")[0]) + if len(addrPart) == 0 { // if the line is commented by # + continue + } + addr, err := common.ParseAddr(addrPart) + if err != nil { + return nil, err + } + // skip the blacklisted addresses + if _, exists := blacklist[addr]; exists { + utils.Logger().Warn().Msgf("local account with address %s is blacklisted", addr.String()) + continue } + localAccounts[addr] = struct{}{} } uniqueAddresses := make([]ethCommon.Address, 0, len(localAccounts)) for addr := range localAccounts { diff --git a/core/genesis_util.go b/core/genesis_util.go index 45a075976d..949a6933ba 100644 --- a/core/genesis_util.go +++ b/core/genesis_util.go @@ -3,8 +3,8 @@ package core import ( "encoding/json" "fmt" - "io/ioutil" "math/big" + "os" "sort" "github.com/ethereum/go-ethereum/rlp" @@ -29,7 +29,7 @@ func encodeGenesisConfig(ga []GenesisItem) string { } func parseGenesisConfigFile(fileName string) genesisConfig { - input, err := ioutil.ReadFile(fileName) + input, err := os.ReadFile(fileName) if err != nil { panic(fmt.Sprintf("cannot open genesisblock config file %v, err %v\n", fileName, err)) } diff --git a/core/tx_pool_test.go b/core/tx_pool_test.go index 4e1c214b1a..0d1dfbd2b9 100644 --- a/core/tx_pool_test.go +++ b/core/tx_pool_test.go @@ -19,7 +19,6 @@ package core import ( "crypto/ecdsa" "fmt" - "io/ioutil" "math/big" "math/rand" "os" @@ -1354,7 +1353,7 @@ func testTransactionJournaling(t *testing.T, nolocals bool) { t.Parallel() // Create a temporary file for the journal - file, err := ioutil.TempFile("", "") + file, err := os.CreateTemp("", "") if err != nil { t.Fatalf("failed to create temporary journal: %v", err) } diff --git a/core/vm/instructions_test.go b/core/vm/instructions_test.go index f2666c2f39..634b5c9276 100644 --- a/core/vm/instructions_test.go +++ b/core/vm/instructions_test.go @@ -20,8 +20,8 @@ import ( "bytes" "encoding/json" "fmt" - "io/ioutil" "math/big" + "os" "testing" "github.com/ethereum/go-ethereum/common" @@ -240,7 +240,7 @@ func TestWriteExpectedValues(t *testing.T) { if err != nil { t.Fatal(err) } - _ = ioutil.WriteFile(fmt.Sprintf("testdata/testcases_%v.json", name), data, 0644) + _ = os.WriteFile(fmt.Sprintf("testdata/testcases_%v.json", name), data, 0644) if err != nil { t.Fatal(err) } @@ -250,7 +250,7 @@ func TestWriteExpectedValues(t *testing.T) { // TestJsonTestcases runs through all the testcases defined as json-files func TestJsonTestcases(t *testing.T) { for name := range twoOpMethods { - data, err := ioutil.ReadFile(fmt.Sprintf("testdata/testcases_%v.json", name)) + data, err := os.ReadFile(fmt.Sprintf("testdata/testcases_%v.json", name)) if err != nil { t.Fatal("Failed to read file", err) } diff --git a/eth/rpc/gzip.go b/eth/rpc/gzip.go index a14fd09d54..4e456a2b66 100644 --- a/eth/rpc/gzip.go +++ b/eth/rpc/gzip.go @@ -19,7 +19,6 @@ package rpc import ( "compress/gzip" "io" - "io/ioutil" "net/http" "strings" "sync" @@ -27,7 +26,7 @@ import ( var gzPool = sync.Pool{ New: func() interface{} { - w := gzip.NewWriter(ioutil.Discard) + w := gzip.NewWriter(io.Discard) return w }, } diff --git a/eth/rpc/http.go b/eth/rpc/http.go index 0de127c808..0dba651e59 100644 --- a/eth/rpc/http.go +++ b/eth/rpc/http.go @@ -23,7 +23,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "mime" "net" "net/http" @@ -172,7 +171,7 @@ func (hc *httpConn) doRequest(ctx context.Context, msg interface{}) (io.ReadClos return nil, err } req := hc.req.WithContext(ctx) - req.Body = ioutil.NopCloser(bytes.NewReader(body)) + req.Body = io.NopCloser(bytes.NewReader(body)) req.ContentLength = int64(len(body)) resp, err := hc.client.Do(req) diff --git a/eth/rpc/method_filter.go b/eth/rpc/method_filter.go index 0cc8b3cdd5..4fbbeb7a9c 100644 --- a/eth/rpc/method_filter.go +++ b/eth/rpc/method_filter.go @@ -3,7 +3,6 @@ package rpc import ( "errors" "fmt" - "io/ioutil" "os" "regexp" "strings" @@ -32,7 +31,7 @@ Deny = [ ... ] func (rmf *RpcMethodFilter) LoadRpcMethodFiltersFromFile(file string) error { // check if file exist if _, err := os.Stat(file); err == nil { - b, errRead := ioutil.ReadFile(file) + b, errRead := os.ReadFile(file) if errRead != nil { return fmt.Errorf("rpc filter file read error - %s", errRead.Error()) } diff --git a/eth/rpc/server_test.go b/eth/rpc/server_test.go index 9002f6dc23..1c32aad989 100644 --- a/eth/rpc/server_test.go +++ b/eth/rpc/server_test.go @@ -20,8 +20,8 @@ import ( "bufio" "bytes" "io" - "io/ioutil" "net" + "os" "path/filepath" "strings" "testing" @@ -52,7 +52,7 @@ func TestServerRegisterName(t *testing.T) { } func TestServer(t *testing.T) { - files, err := ioutil.ReadDir("testdata") + files, err := os.ReadDir("testdata") if err != nil { t.Fatal("where'd my testdata go?") } @@ -70,7 +70,7 @@ func TestServer(t *testing.T) { func runTestScript(t *testing.T, file string) { server := newTestServer() - content, err := ioutil.ReadFile(file) + content, err := os.ReadFile(file) if err != nil { t.Fatal(err) } diff --git a/hmy/tracer.go b/hmy/tracer.go index 2529e7a2f6..debfd59a5e 100644 --- a/hmy/tracer.go +++ b/hmy/tracer.go @@ -22,7 +22,6 @@ import ( "encoding/hex" "errors" "fmt" - "io/ioutil" "math/big" "os" "runtime" @@ -577,7 +576,7 @@ func (hmy *Harmony) standardTraceBlockToFile(ctx context.Context, block *types.B // Generate a unique temporary file to dump it into prefix := fmt.Sprintf("block_%#x-%d-%#x-", block.Hash().Bytes()[:4], i, tx.Hash().Bytes()[:4]) - dump, err = ioutil.TempFile(os.TempDir(), prefix) + dump, err = os.CreateTemp(os.TempDir(), prefix) if err != nil { return nil, err } diff --git a/hmy/tracers/internal/tracers/assets.go b/hmy/tracers/internal/tracers/assets.go index a285c53e33..d7b1b38f1d 100644 --- a/hmy/tracers/internal/tracers/assets.go +++ b/hmy/tracers/internal/tracers/assets.go @@ -19,7 +19,6 @@ import ( "compress/gzip" "fmt" "io" - "io/ioutil" "os" "path/filepath" "strings" @@ -410,7 +409,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/internal/blsgen/kms.go b/internal/blsgen/kms.go index 8318c818b1..0a197a54e0 100644 --- a/internal/blsgen/kms.go +++ b/internal/blsgen/kms.go @@ -3,7 +3,7 @@ package blsgen import ( "encoding/json" "fmt" - "io/ioutil" + "os" "sync" "time" @@ -178,7 +178,7 @@ func newFileACProvider(file string) *fileACProvider { } func (provider *fileACProvider) getAwsConfig() (*AwsConfig, error) { - b, err := ioutil.ReadFile(provider.file) + b, err := os.ReadFile(provider.file) if err != nil { return nil, err } diff --git a/internal/blsgen/kms_test.go b/internal/blsgen/kms_test.go index b4c1d06882..7fde9fdfcb 100644 --- a/internal/blsgen/kms_test.go +++ b/internal/blsgen/kms_test.go @@ -5,7 +5,6 @@ import ( "encoding/json" "errors" "fmt" - "io/ioutil" "os" "path/filepath" "reflect" @@ -107,7 +106,7 @@ func writeAwsConfigFile(file string, config AwsConfig) error { return err } } - return ioutil.WriteFile(file, b, 0700) + return os.WriteFile(file, b, 0700) } func TestPromptACProvider_getAwsConfig(t *testing.T) { diff --git a/internal/blsgen/lib.go b/internal/blsgen/lib.go index 6d579e731a..ec1edc0b4f 100644 --- a/internal/blsgen/lib.go +++ b/internal/blsgen/lib.go @@ -8,7 +8,6 @@ import ( "encoding/hex" "fmt" "io" - "io/ioutil" "os" "strings" @@ -51,7 +50,7 @@ func WriteToFile(filename string, data string) error { // LoadBLSKeyWithPassPhrase loads bls key with passphrase. func LoadBLSKeyWithPassPhrase(fileName, passphrase string) (*ffi_bls.SecretKey, error) { - encryptedPrivateKeyBytes, err := ioutil.ReadFile(fileName) + encryptedPrivateKeyBytes, err := os.ReadFile(fileName) if err != nil { return nil, errors.Wrapf(err, "attempted to load from %s", fileName) } @@ -73,7 +72,7 @@ func LoadBLSKeyWithPassPhrase(fileName, passphrase string) (*ffi_bls.SecretKey, // LoadAwsCMKEncryptedBLSKey loads aws encrypted bls key. func LoadAwsCMKEncryptedBLSKey(fileName string, kmsClient *kms.KMS) (*ffi_bls.SecretKey, error) { - encryptedPrivateKeyBytes, err := ioutil.ReadFile(fileName) + encryptedPrivateKeyBytes, err := os.ReadFile(fileName) if err != nil { return nil, errors.Wrapf(err, "fail read at: %s", fileName) } diff --git a/internal/blsgen/passphrase.go b/internal/blsgen/passphrase.go index e0109fc7dd..e26a0a1ebd 100644 --- a/internal/blsgen/passphrase.go +++ b/internal/blsgen/passphrase.go @@ -3,7 +3,7 @@ package blsgen import ( "errors" "fmt" - "io/ioutil" + "io" "os" "strings" "sync" @@ -177,7 +177,7 @@ func (provider *promptPassProvider) persistPassphrase(keyFile string, passPhrase return err } - return ioutil.WriteFile(passFile, []byte(passPhrase), defWritePassFileMode) + return os.WriteFile(passFile, []byte(passPhrase), defWritePassFileMode) } // staticPassProvider provide the bls passphrase from a static .pass file @@ -222,7 +222,7 @@ func readPassFromFile(file string) (string, error) { } defer f.Close() - b, err := ioutil.ReadAll(f) + b, err := io.ReadAll(f) if err != nil { return "", err } diff --git a/internal/blsgen/passphrase_test.go b/internal/blsgen/passphrase_test.go index dbeab26121..31e99dc8dd 100644 --- a/internal/blsgen/passphrase_test.go +++ b/internal/blsgen/passphrase_test.go @@ -4,7 +4,6 @@ import ( "bytes" "errors" "fmt" - "io/ioutil" "os" "path/filepath" "reflect" @@ -315,7 +314,7 @@ func TestPromptPassProvider_getPassphrase(t *testing.T) { t.Errorf("Test %v: pass file exist %v", i, passFile) } } else { - b, err := ioutil.ReadFile(passFile) + b, err := os.ReadFile(passFile) if err != nil { t.Error(err) continue diff --git a/internal/blsgen/utils_test.go b/internal/blsgen/utils_test.go index 04d0bf2a7f..fcfc1dccfb 100644 --- a/internal/blsgen/utils_test.go +++ b/internal/blsgen/utils_test.go @@ -2,7 +2,6 @@ package blsgen import ( "fmt" - "io/ioutil" "os" "path/filepath" "strings" @@ -47,7 +46,7 @@ var testKeys = []testKey{ func writeFile(file string, data string) error { dir := filepath.Dir(file) os.MkdirAll(dir, 0700) - return ioutil.WriteFile(file, []byte(data), 0600) + return os.WriteFile(file, []byte(data), 0600) } func TestPromptYesNo(t *testing.T) { diff --git a/internal/utils/passphrase.go b/internal/utils/passphrase.go index e46a9e7c5a..35b7606a7c 100644 --- a/internal/utils/passphrase.go +++ b/internal/utils/passphrase.go @@ -3,7 +3,6 @@ package utils import ( "fmt" "io" - "io/ioutil" "os" "strconv" "strings" @@ -28,7 +27,7 @@ func AskForPassphrase(prompt string) string { // readAllAsString reads the entire file contents as a string. func readAllAsString(r io.Reader) (data string, err error) { - bytes, err := ioutil.ReadAll(r) + bytes, err := io.ReadAll(r) return string(bytes), err } diff --git a/internal/utils/testing/tempfile.go b/internal/utils/testing/tempfile.go index b20e61fd4a..4fbf99497a 100644 --- a/internal/utils/testing/tempfile.go +++ b/internal/utils/testing/tempfile.go @@ -2,7 +2,6 @@ package testutils import ( "io" - "io/ioutil" "os" "strings" "testing" @@ -13,7 +12,7 @@ import ( // NewTempFile creates a new, empty temp file for testing. Errors are fatal. func NewTempFile(t *testing.T) *os.File { pattern := strings.ReplaceAll(t.Name(), string(os.PathSeparator), "_") - f, err := ioutil.TempFile("", pattern) + f, err := os.CreateTemp("", pattern) if err != nil { t.Fatal(errors.Wrap(err, "cannot create temp file")) } diff --git a/node/node.go b/node/node.go index 129654e414..981338df5f 100644 --- a/node/node.go +++ b/node/node.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "fmt" - "io/ioutil" "math/big" "os" "runtime/pprof" @@ -1434,7 +1433,7 @@ func (node *Node) syncFromTiKVWriter() { if err != nil { panic(err) } - err = ioutil.WriteFile(fmt.Sprintf("/local/%s", time.Now().Format("hmy_0102150405.error.log")), buf.Bytes(), 0644) + err = os.WriteFile(fmt.Sprintf("/local/%s", time.Now().Format("hmy_0102150405.error.log")), buf.Bytes(), 0644) if err != nil { panic(err) } diff --git a/staking/effective/calculate_test.go b/staking/effective/calculate_test.go index 28ae4cbfb2..7127192c3b 100644 --- a/staking/effective/calculate_test.go +++ b/staking/effective/calculate_test.go @@ -3,9 +3,9 @@ package effective import ( "encoding/json" "fmt" - "io/ioutil" "math/big" "math/rand" + "os" "sort" "testing" @@ -35,7 +35,7 @@ type slotsData struct { } func init() { - input, err := ioutil.ReadFile(eposTestingFile) + input, err := os.ReadFile(eposTestingFile) if err != nil { panic( fmt.Sprintf("cannot open genesisblock config file %v, err %v\n", diff --git a/webhooks/yaml.go b/webhooks/yaml.go index 2c0331fcc0..2d7521c0ee 100644 --- a/webhooks/yaml.go +++ b/webhooks/yaml.go @@ -3,8 +3,9 @@ package webhooks import ( "bytes" "encoding/json" - "io/ioutil" + "io" "net/http" + "os" "gopkg.in/yaml.v2" ) @@ -63,7 +64,7 @@ func DoPost(url string, record interface{}) (*ReportResult, error) { return nil, err } defer resp.Body.Close() - result, err := ioutil.ReadAll(resp.Body) + result, err := io.ReadAll(resp.Body) if err != nil { return nil, err } @@ -76,7 +77,7 @@ func DoPost(url string, record interface{}) (*ReportResult, error) { // NewWebHooksFromPath .. func NewWebHooksFromPath(yamlPath string) (*Hooks, error) { - rawYAML, err := ioutil.ReadFile(yamlPath) + rawYAML, err := os.ReadFile(yamlPath) if err != nil { return nil, err }