From 0f622f3829c15f2130ab56e14617264dafaa811c Mon Sep 17 00:00:00 2001 From: NathanBSC <122502194+NathanBSC@users.noreply.github.com> Date: Fri, 8 Sep 2023 16:36:16 +0800 Subject: [PATCH] lint: fix all golang lint after big merge (#1861) --- .golangci.yml | 19 +--- cmd/faucet/faucet.go | 2 +- cmd/geth/pruneblock_test.go | 10 +- cmd/maliciousvote-submit/main.go | 1 - consensus/ethash/ethash.go | 1 - consensus/parlia/parlia_test.go | 2 - core/blockchain_diff_test.go | 58 ---------- core/blockchain_insert.go | 2 + core/blockchain_test.go | 3 +- core/forkid/forkid.go | 1 - core/rawdb/freezer.go | 2 +- core/rawdb/freezer_test.go | 2 +- core/rawdb/schema.go | 6 - core/state/pruner/pruner.go | 1 - core/state/trie_prefetcher.go | 1 - core/state_prefetcher.go | 1 - core/systemcontracts/upgrade.go | 1 - core/txpool/legacypool/legacypool.go | 3 +- core/vm/lightclient/v1/types.go | 1 - core/vote/vote_pool_test.go | 3 +- core/vote/vote_signer.go | 4 +- crypto/bn256/cloudflare/optate.go | 3 +- eth/catalyst/api.go | 143 ------------------------ eth/catalyst/queue.go | 158 --------------------------- eth/downloader/downloader.go | 48 ++------ eth/filters/api.go | 1 - eth/filters/filter_system.go | 21 ++-- eth/filters/filter_system_test.go | 1 - eth/handler_bsc_test.go | 1 - eth/handler_eth_test.go | 1 - eth/peer.go | 5 - eth/state_accessor.go | 2 +- internal/ethapi/api.go | 27 +++++ internal/ethapi/api_test.go | 1 + miner/worker.go | 1 - trie/secure_trie.go | 4 +- 36 files changed, 67 insertions(+), 474 deletions(-) delete mode 100644 eth/catalyst/api.go delete mode 100644 eth/catalyst/queue.go diff --git a/.golangci.yml b/.golangci.yml index f1db3bf8a..e7d4e36d7 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -48,24 +48,11 @@ linters-settings: issues: exclude-rules: - - path: crypto/blake2b/ + - path: core/state/metrics.go linters: - - deadcode - - path: crypto/bn256/cloudflare + - unused + - path: core/txpool/legacypool/list.go linters: - - deadcode - - path: p2p/discv5/ - linters: - - deadcode - - path: core/vm/instructions_test.go - linters: - - goconst - - path: cmd/faucet/ - linters: - - deadcode - - path: crypto/bn256/cloudflare/optate.go - linters: - - deadcode - staticcheck - path: internal/build/pgp.go text: 'SA1019: "golang.org/x/crypto/openpgp" is deprecated: this package is unmaintained except for security fixes.' diff --git a/cmd/faucet/faucet.go b/cmd/faucet/faucet.go index ba2e45fe1..e4f19892a 100644 --- a/cmd/faucet/faucet.go +++ b/cmd/faucet/faucet.go @@ -148,7 +148,7 @@ func main() { } } website := new(bytes.Buffer) - err := template.Must(template.New("").Parse(string(websiteTmpl))).Execute(website, map[string]interface{}{ + err := template.Must(template.New("").Parse(websiteTmpl)).Execute(website, map[string]interface{}{ "Network": *netnameFlag, "Amounts": amounts, "Recaptcha": *captchaToken, diff --git a/cmd/geth/pruneblock_test.go b/cmd/geth/pruneblock_test.go index 8b19d3e68..a20ba01fa 100644 --- a/cmd/geth/pruneblock_test.go +++ b/cmd/geth/pruneblock_test.go @@ -20,7 +20,6 @@ import ( "bytes" "encoding/hex" "fmt" - "io/ioutil" "math/big" "os" "path/filepath" @@ -70,11 +69,7 @@ func TestOfflineBlockPrune(t *testing.T) { } func testOfflineBlockPruneWithAmountReserved(t *testing.T, amountReserved uint64) { - datadir, err := ioutil.TempDir("", "") - if err != nil { - t.Fatalf("Failed to create temporary datadir: %v", err) - } - os.RemoveAll(datadir) + datadir := t.TempDir() chaindbPath := filepath.Join(datadir, "chaindata") oldAncientPath := filepath.Join(chaindbPath, "ancient") @@ -86,9 +81,6 @@ func testOfflineBlockPruneWithAmountReserved(t *testing.T, amountReserved uint64 //Initialize a block pruner for pruning, only remain amountReserved blocks backward. testBlockPruner := pruner.NewBlockPruner(db, node, oldAncientPath, newAncientPath, amountReserved) - if err != nil { - t.Fatalf("failed to make new blockpruner: %v", err) - } if err := testBlockPruner.BlockPruneBackUp(chaindbPath, 512, utils.MakeDatabaseHandles(0), "", false, false); err != nil { t.Fatalf("Failed to back up block: %v", err) } diff --git a/cmd/maliciousvote-submit/main.go b/cmd/maliciousvote-submit/main.go index 0383a34a4..c83610710 100644 --- a/cmd/maliciousvote-submit/main.go +++ b/cmd/maliciousvote-submit/main.go @@ -134,5 +134,4 @@ func main() { fmt.Fprintln(os.Stderr, err) os.Exit(1) } - } diff --git a/consensus/ethash/ethash.go b/consensus/ethash/ethash.go index 3c3390bcb..f37ec2605 100644 --- a/consensus/ethash/ethash.go +++ b/consensus/ethash/ethash.go @@ -20,7 +20,6 @@ package ethash import ( "time" - "github.com/ethereum/go-ethereum/consensus" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/rpc" diff --git a/consensus/parlia/parlia_test.go b/consensus/parlia/parlia_test.go index 6442f1646..1d4389868 100644 --- a/consensus/parlia/parlia_test.go +++ b/consensus/parlia/parlia_test.go @@ -125,7 +125,6 @@ func simulateValidatorOutOfService(totalValidators int, downValidators int) { } func producerBlockDelay(candidates map[int]bool, height, numOfValidators int) (int, uint64) { - s := rand.NewSource(int64(height)) r := rand.New(s) n := numOfValidators @@ -220,7 +219,6 @@ func (b *MockBlock) GetJustifiedNumberAndHash() (uint64, common.Hash, error) { justifiedBlock = curBlock.parent break } - } return justifiedBlock.blockNumber, justifiedBlock.blockHash, nil diff --git a/core/blockchain_diff_test.go b/core/blockchain_diff_test.go index e283953a7..8ec14bce4 100644 --- a/core/blockchain_diff_test.go +++ b/core/blockchain_diff_test.go @@ -26,8 +26,6 @@ import ( "testing" "time" - "golang.org/x/crypto/sha3" - "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/consensus/ethash" "github.com/ethereum/go-ethereum/core/rawdb" @@ -37,7 +35,6 @@ import ( "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb/memorydb" "github.com/ethereum/go-ethereum/params" - "github.com/ethereum/go-ethereum/rlp" ) var ( @@ -45,40 +42,12 @@ var ( testKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") contractCode, _ = hex.DecodeString("608060405260016000806101000a81548160ff02191690831515021790555034801561002a57600080fd5b506101688061003a6000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c806389a2d8011461003b578063b0483f4814610059575b600080fd5b610043610075565b60405161005091906100f4565b60405180910390f35b610073600480360381019061006e91906100bc565b61008b565b005b60008060009054906101000a900460ff16905090565b806000806101000a81548160ff02191690831515021790555050565b6000813590506100b68161011b565b92915050565b6000602082840312156100ce57600080fd5b60006100dc848285016100a7565b91505092915050565b6100ee8161010f565b82525050565b600060208201905061010960008301846100e5565b92915050565b60008115159050919050565b6101248161010f565b811461012f57600080fd5b5056fea264697066735822122092f788b569bfc3786e90601b5dbec01cfc3d76094164fd66ca7d599c4239fc5164736f6c63430008000033") contractAddr = common.HexToAddress("0xe74a3c7427cda785e0000d42a705b1f3fd371e09") - contractSlot = common.HexToHash("0x290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563") contractData1, _ = hex.DecodeString("b0483f480000000000000000000000000000000000000000000000000000000000000000") contractData2, _ = hex.DecodeString("b0483f480000000000000000000000000000000000000000000000000000000000000001") commonGas = 192138 // testAddr is the Ethereum address of the tester account. testAddr = crypto.PubkeyToAddress(testKey.PublicKey) - checkBlocks = map[int]checkBlockParam{ - 12: { - txs: []checkTransactionParam{ - { - to: &contractAddr, - slot: contractSlot, - value: []byte{01}, - }, - }}, - - 13: { - txs: []checkTransactionParam{ - { - to: &contractAddr, - slot: contractSlot, - value: []byte{}, - }, - }}, - 14: { - txs: []checkTransactionParam{ - { - to: &contractAddr, - slot: contractSlot, - value: []byte{01}, - }, - }}, - } // testBlocks is the test parameters array for specific blocks. testBlocks = []testBlockParam{ { @@ -175,16 +144,6 @@ type testBlockParam struct { txs []testTransactionParam } -type checkTransactionParam struct { - to *common.Address - slot common.Hash - value []byte -} - -type checkBlockParam struct { - txs []checkTransactionParam -} - // testBackend is a mock implementation of the live Ethereum message handler. Its // purpose is to allow testing the request/reply workflows and wire serialization // in the `eth` protocol without actually doing any data processing. @@ -251,7 +210,6 @@ func newTestBackendWithGenerator(blocks int, lightProcess bool) *testBackend { } } } - } bs, _ := GenerateChain(params.TestChainConfig, chain.Genesis(), ethash.NewFaker(), db, blocks, generator) if _, err := chain.InsertChain(bs); err != nil { @@ -271,21 +229,6 @@ func (b *testBackend) close() { func (b *testBackend) Chain() *BlockChain { return b.chain } -func rawDataToDiffLayer(data rlp.RawValue) (*types.DiffLayer, error) { - var diff types.DiffLayer - hasher := sha3.NewLegacyKeccak256() - err := rlp.DecodeBytes(data, &diff) - if err != nil { - return nil, err - } - hasher.Write(data) - var diffHash common.Hash - hasher.Sum(diffHash[:0]) - diff.DiffHash.Store(diffHash) - hasher.Reset() - return &diff, nil -} - func TestFreezeDiffLayer(t *testing.T) { blockNum := 1024 fullBackend := newTestBackend(blockNum, true) @@ -366,7 +309,6 @@ func newTwoForkedBlockchains(len1, len2 int) (chain1 *BlockChain, chain2 *BlockC } } } - } bs1, _ := GenerateChain(params.TestChainConfig, chain1.Genesis(), ethash.NewFaker(), db1, len1, generator1) if _, err := chain1.InsertChain(bs1); err != nil { diff --git a/core/blockchain_insert.go b/core/blockchain_insert.go index 8f496e182..3e913cb87 100644 --- a/core/blockchain_insert.go +++ b/core/blockchain_insert.go @@ -129,6 +129,8 @@ func (it *insertIterator) next() (*types.Block, error) { // // Both header and body validation errors (nil too) is cached into the iterator // to avoid duplicating work on the following next() call. +// +// nolint:unused func (it *insertIterator) peek() (*types.Block, error) { // If we reached the end of the chain, abort if it.index+1 >= len(it.chain) { diff --git a/core/blockchain_test.go b/core/blockchain_test.go index 3ebc12d68..56a5c7763 100644 --- a/core/blockchain_test.go +++ b/core/blockchain_test.go @@ -339,8 +339,8 @@ func testInsertAfterMerge(t *testing.T, blockchain *BlockChain, i, n int, full b // with various length chains. func TestExtendCanonicalHeaders(t *testing.T) { testExtendCanonical(t, false, false) - } + func TestExtendCanonicalBlocks(t *testing.T) { testExtendCanonical(t, true, false) testExtendCanonical(t, true, true) @@ -452,7 +452,6 @@ func TestLongerForkHeaders(t *testing.T) { func TestLongerForkBlocks(t *testing.T) { testLongerFork(t, true, false) testLongerFork(t, true, true) - } func testLongerFork(t *testing.T, full, pipeline bool) { diff --git a/core/forkid/forkid.go b/core/forkid/forkid.go index b5be72ab4..896455884 100644 --- a/core/forkid/forkid.go +++ b/core/forkid/forkid.go @@ -98,7 +98,6 @@ func NewID(config *params.ChainConfig, genesis common.Hash, head, time uint64) I return ID{Hash: checksumToBytes(hash), Next: 0} } - // NewIDWithChain calculates the Ethereum fork ID from an existing chain instance. func NewIDWithChain(chain Blockchain) ID { head := chain.CurrentHeader() diff --git a/core/rawdb/freezer.go b/core/rawdb/freezer.go index 525c96593..0a7bbe436 100644 --- a/core/rawdb/freezer.go +++ b/core/rawdb/freezer.go @@ -159,7 +159,7 @@ func NewFreezer(datadir string, namespace string, readonly bool, offset uint64, // Create the write batch. freezer.writeBatch = newFreezerBatch(freezer) - log.Info("Opened ancient database", "database", datadir, "readonly", readonly, "frozen", freezer.frozen) + log.Info("Opened ancient database", "database", datadir, "readonly", readonly, "frozen", freezer.frozen.Load()) return freezer, nil } diff --git a/core/rawdb/freezer_test.go b/core/rawdb/freezer_test.go index 286b96976..cf53c5d96 100644 --- a/core/rawdb/freezer_test.go +++ b/core/rawdb/freezer_test.go @@ -277,7 +277,7 @@ func TestFreezerReadonlyValidate(t *testing.T) { // Re-openening as readonly should fail when validating // table lengths. - f, err = NewFreezer(dir, "", true, 0, 2049, tables) + _, err = NewFreezer(dir, "", true, 0, 2049, tables) if err == nil { t.Fatal("readonly freezer should fail with differing table lengths") } diff --git a/core/rawdb/schema.go b/core/rawdb/schema.go index 2be4970e4..1de8500ac 100644 --- a/core/rawdb/schema.go +++ b/core/rawdb/schema.go @@ -40,9 +40,6 @@ var ( // headFastBlockKey tracks the latest known incomplete block's hash during fast sync. headFastBlockKey = []byte("LastFast") - // headFinalizedBlockKey tracks the latest known finalized block hash. - headFinalizedBlockKey = []byte("LastFinalized") - // persistentStateIDKey tracks the id of latest stored state(for path-based only). persistentStateIDKey = []byte("LastStateID") @@ -70,9 +67,6 @@ var ( // snapshotSyncStatusKey tracks the snapshot sync status across restarts. snapshotSyncStatusKey = []byte("SnapshotSyncStatus") - // skeletonSyncStatusKey tracks the skeleton sync status across restarts. - skeletonSyncStatusKey = []byte("SkeletonSyncStatus") - // trieJournalKey tracks the in-memory trie node layers across restarts. trieJournalKey = []byte("TrieJournal") diff --git a/core/state/pruner/pruner.go b/core/state/pruner/pruner.go index 615435031..09d87945c 100644 --- a/core/state/pruner/pruner.go +++ b/core/state/pruner/pruner.go @@ -450,7 +450,6 @@ func (p *BlockPruner) backUpOldDb(name string, cache, handles int, namespace str // Backup the ancient data for the old ancient db, i.e. the most recent 128 blocks in ancient db. func (p *BlockPruner) BlockPruneBackUp(name string, cache, handles int, namespace string, readonly, interrupt bool) error { - start := time.Now() if err := p.backUpOldDb(name, cache, handles, namespace, readonly, interrupt); err != nil { diff --git a/core/state/trie_prefetcher.go b/core/state/trie_prefetcher.go index 400ca62be..4184369d9 100644 --- a/core/state/trie_prefetcher.go +++ b/core/state/trie_prefetcher.go @@ -192,7 +192,6 @@ func (p *triePrefetcher) mainLoop() { } fetcher.lock.Unlock() p.storageWasteMeter.Mark(int64(len(fetcher.seen))) - } } } diff --git a/core/state_prefetcher.go b/core/state_prefetcher.go index 322be3aef..f1bb60feb 100644 --- a/core/state_prefetcher.go +++ b/core/state_prefetcher.go @@ -138,7 +138,6 @@ func (p *statePrefetcher) PrefetchMining(txs TransactionsByPriceAndNonce, header return default: if count++; count%checkInterval == 0 { - txset.Forward(*txCurr) } tx := txset.PeekWithUnwrap() diff --git a/core/systemcontracts/upgrade.go b/core/systemcontracts/upgrade.go index e815c2d01..d644533a3 100644 --- a/core/systemcontracts/upgrade.go +++ b/core/systemcontracts/upgrade.go @@ -60,7 +60,6 @@ var ( ) func init() { - // For contract upgrades, the following information is from `bsc-genesis-contract`, to be specifically, // 1) `CommitUrl` is the specific git commit, based on which the byte code is compiled from; // 2) `Code` is the byte code of the contract, which is generated by compiling `bsc-genesis-contract`. diff --git a/core/txpool/legacypool/legacypool.go b/core/txpool/legacypool/legacypool.go index b363358b5..2c23f142b 100644 --- a/core/txpool/legacypool/legacypool.go +++ b/core/txpool/legacypool/legacypool.go @@ -415,7 +415,7 @@ func (pool *LegacyPool) loop() { }() pool.mu.RUnlock() if len(reannoTxs) > 0 { - pool.reannoTxFeed.Send(core.ReannoTxsEvent{reannoTxs}) + pool.reannoTxFeed.Send(core.ReannoTxsEvent{Txs: reannoTxs}) } // Handle local transaction journal rotation @@ -427,7 +427,6 @@ func (pool *LegacyPool) loop() { } pool.mu.Unlock() } - } } } diff --git a/core/vm/lightclient/v1/types.go b/core/vm/lightclient/v1/types.go index 1c15db6df..54b2b913b 100644 --- a/core/vm/lightclient/v1/types.go +++ b/core/vm/lightclient/v1/types.go @@ -36,7 +36,6 @@ type ConsensusState struct { // | chainID | height | appHash | curValidatorSetHash | [{validator pubkey, voting power}] | // | 32 bytes | 8 bytes | 32 bytes | 32 bytes | [{32 bytes, 8 bytes}] | func DecodeConsensusState(input []byte) (ConsensusState, error) { - minimumLength := chainIDLength + heightLength + appHashLength + validatorSetHashLength singleValidatorBytesLength := validatorPubkeyLength + validatorVotingPowerLength diff --git a/core/vote/vote_pool_test.go b/core/vote/vote_pool_test.go index 60958b2b4..44d603725 100644 --- a/core/vote/vote_pool_test.go +++ b/core/vote/vote_pool_test.go @@ -21,7 +21,6 @@ import ( "context" "encoding/json" "fmt" - "io/ioutil" "math/big" "os" "path/filepath" @@ -415,7 +414,7 @@ func setUpKeyManager(t *testing.T) (string, string) { if err := os.MkdirAll(filepath.Dir(walletPasswordDir), 0700); err != nil { t.Fatalf("failed to create walletPassword dir: %v", err) } - if err := ioutil.WriteFile(walletPasswordDir, []byte(password), 0600); err != nil { + if err := os.WriteFile(walletPasswordDir, []byte(password), 0600); err != nil { t.Fatalf("failed to write wallet password dir: %v", err) } diff --git a/core/vote/vote_signer.go b/core/vote/vote_signer.go index 86f277c37..8153f40ca 100644 --- a/core/vote/vote_signer.go +++ b/core/vote/vote_signer.go @@ -3,7 +3,7 @@ package vote import ( "context" "fmt" - "io/ioutil" + "os" "time" "github.com/pkg/errors" @@ -41,7 +41,7 @@ func NewVoteSigner(blsPasswordPath, blsWalletPath string) (*VoteSigner, error) { return nil, fmt.Errorf("BLS wallet did not exists") } - walletPassword, err := ioutil.ReadFile(blsPasswordPath) + walletPassword, err := os.ReadFile(blsPasswordPath) if err != nil { log.Error("Read BLS wallet password", "err", err) return nil, err diff --git a/crypto/bn256/cloudflare/optate.go b/crypto/bn256/cloudflare/optate.go index b71e50e3a..e8caa7a08 100644 --- a/crypto/bn256/cloudflare/optate.go +++ b/crypto/bn256/cloudflare/optate.go @@ -199,9 +199,8 @@ func miller(q *twistPoint, p *curvePoint) *gfP12 { r = newR r2.Square(&minusQ2.y) - a, b, c, newR = lineFunctionAdd(r, minusQ2, bAffine, r2) + a, b, c, _ = lineFunctionAdd(r, minusQ2, bAffine, r2) mulLine(ret, a, b, c) - r = newR return ret } diff --git a/eth/catalyst/api.go b/eth/catalyst/api.go deleted file mode 100644 index eadd76154..000000000 --- a/eth/catalyst/api.go +++ /dev/null @@ -1,143 +0,0 @@ -// Copyright 2021 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -// Package catalyst implements the temporary eth1/eth2 RPC integration. -package catalyst - -import ( - "sync" - "time" - - "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/core/types" - "github.com/ethereum/go-ethereum/eth" - "github.com/ethereum/go-ethereum/log" - "github.com/ethereum/go-ethereum/node" - "github.com/ethereum/go-ethereum/rpc" -) - -// Register adds the engine API to the full node. -func Register(stack *node.Node, backend *eth.Ethereum) error { - log.Warn("Engine API enabled", "protocol", "eth") - stack.RegisterAPIs([]rpc.API{ - { - Namespace: "engine", - Service: NewConsensusAPI(backend), - Authenticated: true, - }, - }) - return nil -} - -const ( - // invalidBlockHitEviction is the number of times an invalid block can be - // referenced in forkchoice update or new payload before it is attempted - // to be reprocessed again. - invalidBlockHitEviction = 128 - - // invalidTipsetsCap is the max number of recent block hashes tracked that - // have lead to some bad ancestor block. It's just an OOM protection. - invalidTipsetsCap = 512 - - // beaconUpdateStartupTimeout is the time to wait for a beacon client to get - // attached before starting to issue warnings. - beaconUpdateStartupTimeout = 30 * time.Second - - // beaconUpdateConsensusTimeout is the max time allowed for a beacon client - // to send a consensus update before it's considered offline and the user is - // warned. - beaconUpdateConsensusTimeout = 2 * time.Minute - - // beaconUpdateWarnFrequency is the frequency at which to warn the user that - // the beacon client is offline. - beaconUpdateWarnFrequency = 5 * time.Minute -) - -// All methods provided over the engine endpoint. -var caps = []string{ - "engine_forkchoiceUpdatedV1", - "engine_forkchoiceUpdatedV2", - "engine_exchangeTransitionConfigurationV1", - "engine_getPayloadV1", - "engine_getPayloadV2", - "engine_getPayloadV3", - "engine_newPayloadV1", - "engine_newPayloadV2", - "engine_newPayloadV3", - "engine_getPayloadBodiesByHashV1", - "engine_getPayloadBodiesByRangeV1", -} - -type ConsensusAPI struct { - eth *eth.Ethereum - - remoteBlocks *headerQueue // Cache of remote payloads received - localBlocks *payloadQueue // Cache of local payloads generated - - // The forkchoice update and new payload method require us to return the - // latest valid hash in an invalid chain. To support that return, we need - // to track historical bad blocks as well as bad tipsets in case a chain - // is constantly built on it. - // - // There are a few important caveats in this mechanism: - // - The bad block tracking is ephemeral, in-memory only. We must never - // persist any bad block information to disk as a bug in Geth could end - // up blocking a valid chain, even if a later Geth update would accept - // it. - // - Bad blocks will get forgotten after a certain threshold of import - // attempts and will be retried. The rationale is that if the network - // really-really-really tries to feed us a block, we should give it a - // new chance, perhaps us being racey instead of the block being legit - // bad (this happened in Geth at a point with import vs. pending race). - // - Tracking all the blocks built on top of the bad one could be a bit - // problematic, so we will only track the head chain segment of a bad - // chain to allow discarding progressing bad chains and side chains, - // without tracking too much bad data. - invalidBlocksHits map[common.Hash]int // Ephemeral cache to track invalid blocks and their hit count - invalidTipsets map[common.Hash]*types.Header // Ephemeral cache to track invalid tipsets and their bad ancestor - invalidLock sync.Mutex // Protects the invalid maps from concurrent access - - // Geth can appear to be stuck or do strange things if the beacon client is - // offline or is sending us strange data. Stash some update stats away so - // that we can warn the user and not have them open issues on our tracker. - lastTransitionUpdate time.Time - lastTransitionLock sync.Mutex - lastForkchoiceUpdate time.Time - lastForkchoiceLock sync.Mutex - lastNewPayloadUpdate time.Time - lastNewPayloadLock sync.Mutex - - forkchoiceLock sync.Mutex // Lock for the forkChoiceUpdated method - newPayloadLock sync.Mutex // Lock for the NewPayload method -} - -// NewConsensusAPI creates a new consensus api for the given backend. -// The underlying blockchain needs to have a valid terminal total difficulty set. -func NewConsensusAPI(eth *eth.Ethereum) *ConsensusAPI { - if eth.BlockChain().Config().TerminalTotalDifficulty == nil { - log.Warn("Engine API started but chain not configured for merge yet") - } - api := &ConsensusAPI{ - eth: eth, - remoteBlocks: newHeaderQueue(), - localBlocks: newPayloadQueue(), - invalidBlocksHits: make(map[common.Hash]int), - invalidTipsets: make(map[common.Hash]*types.Header), - } - // eth.Downloader().SetBadBlockCallback(api.setInvalidAncestor) - - return api -} diff --git a/eth/catalyst/queue.go b/eth/catalyst/queue.go deleted file mode 100644 index 634dc1b2e..000000000 --- a/eth/catalyst/queue.go +++ /dev/null @@ -1,158 +0,0 @@ -// Copyright 2022 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package catalyst - -import ( - "sync" - - "github.com/ethereum/go-ethereum/beacon/engine" - "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/core/types" - "github.com/ethereum/go-ethereum/miner" -) - -// maxTrackedPayloads is the maximum number of prepared payloads the execution -// engine tracks before evicting old ones. Ideally we should only ever track the -// latest one; but have a slight wiggle room for non-ideal conditions. -const maxTrackedPayloads = 10 - -// maxTrackedHeaders is the maximum number of executed payloads the execution -// engine tracks before evicting old ones. These are tracked outside the chain -// during initial sync to allow ForkchoiceUpdate to reference past blocks via -// hashes only. For the sync target it would be enough to track only the latest -// header, but snap sync also needs the latest finalized height for the ancient -// limit. -const maxTrackedHeaders = 96 - -// payloadQueueItem represents an id->payload tuple to store until it's retrieved -// or evicted. -type payloadQueueItem struct { - id engine.PayloadID - payload *miner.Payload -} - -// payloadQueue tracks the latest handful of constructed payloads to be retrieved -// by the beacon chain if block production is requested. -type payloadQueue struct { - payloads []*payloadQueueItem - lock sync.RWMutex -} - -// newPayloadQueue creates a pre-initialized queue with a fixed number of slots -// all containing empty items. -func newPayloadQueue() *payloadQueue { - return &payloadQueue{ - payloads: make([]*payloadQueueItem, maxTrackedPayloads), - } -} - -// put inserts a new payload into the queue at the given id. -func (q *payloadQueue) put(id engine.PayloadID, payload *miner.Payload) { - q.lock.Lock() - defer q.lock.Unlock() - - copy(q.payloads[1:], q.payloads) - q.payloads[0] = &payloadQueueItem{ - id: id, - payload: payload, - } -} - -// get retrieves a previously stored payload item or nil if it does not exist. -func (q *payloadQueue) get(id engine.PayloadID, full bool) *engine.ExecutionPayloadEnvelope { - q.lock.RLock() - defer q.lock.RUnlock() - - for _, item := range q.payloads { - if item == nil { - return nil // no more items - } - if item.id == id { - if !full { - return item.payload.Resolve() - } - return item.payload.ResolveFull() - } - } - return nil -} - -// has checks if a particular payload is already tracked. -func (q *payloadQueue) has(id engine.PayloadID) bool { - q.lock.RLock() - defer q.lock.RUnlock() - - for _, item := range q.payloads { - if item == nil { - return false - } - if item.id == id { - return true - } - } - return false -} - -// headerQueueItem represents an hash->header tuple to store until it's retrieved -// or evicted. -type headerQueueItem struct { - hash common.Hash - header *types.Header -} - -// headerQueue tracks the latest handful of constructed headers to be retrieved -// by the beacon chain if block production is requested. -type headerQueue struct { - headers []*headerQueueItem - lock sync.RWMutex -} - -// newHeaderQueue creates a pre-initialized queue with a fixed number of slots -// all containing empty items. -func newHeaderQueue() *headerQueue { - return &headerQueue{ - headers: make([]*headerQueueItem, maxTrackedHeaders), - } -} - -// put inserts a new header into the queue at the given hash. -func (q *headerQueue) put(hash common.Hash, data *types.Header) { - q.lock.Lock() - defer q.lock.Unlock() - - copy(q.headers[1:], q.headers) - q.headers[0] = &headerQueueItem{ - hash: hash, - header: data, - } -} - -// get retrieves a previously stored header item or nil if it does not exist. -func (q *headerQueue) get(hash common.Hash) *types.Header { - q.lock.RLock() - defer q.lock.RUnlock() - - for _, item := range q.headers { - if item == nil { - return nil // no more items - } - if item.hash == hash { - return item.header - } - } - return nil -} diff --git a/eth/downloader/downloader.go b/eth/downloader/downloader.go index 3a34f3291..a6cadb057 100644 --- a/eth/downloader/downloader.go +++ b/eth/downloader/downloader.go @@ -47,9 +47,6 @@ var ( MaxReceiptFetch = 256 // Amount of transaction receipts to allow fetching per request MaxStateFetch = 384 // Amount of node state values to allow fetching per request - diffFetchTick = 10 * time.Millisecond - diffFetchLimit = 5 - maxQueuedHeaders = 32 * 1024 // [eth/62] Maximum number of headers to queue for import (DOS protection) maxHeadersProcess = 2048 // Number of header download results to import at once into the chain maxResultsProcess = 2048 // Number of content download results to import at once into the chain @@ -89,10 +86,6 @@ var ( // peerDropFn is a callback type for dropping a peer detected as malicious. type peerDropFn func(id string) -// badBlockFn is a callback for the async beacon sync to notify the caller that -// the origin header requested to sync to, produced a chain with a bad block. -type badBlockFn func(invalid *types.Header, origin *types.Header) - // headerTask is a set of downloaded headers to queue along with their precomputed // hashes to avoid constant rehashing. type headerTask struct { @@ -120,7 +113,6 @@ type Downloader struct { // Callbacks dropPeer peerDropFn // Drops a peer for misbehaving - badBlock badBlockFn // Reports a block as rejected by the chain // Status synchroniseMock func(id string, hash common.Hash) error // Replacement for synchronise during testing @@ -783,14 +775,16 @@ func (d *Downloader) findAncestor(p *peerConnection, localHeight uint64, remoteH remoteHeight = remoteHeader.Number.Uint64() ) mode := d.getMode() - switch mode { - case FullSync: - localHeight = d.blockchain.CurrentBlock().Number.Uint64() - case SnapSync: - localHeight = d.blockchain.CurrentSnapBlock().Number.Uint64() - default: - localHeight = d.lightchain.CurrentHeader().Number.Uint64() - } + /* + switch mode { + case FullSync: + localHeight = d.blockchain.CurrentBlock().Number.Uint64() + case SnapSync: + localHeight = d.blockchain.CurrentSnapBlock().Number.Uint64() + default: + localHeight = d.lightchain.CurrentHeader().Number.Uint64() + } + */ p.log.Debug("Looking for common ancestor", "local", localHeight, "remote", remoteHeight) // Recap floor value for binary search @@ -1689,28 +1683,6 @@ func (d *Downloader) DeliverSnapPacket(peer *snap.Peer, packet snap.Packet) erro } } -// readHeaderRange returns a list of headers, using the given last header as the base, -// and going backwards towards genesis. This method assumes that the caller already has -// placed a reasonable cap on count. -func (d *Downloader) readHeaderRange(last *types.Header, count int) []*types.Header { - var ( - current = last - headers []*types.Header - ) - for { - parent := d.lightchain.GetHeaderByHash(current.ParentHash) - if parent == nil { - break // The chain is not continuous, or the chain is exhausted - } - headers = append(headers, parent) - if len(headers) >= count { - break - } - current = parent - } - return headers -} - // reportSnapSyncProgress calculates various status reports and provides it to the user. func (d *Downloader) reportSnapSyncProgress(force bool) { // Initialize the sync start time if it's the first time we're reporting diff --git a/eth/filters/api.go b/eth/filters/api.go index 7e9a01ab9..09b92b710 100644 --- a/eth/filters/api.go +++ b/eth/filters/api.go @@ -390,7 +390,6 @@ func (api *FilterAPI) Logs(ctx context.Context, crit FilterCriteria) (*rpc.Subsc } gopool.Submit(func() { - for { select { case logs := <-matchedLogs: diff --git a/eth/filters/filter_system.go b/eth/filters/filter_system.go index e1af8e8e5..7e1506450 100644 --- a/eth/filters/filter_system.go +++ b/eth/filters/filter_system.go @@ -190,17 +190,16 @@ const ( ) type subscription struct { - id rpc.ID - typ Type - created time.Time - logsCrit ethereum.FilterQuery - logs chan []*types.Log - txs chan []*types.Transaction - headers chan *types.Header - finalizedHeaders chan *types.Header - votes chan *types.VoteEnvelope - installed chan struct{} // closed when the filter is installed - err chan error // closed when the filter is uninstalled + id rpc.ID + typ Type + created time.Time + logsCrit ethereum.FilterQuery + logs chan []*types.Log + txs chan []*types.Transaction + headers chan *types.Header + votes chan *types.VoteEnvelope + installed chan struct{} // closed when the filter is installed + err chan error // closed when the filter is uninstalled } // EventSystem creates subscriptions, processes events and broadcasts them to the diff --git a/eth/filters/filter_system_test.go b/eth/filters/filter_system_test.go index aefbf27e7..ec4fe30d1 100644 --- a/eth/filters/filter_system_test.go +++ b/eth/filters/filter_system_test.go @@ -43,7 +43,6 @@ import ( ) type testBackend struct { - mux *event.TypeMux db ethdb.Database sections uint64 txFeed event.Feed diff --git a/eth/handler_bsc_test.go b/eth/handler_bsc_test.go index d10d3fb11..b807fa626 100644 --- a/eth/handler_bsc_test.go +++ b/eth/handler_bsc_test.go @@ -161,7 +161,6 @@ func testSendVotes(t *testing.T, protocol uint) { t.Errorf("missing vote: %x", vote.Hash()) } } - } func TestRecvVotes67(t *testing.T) { testRecvVotes(t, eth.ETH67) } diff --git a/eth/handler_eth_test.go b/eth/handler_eth_test.go index 82f7c4be6..af07c926f 100644 --- a/eth/handler_eth_test.go +++ b/eth/handler_eth_test.go @@ -865,5 +865,4 @@ func TestOptionMaxPeersPerIP(t *testing.T) { tryFunc(tryNum, "[2001:db8::11]:", "[2001:db8::22]:", true, doneCh5) } close(doneCh5) - } diff --git a/eth/peer.go b/eth/peer.go index 35e480acd..dd6724219 100644 --- a/eth/peer.go +++ b/eth/peer.go @@ -17,8 +17,6 @@ package eth import ( - "time" - "github.com/ethereum/go-ethereum/eth/protocols/bsc" "github.com/ethereum/go-ethereum/eth/protocols/trust" @@ -38,9 +36,6 @@ type ethPeer struct { snapExt *snapPeer // Satellite `snap` connection trustExt *trustPeer bscExt *bscPeer // Satellite `bsc` connection - - syncDrop *time.Timer // Connection dropper if `eth` sync progress isn't validated in time - snapWait chan struct{} // Notification channel for snap connections } // info gathers and returns some `eth` protocol metadata known about a peer. diff --git a/eth/state_accessor.go b/eth/state_accessor.go index 1e2bce6f8..503d63e4a 100644 --- a/eth/state_accessor.go +++ b/eth/state_accessor.go @@ -176,7 +176,7 @@ func (eth *Ethereum) StateAtBlock(ctx context.Context, block *types.Block, reexe return nil, nil, fmt.Errorf("stateAtBlock commit failed, number %d root %v: %w", current.NumberU64(), current.Root().Hex(), err) } - statedb, err = state.New(root, database, nil) + statedb, err = state.New(root, database, nil) // nolint:staticcheck if err != nil { return nil, nil, fmt.Errorf("state reset after block %d failed: %v", current.NumberU64(), err) } diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index 7ee99bfec..810e8bba6 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -1489,6 +1489,33 @@ func (s *BlockChainAPI) replay(ctx context.Context, block *types.Block, accounts return result, statedb, nil } +// GetDiffAccountsWithScope returns detailed changes of some interested accounts in a specific block number. +func (s *BlockChainAPI) GetDiffAccountsWithScope(ctx context.Context, blockNr rpc.BlockNumber, accounts []common.Address) (*types.DiffAccountsInBlock, error) { + if s.b.Chain() == nil { + return nil, fmt.Errorf("blockchain not support get diff accounts") + } + + block, err := s.b.BlockByNumber(ctx, blockNr) + if err != nil { + return nil, fmt.Errorf("block not found for block number (%d): %v", blockNr, err) + } + + needReplay, err := s.needToReplay(ctx, block, accounts) + if err != nil { + return nil, err + } + if !needReplay { + return &types.DiffAccountsInBlock{ + Number: uint64(blockNr), + BlockHash: block.Hash(), + Transactions: make([]types.DiffAccountsInTx, 0), + }, nil + } + + result, _, err := s.replay(ctx, block, accounts) + return result, err +} + func (s *BlockChainAPI) GetVerifyResult(ctx context.Context, blockNr rpc.BlockNumber, blockHash common.Hash, diffHash common.Hash) *core.VerifyResult { return s.b.Chain().GetVerifyResult(uint64(blockNr), blockHash, diffHash) } diff --git a/internal/ethapi/api_test.go b/internal/ethapi/api_test.go index 8f2185c18..6d61d6103 100644 --- a/internal/ethapi/api_test.go +++ b/internal/ethapi/api_test.go @@ -370,6 +370,7 @@ func newTestBackend(t *testing.T, n int, gspec *core.Genesis, generator func(i i return backend } +// nolint:unused func (b *testBackend) setPendingBlock(block *types.Block) { b.pending = block } diff --git a/miner/worker.go b/miner/worker.go index 7b0f2bbe9..b6fc63e13 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -846,7 +846,6 @@ type generateParams struct { coinbase common.Address // The fee recipient address for including transaction random common.Hash // The randomness generated by beacon chain, empty before the merge withdrawals types.Withdrawals // List of withdrawals to include in block. - noExtra bool // Flag whether the extra field assignment is allowed prevWork *environment noTxs bool // Flag whether an empty block without any transaction is expected } diff --git a/trie/secure_trie.go b/trie/secure_trie.go index f0a036931..ffe006c1f 100644 --- a/trie/secure_trie.go +++ b/trie/secure_trie.go @@ -51,7 +51,7 @@ func NewSecure(stateRoot common.Hash, owner common.Hash, root common.Hash, db *D type StateTrie struct { trie Trie preimages *preimageStore - hashKeyBuf [common.HashLength]byte + hashKeyBuf [common.HashLength]byte //nolint:unused secKeyCache map[string][]byte secKeyCacheOwner *StateTrie // Pointer to self, replace the key cache on mismatch } @@ -280,6 +280,8 @@ func (t *StateTrie) MustNodeIterator(start []byte) NodeIterator { // hashKey returns the hash of key as an ephemeral buffer. // The caller must not hold onto the return value because it will become // invalid on the next call to hashKey or secKey. +// +// no use hashKeyBuf for thread safe. func (t *StateTrie) hashKey(key []byte) []byte { hash := make([]byte, common.HashLength) h := newHasher(false)