Skip to content

Commit ae578ba

Browse files
update deprecated ioutil, improve local accounts (#4527)
1 parent ce2c057 commit ae578ba

31 files changed

+88
-100
lines changed

Diff for: accounts/abi/bind/auth.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"crypto/ecdsa"
2121
"errors"
2222
"io"
23-
"io/ioutil"
2423
"math/big"
2524

2625
"github.com/ethereum/go-ethereum/common"
@@ -44,7 +43,7 @@ var ErrNotAuthorized = errors.New("not authorized to sign this account")
4443
// Deprecated: Use NewTransactorWithChainID instead.
4544
func NewTransactor(keyin io.Reader, passphrase string) (*TransactOpts, error) {
4645
log.Warn("WARNING: NewTransactor has been deprecated in favour of NewTransactorWithChainID")
47-
json, err := ioutil.ReadAll(keyin)
46+
json, err := io.ReadAll(keyin)
4847
if err != nil {
4948
return nil, err
5049
}
@@ -103,7 +102,7 @@ func NewKeyedTransactor(key *ecdsa.PrivateKey) *TransactOpts {
103102
// NewTransactorWithChainID is a utility method to easily create a transaction signer from
104103
// an encrypted json key stream and the associated passphrase.
105104
func NewTransactorWithChainID(keyin io.Reader, passphrase string, chainID *big.Int) (*TransactOpts, error) {
106-
json, err := ioutil.ReadAll(keyin)
105+
json, err := io.ReadAll(keyin)
107106
if err != nil {
108107
return nil, err
109108
}

Diff for: accounts/keystore/account_cache_test.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package keystore
1818

1919
import (
2020
"fmt"
21-
"io/ioutil"
2221
"math/rand"
2322
"os"
2423
"path/filepath"
@@ -133,11 +132,11 @@ func TestUpdatedKeyfileContents(t *testing.T) {
133132
return
134133
}
135134

136-
// needed so that modTime of `file` is different to its current value after ioutil.WriteFile
135+
// needed so that modTime of `file` is different to its current value after io.WriteFile
137136
time.Sleep(1000 * time.Millisecond)
138137

139138
// Now replace file contents with crap
140-
if err := ioutil.WriteFile(file, []byte("foo"), 0644); err != nil {
139+
if err := os.WriteFile(file, []byte("foo"), 0644); err != nil {
141140
t.Fatal(err)
142141
return
143142
}
@@ -150,9 +149,9 @@ func TestUpdatedKeyfileContents(t *testing.T) {
150149

151150
// forceCopyFile is like cp.CopyFile, but doesn't complain if the destination exists.
152151
func forceCopyFile(dst, src string) error {
153-
data, err := ioutil.ReadFile(src)
152+
data, err := os.ReadFile(src)
154153
if err != nil {
155154
return err
156155
}
157-
return ioutil.WriteFile(dst, data, 0644)
156+
return os.WriteFile(dst, data, 0644)
158157
}

Diff for: accounts/keystore/file_cache.go

+19-11
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package keystore
1818

1919
import (
20-
"io/ioutil"
20+
"io/fs"
2121
"os"
2222
"path/filepath"
2323
"strings"
@@ -42,7 +42,7 @@ func (fc *fileCache) scan(keyDir string) (mapset.Set, mapset.Set, mapset.Set, er
4242
t0 := time.Now()
4343

4444
// List all the failes from the keystore folder
45-
files, err := ioutil.ReadDir(keyDir)
45+
files, err := os.ReadDir(keyDir)
4646
if err != nil {
4747
return nil, nil, nil, err
4848
}
@@ -63,15 +63,19 @@ func (fc *fileCache) scan(keyDir string) (mapset.Set, mapset.Set, mapset.Set, er
6363
utils.Logger().Debug().Str("path", path).Msg("Ignoring file on account scan")
6464
continue
6565
}
66-
// Gather the set of all and fresly modified files
66+
// Gather the set of all and freshly modified files
6767
all.Add(path)
6868

69-
modified := fi.ModTime()
70-
if modified.After(fc.lastMod) {
71-
mods.Add(path)
72-
}
73-
if modified.After(newLastMod) {
74-
newLastMod = modified
69+
if info, err := fi.Info(); err != nil {
70+
continue
71+
} else {
72+
modified := info.ModTime()
73+
if modified.After(fc.lastMod) {
74+
mods.Add(path)
75+
}
76+
if modified.After(newLastMod) {
77+
newLastMod = modified
78+
}
7579
}
7680
}
7781
t2 := time.Now()
@@ -94,14 +98,18 @@ func (fc *fileCache) scan(keyDir string) (mapset.Set, mapset.Set, mapset.Set, er
9498
}
9599

96100
// nonKeyFile ignores editor backups, hidden files and folders/symlinks.
97-
func nonKeyFile(fi os.FileInfo) bool {
101+
func nonKeyFile(fi fs.DirEntry) bool {
98102
// Skip editor backups and UNIX-style hidden files.
99103
if strings.HasSuffix(fi.Name(), "~") || strings.HasPrefix(fi.Name(), ".") {
100104
return true
101105
}
102106
// Skip misc special files, directories (yes, symlinks too).
103-
if fi.IsDir() || fi.Mode()&os.ModeType != 0 {
107+
if info, err := fi.Info(); err != nil {
104108
return true
109+
} else {
110+
if fi.IsDir() || info.Mode()&os.ModeType != 0 {
111+
return true
112+
}
105113
}
106114
return false
107115
}

Diff for: accounts/keystore/key.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"encoding/json"
2424
"fmt"
2525
"io"
26-
"io/ioutil"
2726
"os"
2827
"path/filepath"
2928
"strings"
@@ -195,7 +194,7 @@ func writeTemporaryKeyFile(file string, content []byte) (string, error) {
195194
}
196195
// Atomic write: create a temporary hidden file first
197196
// then move it into place. TempFile assigns mode 0600.
198-
f, err := ioutil.TempFile(filepath.Dir(file), "."+filepath.Base(file)+".tmp")
197+
f, err := os.CreateTemp(filepath.Dir(file), "."+filepath.Base(file)+".tmp")
199198
if err != nil {
200199
return "", err
201200
}

Diff for: accounts/keystore/keystore_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package keystore
1818

1919
import (
20-
"io/ioutil"
2120
"os"
2221
"runtime"
2322
"strings"
@@ -213,7 +212,7 @@ func TestSignRace(t *testing.T) {
213212
}
214213

215214
func tmpKeyStore(t *testing.T, encrypted bool) (string, *KeyStore) {
216-
d, err := ioutil.TempDir("", "eth-keystore-test")
215+
d, err := os.MkdirTemp("", "eth-keystore-test")
217216
if err != nil {
218217
t.Fatal(err)
219218
}

Diff for: accounts/keystore/passphrase.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import (
3434
"encoding/json"
3535
"fmt"
3636
"io"
37-
"io/ioutil"
3837
"os"
3938
"path/filepath"
4039

@@ -82,7 +81,7 @@ type keyStorePassphrase struct {
8281

8382
func (ks keyStorePassphrase) GetKey(addr common.Address, filename, auth string) (*Key, error) {
8483
// Load the key from the keystore and decrypt its contents
85-
keyjson, err := ioutil.ReadFile(filename)
84+
keyjson, err := os.ReadFile(filename)
8685
if err != nil {
8786
return nil, err
8887
}

Diff for: accounts/keystore/passphrase_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package keystore
1818

1919
import (
20-
"io/ioutil"
20+
"os"
2121
"testing"
2222

2323
"github.com/ethereum/go-ethereum/common"
@@ -30,7 +30,7 @@ const (
3030

3131
// Tests that a json key file can be decrypted and encrypted in multiple rounds.
3232
func TestKeyEncryptDecrypt(t *testing.T) {
33-
keyjson, err := ioutil.ReadFile("testdata/very-light-scrypt.json")
33+
keyjson, err := os.ReadFile("testdata/very-light-scrypt.json")
3434
if err != nil {
3535
t.Fatal(err)
3636
}

Diff for: accounts/keystore/plain_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"crypto/rand"
2121
"encoding/hex"
2222
"fmt"
23-
"io/ioutil"
2423
"os"
2524
"path/filepath"
2625
"reflect"
@@ -32,7 +31,7 @@ import (
3231
)
3332

3433
func tmpKeyStoreIface(t *testing.T, encrypted bool) (dir string, ks keyStore) {
35-
d, err := ioutil.TempDir("", "geth-keystore-test")
34+
d, err := os.MkdirTemp("", "geth-keystore-test")
3635
if err != nil {
3736
t.Fatal(err)
3837
}

Diff for: cmd/harmony/config.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
"errors"
55
"fmt"
6-
"io/ioutil"
76
"os"
87
"strings"
98
"time"
@@ -221,7 +220,7 @@ func promptConfigUpdate() bool {
221220
}
222221

223222
func loadHarmonyConfig(file string) (harmonyconfig.HarmonyConfig, string, error) {
224-
b, err := ioutil.ReadFile(file)
223+
b, err := os.ReadFile(file)
225224
if err != nil {
226225
return harmonyconfig.HarmonyConfig{}, "", err
227226
}
@@ -234,12 +233,12 @@ func loadHarmonyConfig(file string) (harmonyconfig.HarmonyConfig, string, error)
234233
}
235234

236235
func updateConfigFile(file string) error {
237-
configBytes, err := ioutil.ReadFile(file)
236+
configBytes, err := os.ReadFile(file)
238237
if err != nil {
239238
return err
240239
}
241240
backup := fmt.Sprintf("%s.backup", file)
242-
if err := ioutil.WriteFile(backup, configBytes, 0664); err != nil {
241+
if err := os.WriteFile(backup, configBytes, 0664); err != nil {
243242
return err
244243
}
245244
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
259258
if err != nil {
260259
return err
261260
}
262-
return ioutil.WriteFile(file, b, 0644)
261+
return os.WriteFile(file, b, 0644)
263262
}

Diff for: cmd/harmony/config_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"fmt"
5-
"io/ioutil"
65
"os"
76
"path/filepath"
87
"reflect"
@@ -120,7 +119,7 @@ Version = "1.0.4"
120119
os.RemoveAll(testDir)
121120
os.MkdirAll(testDir, 0777)
122121
file := filepath.Join(testDir, "test.config")
123-
err := ioutil.WriteFile(file, []byte(testConfig), 0644)
122+
err := os.WriteFile(file, []byte(testConfig), 0644)
124123
if err != nil {
125124
t.Fatal(err)
126125
}

Diff for: cmd/harmony/main.go

+19-19
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"fmt"
5-
"io/ioutil"
65
"math/big"
76
"math/rand"
87
_ "net/http/pprof"
@@ -1034,7 +1033,7 @@ func setupBlacklist(hc harmonyconfig.HarmonyConfig) (map[ethCommon.Address]struc
10341033
rosetta_common.InitRosettaFile(hc.TxPool.RosettaFixFile)
10351034

10361035
utils.Logger().Debug().Msgf("Using blacklist file at `%s`", hc.TxPool.BlacklistFile)
1037-
dat, err := ioutil.ReadFile(hc.TxPool.BlacklistFile)
1036+
dat, err := os.ReadFile(hc.TxPool.BlacklistFile)
10381037
if err != nil {
10391038
return nil, err
10401039
}
@@ -1085,7 +1084,7 @@ func parseAllowedTxs(data []byte) (map[ethCommon.Address]core.AllowedTxData, err
10851084

10861085
func setupAllowedTxs(hc harmonyconfig.HarmonyConfig) (map[ethCommon.Address]core.AllowedTxData, error) {
10871086
utils.Logger().Debug().Msgf("Using AllowedTxs file at `%s`", hc.TxPool.AllowedTxsFile)
1088-
data, err := ioutil.ReadFile(hc.TxPool.AllowedTxsFile)
1087+
data, err := os.ReadFile(hc.TxPool.AllowedTxsFile)
10891088
if err != nil {
10901089
return nil, err
10911090
}
@@ -1097,7 +1096,7 @@ func setupLocalAccounts(hc harmonyconfig.HarmonyConfig, blacklist map[ethCommon.
10971096
// check if file exist
10981097
var fileData string
10991098
if _, err := os.Stat(file); err == nil {
1100-
b, err := ioutil.ReadFile(file)
1099+
b, err := os.ReadFile(file)
11011100
if err != nil {
11021101
return nil, err
11031102
}
@@ -1113,22 +1112,23 @@ func setupLocalAccounts(hc harmonyconfig.HarmonyConfig, blacklist map[ethCommon.
11131112
localAccounts := make(map[ethCommon.Address]struct{})
11141113
lines := strings.Split(fileData, "\n")
11151114
for _, line := range lines {
1116-
if len(line) != 0 { // the file may have trailing empty string line
1117-
trimmedLine := strings.TrimSpace(line)
1118-
if strings.HasPrefix(trimmedLine, "#") { //check the line is not commented
1119-
continue
1120-
}
1121-
addr, err := common.Bech32ToAddress(trimmedLine)
1122-
if err != nil {
1123-
return nil, err
1124-
}
1125-
// skip the blacklisted addresses
1126-
if _, exists := blacklist[addr]; exists {
1127-
utils.Logger().Warn().Msgf("local account with address %s is blacklisted", addr.String())
1128-
continue
1129-
}
1130-
localAccounts[addr] = struct{}{}
1115+
if len(line) == 0 { // the file may have trailing empty string line
1116+
continue
1117+
}
1118+
addrPart := strings.TrimSpace(strings.Split(string(line), "#")[0])
1119+
if len(addrPart) == 0 { // if the line is commented by #
1120+
continue
1121+
}
1122+
addr, err := common.ParseAddr(addrPart)
1123+
if err != nil {
1124+
return nil, err
1125+
}
1126+
// skip the blacklisted addresses
1127+
if _, exists := blacklist[addr]; exists {
1128+
utils.Logger().Warn().Msgf("local account with address %s is blacklisted", addr.String())
1129+
continue
11311130
}
1131+
localAccounts[addr] = struct{}{}
11321132
}
11331133
uniqueAddresses := make([]ethCommon.Address, 0, len(localAccounts))
11341134
for addr := range localAccounts {

Diff for: core/genesis_util.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package core
33
import (
44
"encoding/json"
55
"fmt"
6-
"io/ioutil"
76
"math/big"
7+
"os"
88
"sort"
99

1010
"github.com/ethereum/go-ethereum/rlp"
@@ -29,7 +29,7 @@ func encodeGenesisConfig(ga []GenesisItem) string {
2929
}
3030

3131
func parseGenesisConfigFile(fileName string) genesisConfig {
32-
input, err := ioutil.ReadFile(fileName)
32+
input, err := os.ReadFile(fileName)
3333
if err != nil {
3434
panic(fmt.Sprintf("cannot open genesisblock config file %v, err %v\n", fileName, err))
3535
}

Diff for: core/tx_pool_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package core
1919
import (
2020
"crypto/ecdsa"
2121
"fmt"
22-
"io/ioutil"
2322
"math/big"
2423
"math/rand"
2524
"os"
@@ -1354,7 +1353,7 @@ func testTransactionJournaling(t *testing.T, nolocals bool) {
13541353
t.Parallel()
13551354

13561355
// Create a temporary file for the journal
1357-
file, err := ioutil.TempFile("", "")
1356+
file, err := os.CreateTemp("", "")
13581357
if err != nil {
13591358
t.Fatalf("failed to create temporary journal: %v", err)
13601359
}

0 commit comments

Comments
 (0)