From 131d7b221fec5903b1bf6b537e96190bba1979b3 Mon Sep 17 00:00:00 2001 From: marcello33 Date: Thu, 15 Jun 2023 14:54:08 +0200 Subject: [PATCH] dev: fix: solve more lint issues --- accounts/abi/abi.go | 2 + accounts/abi/selector_parser.go | 2 + accounts/abi/type.go | 5 ++ accounts/abi/unpack.go | 10 +++ accounts/abi/unpack_test.go | 12 +++ accounts/abi/utils.go | 2 + accounts/keystore/watch.go | 1 - accounts/usbwallet/trezor.go | 2 + cmd/abigen/main.go | 2 + cmd/checkpoint-admin/exec.go | 2 + cmd/checkpoint-admin/main.go | 2 + cmd/checkpoint-admin/status.go | 1 - cmd/devp2p/discv4cmd.go | 2 + cmd/devp2p/discv5cmd.go | 2 + cmd/devp2p/dns_cloudflare.go | 2 + cmd/devp2p/dns_route53.go | 2 + cmd/devp2p/dnscmd.go | 2 + cmd/devp2p/enrcmd.go | 2 + cmd/devp2p/internal/ethtest/chain.go | 1 + cmd/devp2p/internal/ethtest/helpers.go | 1 + cmd/devp2p/internal/ethtest/suite.go | 3 + cmd/devp2p/internal/ethtest/transaction.go | 2 + cmd/devp2p/internal/v5test/framework.go | 1 + cmd/devp2p/keycmd.go | 2 + cmd/devp2p/main.go | 2 + cmd/devp2p/nodesetcmd.go | 2 + cmd/devp2p/rlpxcmd.go | 2 + cmd/devp2p/runtest.go | 2 + cmd/ethkey/changepassword.go | 2 + cmd/ethkey/inspect.go | 2 + cmd/ethkey/main.go | 1 - cmd/ethkey/message.go | 2 + cmd/ethkey/utils.go | 1 - cmd/evm/disasm.go | 1 - cmd/evm/internal/t8ntool/flags.go | 2 + cmd/evm/main.go | 2 + cmd/evm/runner.go | 2 + cmd/evm/staterunner.go | 2 + cmd/geth/accountcmd.go | 2 + cmd/geth/chaincmd.go | 2 + cmd/geth/consolecmd.go | 3 + cmd/geth/misccmd.go | 2 + cmd/geth/snapshot.go | 2 + cmd/geth/version_check.go | 1 - cmd/p2psim/main.go | 2 + cmd/rlpdump/main.go | 1 + cmd/rlpdump/rlpdump_test.go | 1 + cmd/utils/bor_flags.go | 3 + cmd/utils/flags.go | 5 ++ common/prque/lazyqueue.go | 1 - common/types_test.go | 1 + consensus/beacon/consensus.go | 12 +++ consensus/ethash/consensus.go | 3 +- consensus/ethash/consensus_test.go | 1 + consensus/ethash/ethash.go | 2 + consensus/misc/eip4844.go | 3 + consensus/misc/eip4844_test.go | 5 ++ core/asm/compiler.go | 1 + core/blockchain.go | 1 + core/bloombits/generator_test.go | 2 +- core/bloombits/matcher_test.go | 2 + core/forkchoice_test.go | 1 + core/forkid/forkid.go | 11 +++ core/rawdb/freezer.go | 2 + core/state/iterator_test.go | 9 +- core/state/statedb.go | 27 +++++- core/state/statedb_test.go | 15 +++- core/state/sync.go | 2 + core/state/sync_test.go | 86 +++++++++++++++++-- core/state/transient_storage.go | 3 + core/state/trie_prefetcher.go | 1 + core/state/trie_prefetcher_test.go | 1 + core/txpool/list_test.go | 2 + core/types/receipt_test.go | 2 + core/vm/analysis.go | 1 + core/vm/eips.go | 5 ++ core/vm/gas_table.go | 6 ++ core/vm/gas_table_test.go | 10 +++ core/vm/instructions.go | 1 + core/vm/instructions_test.go | 9 +- core/vm/interpreter.go | 8 ++ core/vm/jump_table.go | 3 + core/vm/jump_table_export.go | 1 + eth/backend.go | 1 + eth/peer.go | 1 + eth/protocols/eth/handler_test.go | 1 + eth/protocols/snap/sync.go | 7 +- eth/tracers/api_test.go | 3 + eth/tracers/logger/logger.go | 9 ++ eth/tracers/logger/logger_test.go | 1 + ethclient/gethclient/gethclient_test.go | 1 + graphql/graphql_test.go | 2 + internal/cli/server/server.go | 2 + internal/flags/helpers.go | 2 + les/utils/timeutils_test.go | 1 + metrics/counter_float64.go | 2 + miner/test_backend.go | 2 +- node/node.go | 4 +- p2p/dial.go | 1 + p2p/discover/lookup.go | 1 + p2p/discover/table.go | 1 + p2p/discover/table_test.go | 5 ++ p2p/discover/v5_udp.go | 6 ++ p2p/discover/v5_udp_test.go | 4 +- p2p/discover/v5wire/encoding.go | 2 + p2p/discover/v5wire/msg.go | 1 + p2p/dnsdisc/client_test.go | 5 +- p2p/enode/iter.go | 1 + p2p/enr/enr.go | 3 + p2p/enr/enr_test.go | 5 +- p2p/enr/entries.go | 1 + p2p/message.go | 1 + p2p/peer_error.go | 1 + p2p/simulations/mocker_test.go | 1 + rlp/decode.go | 5 +- rlp/decode_test.go | 4 +- rlp/encbuffer.go | 3 + rlp/encode.go | 4 +- rlp/encode_test.go | 14 +-- rlp/raw_test.go | 2 + .../apitypes/signed_data_internal_test.go | 10 +++ signer/core/apitypes/types.go | 13 +++ signer/fourbyte/fourbyte.go | 2 + tests/bor/bor_sprint_length_change_test.go | 3 +- tests/fuzzers/difficulty/debug/main.go | 1 + tests/fuzzers/rlp/rlp_fuzzer.go | 2 + trie/sync.go | 2 +- 127 files changed, 460 insertions(+), 37 deletions(-) diff --git a/accounts/abi/abi.go b/accounts/abi/abi.go index 62b860e18c..e96a0a0425 100644 --- a/accounts/abi/abi.go +++ b/accounts/abi/abi.go @@ -246,7 +246,9 @@ func UnpackRevert(data []byte) (string, error) { if !bytes.Equal(data[:4], revertSelector) { return "", errors.New("invalid data for unpacking") } + typ, err := NewType("string", "", nil) + if err != nil { return "", err } diff --git a/accounts/abi/selector_parser.go b/accounts/abi/selector_parser.go index d5472e374f..048c87eca6 100644 --- a/accounts/abi/selector_parser.go +++ b/accounts/abi/selector_parser.go @@ -102,6 +102,7 @@ func parseCompositeType(unescapedSelector string) ([]interface{}, string, error) if len(rest) == 0 || rest[0] != ')' { return nil, "", fmt.Errorf("expected ')', got '%s'", rest) } + if len(rest) >= 3 && rest[1] == '[' && rest[2] == ']' { return append(result, "[]"), rest[3:], nil } @@ -131,6 +132,7 @@ func assembleArgs(args []interface{}) ([]ArgumentMarshaling, error) { if err != nil { return nil, fmt.Errorf("failed to assemble components: %v", err) } + // nolint:goconst tupleType := "tuple" if len(subArgs) != 0 && subArgs[len(subArgs)-1].Type == "[]" { subArgs = subArgs[:len(subArgs)-1] diff --git a/accounts/abi/type.go b/accounts/abi/type.go index 7f74907a84..015558b3e7 100644 --- a/accounts/abi/type.go +++ b/accounts/abi/type.go @@ -174,15 +174,20 @@ func NewType(t string, internalType string, components []ArgumentMarshaling) (ty if err != nil { return Type{}, err } + name := ToCamelCase(c.Name) + if name == "" { return Type{}, errors.New("abi: purely anonymous or underscored field is not supported") } + fieldName := ResolveNameConflict(name, func(s string) bool { return used[s] }) if err != nil { return Type{}, err } + used[fieldName] = true + if !isValidFieldName(fieldName) { return Type{}, fmt.Errorf("field %d has invalid name", idx) } diff --git a/accounts/abi/unpack.go b/accounts/abi/unpack.go index 1e778a6ff0..1a333f8754 100644 --- a/accounts/abi/unpack.go +++ b/accounts/abi/unpack.go @@ -34,6 +34,7 @@ var ( ) // ReadInteger reads the integer based on its kind and returns the appropriate value. +// nolint:gocognit func ReadInteger(typ Type, b []byte) (interface{}, error) { ret := new(big.Int).SetBytes(b) @@ -44,21 +45,25 @@ func ReadInteger(typ Type, b []byte) (interface{}, error) { if !isu64 || u64 > math.MaxUint8 { return nil, errBadUint8 } + return byte(u64), nil case 16: if !isu64 || u64 > math.MaxUint16 { return nil, errBadUint16 } + return uint16(u64), nil case 32: if !isu64 || u64 > math.MaxUint32 { return nil, errBadUint32 } + return uint32(u64), nil case 64: if !isu64 { return nil, errBadUint64 } + return u64, nil default: // the only case left for unsigned integer is uint256. @@ -74,27 +79,32 @@ func ReadInteger(typ Type, b []byte) (interface{}, error) { ret.Add(ret, common.Big1) ret.Neg(ret) } + i64, isi64 := ret.Int64(), ret.IsInt64() switch typ.Size { case 8: if !isi64 || i64 < math.MinInt8 || i64 > math.MaxInt8 { return nil, errBadInt8 } + return int8(i64), nil case 16: if !isi64 || i64 < math.MinInt16 || i64 > math.MaxInt16 { return nil, errBadInt16 } + return int16(i64), nil case 32: if !isi64 || i64 < math.MinInt32 || i64 > math.MaxInt32 { return nil, errBadInt32 } + return int32(i64), nil case 64: if !isi64 { return nil, errBadInt64 } + return i64, nil default: // the only case left for integer is int256 diff --git a/accounts/abi/unpack_test.go b/accounts/abi/unpack_test.go index 1a5b7b2bb0..6c2624fb30 100644 --- a/accounts/abi/unpack_test.go +++ b/accounts/abi/unpack_test.go @@ -430,6 +430,7 @@ func TestMultiReturnWithStringArray(t *testing.T) { } buff := new(bytes.Buffer) buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000005c1b78ea0000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000001a055690d9db80000000000000000000000000000ab1257528b3782fb40d7ed5f72e624b744dffb2f00000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000008457468657265756d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001048656c6c6f2c20457468657265756d2100000000000000000000000000000000")) + temp, _ := new(big.Int).SetString("30000000000000000000", 10) ret1, ret1Exp := new([3]*big.Int), [3]*big.Int{big.NewInt(1545304298), big.NewInt(6), temp} ret2, ret2Exp := new(common.Address), common.HexToAddress("ab1257528b3782fb40d7ed5f72e624b744dffb2f") @@ -949,10 +950,13 @@ func TestPackAndUnpackIncompatibleNumber(t *testing.T) { t.Parallel() var encodeABI Arguments + uint256Ty, err := NewType("uint256", "", nil) + if err != nil { panic(err) } + encodeABI = Arguments{ {Type: uint256Ty}, } @@ -961,6 +965,7 @@ func TestPackAndUnpackIncompatibleNumber(t *testing.T) { if !ok { panic("bug") } + maxU64Plus1 := new(big.Int).Add(maxU64, big.NewInt(1)) cases := []struct { decodeType string @@ -1083,25 +1088,32 @@ func TestPackAndUnpackIncompatibleNumber(t *testing.T) { expectValue: int64(math.MaxInt64), }, } + for i, testCase := range cases { packed, err := encodeABI.Pack(testCase.inputValue) if err != nil { panic(err) } + ty, err := NewType(testCase.decodeType, "", nil) + if err != nil { panic(err) } + decodeABI := Arguments{ {Type: ty}, } decoded, err := decodeABI.Unpack(packed) + if err != testCase.err { t.Fatalf("Expected error %v, actual error %v. case %d", testCase.err, err, i) } + if err != nil { continue } + if !reflect.DeepEqual(decoded[0], testCase.expectValue) { t.Fatalf("Expected value %v, actual value %v", testCase.expectValue, decoded[0]) } diff --git a/accounts/abi/utils.go b/accounts/abi/utils.go index b1537ca58d..e01f371d50 100644 --- a/accounts/abi/utils.go +++ b/accounts/abi/utils.go @@ -32,9 +32,11 @@ import "fmt" func ResolveNameConflict(rawName string, used func(string) bool) string { name := rawName ok := used(name) + for idx := 0; ok; idx++ { name = fmt.Sprintf("%s%d", rawName, idx) ok = used(name) } + return name } diff --git a/accounts/keystore/watch.go b/accounts/keystore/watch.go index 20071374f4..f7bcfae2cc 100644 --- a/accounts/keystore/watch.go +++ b/accounts/keystore/watch.go @@ -22,7 +22,6 @@ package keystore import ( "time" - "github.com/ethereum/go-ethereum/log" "github.com/fsnotify/fsnotify" "github.com/ethereum/go-ethereum/log" diff --git a/accounts/usbwallet/trezor.go b/accounts/usbwallet/trezor.go index 9644dc4e02..7b4889ba7a 100644 --- a/accounts/usbwallet/trezor.go +++ b/accounts/usbwallet/trezor.go @@ -196,9 +196,11 @@ func (w *trezorDriver) trezorDerive(derivationPath []uint32) (common.Address, er if _, err := w.trezorExchange(&trezor.EthereumGetAddress{AddressN: derivationPath}, address); err != nil { return common.Address{}, err } + if addr := address.GetAddressBin(); len(addr) > 0 { // Older firmwares use binary formats return common.BytesToAddress(addr), nil } + if addr := address.GetAddressHex(); len(addr) > 0 { // Newer firmwares use hexadecimal formats return common.HexToAddress(addr), nil } diff --git a/cmd/abigen/main.go b/cmd/abigen/main.go index 80d2addfe9..a98bfd2c7f 100644 --- a/cmd/abigen/main.go +++ b/cmd/abigen/main.go @@ -24,6 +24,8 @@ import ( "regexp" "strings" + "github.com/urfave/cli/v2" + "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/cmd/utils" "github.com/ethereum/go-ethereum/common/compiler" diff --git a/cmd/checkpoint-admin/exec.go b/cmd/checkpoint-admin/exec.go index 7d17490005..dae4301431 100644 --- a/cmd/checkpoint-admin/exec.go +++ b/cmd/checkpoint-admin/exec.go @@ -25,6 +25,8 @@ import ( "strings" "time" + "github.com/urfave/cli/v2" + "github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/cmd/utils" "github.com/ethereum/go-ethereum/common" diff --git a/cmd/checkpoint-admin/main.go b/cmd/checkpoint-admin/main.go index 766c9be8ea..46fd21f163 100644 --- a/cmd/checkpoint-admin/main.go +++ b/cmd/checkpoint-admin/main.go @@ -22,6 +22,8 @@ import ( "fmt" "os" + "github.com/urfave/cli/v2" + "github.com/ethereum/go-ethereum/common/fdlimit" "github.com/ethereum/go-ethereum/internal/flags" "github.com/ethereum/go-ethereum/log" diff --git a/cmd/checkpoint-admin/status.go b/cmd/checkpoint-admin/status.go index d0231b4661..d723531a4b 100644 --- a/cmd/checkpoint-admin/status.go +++ b/cmd/checkpoint-admin/status.go @@ -19,7 +19,6 @@ package main import ( "fmt" - "github.com/ethereum/go-ethereum/common" "github.com/urfave/cli/v2" "github.com/ethereum/go-ethereum/common" diff --git a/cmd/devp2p/discv4cmd.go b/cmd/devp2p/discv4cmd.go index 35cc1cbbd3..5a25e46b5c 100644 --- a/cmd/devp2p/discv4cmd.go +++ b/cmd/devp2p/discv4cmd.go @@ -23,6 +23,8 @@ import ( "strings" "time" + "github.com/urfave/cli/v2" + "github.com/ethereum/go-ethereum/cmd/devp2p/internal/v4test" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" diff --git a/cmd/devp2p/discv5cmd.go b/cmd/devp2p/discv5cmd.go index f706563e30..08792a063a 100644 --- a/cmd/devp2p/discv5cmd.go +++ b/cmd/devp2p/discv5cmd.go @@ -20,6 +20,8 @@ import ( "fmt" "time" + "github.com/urfave/cli/v2" + "github.com/ethereum/go-ethereum/cmd/devp2p/internal/v5test" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/internal/flags" diff --git a/cmd/devp2p/dns_cloudflare.go b/cmd/devp2p/dns_cloudflare.go index aeb861eece..53e37b1682 100644 --- a/cmd/devp2p/dns_cloudflare.go +++ b/cmd/devp2p/dns_cloudflare.go @@ -21,6 +21,8 @@ import ( "fmt" "strings" + "github.com/urfave/cli/v2" + "github.com/cloudflare/cloudflare-go" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/p2p/dnsdisc" diff --git a/cmd/devp2p/dns_route53.go b/cmd/devp2p/dns_route53.go index fbab05e709..42b924ddfe 100644 --- a/cmd/devp2p/dns_route53.go +++ b/cmd/devp2p/dns_route53.go @@ -25,6 +25,8 @@ import ( "strings" "time" + "github.com/urfave/cli/v2" + "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/credentials" diff --git a/cmd/devp2p/dnscmd.go b/cmd/devp2p/dnscmd.go index 43c3c63601..5caa1d0930 100644 --- a/cmd/devp2p/dnscmd.go +++ b/cmd/devp2p/dnscmd.go @@ -24,6 +24,8 @@ import ( "path/filepath" "time" + "github.com/urfave/cli/v2" + "github.com/ethereum/go-ethereum/accounts/keystore" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/console/prompt" diff --git a/cmd/devp2p/enrcmd.go b/cmd/devp2p/enrcmd.go index cb333a00ea..a5b5dc3e57 100644 --- a/cmd/devp2p/enrcmd.go +++ b/cmd/devp2p/enrcmd.go @@ -27,6 +27,8 @@ import ( "strconv" "strings" + "github.com/urfave/cli/v2" + "github.com/ethereum/go-ethereum/p2p/enode" "github.com/ethereum/go-ethereum/p2p/enr" "github.com/ethereum/go-ethereum/rlp" diff --git a/cmd/devp2p/internal/ethtest/chain.go b/cmd/devp2p/internal/ethtest/chain.go index dec46c572d..65adcfe050 100644 --- a/cmd/devp2p/internal/ethtest/chain.go +++ b/cmd/devp2p/internal/ethtest/chain.go @@ -96,6 +96,7 @@ func (c *Chain) Head() *types.Block { return c.blocks[c.Len()-1] } +// nolint:typecheck func (c *Chain) GetHeaders(req *GetBlockHeaders) ([]*types.Header, error) { if req.Amount < 1 { return nil, fmt.Errorf("no block headers requested") diff --git a/cmd/devp2p/internal/ethtest/helpers.go b/cmd/devp2p/internal/ethtest/helpers.go index ef0e7e0731..510bd606cf 100644 --- a/cmd/devp2p/internal/ethtest/helpers.go +++ b/cmd/devp2p/internal/ethtest/helpers.go @@ -571,6 +571,7 @@ func (s *Suite) maliciousStatus(conn *Conn) error { } } +// nolint:typecheck func (s *Suite) hashAnnounce() error { // create connections sendConn, recvConn, err := s.createSendAndRecvConns() diff --git a/cmd/devp2p/internal/ethtest/suite.go b/cmd/devp2p/internal/ethtest/suite.go index c76b78e155..9691411d05 100644 --- a/cmd/devp2p/internal/ethtest/suite.go +++ b/cmd/devp2p/internal/ethtest/suite.go @@ -136,6 +136,7 @@ func (s *Suite) TestGetBlockHeaders(t *utesting.T) { // TestSimultaneousRequests sends two simultaneous `GetBlockHeader` requests from // the same connection with different request IDs and checks to make sure the node // responds with the correct headers per request. +// nolint:typecheck func (s *Suite) TestSimultaneousRequests(t *utesting.T) { // create a connection conn, err := s.dial() @@ -214,6 +215,7 @@ func (s *Suite) TestSimultaneousRequests(t *utesting.T) { // TestSameRequestID sends two requests with the same request ID to a // single node. +// nolint:typecheck func (s *Suite) TestSameRequestID(t *utesting.T) { conn, err := s.dial() if err != nil { @@ -462,6 +464,7 @@ func (s *Suite) TestMaliciousTx(t *utesting.T) { // TestLargeTxRequest tests whether a node can fulfill a large GetPooledTransactions // request. +// nolint:typecheck func (s *Suite) TestLargeTxRequest(t *utesting.T) { // send the next block to ensure the node is no longer syncing and // is able to accept txs diff --git a/cmd/devp2p/internal/ethtest/transaction.go b/cmd/devp2p/internal/ethtest/transaction.go index 56c33e16f5..c99f91c5d8 100644 --- a/cmd/devp2p/internal/ethtest/transaction.go +++ b/cmd/devp2p/internal/ethtest/transaction.go @@ -55,6 +55,7 @@ func (s *Suite) sendSuccessfulTxs(t *utesting.T) error { return nil } +// nolint:typecheck func sendSuccessfulTx(s *Suite, tx *types.Transaction, prevTx *types.Transaction) error { sendConn, recvConn, err := s.createSendAndRecvConns() if err != nil { @@ -269,6 +270,7 @@ func sendMultipleSuccessfulTxs(t *utesting.T, s *Suite, txs []*types.Transaction // checkMaliciousTxPropagation checks whether the given malicious transactions were // propagated by the node. +// nolint:typecheck func checkMaliciousTxPropagation(s *Suite, txs []*types.Transaction, conn *Conn) error { switch msg := conn.readAndServe(s.chain, time.Second*8).(type) { case *Transactions: diff --git a/cmd/devp2p/internal/v5test/framework.go b/cmd/devp2p/internal/v5test/framework.go index 10856a50bc..065bb8ae36 100644 --- a/cmd/devp2p/internal/v5test/framework.go +++ b/cmd/devp2p/internal/v5test/framework.go @@ -176,6 +176,7 @@ func (tc *conn) findnode(c net.PacketConn, dists []uint) ([]*enode.Node, error) if resp.RespCount == 0 || resp.RespCount > 6 { return nil, fmt.Errorf("invalid NODES response count %d (not in (0,7))", resp.RespCount) } + total = resp.RespCount n = int(total) - 1 first = false diff --git a/cmd/devp2p/keycmd.go b/cmd/devp2p/keycmd.go index 3ec28581b2..79d189c5a4 100644 --- a/cmd/devp2p/keycmd.go +++ b/cmd/devp2p/keycmd.go @@ -20,6 +20,8 @@ import ( "fmt" "net" + "github.com/urfave/cli/v2" + "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/p2p/enode" "github.com/ethereum/go-ethereum/p2p/enr" diff --git a/cmd/devp2p/main.go b/cmd/devp2p/main.go index c6e75f172a..58411f0f0b 100644 --- a/cmd/devp2p/main.go +++ b/cmd/devp2p/main.go @@ -20,6 +20,8 @@ import ( "fmt" "os" + "github.com/urfave/cli/v2" + "github.com/ethereum/go-ethereum/internal/debug" "github.com/ethereum/go-ethereum/internal/flags" "github.com/ethereum/go-ethereum/p2p/enode" diff --git a/cmd/devp2p/nodesetcmd.go b/cmd/devp2p/nodesetcmd.go index aaffd56b2c..5d6977bcf0 100644 --- a/cmd/devp2p/nodesetcmd.go +++ b/cmd/devp2p/nodesetcmd.go @@ -25,6 +25,8 @@ import ( "strings" "time" + "github.com/urfave/cli/v2" + "github.com/ethereum/go-ethereum/core/forkid" "github.com/ethereum/go-ethereum/p2p/enr" "github.com/ethereum/go-ethereum/params" diff --git a/cmd/devp2p/rlpxcmd.go b/cmd/devp2p/rlpxcmd.go index a36171e738..81fa04c2a4 100644 --- a/cmd/devp2p/rlpxcmd.go +++ b/cmd/devp2p/rlpxcmd.go @@ -20,6 +20,8 @@ import ( "fmt" "net" + "github.com/urfave/cli/v2" + "github.com/ethereum/go-ethereum/cmd/devp2p/internal/ethtest" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/p2p" diff --git a/cmd/devp2p/runtest.go b/cmd/devp2p/runtest.go index bf19d62b14..b1374678aa 100644 --- a/cmd/devp2p/runtest.go +++ b/cmd/devp2p/runtest.go @@ -19,6 +19,8 @@ package main import ( "os" + "github.com/urfave/cli/v2" + "github.com/ethereum/go-ethereum/cmd/devp2p/internal/v4test" "github.com/ethereum/go-ethereum/internal/utesting" "github.com/ethereum/go-ethereum/log" diff --git a/cmd/ethkey/changepassword.go b/cmd/ethkey/changepassword.go index 8eb1b64a0a..8928560d42 100644 --- a/cmd/ethkey/changepassword.go +++ b/cmd/ethkey/changepassword.go @@ -21,6 +21,8 @@ import ( "os" "strings" + "github.com/urfave/cli/v2" + "github.com/ethereum/go-ethereum/accounts/keystore" "github.com/ethereum/go-ethereum/cmd/utils" ) diff --git a/cmd/ethkey/inspect.go b/cmd/ethkey/inspect.go index 0164085e22..7aacdf9298 100644 --- a/cmd/ethkey/inspect.go +++ b/cmd/ethkey/inspect.go @@ -21,6 +21,8 @@ import ( "fmt" "os" + "github.com/urfave/cli/v2" + "github.com/ethereum/go-ethereum/accounts/keystore" "github.com/ethereum/go-ethereum/cmd/utils" "github.com/ethereum/go-ethereum/crypto" diff --git a/cmd/ethkey/main.go b/cmd/ethkey/main.go index 1acc13af9e..50d611b44c 100644 --- a/cmd/ethkey/main.go +++ b/cmd/ethkey/main.go @@ -20,7 +20,6 @@ import ( "fmt" "os" - "github.com/ethereum/go-ethereum/internal/flags" "github.com/urfave/cli/v2" "github.com/ethereum/go-ethereum/internal/flags" diff --git a/cmd/ethkey/message.go b/cmd/ethkey/message.go index 1ca610ebb9..387fc3873a 100644 --- a/cmd/ethkey/message.go +++ b/cmd/ethkey/message.go @@ -21,6 +21,8 @@ import ( "fmt" "os" + "github.com/urfave/cli/v2" + "github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/accounts/keystore" "github.com/ethereum/go-ethereum/cmd/utils" diff --git a/cmd/ethkey/utils.go b/cmd/ethkey/utils.go index 94e55c52a0..aef0403e9a 100644 --- a/cmd/ethkey/utils.go +++ b/cmd/ethkey/utils.go @@ -22,7 +22,6 @@ import ( "os" "strings" - "github.com/ethereum/go-ethereum/cmd/utils" "github.com/urfave/cli/v2" "github.com/ethereum/go-ethereum/cmd/utils" diff --git a/cmd/evm/disasm.go b/cmd/evm/disasm.go index d4137400df..92897a1637 100644 --- a/cmd/evm/disasm.go +++ b/cmd/evm/disasm.go @@ -22,7 +22,6 @@ import ( "os" "strings" - "github.com/ethereum/go-ethereum/core/asm" "github.com/urfave/cli/v2" "github.com/ethereum/go-ethereum/core/asm" diff --git a/cmd/evm/internal/t8ntool/flags.go b/cmd/evm/internal/t8ntool/flags.go index b53e294f1a..29b2fe765d 100644 --- a/cmd/evm/internal/t8ntool/flags.go +++ b/cmd/evm/internal/t8ntool/flags.go @@ -20,6 +20,8 @@ import ( "fmt" "strings" + "github.com/urfave/cli/v2" + "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/tests" ) diff --git a/cmd/evm/main.go b/cmd/evm/main.go index 7655babdcf..ad40064d51 100644 --- a/cmd/evm/main.go +++ b/cmd/evm/main.go @@ -22,6 +22,8 @@ import ( "math/big" "os" + "github.com/urfave/cli/v2" + "github.com/ethereum/go-ethereum/cmd/evm/internal/t8ntool" "github.com/ethereum/go-ethereum/internal/flags" ) diff --git a/cmd/evm/runner.go b/cmd/evm/runner.go index 16fb8f1eae..153a3471eb 100644 --- a/cmd/evm/runner.go +++ b/cmd/evm/runner.go @@ -28,6 +28,8 @@ import ( "testing" "time" + "github.com/urfave/cli/v2" + "github.com/ethereum/go-ethereum/cmd/evm/internal/compiler" "github.com/ethereum/go-ethereum/cmd/utils" "github.com/ethereum/go-ethereum/common" diff --git a/cmd/evm/staterunner.go b/cmd/evm/staterunner.go index 42cdf7279d..a6b0d83e36 100644 --- a/cmd/evm/staterunner.go +++ b/cmd/evm/staterunner.go @@ -22,6 +22,8 @@ import ( "fmt" "os" + "github.com/urfave/cli/v2" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/vm" diff --git a/cmd/geth/accountcmd.go b/cmd/geth/accountcmd.go index 9bd437d7b4..5ec0a4d736 100644 --- a/cmd/geth/accountcmd.go +++ b/cmd/geth/accountcmd.go @@ -20,6 +20,8 @@ import ( "fmt" "os" + "github.com/urfave/cli/v2" + "github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/accounts/keystore" "github.com/ethereum/go-ethereum/cmd/utils" diff --git a/cmd/geth/chaincmd.go b/cmd/geth/chaincmd.go index 16472617e4..d3caaa82ce 100644 --- a/cmd/geth/chaincmd.go +++ b/cmd/geth/chaincmd.go @@ -26,6 +26,8 @@ import ( "sync/atomic" "time" + "github.com/urfave/cli/v2" + "github.com/ethereum/go-ethereum/cmd/utils" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" diff --git a/cmd/geth/consolecmd.go b/cmd/geth/consolecmd.go index e44b75ec50..49ea43e955 100644 --- a/cmd/geth/consolecmd.go +++ b/cmd/geth/consolecmd.go @@ -22,9 +22,12 @@ import ( "path/filepath" "strings" + "github.com/urfave/cli/v2" + "github.com/ethereum/go-ethereum/cmd/utils" "github.com/ethereum/go-ethereum/console" "github.com/ethereum/go-ethereum/internal/flags" + "github.com/ethereum/go-ethereum/node" ) var ( diff --git a/cmd/geth/misccmd.go b/cmd/geth/misccmd.go index 130619c4be..5e7aa7bb12 100644 --- a/cmd/geth/misccmd.go +++ b/cmd/geth/misccmd.go @@ -23,6 +23,8 @@ import ( "strconv" "strings" + "github.com/urfave/cli/v2" + "github.com/ethereum/go-ethereum/cmd/utils" "github.com/ethereum/go-ethereum/consensus/ethash" "github.com/ethereum/go-ethereum/internal/version" diff --git a/cmd/geth/snapshot.go b/cmd/geth/snapshot.go index 2262a9b0cb..61bd79d8d7 100644 --- a/cmd/geth/snapshot.go +++ b/cmd/geth/snapshot.go @@ -23,6 +23,8 @@ import ( "os" "time" + "github.com/urfave/cli/v2" + "github.com/ethereum/go-ethereum/cmd/utils" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/rawdb" diff --git a/cmd/geth/version_check.go b/cmd/geth/version_check.go index e459d78c65..96ccb0eebc 100644 --- a/cmd/geth/version_check.go +++ b/cmd/geth/version_check.go @@ -26,7 +26,6 @@ import ( "regexp" "strings" - "github.com/ethereum/go-ethereum/log" "github.com/jedisct1/go-minisign" "github.com/urfave/cli/v2" diff --git a/cmd/p2psim/main.go b/cmd/p2psim/main.go index 2424509ca3..e1cdd857c8 100644 --- a/cmd/p2psim/main.go +++ b/cmd/p2psim/main.go @@ -44,6 +44,8 @@ import ( "strings" "text/tabwriter" + "github.com/urfave/cli/v2" + "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/internal/flags" "github.com/ethereum/go-ethereum/p2p" diff --git a/cmd/rlpdump/main.go b/cmd/rlpdump/main.go index 70337749ae..d57e1d1878 100644 --- a/cmd/rlpdump/main.go +++ b/cmd/rlpdump/main.go @@ -83,6 +83,7 @@ func main() { if err != nil { die(err) } + fmt.Printf("%#x\n", data) return } else { diff --git a/cmd/rlpdump/rlpdump_test.go b/cmd/rlpdump/rlpdump_test.go index a9ab57fdb8..534bd87ece 100644 --- a/cmd/rlpdump/rlpdump_test.go +++ b/cmd/rlpdump/rlpdump_test.go @@ -43,6 +43,7 @@ func TestRoundtrip(t *testing.T) { t.Errorf("test %d: error %v", i, err) continue } + have := fmt.Sprintf("%#x", rlpBytes) if have != want { t.Errorf("test %d: have\n%v\nwant:\n%v\n", i, have, want) diff --git a/cmd/utils/bor_flags.go b/cmd/utils/bor_flags.go index 72b66984dd..83c94d82f7 100644 --- a/cmd/utils/bor_flags.go +++ b/cmd/utils/bor_flags.go @@ -4,6 +4,9 @@ import ( "encoding/json" "os" + "github.com/ethereum/go-ethereum/eth/ethconfig" + "github.com/urfave/cli/v2" + "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/eth" "github.com/ethereum/go-ethereum/log" diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 67b246c03e..01d062201b 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -34,6 +34,10 @@ import ( "strings" "time" + pcsclite "github.com/gballet/go-libpcsclite" + gopsutil "github.com/shirou/gopsutil/mem" + "github.com/urfave/cli/v2" + "github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/accounts/keystore" "github.com/ethereum/go-ethereum/common" @@ -74,6 +78,7 @@ import ( "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rpc" + "github.com/ethereum/go-ethereum/trie" ) // These are all the command line flags we support. diff --git a/common/prque/lazyqueue.go b/common/prque/lazyqueue.go index ee0bd7e47f..51ab3c1bfd 100644 --- a/common/prque/lazyqueue.go +++ b/common/prque/lazyqueue.go @@ -20,7 +20,6 @@ import ( "container/heap" "time" - "github.com/ethereum/go-ethereum/common/mclock" "golang.org/x/exp/constraints" "github.com/ethereum/go-ethereum/common/mclock" diff --git a/common/types_test.go b/common/types_test.go index 302d9d6d3f..1e29f1998d 100644 --- a/common/types_test.go +++ b/common/types_test.go @@ -179,6 +179,7 @@ func TestMixedcaseAddressMarshal(t *testing.T) { if err != nil { t.Fatal(err) } + _ = json.Unmarshal(blob, &output) if output != input { diff --git a/consensus/beacon/consensus.go b/consensus/beacon/consensus.go index 5ccf7d5073..97d0b3627a 100644 --- a/consensus/beacon/consensus.go +++ b/consensus/beacon/consensus.go @@ -102,6 +102,7 @@ func errOut(n int, err error) chan error { for i := 0; i < n; i++ { errs <- err } + return errs } @@ -116,7 +117,9 @@ func (beacon *Beacon) splitHeaders(chain consensus.ChainHeaderReader, headers [] if ttd == nil { return headers, nil, nil } + ptd := chain.GetTd(headers[0].ParentHash, headers[0].Number.Uint64()-1) + if ptd == nil { return nil, nil, consensus.ErrUnknownAncestor } @@ -130,19 +133,23 @@ func (beacon *Beacon) splitHeaders(chain consensus.ChainHeaderReader, headers [] td = new(big.Int).Set(ptd) tdPassed bool ) + for i, header := range headers { if tdPassed { preHeaders = headers[:i] postHeaders = headers[i:] break } + td = td.Add(td, header.Difficulty) + if td.Cmp(ttd) >= 0 { // This is the last PoW header, it still belongs to // the preHeaders, so we cannot split+break yet. tdPassed = true } } + return preHeaders, postHeaders, nil } @@ -155,6 +162,7 @@ func (beacon *Beacon) VerifyHeaders(chain consensus.ChainHeaderReader, headers [ if err != nil { return make(chan struct{}), errOut(len(headers), err) } + if len(postHeaders) == 0 { return beacon.ethone.VerifyHeaders(chain, headers, seals) } @@ -267,6 +275,7 @@ func (beacon *Beacon) verifyHeader(chain consensus.ChainHeaderReader, header, pa if shanghai && header.WithdrawalsHash == nil { return errors.New("missing withdrawalsHash") } + if !shanghai && header.WithdrawalsHash != nil { return fmt.Errorf("invalid withdrawalsHash: have %x, expected nil", header.WithdrawalsHash) } @@ -275,9 +284,11 @@ func (beacon *Beacon) verifyHeader(chain consensus.ChainHeaderReader, header, pa if cancun && header.ExcessDataGas == nil { return errors.New("missing excessDataGas") } + if !cancun && header.ExcessDataGas != nil { return fmt.Errorf("invalid excessDataGas: have %d, expected nil", header.ExcessDataGas) } + return nil } @@ -455,6 +466,7 @@ func IsTTDReached(chain consensus.ChainHeaderReader, parentHash common.Hash, par if chain.Config().TerminalTotalDifficulty == nil { return false, nil } + td := chain.GetTd(parentHash, parentNumber) if td == nil { return false, consensus.ErrUnknownAncestor diff --git a/consensus/ethash/consensus.go b/consensus/ethash/consensus.go index 81aba374ba..f58c0f9287 100644 --- a/consensus/ethash/consensus.go +++ b/consensus/ethash/consensus.go @@ -37,7 +37,6 @@ import ( "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/trie" - "golang.org/x/crypto/sha3" ) // Ethash proof-of-work protocol constants. @@ -317,6 +316,7 @@ func (ethash *Ethash) verifyHeader(chain consensus.ChainHeaderReader, header, pa if chain.Config().IsShanghai(header.Time) { return fmt.Errorf("ethash does not support shanghai fork") } + if chain.Config().IsCancun(header.Time) { return fmt.Errorf("ethash does not support cancun fork") } @@ -646,6 +646,7 @@ func (ethash *Ethash) SealHash(header *types.Header) (hash common.Hash) { if header.BaseFee != nil { enc = append(enc, header.BaseFee) } + if header.WithdrawalsHash != nil { panic("withdrawal hash set on ethash") } diff --git a/consensus/ethash/consensus_test.go b/consensus/ethash/consensus_test.go index e3793cd1b0..2d8d3f6e55 100644 --- a/consensus/ethash/consensus_test.go +++ b/consensus/ethash/consensus_test.go @@ -103,6 +103,7 @@ func TestDifficultyCalculators(t *testing.T) { for i := 0; i < 5000; i++ { // 1 to 300 seconds diff var timeDelta = uint64(1 + rand.Uint32()%3000) + diffBig := new(big.Int).SetBytes(randSlice(2, 10)) if diffBig.Cmp(params.MinimumDifficulty) < 0 { diffBig.Set(params.MinimumDifficulty) diff --git a/consensus/ethash/ethash.go b/consensus/ethash/ethash.go index ac0115b42b..0ad235bee1 100644 --- a/consensus/ethash/ethash.go +++ b/consensus/ethash/ethash.go @@ -193,6 +193,7 @@ func newlru[T cacheOrDataset](maxItems int, newLru func(epoch uint64) T) *lru[T] default: panic("unknown type") } + return &lru[T]{ what: what, new: newLru, @@ -610,6 +611,7 @@ func (ethash *Ethash) dataset(block uint64, async bool) *dataset { if async && !current.generated() { go func() { current.generate(ethash.config.DatasetDir, ethash.config.DatasetsOnDisk, ethash.config.DatasetsLockMmap, ethash.config.PowMode == ModeTest) + if future != nil { future.generate(ethash.config.DatasetDir, ethash.config.DatasetsOnDisk, ethash.config.DatasetsLockMmap, ethash.config.PowMode == ModeTest) } diff --git a/consensus/misc/eip4844.go b/consensus/misc/eip4844.go index 66ca9bd26d..ea4e534214 100644 --- a/consensus/misc/eip4844.go +++ b/consensus/misc/eip4844.go @@ -33,6 +33,7 @@ func CalcBlobFee(excessDataGas *big.Int) *big.Int { if excessDataGas == nil { return big.NewInt(params.BlobTxMinDataGasprice) } + return fakeExponential(minDataGasPrice, excessDataGas, dataGaspriceUpdateFraction) } @@ -43,6 +44,7 @@ func fakeExponential(factor, numerator, denominator *big.Int) *big.Int { output = new(big.Int) accum = new(big.Int).Mul(factor, denominator) ) + for i := 1; accum.Sign() > 0; i++ { output.Add(output, accum) @@ -50,5 +52,6 @@ func fakeExponential(factor, numerator, denominator *big.Int) *big.Int { accum.Div(accum, denominator) accum.Div(accum, big.NewInt(int64(i))) } + return output.Div(output, denominator) } diff --git a/consensus/misc/eip4844_test.go b/consensus/misc/eip4844_test.go index 4f23fae3ab..ee365ed0d5 100644 --- a/consensus/misc/eip4844_test.go +++ b/consensus/misc/eip4844_test.go @@ -37,9 +37,11 @@ func TestCalcBlobFee(t *testing.T) { {10 * 1024 * 1024, 111}, } have := CalcBlobFee(nil) + if have.Int64() != params.BlobTxMinDataGasprice { t.Errorf("nil test: blobfee mismatch: have %v, want %v", have, params.BlobTxMinDataGasprice) } + for i, tt := range tests { have := CalcBlobFee(big.NewInt(tt.excessDataGas)) if have.Int64() != tt.blobfee { @@ -78,10 +80,13 @@ func TestFakeExponential(t *testing.T) { f, n, d := big.NewInt(tt.factor), big.NewInt(tt.numerator), big.NewInt(tt.denominator) original := fmt.Sprintf("%d %d %d", f, n, d) have := fakeExponential(f, n, d) + if have.Int64() != tt.want { t.Errorf("test %d: fake exponential mismatch: have %v want %v", i, have, tt.want) } + later := fmt.Sprintf("%d %d %d", f, n, d) + if original != later { t.Errorf("test %d: fake exponential modified arguments: have\n%v\nwant\n%v", i, later, original) } diff --git a/core/asm/compiler.go b/core/asm/compiler.go index ce2d23abd0..050dc6233f 100644 --- a/core/asm/compiler.go +++ b/core/asm/compiler.go @@ -114,6 +114,7 @@ func (c *Compiler) Compile() (string, []error) { bin.WriteString(fmt.Sprintf("%x", v)) } } + return bin.String(), errors } diff --git a/core/blockchain.go b/core/blockchain.go index 87b2fdb2fc..e4ac7d7a46 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -57,6 +57,7 @@ import ( "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/metrics" "github.com/ethereum/go-ethereum/params" + "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/trie" ) diff --git a/core/bloombits/generator_test.go b/core/bloombits/generator_test.go index ac1aee0b25..4739d4d5c1 100644 --- a/core/bloombits/generator_test.go +++ b/core/bloombits/generator_test.go @@ -79,7 +79,7 @@ func BenchmarkGenerator(b *testing.B) { } }) for i := 0; i < types.BloomBitLength; i++ { - crand.Read(input[i][:]) + _, _ = crand.Read(input[i][:]) } b.Run("random", func(b *testing.B) { b.ReportAllocs() diff --git a/core/bloombits/matcher_test.go b/core/bloombits/matcher_test.go index 36764c3f17..c0e3a76a1c 100644 --- a/core/bloombits/matcher_test.go +++ b/core/bloombits/matcher_test.go @@ -211,6 +211,7 @@ func testMatcher(t *testing.T, filter [][]bloomIndexes, start, blocks uint64, in if retrievals != 0 && requested.Load() != retrievals { t.Errorf("filter = %v blocks = %v intermittent = %v: request count mismatch, have #%v, want #%v", filter, blocks, intermittent, requested.Load(), retrievals) } + return requested.Load() } @@ -238,6 +239,7 @@ func startRetrievers(session *MatcherSession, quit chan struct{}, retrievals *at for i, section := range task.Sections { if rand.Int()%4 != 0 { // Handle occasional missing deliveries task.Bitsets[i] = generateBitset(task.Bit, section) + retrievals.Add(1) } } diff --git a/core/forkchoice_test.go b/core/forkchoice_test.go index 03e684ac35..f05893ca92 100644 --- a/core/forkchoice_test.go +++ b/core/forkchoice_test.go @@ -9,6 +9,7 @@ import ( "github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/params" + "github.com/ethereum/go-ethereum/trie" ) // chainValidatorFake is a mock for the chain validator service diff --git a/core/forkid/forkid.go b/core/forkid/forkid.go index f536019dac..4d48299c7f 100644 --- a/core/forkid/forkid.go +++ b/core/forkid/forkid.go @@ -85,16 +85,20 @@ func NewID(config *params.ChainConfig, genesis common.Hash, head, time uint64) I hash = checksumUpdate(hash, fork) continue } + return ID{Hash: checksumToBytes(hash), Next: fork} } + for _, fork := range forksByTime { if fork <= time { // Fork already passed, checksum the previous hash and fork timestamp hash = checksumUpdate(hash, fork) continue } + return ID{Hash: checksumToBytes(hash), Next: fork} } + return ID{Hash: checksumToBytes(hash), Next: 0} } @@ -132,6 +136,7 @@ func NewStaticFilter(config *params.ChainConfig, genesis common.Hash) Filter { // newFilter is the internal version of NewFilter, taking closures as its arguments // instead of a chain. The reason is to allow testing it without having to simulate // an entire blockchain. +// nolint:gocognit func newFilter(config *params.ChainConfig, genesis common.Hash, headfn func() (uint64, uint64)) Filter { // Calculate the all the valid fork hash and fork next combos var ( @@ -148,6 +153,7 @@ func newFilter(config *params.ChainConfig, genesis common.Hash, headfn func() (u // Add two sentries to simplify the fork checks and don't require special // casing the last one. forks = append(forks, math.MaxUint64) // Last fork will never be passed + if len(forksByTime) == 0 { // In purely block based forks, avoid the sentry spilling into timestapt territory forksByBlock = append(forksByBlock, math.MaxUint64) // Last fork will never be passed @@ -245,6 +251,7 @@ func gatherForks(config *params.ChainConfig) ([]uint64, []uint64) { kind := reflect.TypeOf(params.ChainConfig{}) conf := reflect.ValueOf(config).Elem() x := uint64(0) + var ( forksByBlock []uint64 forksByTime []uint64 @@ -264,6 +271,7 @@ func gatherForks(config *params.ChainConfig) ([]uint64, []uint64) { forksByTime = append(forksByTime, *rule) } } + if field.Type == reflect.TypeOf(new(big.Int)) { if rule := conf.Field(i).Interface().(*big.Int); rule != nil { forksByBlock = append(forksByBlock, rule.Uint64()) @@ -280,6 +288,7 @@ func gatherForks(config *params.ChainConfig) ([]uint64, []uint64) { i-- } } + for i := 1; i < len(forksByTime); i++ { if forksByTime[i] == forksByTime[i-1] { forksByTime = append(forksByTime[:i], forksByTime[i+1:]...) @@ -290,8 +299,10 @@ func gatherForks(config *params.ChainConfig) ([]uint64, []uint64) { if len(forksByBlock) > 0 && forksByBlock[0] == 0 { forksByBlock = forksByBlock[1:] } + if len(forksByTime) > 0 && forksByTime[0] == 0 { forksByTime = forksByTime[1:] } + return forksByBlock, forksByTime } diff --git a/core/rawdb/freezer.go b/core/rawdb/freezer.go index 6e763c2b51..7cc3477ec9 100644 --- a/core/rawdb/freezer.go +++ b/core/rawdb/freezer.go @@ -26,6 +26,8 @@ import ( "sync/atomic" "time" + "github.com/gofrs/flock" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/log" diff --git a/core/state/iterator_test.go b/core/state/iterator_test.go index ab06cb422f..5327f8ee8b 100644 --- a/core/state/iterator_test.go +++ b/core/state/iterator_test.go @@ -45,12 +45,15 @@ func TestNodeIteratorCoverage(t *testing.T) { seenNodes = make(map[common.Hash]struct{}) seenCodes = make(map[common.Hash]struct{}) ) + it := db.NewIterator(nil, nil) + for it.Next() { ok, hash := isTrieNode(sdb.TrieDB().Scheme(), it.Key(), it.Value()) if !ok { continue } + seenNodes[hash] = struct{}{} } it.Release() @@ -62,19 +65,22 @@ func TestNodeIteratorCoverage(t *testing.T) { if !ok { continue } + if _, ok := hashes[common.BytesToHash(hash)]; !ok { t.Errorf("state entry not reported %x", it.Key()) } + seenCodes[common.BytesToHash(hash)] = struct{}{} } it.Release() - // Cross check the iterated hashes and the database/nodepool content + // Cross-check the iterated hashes and the database/nodepool content for hash := range hashes { _, ok := seenNodes[hash] if !ok { _, ok = seenCodes[hash] } + if !ok { t.Errorf("failed to retrieve reported node %x", hash) } @@ -89,5 +95,6 @@ func isTrieNode(scheme string, key, val []byte) (bool, common.Hash) { return true, common.BytesToHash(key) } } + return false, common.Hash{} } diff --git a/core/state/statedb.go b/core/state/statedb.go index eee5e621da..8fe81eeaff 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -833,6 +833,7 @@ func (s *StateDB) SetTransientState(addr common.Address, key, value common.Hash) if prev == value { return } + s.journal.append(transientStorageChange{ account: &addr, key: key, @@ -1044,10 +1045,13 @@ func (s *StateDB) ForEachStorage(addr common.Address, cb func(key, value common. if so == nil { return nil } + tr, err := so.getTrie(s.db) + if err != nil { return err } + it := trie.NewIterator(tr.NodeIterator(nil)) for it.Next() { @@ -1337,6 +1341,7 @@ func (s *StateDB) clearJournalAndRefund() { s.journal = newJournal() s.refund = 0 } + s.validRevisions = s.validRevisions[:0] // Snapshots can be created without journal entries } @@ -1375,6 +1380,7 @@ func (s *StateDB) Commit(deleteEmptyObjects bool) (common.Hash, error) { if err := nodes.Merge(set); err != nil { return common.Hash{}, err } + updates, deleted := set.Size() storageTrieNodesUpdated += updates storageTrieNodesDeleted += deleted @@ -1400,12 +1406,14 @@ func (s *StateDB) Commit(deleteEmptyObjects bool) (common.Hash, error) { if metrics.EnabledExpensive { start = time.Now() } + root, set := s.trie.Commit(true) // Merge the dirty nodes of account trie into global set if set != nil { if err := nodes.Merge(set); err != nil { return common.Hash{}, err } + accountTrieNodesUpdated, accountTrieNodesDeleted = set.Size() } if metrics.EnabledExpensive { @@ -1438,31 +1446,42 @@ func (s *StateDB) Commit(deleteEmptyObjects bool) (common.Hash, error) { log.Warn("Failed to cap snapshot tree", "root", root, "layers", 128, "err", err) } } + if metrics.EnabledExpensive { s.SnapshotCommits += time.Since(start) } + s.snap, s.snapAccounts, s.snapStorage = nil, nil, nil } + if len(s.stateObjectsDestruct) > 0 { s.stateObjectsDestruct = make(map[common.Address]struct{}) } + if root == (common.Hash{}) { root = types.EmptyRootHash } + origin := s.originalRoot + if origin == (common.Hash{}) { origin = types.EmptyRootHash } + if root != origin { start := time.Now() + if err := s.db.TrieDB().Update(nodes); err != nil { return common.Hash{}, err } + s.originalRoot = root + if metrics.EnabledExpensive { s.TrieDBCommits += time.Since(start) } } + return root, nil } @@ -1486,15 +1505,19 @@ func (s *StateDB) Prepare(rules params.Rules, sender, coinbase common.Address, d s.accessList = al al.AddAddress(sender) + if dst != nil { - al.AddAddress(*dst) // If it's a create-tx, the destination will be added inside evm.create + al.AddAddress(*dst) } + for _, addr := range precompiles { al.AddAddress(addr) } + for _, el := range list { al.AddAddress(el.Address) + for _, key := range el.StorageKeys { al.AddSlot(el.Address, key) } @@ -1546,6 +1569,7 @@ func (s *StateDB) SlotInAccessList(addr common.Address, slot common.Hash) (addre // convertAccountSet converts a provided account set from address keyed to hash keyed. func (s *StateDB) convertAccountSet(set map[common.Address]struct{}) map[common.Hash]struct{} { ret := make(map[common.Hash]struct{}) + for addr := range set { obj, exist := s.stateObjects[addr] if !exist { @@ -1554,5 +1578,6 @@ func (s *StateDB) convertAccountSet(set map[common.Address]struct{}) map[common. ret[obj.addrHash] = struct{}{} } } + return ret } diff --git a/core/state/statedb_test.go b/core/state/statedb_test.go index a2202aac3f..bda4a7cc97 100644 --- a/core/state/statedb_test.go +++ b/core/state/statedb_test.go @@ -104,11 +104,12 @@ func TestIntermediateLeaks(t *testing.T) { modify(finalState, common.Address{i}, i, 99) } - // Commit and cross check the databases. + // Commit and cross-check the databases. transRoot, err := transState.Commit(false) if err != nil { t.Fatalf("failed to commit transition state: %v", err) } + if err = transState.Database().TrieDB().Commit(transRoot, false); err != nil { t.Errorf("can not commit trie %v to persistent database", transRoot.Hex()) } @@ -117,6 +118,7 @@ func TestIntermediateLeaks(t *testing.T) { if err != nil { t.Fatalf("failed to commit final state: %v", err) } + if err = finalState.Database().TrieDB().Commit(finalRoot, false); err != nil { t.Errorf("can not commit trie %v to persistent database", finalRoot.Hex()) } @@ -476,6 +478,7 @@ func (test *snapshotTest) checkEqual(state, checkstate *StateDB) error { return fmt.Errorf("got GetRefund() == %d, want GetRefund() == %d", state.GetRefund(), checkstate.GetRefund()) } + if !reflect.DeepEqual(state.GetLogs(common.Hash{}, 0, common.Hash{}), checkstate.GetLogs(common.Hash{}, 0, common.Hash{})) { return fmt.Errorf("got GetLogs(common.Hash{}) == %v, want GetLogs(common.Hash{}) == %v", state.GetLogs(common.Hash{}, 0, common.Hash{}), checkstate.GetLogs(common.Hash{}, 0, common.Hash{})) @@ -1390,20 +1393,26 @@ func TestFlushOrderDataLoss(t *testing.T) { statedb = NewDatabase(memdb) state, _ = New(common.Hash{}, statedb, nil) ) + for a := byte(0); a < 10; a++ { state.CreateAccount(common.Address{a}) for s := byte(0); s < 10; s++ { state.SetState(common.Address{a}, common.Hash{a, s}, common.Hash{a, s}) } } + root, err := state.Commit(false) + if err != nil { t.Fatalf("failed to commit state trie: %v", err) } + statedb.TrieDB().Reference(root, common.Hash{}) + if err := statedb.TrieDB().Cap(1024); err != nil { t.Fatalf("failed to cap trie dirty cache: %v", err) } + if err := statedb.TrieDB().Commit(root, false); err != nil { t.Fatalf("failed to commit state trie: %v", err) } @@ -1412,6 +1421,7 @@ func TestFlushOrderDataLoss(t *testing.T) { if err != nil { t.Fatalf("failed to reopen state trie: %v", err) } + for a := byte(0); a < 10; a++ { for s := byte(0); s < 10; s++ { if have := state.GetState(common.Address{a}, common.Hash{a, s}); have != (common.Hash{a, s}) { @@ -1433,6 +1443,7 @@ func TestStateDBTransientStorage(t *testing.T) { addr := common.Address{} state.SetTransientState(addr, key, value) + if exp, got := 1, state.journal.length(); exp != got { t.Fatalf("journal length mismatch: have %d, want %d", got, exp) } @@ -1444,6 +1455,7 @@ func TestStateDBTransientStorage(t *testing.T) { // revert the transient state being set and then check that the // value is now the empty hash state.journal.revert(state, 0) + if got, exp := state.GetTransientState(addr, key), (common.Hash{}); exp != got { t.Fatalf("transient storage mismatch: have %x, want %x", got, exp) } @@ -1452,6 +1464,7 @@ func TestStateDBTransientStorage(t *testing.T) { // the transient state is copied state.SetTransientState(addr, key, value) cpy := state.Copy() + if got := cpy.GetTransientState(addr, key); got != value { t.Fatalf("transient storage mismatch: have %x, want %x", got, value) } diff --git a/core/state/sync.go b/core/state/sync.go index 61097c6462..122b10265f 100644 --- a/core/state/sync.go +++ b/core/state/sync.go @@ -38,6 +38,7 @@ func NewStateSync(root common.Hash, database ethdb.KeyValueReader, onLeaf func(k // Register the account callback to connect the state trie and the storage // trie belongs to the contract. var syncer *trie.Sync + onAccount := func(keys [][]byte, path []byte, leaf []byte, parent common.Hash, parentPath []byte) error { if onLeaf != nil { if err := onLeaf(keys, leaf); err != nil { @@ -48,6 +49,7 @@ func NewStateSync(root common.Hash, database ethdb.KeyValueReader, onLeaf func(k if err := rlp.Decode(bytes.NewReader(leaf), &obj); err != nil { return err } + syncer.AddSubTrie(obj.Root, path, parent, parentPath, onSlot) syncer.AddCodeEntry(common.BytesToHash(obj.CodeHash), path, parent, parentPath) return nil diff --git a/core/state/sync_test.go b/core/state/sync_test.go index 090d55e473..04e2005906 100644 --- a/core/state/sync_test.go +++ b/core/state/sync_test.go @@ -105,11 +105,13 @@ func checkTrieConsistency(db ethdb.Database, root common.Hash) error { if v, _ := db.Get(root[:]); v == nil { return nil // Consider a non existent state consistent. } - trie, err := trie.New(trie.StateTrieID(root), trie.NewDatabase(db)) + + t, err := trie.New(trie.StateTrieID(root), trie.NewDatabase(db)) + if err != nil { return err } - it := trie.NodeIterator(nil) + it := t.NodeIterator(nil) for it.Next(true) { } return it.Error() @@ -135,6 +137,7 @@ func checkStateConsistency(db ethdb.Database, root common.Hash) error { func TestEmptyStateSync(t *testing.T) { db := trie.NewDatabase(rawdb.NewMemoryDatabase()) sync := NewStateSync(types.EmptyRootHash, rawdb.NewMemoryDatabase(), nil, db.Scheme()) + if paths, nodes, codes := sync.Missing(1); len(paths) != 0 || len(nodes) != 0 || len(codes) != 0 { t.Errorf("content requested for empty state: %v, %v, %v", nodes, paths, codes) } @@ -175,6 +178,7 @@ func testIterativeStateSync(t *testing.T, count int, commit bool, bypath bool) { if commit { srcDb.TrieDB().Commit(srcRoot, false) } + srcTrie, _ := trie.New(trie.StateTrieID(srcRoot), srcDb.TrieDB()) // Create a destination state and sync with the scheduler @@ -186,6 +190,7 @@ func testIterativeStateSync(t *testing.T, count int, commit bool, bypath bool) { codeElements []stateElement ) paths, nodes, codes := sched.Missing(count) + for i := 0; i < len(paths); i++ { nodeElements = append(nodeElements, stateElement{ path: paths[i], @@ -193,23 +198,28 @@ func testIterativeStateSync(t *testing.T, count int, commit bool, bypath bool) { syncPath: trie.NewSyncPath([]byte(paths[i])), }) } + for i := 0; i < len(codes); i++ { codeElements = append(codeElements, stateElement{ code: codes[i], }) } + for len(nodeElements)+len(codeElements) > 0 { var ( nodeResults = make([]trie.NodeSyncResult, len(nodeElements)) codeResults = make([]trie.CodeSyncResult, len(codeElements)) ) + for i, element := range codeElements { data, err := srcDb.ContractCode(common.Hash{}, element.code) if err != nil { t.Fatalf("failed to retrieve contract bytecode for hash %x", element.code) } + codeResults[i] = trie.CodeSyncResult{Hash: element.code, Data: data} } + for i, node := range nodeElements { if bypath { if len(node.syncPath) == 1 { @@ -217,6 +227,7 @@ func testIterativeStateSync(t *testing.T, count int, commit bool, bypath bool) { if err != nil { t.Fatalf("failed to retrieve node data for path %x: %v", node.syncPath[0], err) } + nodeResults[i] = trie.NodeSyncResult{Path: node.path, Data: data} } else { var acc types.StateAccount @@ -242,11 +253,13 @@ func testIterativeStateSync(t *testing.T, count int, commit bool, bypath bool) { nodeResults[i] = trie.NodeSyncResult{Path: node.path, Data: data} } } + for _, result := range codeResults { if err := sched.ProcessCode(result); err != nil { t.Errorf("failed to process result %v", err) } } + for _, result := range nodeResults { if err := sched.ProcessNode(result); err != nil { t.Errorf("failed to process result %v", err) @@ -260,6 +273,7 @@ func testIterativeStateSync(t *testing.T, count int, commit bool, bypath bool) { paths, nodes, codes = sched.Missing(count) nodeElements = nodeElements[:0] + for i := 0; i < len(paths); i++ { nodeElements = append(nodeElements, stateElement{ path: paths[i], @@ -267,7 +281,9 @@ func testIterativeStateSync(t *testing.T, count int, commit bool, bypath bool) { syncPath: trie.NewSyncPath([]byte(paths[i])), }) } + codeElements = codeElements[:0] + for i := 0; i < len(codes); i++ { codeElements = append(codeElements, stateElement{ code: codes[i], @@ -292,7 +308,9 @@ func TestIterativeDelayedStateSync(t *testing.T) { nodeElements []stateElement codeElements []stateElement ) + paths, nodes, codes := sched.Missing(0) + for i := 0; i < len(paths); i++ { nodeElements = append(nodeElements, stateElement{ path: paths[i], @@ -300,15 +318,19 @@ func TestIterativeDelayedStateSync(t *testing.T) { syncPath: trie.NewSyncPath([]byte(paths[i])), }) } + for i := 0; i < len(codes); i++ { codeElements = append(codeElements, stateElement{ code: codes[i], }) } + for len(nodeElements)+len(codeElements) > 0 { // Sync only half of the scheduled nodes var nodeProcessed int + var codeProcessed int + if len(codeElements) > 0 { codeResults := make([]trie.CodeSyncResult, len(codeElements)/2+1) for i, element := range codeElements[:len(codeResults)] { @@ -316,15 +338,19 @@ func TestIterativeDelayedStateSync(t *testing.T) { if err != nil { t.Fatalf("failed to retrieve contract bytecode for %x", element.code) } + codeResults[i] = trie.CodeSyncResult{Hash: element.code, Data: data} } + for _, result := range codeResults { if err := sched.ProcessCode(result); err != nil { t.Fatalf("failed to process result %v", err) } } + codeProcessed = len(codeResults) } + if len(nodeElements) > 0 { nodeResults := make([]trie.NodeSyncResult, len(nodeElements)/2+1) for i, element := range nodeElements[:len(nodeResults)] { @@ -332,13 +358,16 @@ func TestIterativeDelayedStateSync(t *testing.T) { if err != nil { t.Fatalf("failed to retrieve contract bytecode for %x", element.code) } + nodeResults[i] = trie.NodeSyncResult{Path: element.path, Data: data} } + for _, result := range nodeResults { if err := sched.ProcessNode(result); err != nil { t.Fatalf("failed to process result %v", err) } } + nodeProcessed = len(nodeResults) } batch := dstDb.NewBatch() @@ -349,6 +378,7 @@ func TestIterativeDelayedStateSync(t *testing.T) { paths, nodes, codes = sched.Missing(0) nodeElements = nodeElements[nodeProcessed:] + for i := 0; i < len(paths); i++ { nodeElements = append(nodeElements, stateElement{ path: paths[i], @@ -356,7 +386,9 @@ func TestIterativeDelayedStateSync(t *testing.T) { syncPath: trie.NewSyncPath([]byte(paths[i])), }) } + codeElements = codeElements[codeProcessed:] + for i := 0; i < len(codes); i++ { codeElements = append(codeElements, stateElement{ code: codes[i], @@ -384,6 +416,7 @@ func testIterativeRandomStateSync(t *testing.T, count int) { nodeQueue := make(map[string]stateElement) codeQueue := make(map[common.Hash]struct{}) paths, nodes, codes := sched.Missing(count) + for i, path := range paths { nodeQueue[path] = stateElement{ path: path, @@ -391,35 +424,44 @@ func testIterativeRandomStateSync(t *testing.T, count int) { syncPath: trie.NewSyncPath([]byte(path)), } } + for _, hash := range codes { codeQueue[hash] = struct{}{} } + for len(nodeQueue)+len(codeQueue) > 0 { // Fetch all the queued nodes in a random order if len(codeQueue) > 0 { results := make([]trie.CodeSyncResult, 0, len(codeQueue)) + for hash := range codeQueue { data, err := srcDb.ContractCode(common.Hash{}, hash) if err != nil { t.Fatalf("failed to retrieve node data for %x", hash) } + results = append(results, trie.CodeSyncResult{Hash: hash, Data: data}) } + for _, result := range results { if err := sched.ProcessCode(result); err != nil { t.Fatalf("failed to process result %v", err) } } } + if len(nodeQueue) > 0 { results := make([]trie.NodeSyncResult, 0, len(nodeQueue)) + for path, element := range nodeQueue { data, err := srcDb.TrieDB().Node(element.hash) if err != nil { t.Fatalf("failed to retrieve node data for %x %v %v", element.hash, []byte(element.path), element.path) } + results = append(results, trie.NodeSyncResult{Path: path, Data: data}) } + for _, result := range results { if err := sched.ProcessNode(result); err != nil { t.Fatalf("failed to process result %v", err) @@ -436,6 +478,7 @@ func testIterativeRandomStateSync(t *testing.T, count int) { nodeQueue = make(map[string]stateElement) codeQueue = make(map[common.Hash]struct{}) paths, nodes, codes := sched.Missing(count) + for i, path := range paths { nodeQueue[path] = stateElement{ path: path, @@ -443,11 +486,12 @@ func testIterativeRandomStateSync(t *testing.T, count int) { syncPath: trie.NewSyncPath([]byte(path)), } } + for _, hash := range codes { codeQueue[hash] = struct{}{} } } - // Cross check that the two states are in sync + // Cross-check that the two states are in sync checkStateAccounts(t, dstDb, srcRoot, srcAccounts) } @@ -464,6 +508,7 @@ func TestIterativeRandomDelayedStateSync(t *testing.T) { nodeQueue := make(map[string]stateElement) codeQueue := make(map[common.Hash]struct{}) paths, nodes, codes := sched.Missing(0) + for i, path := range paths { nodeQueue[path] = stateElement{ path: path, @@ -471,13 +516,16 @@ func TestIterativeRandomDelayedStateSync(t *testing.T) { syncPath: trie.NewSyncPath([]byte(path)), } } + for _, hash := range codes { codeQueue[hash] = struct{}{} } + for len(nodeQueue)+len(codeQueue) > 0 { // Sync only half of the scheduled nodes, even those in random order if len(codeQueue) > 0 { results := make([]trie.CodeSyncResult, 0, len(codeQueue)/2+1) + for hash := range codeQueue { delete(codeQueue, hash) @@ -485,24 +533,29 @@ func TestIterativeRandomDelayedStateSync(t *testing.T) { if err != nil { t.Fatalf("failed to retrieve node data for %x", hash) } + results = append(results, trie.CodeSyncResult{Hash: hash, Data: data}) if len(results) >= cap(results) { break } } + for _, result := range results { if err := sched.ProcessCode(result); err != nil { t.Fatalf("failed to process result %v", err) } } } + if len(nodeQueue) > 0 { results := make([]trie.NodeSyncResult, 0, len(nodeQueue)/2+1) + for path, element := range nodeQueue { delete(nodeQueue, path) data, err := srcDb.TrieDB().Node(element.hash) + if err != nil { t.Fatalf("failed to retrieve node data for %x", element.hash) } @@ -533,11 +586,12 @@ func TestIterativeRandomDelayedStateSync(t *testing.T) { syncPath: trie.NewSyncPath([]byte(path)), } } + for _, hash := range codes { codeQueue[hash] = struct{}{} } } - // Cross check that the two states are in sync + // Cross-check that the two states are in sync checkStateAccounts(t, dstDb, srcRoot, srcAccounts) } @@ -554,8 +608,9 @@ func TestIncompleteStateSync(t *testing.T) { isCode[crypto.Keccak256Hash(acc.code)] = struct{}{} } } + isCode[types.EmptyCodeHash] = struct{}{} - checkTrieConsistency(db, srcRoot) + _ = checkTrieConsistency(db, srcRoot) // Create a destination state and sync with the scheduler dstDb := rawdb.NewMemoryDatabase() @@ -566,9 +621,11 @@ func TestIncompleteStateSync(t *testing.T) { addedPaths []string addedHashes []common.Hash ) + nodeQueue := make(map[string]stateElement) codeQueue := make(map[common.Hash]struct{}) paths, nodes, codes := sched.Missing(1) + for i, path := range paths { nodeQueue[path] = stateElement{ path: path, @@ -576,18 +633,22 @@ func TestIncompleteStateSync(t *testing.T) { syncPath: trie.NewSyncPath([]byte(path)), } } + for _, hash := range codes { codeQueue[hash] = struct{}{} } + for len(nodeQueue)+len(codeQueue) > 0 { // Fetch a batch of state nodes if len(codeQueue) > 0 { results := make([]trie.CodeSyncResult, 0, len(codeQueue)) + for hash := range codeQueue { data, err := srcDb.ContractCode(common.Hash{}, hash) if err != nil { t.Fatalf("failed to retrieve node data for %x", hash) } + results = append(results, trie.CodeSyncResult{Hash: hash, Data: data}) addedCodes = append(addedCodes, hash) } @@ -598,20 +659,25 @@ func TestIncompleteStateSync(t *testing.T) { } } } + var nodehashes []common.Hash + if len(nodeQueue) > 0 { results := make([]trie.NodeSyncResult, 0, len(nodeQueue)) + for path, element := range nodeQueue { data, err := srcDb.TrieDB().Node(element.hash) if err != nil { t.Fatalf("failed to retrieve node data for %x", element.hash) } + results = append(results, trie.NodeSyncResult{Path: path, Data: data}) if element.hash != srcRoot { addedPaths = append(addedPaths, element.path) addedHashes = append(addedHashes, element.hash) } + nodehashes = append(nodehashes, element.hash) } // Process each of the state nodes @@ -638,6 +704,7 @@ func TestIncompleteStateSync(t *testing.T) { nodeQueue = make(map[string]stateElement) codeQueue = make(map[common.Hash]struct{}) paths, nodes, codes := sched.Missing(1) + for i, path := range paths { nodeQueue[path] = stateElement{ path: path, @@ -645,6 +712,7 @@ func TestIncompleteStateSync(t *testing.T) { syncPath: trie.NewSyncPath([]byte(path)), } } + for _, hash := range codes { codeQueue[hash] = struct{}{} } @@ -653,23 +721,31 @@ func TestIncompleteStateSync(t *testing.T) { for _, node := range addedCodes { val := rawdb.ReadCode(dstDb, node) rawdb.DeleteCode(dstDb, node) + if err := checkStateConsistency(dstDb, srcRoot); err == nil { t.Errorf("trie inconsistency not caught, missing: %x", node) } + rawdb.WriteCode(dstDb, node, val) } + scheme := srcDb.TrieDB().Scheme() + for i, path := range addedPaths { owner, inner := trie.ResolvePath([]byte(path)) hash := addedHashes[i] val := rawdb.ReadTrieNode(dstDb, owner, inner, hash, scheme) + if val == nil { t.Error("missing trie node") } + rawdb.DeleteTrieNode(dstDb, owner, inner, hash, scheme) + if err := checkStateConsistency(dstDb, srcRoot); err == nil { t.Errorf("trie inconsistency not caught, missing: %v", path) } + rawdb.WriteTrieNode(dstDb, owner, inner, hash, val, scheme) } } diff --git a/core/state/transient_storage.go b/core/state/transient_storage.go index 66e563efa7..164cccad5c 100644 --- a/core/state/transient_storage.go +++ b/core/state/transient_storage.go @@ -33,6 +33,7 @@ func (t transientStorage) Set(addr common.Address, key, value common.Hash) { if _, ok := t[addr]; !ok { t[addr] = make(Storage) } + t[addr][key] = value } @@ -42,6 +43,7 @@ func (t transientStorage) Get(addr common.Address, key common.Hash) common.Hash if !ok { return common.Hash{} } + return val[key] } @@ -51,5 +53,6 @@ func (t transientStorage) Copy() transientStorage { for key, value := range t { storage[key] = value.Copy() } + return storage } diff --git a/core/state/trie_prefetcher.go b/core/state/trie_prefetcher.go index 844f72fc10..a227aff0bb 100644 --- a/core/state/trie_prefetcher.go +++ b/core/state/trie_prefetcher.go @@ -300,6 +300,7 @@ func (sf *subfetcher) loop() { log.Warn("Trie prefetcher failed opening trie", "root", sf.root, "err", err) return } + sf.trie = trie } else { trie, err := sf.db.OpenStorageTrie(sf.state, sf.owner, sf.root) diff --git a/core/state/trie_prefetcher_test.go b/core/state/trie_prefetcher_test.go index 501bb70840..e261e8a5ee 100644 --- a/core/state/trie_prefetcher_test.go +++ b/core/state/trie_prefetcher_test.go @@ -50,6 +50,7 @@ func TestCopyAndClose(t *testing.T) { prefetcher.prefetch(common.Hash{}, db.originalRoot, common.Address{}, [][]byte{skey.Bytes()}) prefetcher.prefetch(common.Hash{}, db.originalRoot, common.Address{}, [][]byte{skey.Bytes()}) time.Sleep(1 * time.Second) + a := prefetcher.trie(common.Hash{}, db.originalRoot) prefetcher.prefetch(common.Hash{}, db.originalRoot, common.Address{}, [][]byte{skey.Bytes()}) b := prefetcher.trie(common.Hash{}, db.originalRoot) diff --git a/core/txpool/list_test.go b/core/txpool/list_test.go index 19d721a743..46c94357c1 100644 --- a/core/txpool/list_test.go +++ b/core/txpool/list_test.go @@ -21,6 +21,8 @@ import ( "math/rand" "testing" + "github.com/holiman/uint256" + "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" ) diff --git a/core/types/receipt_test.go b/core/types/receipt_test.go index a5d28a0a7e..d9a741db5d 100644 --- a/core/types/receipt_test.go +++ b/core/types/receipt_test.go @@ -24,6 +24,8 @@ import ( "reflect" "testing" + "github.com/kylelemons/godebug/diff" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/rlp" diff --git a/core/vm/analysis.go b/core/vm/analysis.go index 38af9084ac..1f40da2331 100644 --- a/core/vm/analysis.go +++ b/core/vm/analysis.go @@ -76,6 +76,7 @@ func codeBitmapInternal(code, bits bitvec) bitvec { for pc := uint64(0); pc < uint64(len(code)); { op := OpCode(code[pc]) pc++ + if int8(op) < int8(PUSH1) { // If not PUSH (the int8(op) > int(PUSH32) is always false). continue } diff --git a/core/vm/eips.go b/core/vm/eips.go index 29ff27c552..6d4f37eb7a 100644 --- a/core/vm/eips.go +++ b/core/vm/eips.go @@ -197,6 +197,8 @@ func opTload(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]by hash := common.Hash(loc.Bytes32()) val := interpreter.evm.StateDB.GetTransientState(scope.Contract.Address(), hash) loc.SetBytes(val.Bytes()) + + // nolint:nilnil return nil, nil } @@ -205,9 +207,12 @@ func opTstore(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]b if interpreter.readOnly { return nil, ErrWriteProtection } + loc := scope.Stack.pop() val := scope.Stack.pop() interpreter.evm.StateDB.SetTransientState(scope.Contract.Address(), loc.Bytes32(), val.Bytes32()) + + // nolint:nilnil return nil, nil } diff --git a/core/vm/gas_table.go b/core/vm/gas_table.go index 4f961ef4db..dfd44d2ac9 100644 --- a/core/vm/gas_table.go +++ b/core/vm/gas_table.go @@ -307,7 +307,9 @@ func gasCreateEip3860(evm *EVM, contract *Contract, stack *Stack, mem *Memory, m if err != nil { return 0, err } + size, overflow := stack.Back(2).Uint64WithOverflow() + if overflow || size > params.MaxInitCodeSize { return 0, ErrGasUintOverflow } @@ -316,6 +318,7 @@ func gasCreateEip3860(evm *EVM, contract *Contract, stack *Stack, mem *Memory, m if gas, overflow = math.SafeAdd(gas, moreGas); overflow { return 0, ErrGasUintOverflow } + return gas, nil } func gasCreate2Eip3860(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { @@ -323,7 +326,9 @@ func gasCreate2Eip3860(evm *EVM, contract *Contract, stack *Stack, mem *Memory, if err != nil { return 0, err } + size, overflow := stack.Back(2).Uint64WithOverflow() + if overflow || size > params.MaxInitCodeSize { return 0, ErrGasUintOverflow } @@ -332,6 +337,7 @@ func gasCreate2Eip3860(evm *EVM, contract *Contract, stack *Stack, mem *Memory, if gas, overflow = math.SafeAdd(gas, moreGas); overflow { return 0, ErrGasUintOverflow } + return gas, nil } diff --git a/core/vm/gas_table_test.go b/core/vm/gas_table_test.go index c36ce707bf..66798d1e44 100644 --- a/core/vm/gas_table_test.go +++ b/core/vm/gas_table_test.go @@ -135,39 +135,49 @@ func TestCreateGas(t *testing.T) { for i, tt := range createGasTests { var gasUsed = uint64(0) + doCheck := func(testGas int) bool { address := common.BytesToAddress([]byte("contract")) statedb, _ := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil) statedb.CreateAccount(address) statedb.SetCode(address, hexutil.MustDecode(tt.code)) statedb.Finalise(true) + vmctx := BlockContext{ CanTransfer: func(StateDB, common.Address, *big.Int) bool { return true }, Transfer: func(StateDB, common.Address, common.Address, *big.Int) {}, BlockNumber: big.NewInt(0), } config := Config{} + if tt.eip3860 { config.ExtraEips = []int{3860} } vmenv := NewEVM(vmctx, TxContext{}, statedb, params.AllEthashProtocolChanges, config) + var startGas = uint64(testGas) ret, gas, err := vmenv.Call(AccountRef(common.Address{}), address, nil, startGas, new(big.Int), nil) + if err != nil { return false } + gasUsed = startGas - gas + if len(ret) != 32 { t.Fatalf("test %d: expected 32 bytes returned, have %d", i, len(ret)) } + if bytes.Equal(ret, make([]byte, 32)) { // Failure return false } + return true } minGas := sort.Search(100_000, doCheck) + if uint64(minGas) != tt.minimumGas { t.Fatalf("test %d: min gas error, want %d, have %d", i, tt.minimumGas, minGas) } diff --git a/core/vm/instructions.go b/core/vm/instructions.go index 4913e9f7bc..6d57fb969f 100644 --- a/core/vm/instructions.go +++ b/core/vm/instructions.go @@ -822,6 +822,7 @@ func opSelfdestruct(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext balance := interpreter.evm.StateDB.GetBalance(scope.Contract.Address()) interpreter.evm.StateDB.AddBalance(beneficiary.Bytes20(), balance) interpreter.evm.StateDB.Suicide(scope.Contract.Address()) + if tracer := interpreter.evm.Config.Tracer; tracer != nil { tracer.CaptureEnter(SELFDESTRUCT, scope.Contract.Address(), beneficiary.Bytes20(), []byte{}, 0, balance) tracer.CaptureExit([]byte{}, 0, nil) diff --git a/core/vm/instructions_test.go b/core/vm/instructions_test.go index 5fd817556d..e7bfbac2de 100644 --- a/core/vm/instructions_test.go +++ b/core/vm/instructions_test.go @@ -251,15 +251,19 @@ func TestWriteExpectedValues(t *testing.T) { interpreter = env.interpreter ) result := make([]TwoOperandTestcase, len(args)) + for i, param := range args { x := new(uint256.Int).SetBytes(common.Hex2Bytes(param.x)) y := new(uint256.Int).SetBytes(common.Hex2Bytes(param.y)) + stack.push(x) stack.push(y) - opFn(&pc, interpreter, &ScopeContext{nil, stack, nil}) + + _, _ = opFn(&pc, interpreter, &ScopeContext{nil, stack, nil}) actual := stack.pop() result[i] = TwoOperandTestcase{param.x, param.y, fmt.Sprintf("%064x", actual)} } + return result } @@ -268,6 +272,7 @@ func TestWriteExpectedValues(t *testing.T) { if err != nil { t.Fatal(err) } + _ = os.WriteFile(fmt.Sprintf("testdata/testcases_%v.json", name), data, 0644) if err != nil { t.Fatal(err) @@ -616,7 +621,9 @@ func TestOpTstore(t *testing.T) { if stack.len() != 1 { t.Fatal("stack wrong size") } + val := stack.peek() + if !bytes.Equal(val.Bytes(), value) { t.Fatal("incorrect element read from transient storage") } diff --git a/core/vm/interpreter.go b/core/vm/interpreter.go index 6810602d0f..a0fda2af6c 100644 --- a/core/vm/interpreter.go +++ b/core/vm/interpreter.go @@ -130,6 +130,7 @@ func PutCache(ctx context.Context, cache *TxCache) context.Context { func NewEVMInterpreter(evm *EVM) *EVMInterpreter { // If jump table was not initialised we set the default one. var table *JumpTable + switch { // TODO marcello double check case evm.chainRules.IsShanghai: @@ -155,11 +156,14 @@ func NewEVMInterpreter(evm *EVM) *EVMInterpreter { default: table = &frontierInstructionSet } + var extraEips []int + if len(evm.Config.ExtraEips) > 0 { // Deep-copy jumptable to prevent modification of opcodes in other tables table = copyJumpTable(table) } + for _, eip := range evm.Config.ExtraEips { if err := EnableEIP(eip, table); err != nil { // Disable it, so caller can check if it's activated or not @@ -168,7 +172,9 @@ func NewEVMInterpreter(evm *EVM) *EVMInterpreter { extraEips = append(extraEips, eip) } } + evm.Config.ExtraEips = extraEips + return &EVMInterpreter{evm: evm, table: table} } @@ -244,6 +250,7 @@ func (in *EVMInterpreter) Run(contract *Contract, input []byte, readOnly bool, i defer func() { returnStack(stack) }() + contract.Input = input if debug { @@ -504,6 +511,7 @@ func (in *EVMInterpreter) RunWithDelay(contract *Contract, input []byte, readOnl // Do tracing before memory expansion if debug { in.evm.Config.Tracer.CaptureState(pc, op, gasCopy, cost, callContext, in.returnData, in.evm.depth, err) + logged = true } if memorySize > 0 { diff --git a/core/vm/jump_table.go b/core/vm/jump_table.go index a45287de80..dbf75e3876 100644 --- a/core/vm/jump_table.go +++ b/core/vm/jump_table.go @@ -83,6 +83,7 @@ func newShanghaiInstructionSet() JumpTable { instructionSet := newMergeInstructionSet() enable3855(&instructionSet) // PUSH0 instruction enable3860(&instructionSet) // Limit and meter initcode + return validate(instructionSet) } @@ -1054,11 +1055,13 @@ func newFrontierInstructionSet() JumpTable { func copyJumpTable(source *JumpTable) *JumpTable { dest := *source + for i, op := range source { if op != nil { opCopy := *op dest[i] = &opCopy } } + return &dest } diff --git a/core/vm/jump_table_export.go b/core/vm/jump_table_export.go index 0d61b00ede..3cab55bc8f 100644 --- a/core/vm/jump_table_export.go +++ b/core/vm/jump_table_export.go @@ -51,6 +51,7 @@ func LookupInstructionSet(rules params.Rules) (JumpTable, error) { case rules.IsHomestead: return newHomesteadInstructionSet(), nil } + return newFrontierInstructionSet(), nil } diff --git a/eth/backend.go b/eth/backend.go index 820442f10a..32abf35e69 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -61,6 +61,7 @@ import ( "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rpc" + "github.com/ethereum/go-ethereum/trie" ) // Config contains the configuration options of the ETH protocol. diff --git a/eth/peer.go b/eth/peer.go index 7618777716..2e6d08329c 100644 --- a/eth/peer.go +++ b/eth/peer.go @@ -34,6 +34,7 @@ type ethPeer struct { } // info gathers and returns some `eth` protocol metadata known about a peer. +// nolint:typecheck func (p *ethPeer) info() *ethPeerInfo { return ðPeerInfo{ Version: p.Version(), diff --git a/eth/protocols/eth/handler_test.go b/eth/protocols/eth/handler_test.go index d1453fbdef..888e3b5578 100644 --- a/eth/protocols/eth/handler_test.go +++ b/eth/protocols/eth/handler_test.go @@ -65,6 +65,7 @@ func newTestBackend(blocks int) *testBackend { // newTestBackend creates a chain with a number of explicitly defined blocks and // wraps it into a mock backend. +// nolint:typecheck func newTestBackendWithGenerator(blocks int, shanghai bool, generator func(int, *core.BlockGen)) *testBackend { var ( // Create a database pre-initialize with a genesis block diff --git a/eth/protocols/snap/sync.go b/eth/protocols/snap/sync.go index 4292ba8ac9..105fbb3cd4 100644 --- a/eth/protocols/snap/sync.go +++ b/eth/protocols/snap/sync.go @@ -21,6 +21,7 @@ import ( "encoding/json" "errors" "fmt" + "golang.org/x/crypto/sha3" gomath "math" "math/big" "math/rand" @@ -2744,7 +2745,7 @@ func (s *Syncer) OnTrieNodes(peer SyncPeer, id uint64, trienodes [][]byte) error } s.lock.Unlock() - // Cross reference the requested trienodes with the response to find gaps + // Cross-reference the requested trie-nodes with the response to find gaps // that the serving node is missing var ( hasher = sha3.NewLegacyKeccak256().(crypto.KeccakState) @@ -2755,8 +2756,8 @@ func (s *Syncer) OnTrieNodes(peer SyncPeer, id uint64, trienodes [][]byte) error for i, j := 0, 0; i < len(trienodes); i++ { // Find the next hash that we've been served, leaving misses with nils hasher.Reset() - hasher.Write(trienodes[i]) - hasher.Read(hash) + _, _ = hasher.Write(trienodes[i]) + _, _ = hasher.Read(hash) for j < len(req.hashes) && !bytes.Equal(hash, req.hashes[j][:]) { j++ diff --git a/eth/tracers/api_test.go b/eth/tracers/api_test.go index 26826349f3..8ec442c3eb 100644 --- a/eth/tracers/api_test.go +++ b/eth/tracers/api_test.go @@ -391,6 +391,7 @@ func TestTraceTransaction(t *testing.T) { } } +// nolint:typecheck func TestTraceBlock(t *testing.T) { t.Parallel() @@ -552,6 +553,7 @@ func TestIOdump(t *testing.T) { } } +// nolint:typecheck func TestTracingWithOverrides(t *testing.T) { t.Parallel() // Initialize test accounts @@ -923,6 +925,7 @@ func newStates(keys []common.Hash, vals []common.Hash) *map[common.Hash]common.H return &m } +// nolint:typecheck func TestTraceChain(t *testing.T) { t.Parallel() diff --git a/eth/tracers/logger/logger.go b/eth/tracers/logger/logger.go index c7f171c5bd..ab10edc53f 100644 --- a/eth/tracers/logger/logger.go +++ b/eth/tracers/logger/logger.go @@ -240,6 +240,7 @@ func (l *StructLogger) GetResult() (json.RawMessage, error) { if l.reason != nil { return nil, l.reason } + failed := l.err != nil returnData := common.CopyBytes(l.output) // Return data when successful and revert reason when reverted, otherwise empty. @@ -247,6 +248,7 @@ func (l *StructLogger) GetResult() (json.RawMessage, error) { if failed && l.err != vm.ErrExecutionReverted { returnVal = "" } + return json.Marshal(&ExecutionResult{ Gas: l.usedGas, Failed: failed, @@ -436,27 +438,34 @@ func formatLogs(logs []StructLog) []StructLogRes { Error: trace.ErrorString(), RefundCounter: trace.RefundCounter, } + if trace.Stack != nil { stack := make([]string, len(trace.Stack)) for i, stackValue := range trace.Stack { stack[i] = stackValue.Hex() } + formatted[index].Stack = &stack } + if trace.Memory != nil { memory := make([]string, 0, (len(trace.Memory)+31)/32) for i := 0; i+32 <= len(trace.Memory); i += 32 { memory = append(memory, fmt.Sprintf("%x", trace.Memory[i:i+32])) } + formatted[index].Memory = &memory } + if trace.Storage != nil { storage := make(map[string]string) for i, storageValue := range trace.Storage { storage[fmt.Sprintf("%x", i)] = fmt.Sprintf("%x", storageValue) } + formatted[index].Storage = &storage } } + return formatted } diff --git a/eth/tracers/logger/logger_test.go b/eth/tracers/logger/logger_test.go index 6a5889aabf..4539a01c37 100644 --- a/eth/tracers/logger/logger_test.go +++ b/eth/tracers/logger/logger_test.go @@ -51,6 +51,7 @@ type dummyStatedb struct { func (*dummyStatedb) GetRefund() uint64 { return 1337 } func (*dummyStatedb) GetState(_ common.Address, _ common.Hash) common.Hash { return common.Hash{} } func (*dummyStatedb) SetState(_ common.Address, _ common.Hash, _ common.Hash) {} +func (*dummyStatedb) AddAddressToAccessList(address common.Address) {} func TestStoreCapture(t *testing.T) { var ( diff --git a/ethclient/gethclient/gethclient_test.go b/ethclient/gethclient/gethclient_test.go index ad7909d14d..78b42c3369 100644 --- a/ethclient/gethclient/gethclient_test.go +++ b/ethclient/gethclient/gethclient_test.go @@ -79,6 +79,7 @@ func newTestBackend(t *testing.T) (*node.Node, []*types.Block) { return n, blocks } +// nolint:typecheck func generateTestChain() (*core.Genesis, []*types.Block) { genesis := &core.Genesis{ Config: params.AllEthashProtocolChanges, diff --git a/graphql/graphql_test.go b/graphql/graphql_test.go index 37a741de90..93c69904de 100644 --- a/graphql/graphql_test.go +++ b/graphql/graphql_test.go @@ -172,6 +172,7 @@ func TestGraphQLBlockSerialization(t *testing.T) { } } +// nolint:typecheck func TestGraphQLBlockSerializationEIP2718(t *testing.T) { // Account for signing txes var ( @@ -277,6 +278,7 @@ func TestGraphQLHTTPOnSamePort_GQLRequest_Unsuccessful(t *testing.T) { assert.Equal(t, http.StatusNotFound, resp.StatusCode) } +// nolint:typecheck func TestGraphQLConcurrentResolvers(t *testing.T) { t.Parallel() diff --git a/internal/cli/server/server.go b/internal/cli/server/server.go index 7e73361669..9a3d6ab316 100644 --- a/internal/cli/server/server.go +++ b/internal/cli/server/server.go @@ -24,10 +24,12 @@ import ( "github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/accounts/keystore" + "github.com/ethereum/go-ethereum/cmd/utils" "github.com/ethereum/go-ethereum/consensus/beacon" //nolint:typecheck "github.com/ethereum/go-ethereum/consensus/bor" //nolint:typecheck "github.com/ethereum/go-ethereum/consensus/clique" "github.com/ethereum/go-ethereum/eth" + "github.com/ethereum/go-ethereum/eth/ethconfig" "github.com/ethereum/go-ethereum/eth/tracers" "github.com/ethereum/go-ethereum/ethstats" "github.com/ethereum/go-ethereum/graphql" diff --git a/internal/flags/helpers.go b/internal/flags/helpers.go index 6f9e4f7e82..1ce7156ba0 100644 --- a/internal/flags/helpers.go +++ b/internal/flags/helpers.go @@ -20,6 +20,8 @@ import ( "fmt" "strings" + "github.com/urfave/cli/v2" + "github.com/ethereum/go-ethereum/internal/version" "github.com/ethereum/go-ethereum/params" ) diff --git a/les/utils/timeutils_test.go b/les/utils/timeutils_test.go index b219d0439d..431b651435 100644 --- a/les/utils/timeutils_test.go +++ b/les/utils/timeutils_test.go @@ -37,6 +37,7 @@ func TestUpdateTimer(t *testing.T) { if updated := timer.Update(func(diff time.Duration) bool { return true }); !updated { t.Fatalf("Doesn't update the clock when reaching the threshold") } + if updated := timer.UpdateAt(sim.Now().Add(time.Second), func(diff time.Duration) bool { return true }); !updated { t.Fatalf("Doesn't update the clock when reaching the threshold") } diff --git a/metrics/counter_float64.go b/metrics/counter_float64.go index 370ac1c225..075814d2f4 100644 --- a/metrics/counter_float64.go +++ b/metrics/counter_float64.go @@ -58,6 +58,7 @@ func NewRegisteredCounterFloat64(name string, r Registry) CounterFloat64 { if nil == r { r = DefaultRegistry } + _ = r.Register(name, c) return c @@ -73,6 +74,7 @@ func NewRegisteredCounterFloat64Forced(name string, r Registry) CounterFloat64 { if nil == r { r = DefaultRegistry } + _ = r.Register(name, c) return c diff --git a/miner/test_backend.go b/miner/test_backend.go index a0867165e3..f9070d6cbc 100644 --- a/miner/test_backend.go +++ b/miner/test_backend.go @@ -5,7 +5,7 @@ import ( "crypto/rand" "errors" "fmt" - "github.com/ethereum/go-ethereum/accounts" + "github.com/ethereum/go-ethereum/accounts" // nolint:typecheck "github.com/ethereum/go-ethereum/consensus/bor" "github.com/ethereum/go-ethereum/consensus/clique" "github.com/ethereum/go-ethereum/consensus/ethash" diff --git a/node/node.go b/node/node.go index 39e7321504..9ebc1b0876 100644 --- a/node/node.go +++ b/node/node.go @@ -29,6 +29,8 @@ import ( "strings" "sync" + "github.com/gofrs/flock" + "github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" @@ -336,7 +338,7 @@ func (n *Node) openDataDir() error { func (n *Node) closeDataDir() { // Release instance directory lock. if n.dirLock != nil && n.dirLock.Locked() { - n.dirLock.Unlock() + _ = n.dirLock.Unlock() n.dirLock = nil } } diff --git a/p2p/dial.go b/p2p/dial.go index 134e6e2eae..ff0cd52cdd 100644 --- a/p2p/dial.go +++ b/p2p/dial.go @@ -347,6 +347,7 @@ func (d *dialScheduler) rearmHistoryTimer() { if len(d.history) == 0 { return } + d.historyTimer.Schedule(d.history.nextExpiry()) } diff --git a/p2p/discover/lookup.go b/p2p/discover/lookup.go index b8d97b44e1..55d24ffa50 100644 --- a/p2p/discover/lookup.go +++ b/p2p/discover/lookup.go @@ -142,6 +142,7 @@ func (it *lookup) slowdown() { func (it *lookup) query(n *node, reply chan<- []*node) { fails := it.tab.db.FindFails(n.ID(), n.IP()) r, err := it.queryfunc(n) + if errors.Is(err, errClosed) { // Avoid recording failures on shutdown. reply <- nil diff --git a/p2p/discover/table.go b/p2p/discover/table.go index 41d5ac6e34..06f02c510c 100644 --- a/p2p/discover/table.go +++ b/p2p/discover/table.go @@ -677,6 +677,7 @@ func (h *nodesByDistance) push(n *node, maxElems int) { if len(h.entries) < maxElems { h.entries = append(h.entries, n) } + if ix < end { // Slide existing entries down to make room. // This will overwrite the entry we just appended. diff --git a/p2p/discover/table_test.go b/p2p/discover/table_test.go index 747bddad6a..2e8c7908be 100644 --- a/p2p/discover/table_test.go +++ b/p2p/discover/table_test.go @@ -398,6 +398,7 @@ func TestTable_revalidateSyncRecord(t *testing.T) { func TestNodesPush(t *testing.T) { t.Parallel() + var target enode.ID n1 := nodeAtDistance(target, 255, intIP(1)) n2 := nodeAtDistance(target, 254, intIP(2)) @@ -417,6 +418,7 @@ func TestNodesPush(t *testing.T) { for _, n := range nodes { list.push(n, 3) } + if !slicesEqual(list.entries, perm[0], nodeIDEqual) { t.Fatal("not equal") } @@ -428,6 +430,7 @@ func TestNodesPush(t *testing.T) { for _, n := range nodes { list.push(n, 2) } + if !slicesEqual(list.entries, perm[0][:2], nodeIDEqual) { t.Fatal("not equal") } @@ -442,11 +445,13 @@ func slicesEqual[T any](s1, s2 []T, check func(e1, e2 T) bool) bool { if len(s1) != len(s2) { return false } + for i := range s1 { if !check(s1[i], s2[i]) { return false } } + return true } diff --git a/p2p/discover/v5_udp.go b/p2p/discover/v5_udp.go index 38f5b3b652..582164b1e4 100644 --- a/p2p/discover/v5_udp.go +++ b/p2p/discover/v5_udp.go @@ -309,6 +309,7 @@ func (t *UDPv5) lookupWorker(destNode *node, target enode.ID) ([]*node, error) { ) var r []*enode.Node r, err = t.findnode(unwrapNode(destNode), dists) + if errors.Is(err, errClosed) { return nil, err } @@ -411,6 +412,7 @@ func (t *UDPv5) verifyResponseNode(c *callV5, r *enr.Record, distances []uint, s if err := netutil.CheckRelayIP(c.node.IP(), node.IP()); err != nil { return nil, err } + if t.netrestrict != nil && !t.netrestrict.Contains(node.IP()) { return nil, errors.New("not contained in netrestrict list") } @@ -665,6 +667,7 @@ func (t *UDPv5) handlePacket(rawpacket []byte, fromAddr *net.UDPAddr) error { up := ReadPacket{Data: make([]byte, len(rawpacket)), Addr: fromAddr} copy(up.Data, rawpacket) t.unhandled <- up + return nil } t.log.Debug("Bad discv5 packet", "id", fromID, "addr", addr, "err", err) @@ -861,16 +864,19 @@ func packNodes(reqid []byte, nodes []*enode.Node) []*v5wire.Nodes { for len(nodes) > 0 { p := &v5wire.Nodes{ReqID: reqid} size := uint64(0) + for len(nodes) > 0 { r := nodes[0].Record() if size += r.Size(); size > sizeLimit { break } + p.Nodes = append(p.Nodes, r) nodes = nodes[1:] } resp = append(resp, p) } + for _, msg := range resp { msg.RespCount = uint8(len(resp)) } diff --git a/p2p/discover/v5_udp_test.go b/p2p/discover/v5_udp_test.go index 90fab253b7..8d2e0c5244 100644 --- a/p2p/discover/v5_udp_test.go +++ b/p2p/discover/v5_udp_test.go @@ -28,6 +28,8 @@ import ( "testing" "time" + "github.com/stretchr/testify/require" + "github.com/ethereum/go-ethereum/internal/testlog" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/p2p/discover/v5wire" @@ -553,7 +555,7 @@ func TestUDPv5_lookupDistances(t *testing.T) { t.Run("target distance of 256", func(t *testing.T) { t.Parallel() node := nodeAtDistance(lnID, 256, intIP(0)) - dists := lookupDistances(lnID, node.ID()) + dists := lookupDistances(lnID, node.ID()) // nolint:typecheck require.Equal(t, []uint{256, 255, 254}, dists) }) } diff --git a/p2p/discover/v5wire/encoding.go b/p2p/discover/v5wire/encoding.go index 5108910620..82afefb113 100644 --- a/p2p/discover/v5wire/encoding.go +++ b/p2p/discover/v5wire/encoding.go @@ -453,6 +453,7 @@ func (c *Codec) Decode(inputData []byte, addr string) (src enode.ID, n *enode.No c.reader.Reset(staticHeader) binary.Read(&c.reader, binary.BigEndian, &head.StaticHeader) remainingInput := len(input) - sizeofStaticPacketData + if err := head.checkValid(remainingInput, c.protocolID); err != nil { return enode.ID{}, nil, nil, err } @@ -620,6 +621,7 @@ func (c *Codec) decodeMessage(fromAddr string, head *Header, headerData, msgData // Try decrypting the message. key := c.sc.readKey(auth.SrcID, fromAddr) msg, err := c.decryptMessage(msgData, head.Nonce[:], headerData, key) + if errors.Is(err, errMessageDecrypt) { // It didn't work. Start the handshake since this is an ordinary message packet. return &Unknown{Nonce: head.Nonce}, nil diff --git a/p2p/discover/v5wire/msg.go b/p2p/discover/v5wire/msg.go index fb8e1e12c2..afc7bb54f9 100644 --- a/p2p/discover/v5wire/msg.go +++ b/p2p/discover/v5wire/msg.go @@ -194,6 +194,7 @@ func (p *Findnode) AppendLogInfo(ctx []interface{}) []interface{} { if p.OpID != 0 { ctx = append(ctx, "opid", p.OpID) } + return ctx } diff --git a/p2p/dnsdisc/client_test.go b/p2p/dnsdisc/client_test.go index b7198f7ad2..e29b636b50 100644 --- a/p2p/dnsdisc/client_test.go +++ b/p2p/dnsdisc/client_test.go @@ -472,13 +472,16 @@ func (mr mapResolver) LookupTXT(ctx context.Context, name string) ([]string, err } func parseNodes(rec []string) []*enode.Node { - var ns []*enode.Node + ns := make([]*enode.Node, len(rec)) + for _, r := range rec { var n enode.Node if err := n.UnmarshalText([]byte(r)); err != nil { panic(err) } + ns = append(ns, &n) } + return ns } diff --git a/p2p/enode/iter.go b/p2p/enode/iter.go index b8ab4a758a..53ec4cf43e 100644 --- a/p2p/enode/iter.go +++ b/p2p/enode/iter.go @@ -213,6 +213,7 @@ func (m *FairMix) Next() bool { if source.timeout >= 0 { timer := time.NewTimer(source.timeout) timeout = timer.C + defer timer.Stop() } diff --git a/p2p/enr/enr.go b/p2p/enr/enr.go index 2b093b2f1a..e049930913 100644 --- a/p2p/enr/enr.go +++ b/p2p/enr/enr.go @@ -101,16 +101,19 @@ func (r *Record) Size() uint64 { if r.raw != nil { return uint64(len(r.raw)) } + return computeSize(r) } func computeSize(r *Record) uint64 { size := uint64(rlp.IntSize(r.seq)) size += rlp.BytesSize(r.signature) + for _, p := range r.pairs { size += rlp.StringSize(p.k) size += uint64(len(p.v)) } + return rlp.ListSize(size) } diff --git a/p2p/enr/enr_test.go b/p2p/enr/enr_test.go index dec181ce5f..00fe9bdbc6 100644 --- a/p2p/enr/enr_test.go +++ b/p2p/enr/enr_test.go @@ -171,6 +171,7 @@ func TestDirty(t *testing.T) { func TestSize(t *testing.T) { t.Parallel() + var r Record // Empty record size is 3 bytes. @@ -185,12 +186,14 @@ func TestSize(t *testing.T) { // Now add a signature. nodeid := []byte{1, 2, 3, 4, 5, 6, 7, 8} - signTest(nodeid, &r) + _ = signTest(nodeid, &r) assert.Equal(t, uint64(45), r.Size()) enc, _ := rlp.EncodeToBytes(&r) + if r.Size() != uint64(len(enc)) { t.Error("Size() not equal encoded length", len(enc)) } + if r.Size() != computeSize(&r) { t.Error("Size() not equal computed size", computeSize(&r)) } diff --git a/p2p/enr/entries.go b/p2p/enr/entries.go index 9945a436c9..6df2813258 100644 --- a/p2p/enr/entries.go +++ b/p2p/enr/entries.go @@ -192,5 +192,6 @@ func IsNotFound(err error) bool { if errors.As(err, &ke) { return ke.Err == errNotFound } + return false } diff --git a/p2p/message.go b/p2p/message.go index 24f21456d8..10cb50d8ad 100644 --- a/p2p/message.go +++ b/p2p/message.go @@ -243,6 +243,7 @@ func ExpectMsg(r MsgReader, code uint64, content interface{}) error { if int(msg.Size) != len(contentEnc) { return fmt.Errorf("message size mismatch: got %d, want %d", msg.Size, len(contentEnc)) } + actualContent, err := io.ReadAll(msg.Payload) if err != nil { return err diff --git a/p2p/peer_error.go b/p2p/peer_error.go index ecc8094185..ff1a1dfdd4 100644 --- a/p2p/peer_error.go +++ b/p2p/peer_error.go @@ -104,6 +104,7 @@ func discReasonForError(err error) DiscReason { if reason, ok := err.(DiscReason); ok { return reason } + if errors.Is(err, errProtocolReturned) { return DiscQuitting } diff --git a/p2p/simulations/mocker_test.go b/p2p/simulations/mocker_test.go index a577fc8e7e..d01e227a67 100644 --- a/p2p/simulations/mocker_test.go +++ b/p2p/simulations/mocker_test.go @@ -28,6 +28,7 @@ import ( "testing" "time" + "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/p2p/enode" ) diff --git a/rlp/decode.go b/rlp/decode.go index 8df281aaa6..b1d826ac37 100644 --- a/rlp/decode.go +++ b/rlp/decode.go @@ -28,7 +28,6 @@ import ( "strings" "sync" - "github.com/ethereum/go-ethereum/rlp/internal/rlpstruct" "github.com/holiman/uint256" "github.com/ethereum/go-ethereum/rlp/internal/rlpstruct" @@ -259,6 +258,7 @@ func decodeU256(s *Stream, val reflect.Value) error { if err != nil { return wrapStreamError(err, val.Type()) } + return nil } @@ -893,7 +893,9 @@ func (s *Stream) decodeBigInt(dst *big.Int) error { // ReadUint256 decodes the next value as a uint256. func (s *Stream) ReadUint256(dst *uint256.Int) error { var buffer []byte + kind, size, err := s.Kind() + switch { case err != nil: return err @@ -926,6 +928,7 @@ func (s *Stream) ReadUint256(dst *uint256.Int) error { } // Set the integer bytes. dst.SetBytes(buffer) + return nil } diff --git a/rlp/decode_test.go b/rlp/decode_test.go index c93fef7991..cb0e1376e4 100644 --- a/rlp/decode_test.go +++ b/rlp/decode_test.go @@ -27,7 +27,6 @@ import ( "strings" "testing" - "github.com/ethereum/go-ethereum/common/math" "github.com/holiman/uint256" "github.com/ethereum/go-ethereum/common/math" @@ -1251,10 +1250,13 @@ func BenchmarkDecodeU256Ints(b *testing.B) { for i := range ints { ints[i], _ = uint256.FromBig(math.BigPow(2, int64(i))) } + enc, err := EncodeToBytes(ints) + if err != nil { b.Fatal(err) } + b.SetBytes(int64(len(enc))) b.ReportAllocs() b.ResetTimer() diff --git a/rlp/encbuffer.go b/rlp/encbuffer.go index 9ac4fcb2ca..a95490420b 100644 --- a/rlp/encbuffer.go +++ b/rlp/encbuffer.go @@ -179,8 +179,11 @@ func (w *encBuffer) writeUint256(z *uint256.Int) { w.writeUint64(z.Uint64()) return } + nBytes := byte((bitlen + 7) / 8) + var b [33]byte + binary.BigEndian.PutUint64(b[1:9], z[3]) binary.BigEndian.PutUint64(b[9:17], z[2]) binary.BigEndian.PutUint64(b[17:25], z[1]) diff --git a/rlp/encode.go b/rlp/encode.go index 06f6885615..a7896d8936 100644 --- a/rlp/encode.go +++ b/rlp/encode.go @@ -23,7 +23,6 @@ import ( "math/big" "reflect" - "github.com/ethereum/go-ethereum/rlp/internal/rlpstruct" "github.com/holiman/uint256" "github.com/ethereum/go-ethereum/rlp/internal/rlpstruct" @@ -219,13 +218,16 @@ func writeU256IntPtr(val reflect.Value, w *encBuffer) error { w.str = append(w.str, 0x80) return nil } + w.writeUint256(ptr) + return nil } func writeU256IntNoPtr(val reflect.Value, w *encBuffer) error { i := val.Interface().(uint256.Int) w.writeUint256(&i) + return nil } diff --git a/rlp/encode_test.go b/rlp/encode_test.go index 911fb5a3a9..46e7a8f8b7 100644 --- a/rlp/encode_test.go +++ b/rlp/encode_test.go @@ -26,7 +26,6 @@ import ( "sync" "testing" - "github.com/ethereum/go-ethereum/common/math" "github.com/holiman/uint256" "github.com/ethereum/go-ethereum/common/math" @@ -498,11 +497,11 @@ func TestEncodeToReaderReturnToPool(t *testing.T) { go func() { for i := 0; i < 1000; i++ { _, r, _ := EncodeToReader("foo") - io.ReadAll(r) - r.Read(buf) - r.Read(buf) - r.Read(buf) - r.Read(buf) + _, _ = io.ReadAll(r) + _, _ = r.Read(buf) + _, _ = r.Read(buf) + _, _ = r.Read(buf) + _, _ = r.Read(buf) } wg.Done() }() @@ -548,12 +547,15 @@ func BenchmarkEncodeU256Ints(b *testing.B) { for i := range ints { ints[i], _ = uint256.FromBig(math.BigPow(2, int64(i))) } + out := bytes.NewBuffer(make([]byte, 0, 4096)) + b.ResetTimer() b.ReportAllocs() for i := 0; i < b.N; i++ { out.Reset() + if err := Encode(out, ints); err != nil { b.Fatal(err) } diff --git a/rlp/raw_test.go b/rlp/raw_test.go index 3b4a528152..c5d81c618e 100644 --- a/rlp/raw_test.go +++ b/rlp/raw_test.go @@ -331,7 +331,9 @@ func TestBytesSize(t *testing.T) { if s != test.size { t.Errorf("BytesSize(%#x) -> %d, want %d", test.v, s, test.size) } + s = StringSize(string(test.v)) + if s != test.size { t.Errorf("StringSize(%#x) -> %d, want %d", test.v, s, test.size) } diff --git a/signer/core/apitypes/signed_data_internal_test.go b/signer/core/apitypes/signed_data_internal_test.go index 266bb98fce..340ef40ad8 100644 --- a/signer/core/apitypes/signed_data_internal_test.go +++ b/signer/core/apitypes/signed_data_internal_test.go @@ -123,14 +123,18 @@ func TestParseAddress(t *testing.T) { if err == nil { t.Errorf("test %d: expected error, got no error (result %x)", i, val) } + continue } + if err != nil { t.Errorf("test %d: expected no error, got %v", i, err) } + if have, want := len(val), 32; have != want { t.Errorf("test %d: have len %d, want %d", i, have, want) } + if !bytes.Equal(val, test.Output) { t.Errorf("test %d: want %x, have %x", i, test.Output, val) } @@ -205,8 +209,10 @@ func TestConvertStringDataToSlice(t *testing.T) { t.Parallel() slice := []string{"a", "b", "c"} + var it interface{} = slice _, err := convertDataToSlice(it) + if err != nil { t.Fatal(err) } @@ -220,8 +226,10 @@ func TestConvertUint256DataToSlice(t *testing.T) { math.NewHexOrDecimal256(2), math.NewHexOrDecimal256(3), } + var it interface{} = slice _, err := convertDataToSlice(it) + if err != nil { t.Fatal(err) } @@ -235,8 +243,10 @@ func TestConvertAddressDataToSlice(t *testing.T) { common.HexToAddress("0x0000000000000000000000000000000000000002"), common.HexToAddress("0x0000000000000000000000000000000000000003"), } + var it interface{} = slice _, err := convertDataToSlice(it) + if err != nil { t.Fatal(err) } diff --git a/signer/core/apitypes/types.go b/signer/core/apitypes/types.go index 8218e754d3..9963df6be7 100644 --- a/signer/core/apitypes/types.go +++ b/signer/core/apitypes/types.go @@ -251,11 +251,15 @@ func TypedDataAndHash(typedData TypedData) ([]byte, string, error) { if err != nil { return nil, "", err } + typedDataHash, err := typedData.HashStruct(typedData.PrimaryType, typedData.Message) + if err != nil { return nil, "", err } + rawData := fmt.Sprintf("\x19\x01%s%s", string(domainSeparator), string(typedDataHash)) + return crypto.Keccak256([]byte(rawData)), rawData, nil } @@ -412,6 +416,7 @@ func parseBytes(encType interface{}) ([]byte, bool) { if val.Kind() == reflect.Array && val.Type().Elem().Kind() == reflect.Uint8 { v := reflect.MakeSlice(reflect.TypeOf([]byte{}), val.Len(), val.Len()) reflect.Copy(v, val) + return v.Bytes(), true } @@ -490,6 +495,7 @@ func (typedData *TypedData) EncodePrimitiveValue(encType string, encValue interf switch encType { case "address": retval := make([]byte, 32) + switch val := encValue.(type) { case string: if common.IsHexAddress(val) { @@ -505,6 +511,7 @@ func (typedData *TypedData) EncodePrimitiveValue(encType string, encValue interf copy(retval[12:], val[:]) return retval, nil } + return nil, dataMismatchError(encType, encValue) case "bool": boolValue, ok := encValue.(bool) @@ -564,7 +571,9 @@ func dataMismatchError(encType string, encValue interface{}) error { func convertDataToSlice(encValue interface{}) ([]interface{}, error) { var outEncValue []interface{} + rv := reflect.ValueOf(encValue) + if rv.Kind() == reflect.Slice { for i := 0; i < rv.Len(); i++ { outEncValue = append(outEncValue, rv.Index(i).Interface()) @@ -572,6 +581,7 @@ func convertDataToSlice(encValue interface{}) ([]interface{}, error) { } else { return outEncValue, fmt.Errorf("provided data '%v' is not slice", encValue) } + return outEncValue, nil } @@ -720,6 +730,7 @@ func (t Types) validate() error { if typeKey == typeObj.Type { return fmt.Errorf("type %q cannot reference itself", typeObj.Type) } + if isPrimitiveTypeValid(typeObj.Type) { continue } @@ -727,6 +738,7 @@ func (t Types) validate() error { if _, exist := t[typeObj.typeName()]; !exist { return fmt.Errorf("reference type %q is undefined", typeObj.Type) } + if !typedDataReferenceTypeRegexp.MatchString(typeObj.Type) { return fmt.Errorf("unknown reference type %q", typeObj.Type) } @@ -763,6 +775,7 @@ func isPrimitiveTypeValid(primitiveType string) bool { if primitiveType == fmt.Sprintf("int%d", n) || primitiveType == fmt.Sprintf("int%d[]", n) { return true } + if primitiveType == fmt.Sprintf("uint%d", n) || primitiveType == fmt.Sprintf("uint%d[]", n) { return true } diff --git a/signer/fourbyte/fourbyte.go b/signer/fourbyte/fourbyte.go index f2256a28f0..fcaa055dd7 100644 --- a/signer/fourbyte/fourbyte.go +++ b/signer/fourbyte/fourbyte.go @@ -82,6 +82,7 @@ func NewWithFile(path string) (*Database, error) { // Custom file may not exist. Will be created during save, if needed. if _, err := os.Stat(path); err == nil { var blob []byte + if blob, err = os.ReadFile(path); err != nil { return nil, err } @@ -136,5 +137,6 @@ func (db *Database) AddSelector(selector string, data []byte) error { if err != nil { return err } + return os.WriteFile(db.customPath, blob, 0600) } diff --git a/tests/bor/bor_sprint_length_change_test.go b/tests/bor/bor_sprint_length_change_test.go index bab5170f86..fcc8766045 100644 --- a/tests/bor/bor_sprint_length_change_test.go +++ b/tests/bor/bor_sprint_length_change_test.go @@ -5,6 +5,7 @@ import ( "encoding/csv" "encoding/json" "fmt" + "github.com/ethereum/go-ethereum/core/txpool" "io/ioutil" // nolint: staticcheck _log "log" "math/big" @@ -35,7 +36,7 @@ var ( // Only this account is a validator for the 0th span keySprintLength, _ = crypto.HexToECDSA(privKeySprintLength) - // This account is one the validators for 1st span (0-indexed) + // This account is one of the validators for 1st span (0-indexed) keySprintLength2, _ = crypto.HexToECDSA(privKeySprintLength2) keysSprintLength = []*ecdsa.PrivateKey{keySprintLength, keySprintLength2} diff --git a/tests/fuzzers/difficulty/debug/main.go b/tests/fuzzers/difficulty/debug/main.go index 70cf092568..88e5696eaf 100644 --- a/tests/fuzzers/difficulty/debug/main.go +++ b/tests/fuzzers/difficulty/debug/main.go @@ -30,6 +30,7 @@ func main() { } crasher := os.Args[1] data, err := os.ReadFile(crasher) + if err != nil { fmt.Fprintf(os.Stderr, "error loading crasher %v: %v", crasher, err) os.Exit(1) diff --git a/tests/fuzzers/rlp/rlp_fuzzer.go b/tests/fuzzers/rlp/rlp_fuzzer.go index 79a1ac1ecb..44432f13d4 100644 --- a/tests/fuzzers/rlp/rlp_fuzzer.go +++ b/tests/fuzzers/rlp/rlp_fuzzer.go @@ -21,6 +21,8 @@ import ( "fmt" "math/big" + "github.com/holiman/uint256" + "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/rlp" ) diff --git a/trie/sync.go b/trie/sync.go index 4f55845991..201f3d626b 100644 --- a/trie/sync.go +++ b/trie/sync.go @@ -26,7 +26,7 @@ import ( "github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/ethdb" - "github.com/ethereum/go-ethereum/log" + "github.com/ethereum/go-ethereum/log" // nolint:typecheck ) // ErrNotRequested is returned by the trie sync when it's requested to process a