Skip to content

Commit

Permalink
update deprecated ioutil, improve local accounts (#4527)
Browse files Browse the repository at this point in the history
  • Loading branch information
GheisMohammadi authored Oct 15, 2023
1 parent ce2c057 commit ae578ba
Show file tree
Hide file tree
Showing 31 changed files with 88 additions and 100 deletions.
5 changes: 2 additions & 3 deletions accounts/abi/bind/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"crypto/ecdsa"
"errors"
"io"
"io/ioutil"
"math/big"

"github.com/ethereum/go-ethereum/common"
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
9 changes: 4 additions & 5 deletions accounts/keystore/account_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package keystore

import (
"fmt"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
}
30 changes: 19 additions & 11 deletions accounts/keystore/file_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package keystore

import (
"io/ioutil"
"io/fs"
"os"
"path/filepath"
"strings"
Expand All @@ -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
}
Expand All @@ -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()
Expand All @@ -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
}
3 changes: 1 addition & 2 deletions accounts/keystore/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 1 addition & 2 deletions accounts/keystore/keystore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package keystore

import (
"io/ioutil"
"os"
"runtime"
"strings"
Expand Down Expand Up @@ -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)
}
Expand Down
3 changes: 1 addition & 2 deletions accounts/keystore/passphrase.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"

Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions accounts/keystore/passphrase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package keystore

import (
"io/ioutil"
"os"
"testing"

"github.com/ethereum/go-ethereum/common"
Expand All @@ -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)
}
Expand Down
3 changes: 1 addition & 2 deletions accounts/keystore/plain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"crypto/rand"
"encoding/hex"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
Expand All @@ -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)
}
Expand Down
9 changes: 4 additions & 5 deletions cmd/harmony/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"errors"
"fmt"
"io/ioutil"
"os"
"strings"
"time"
Expand Down Expand Up @@ -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
}
Expand All @@ -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))
Expand All @@ -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)
}
3 changes: 1 addition & 2 deletions cmd/harmony/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -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)
}
Expand Down
38 changes: 19 additions & 19 deletions cmd/harmony/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"fmt"
"io/ioutil"
"math/big"
"math/rand"
_ "net/http/pprof"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions core/genesis_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package core
import (
"encoding/json"
"fmt"
"io/ioutil"
"math/big"
"os"
"sort"

"github.com/ethereum/go-ethereum/rlp"
Expand All @@ -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))
}
Expand Down
3 changes: 1 addition & 2 deletions core/tx_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package core
import (
"crypto/ecdsa"
"fmt"
"io/ioutil"
"math/big"
"math/rand"
"os"
Expand Down Expand Up @@ -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)
}
Expand Down
Loading

0 comments on commit ae578ba

Please sign in to comment.