Skip to content

Commit

Permalink
all: fix some go-critic linter warnings (#23709)
Browse files Browse the repository at this point in the history
This doesn't fix all go-critic warnings, just the most serious ones.

Co-authored-by: Felix Lange <fjl@twurst.com>
Co-authored-by: Martin Holst Swende <martin@swende.se>
  • Loading branch information
3 people committed Oct 13, 2021
1 parent e4f570f commit 778ff94
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 38 deletions.
11 changes: 2 additions & 9 deletions accounts/abi/reflect.go
Expand Up @@ -123,15 +123,8 @@ func set(dst, src reflect.Value) error {
func setSlice(dst, src reflect.Value) error {
slice := reflect.MakeSlice(dst.Type(), src.Len(), src.Len())
for i := 0; i < src.Len(); i++ {
if src.Index(i).Kind() == reflect.Struct {
if err := set(slice.Index(i), src.Index(i)); err != nil {
return err
}
} else {
// e.g. [][32]uint8 to []common.Hash
if err := set(slice.Index(i), src.Index(i)); err != nil {
return err
}
if err := set(slice.Index(i), src.Index(i)); err != nil {
return err
}
}
if dst.CanSet() {
Expand Down
4 changes: 2 additions & 2 deletions cmd/evm/internal/t8ntool/transition.go
Expand Up @@ -419,15 +419,15 @@ func dispatchOutput(ctx *cli.Context, baseDir string, result *ExecutionResult, a
return NewError(ErrorJson, fmt.Errorf("failed marshalling output: %v", err))
}
os.Stdout.Write(b)
os.Stdout.Write([]byte("\n"))
os.Stdout.WriteString("\n")
}
if len(stdErrObject) > 0 {
b, err := json.MarshalIndent(stdErrObject, "", " ")
if err != nil {
return NewError(ErrorJson, fmt.Errorf("failed marshalling output: %v", err))
}
os.Stderr.Write(b)
os.Stderr.Write([]byte("\n"))
os.Stderr.WriteString("\n")
}
return nil
}
4 changes: 2 additions & 2 deletions cmd/faucet/faucet.go
Expand Up @@ -741,7 +741,7 @@ func authTwitter(url string, tokenV1, tokenV2 string) (string, string, string, c
return "", "", "", common.Address{}, errors.New("No Ethereum address found to fund")
}
var avatar string
if parts = regexp.MustCompile("src=\"([^\"]+twimg.com/profile_images[^\"]+)\"").FindStringSubmatch(string(body)); len(parts) == 2 {
if parts = regexp.MustCompile(`src="([^"]+twimg\.com/profile_images[^"]+)"`).FindStringSubmatch(string(body)); len(parts) == 2 {
avatar = parts[1]
}
return username + "@twitter", username, avatar, address, nil
Expand Down Expand Up @@ -867,7 +867,7 @@ func authFacebook(url string) (string, string, common.Address, error) {
return "", "", common.Address{}, errors.New("No Ethereum address found to fund")
}
var avatar string
if parts = regexp.MustCompile("src=\"([^\"]+fbcdn.net[^\"]+)\"").FindStringSubmatch(string(body)); len(parts) == 2 {
if parts = regexp.MustCompile(`src="([^"]+fbcdn\.net[^"]+)"`).FindStringSubmatch(string(body)); len(parts) == 2 {
avatar = parts[1]
}
return username + "@facebook", avatar, address, nil
Expand Down
4 changes: 2 additions & 2 deletions core/bloombits/generator_test.go
Expand Up @@ -70,7 +70,7 @@ func BenchmarkGenerator(b *testing.B) {
if err != nil {
b.Fatalf("failed to create bloombit generator: %v", err)
}
for j, bloom := range input {
for j, bloom := range &input {
if err := gen.AddBloom(uint(j), bloom); err != nil {
b.Fatalf("bloom %d: failed to add: %v", i, err)
}
Expand All @@ -89,7 +89,7 @@ func BenchmarkGenerator(b *testing.B) {
if err != nil {
b.Fatalf("failed to create bloombit generator: %v", err)
}
for j, bloom := range input {
for j, bloom := range &input {
if err := gen.AddBloom(uint(j), bloom); err != nil {
b.Fatalf("bloom %d: failed to add: %v", i, err)
}
Expand Down
40 changes: 21 additions & 19 deletions eth/protocols/eth/handler_test.go
Expand Up @@ -385,7 +385,7 @@ func testGetNodeData(t *testing.T, protocol uint) {
acc2Addr := crypto.PubkeyToAddress(acc2Key.PublicKey)

signer := types.HomesteadSigner{}
// Create a chain generator with some simple transactions (blatantly stolen from @fjl/chain_markets_test)
// Create a chain generator with some simple transactions (blatantly stolen from @fjl/chain_makers_test)
generator := func(i int, block *core.BlockGen) {
switch i {
case 0:
Expand Down Expand Up @@ -420,9 +420,8 @@ func testGetNodeData(t *testing.T, protocol uint) {
peer, _ := newTestPeer("peer", protocol, backend)
defer peer.close()

// Fetch for now the entire chain db
// Collect all state tree hashes.
var hashes []common.Hash

it := backend.db.NewIterator(nil, nil)
for it.Next() {
if key := it.Key(); len(key) == common.HashLength {
Expand All @@ -431,6 +430,7 @@ func testGetNodeData(t *testing.T, protocol uint) {
}
it.Release()

// Request all hashes.
p2p.Send(peer.app, GetNodeDataMsg, GetNodeDataPacket66{
RequestId: 123,
GetNodeDataPacket: hashes,
Expand All @@ -442,38 +442,40 @@ func testGetNodeData(t *testing.T, protocol uint) {
if msg.Code != NodeDataMsg {
t.Fatalf("response packet code mismatch: have %x, want %x", msg.Code, NodeDataMsg)
}
var (
data [][]byte
res NodeDataPacket66
)
var res NodeDataPacket66
if err := msg.Decode(&res); err != nil {
t.Fatalf("failed to decode response node data: %v", err)
}
data = res.NodeDataPacket
// Verify that all hashes correspond to the requested data, and reconstruct a state tree

// Verify that all hashes correspond to the requested data.
data := res.NodeDataPacket
for i, want := range hashes {
if hash := crypto.Keccak256Hash(data[i]); hash != want {
t.Errorf("data hash mismatch: have %x, want %x", hash, want)
}
}
statedb := rawdb.NewMemoryDatabase()

// Reconstruct state tree from the received data.
reconstructDB := rawdb.NewMemoryDatabase()
for i := 0; i < len(data); i++ {
statedb.Put(hashes[i].Bytes(), data[i])
rawdb.WriteTrieNode(reconstructDB, hashes[i], data[i])
}

// Sanity check whether all state matches.
accounts := []common.Address{testAddr, acc1Addr, acc2Addr}
for i := uint64(0); i <= backend.chain.CurrentBlock().NumberU64(); i++ {
trie, _ := state.New(backend.chain.GetBlockByNumber(i).Root(), state.NewDatabase(statedb), nil)

root := backend.chain.GetBlockByNumber(i).Root()
reconstructed, _ := state.New(root, state.NewDatabase(reconstructDB), nil)
for j, acc := range accounts {
state, _ := backend.chain.State()
state, _ := backend.chain.StateAt(root)
bw := state.GetBalance(acc)
bh := trie.GetBalance(acc)
bh := reconstructed.GetBalance(acc)

if (bw != nil && bh == nil) || (bw == nil && bh != nil) {
t.Errorf("test %d, account %d: balance mismatch: have %v, want %v", i, j, bh, bw)
if (bw == nil) != (bh == nil) {
t.Errorf("block %d, account %d: balance mismatch: have %v, want %v", i, j, bh, bw)
}
if bw != nil && bh != nil && bw.Cmp(bw) != 0 {
t.Errorf("test %d, account %d: balance mismatch: have %v, want %v", i, j, bh, bw)
if bw != nil && bh != nil && bw.Cmp(bh) != 0 {
t.Errorf("block %d, account %d: balance mismatch: have %v, want %v", i, j, bh, bw)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions node/api.go
Expand Up @@ -218,7 +218,7 @@ func (api *privateAdminAPI) StartHTTP(host *string, port *int, cors *string, api
}

// StartRPC starts the HTTP RPC API server.
// This method is deprecated. Use StartHTTP instead.
// Deprecated: use StartHTTP instead.
func (api *privateAdminAPI) StartRPC(host *string, port *int, cors *string, apis *string, vhosts *string) (bool, error) {
log.Warn("Deprecation warning", "method", "admin.StartRPC", "use-instead", "admin.StartHTTP")
return api.StartHTTP(host, port, cors, apis, vhosts)
Expand All @@ -231,7 +231,7 @@ func (api *privateAdminAPI) StopHTTP() (bool, error) {
}

// StopRPC shuts down the HTTP server.
// This method is deprecated. Use StopHTTP instead.
// Deprecated: use StopHTTP instead.
func (api *privateAdminAPI) StopRPC() (bool, error) {
log.Warn("Deprecation warning", "method", "admin.StopRPC", "use-instead", "admin.StopHTTP")
return api.StopHTTP()
Expand Down
4 changes: 3 additions & 1 deletion signer/core/signed_data.go
Expand Up @@ -29,6 +29,7 @@ import (
"strconv"
"strings"
"unicode"
"unicode/utf8"

"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -100,7 +101,8 @@ func (t *Type) isReferenceType() bool {
return false
}
// Reference types must have a leading uppercase character
return unicode.IsUpper([]rune(t.Type)[0])
r, _ := utf8.DecodeRuneInString(t.Type)
return unicode.IsUpper(r)
}

type Types map[string][]Type
Expand Down
2 changes: 1 addition & 1 deletion signer/core/validation.go
Expand Up @@ -21,7 +21,7 @@ import (
"regexp"
)

var printable7BitAscii = regexp.MustCompile("^[A-Za-z0-9!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~ ]+$")
var printable7BitAscii = regexp.MustCompile("^[A-Za-z0-9!\"#$%&'()*+,\\-./:;<=>?@[\\]^_`{|}~ ]+$")

// ValidatePasswordFormat returns an error if the password is too short, or consists of characters
// outside the range of the printable 7bit ascii set
Expand Down

0 comments on commit 778ff94

Please sign in to comment.