2015-07-07 03:54:22 +03:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
2015-07-22 19:48:40 +03:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-07 03:54:22 +03:00
|
|
|
//
|
2015-07-23 19:35:11 +03:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-07 03:54:22 +03:00
|
|
|
// 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.
|
|
|
|
//
|
2015-07-22 19:48:40 +03:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-07 03:54:22 +03:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 19:48:40 +03:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-07 03:54:22 +03:00
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
2015-07-22 19:48:40 +03:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-07 03:54:22 +03:00
|
|
|
|
2015-04-18 02:11:09 +03:00
|
|
|
package eth
|
|
|
|
|
|
|
|
import (
|
2015-09-01 17:35:14 +03:00
|
|
|
"errors"
|
2015-06-04 18:46:07 +03:00
|
|
|
"math"
|
2015-07-09 13:55:06 +03:00
|
|
|
"math/big"
|
2023-05-25 03:57:02 +03:00
|
|
|
"strings"
|
2015-04-18 02:11:09 +03:00
|
|
|
"sync"
|
2016-05-17 14:17:20 +03:00
|
|
|
"sync/atomic"
|
2015-04-24 15:40:32 +03:00
|
|
|
"time"
|
2015-04-18 02:11:09 +03:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
all: core rework for the merge transition (#23761)
* all: work for eth1/2 transtition
* consensus/beacon, eth: change beacon difficulty to 0
* eth: updates
* all: add terminalBlockDifficulty config, fix rebasing issues
* eth: implemented merge interop spec
* internal/ethapi: update to v1.0.0.alpha.2
This commit updates the code to the new spec, moving payloadId into
it's own object. It also fixes an issue with finalizing an empty blockhash.
It also properly sets the basefee
* all: sync polishes, other fixes + refactors
* core, eth: correct semantics for LeavePoW, EnterPoS
* core: fixed rebasing artifacts
* core: light: performance improvements
* core: use keyed field (f)
* core: eth: fix compilation issues + tests
* eth/catalyst: dbetter error codes
* all: move Merger to consensus/, remove reliance on it in bc
* all: renamed EnterPoS and LeavePoW to ReachTDD and FinalizePoS
* core: make mergelogs a function
* core: use InsertChain instead of InsertBlock
* les: drop merger from lightchain object
* consensus: add merger
* core: recoverAncestors in catalyst mode
* core: fix nitpick
* all: removed merger from beacon, use TTD, nitpicks
* consensus: eth: add docstring, removed unnecessary code duplication
* consensus/beacon: better comment
* all: easy to fix nitpicks by karalabe
* consensus/beacon: verify known headers to be sure
* core: comments
* core: eth: don't drop peers who advertise blocks, nitpicks
* core: never add beacon blocks to the future queue
* core: fixed nitpicks
* consensus/beacon: simplify IsTTDReached check
* consensus/beacon: correct IsTTDReached check
Co-authored-by: rjl493456442 <garyrong0905@gmail.com>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2021-11-26 14:23:02 +03:00
|
|
|
"github.com/ethereum/go-ethereum/consensus"
|
|
|
|
"github.com/ethereum/go-ethereum/consensus/beacon"
|
2015-04-18 03:21:07 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core"
|
2019-09-30 21:28:50 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/forkid"
|
2023-05-11 09:45:15 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/monitor"
|
2022-07-05 06:14:21 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
2023-06-16 15:29:40 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/txpool"
|
2023-12-07 11:07:48 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/txpool/legacypool"
|
2015-04-18 02:11:09 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/eth/downloader"
|
2015-06-17 16:53:28 +03:00
|
|
|
"github.com/ethereum/go-ethereum/eth/fetcher"
|
2023-04-10 13:36:45 +03:00
|
|
|
"github.com/ethereum/go-ethereum/eth/protocols/bsc"
|
2020-12-14 12:27:15 +03:00
|
|
|
"github.com/ethereum/go-ethereum/eth/protocols/eth"
|
|
|
|
"github.com/ethereum/go-ethereum/eth/protocols/snap"
|
2022-07-05 06:14:21 +03:00
|
|
|
"github.com/ethereum/go-ethereum/eth/protocols/trust"
|
2015-09-14 10:35:57 +03:00
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
2015-04-22 18:56:06 +03:00
|
|
|
"github.com/ethereum/go-ethereum/event"
|
2017-02-22 15:10:07 +03:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2023-07-06 17:20:31 +03:00
|
|
|
"github.com/ethereum/go-ethereum/metrics"
|
2015-04-18 02:11:09 +03:00
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
|
|
|
)
|
|
|
|
|
2015-09-07 20:43:01 +03:00
|
|
|
const (
|
2018-05-18 11:45:52 +03:00
|
|
|
// txChanSize is the size of channel listening to NewTxsEvent.
|
2017-08-18 13:58:36 +03:00
|
|
|
// The number is referenced from the size of tx pool.
|
|
|
|
txChanSize = 4096
|
2023-04-10 13:36:45 +03:00
|
|
|
|
|
|
|
// voteChanSize is the size of channel listening to NewVotesEvent.
|
|
|
|
voteChanSize = 256
|
|
|
|
|
|
|
|
// deltaTdThreshold is the threshold of TD difference for peers to broadcast votes.
|
|
|
|
deltaTdThreshold = 20
|
2023-08-23 12:46:08 +03:00
|
|
|
|
2023-06-28 12:06:20 +03:00
|
|
|
// txMaxBroadcastSize is the max size of a transaction that will be broadcasted.
|
|
|
|
// All transactions with a higher size will be announced and need to be fetched
|
|
|
|
// by the peer.
|
|
|
|
txMaxBroadcastSize = 4096
|
2015-09-07 20:43:01 +03:00
|
|
|
)
|
2015-06-09 13:00:41 +03:00
|
|
|
|
2016-07-08 20:59:11 +03:00
|
|
|
var (
|
2023-12-07 11:07:48 +03:00
|
|
|
syncChallengeTimeout = 15 * time.Second // Time allowance for a node to reply to the sync progress challenge
|
|
|
|
accountBlacklistPeerCounter = metrics.NewRegisteredCounter("eth/count/blacklist", nil)
|
2016-07-08 20:59:11 +03:00
|
|
|
)
|
|
|
|
|
2020-12-14 12:27:15 +03:00
|
|
|
// txPool defines the methods needed from a transaction pool implementation to
|
|
|
|
// support all the operations needed by the Ethereum chain protocols.
|
|
|
|
type txPool interface {
|
|
|
|
// Has returns an indicator whether txpool has a transaction
|
|
|
|
// cached with the given hash.
|
|
|
|
Has(hash common.Hash) bool
|
|
|
|
|
|
|
|
// Get retrieves the transaction from local txpool with given
|
|
|
|
// tx hash.
|
2023-06-16 15:29:40 +03:00
|
|
|
Get(hash common.Hash) *txpool.Transaction
|
2020-12-14 12:27:15 +03:00
|
|
|
|
2023-06-16 15:29:40 +03:00
|
|
|
// Add should add the given transactions to the pool.
|
|
|
|
Add(txs []*txpool.Transaction, local bool, sync bool) []error
|
2020-12-14 12:27:15 +03:00
|
|
|
|
|
|
|
// Pending should return pending transactions.
|
|
|
|
// The slice should be modifiable by the caller.
|
2023-07-27 13:45:35 +03:00
|
|
|
Pending(enforceTips bool) map[common.Address][]*txpool.LazyTransaction
|
2020-12-14 12:27:15 +03:00
|
|
|
|
|
|
|
// SubscribeNewTxsEvent should return an event subscription of
|
|
|
|
// NewTxsEvent and send events to the given channel.
|
|
|
|
SubscribeNewTxsEvent(chan<- core.NewTxsEvent) event.Subscription
|
2022-07-05 06:14:21 +03:00
|
|
|
|
|
|
|
// SubscribeReannoTxsEvent should return an event subscription of
|
|
|
|
// ReannoTxsEvent and send events to the given channel.
|
|
|
|
SubscribeReannoTxsEvent(chan<- core.ReannoTxsEvent) event.Subscription
|
2015-04-18 02:11:09 +03:00
|
|
|
}
|
|
|
|
|
2023-04-10 13:36:45 +03:00
|
|
|
// votePool defines the methods needed from a votes pool implementation to
|
|
|
|
// support all the operations needed by the Ethereum chain protocols.
|
|
|
|
type votePool interface {
|
|
|
|
PutVote(vote *types.VoteEnvelope)
|
|
|
|
GetVotes() []*types.VoteEnvelope
|
|
|
|
|
|
|
|
// SubscribeNewVoteEvent should return an event subscription of
|
|
|
|
// NewVotesEvent and send events to the given channel.
|
|
|
|
SubscribeNewVoteEvent(ch chan<- core.NewVoteEvent) event.Subscription
|
|
|
|
}
|
|
|
|
|
2020-12-14 12:27:15 +03:00
|
|
|
// handlerConfig is the collection of initialization parameters to create a full
|
|
|
|
// node network handler.
|
|
|
|
type handlerConfig struct {
|
2023-04-10 13:36:45 +03:00
|
|
|
Database ethdb.Database // Database for direct sync insertions
|
|
|
|
Chain *core.BlockChain // Blockchain to serve data from
|
|
|
|
TxPool txPool // Transaction pool to propagate from
|
|
|
|
VotePool votePool
|
2023-08-23 12:46:08 +03:00
|
|
|
Merger *consensus.Merger // The manager for eth1/2 transition
|
|
|
|
Network uint64 // Network identifier to adfvertise
|
|
|
|
Sync downloader.SyncMode // Whether to snap or full sync
|
|
|
|
BloomCache uint64 // Megabytes to alloc for snap sync bloom
|
|
|
|
EventMux *event.TypeMux // Legacy event mux, deprecate for `feed`
|
|
|
|
RequiredBlocks map[uint64]common.Hash // Hard coded map of required block hashes for sync challenges
|
2022-07-05 06:14:21 +03:00
|
|
|
DirectBroadcast bool
|
|
|
|
DisablePeerTxBroadcast bool
|
|
|
|
PeerSet *peerSet
|
2015-04-18 02:11:09 +03:00
|
|
|
}
|
|
|
|
|
2020-12-14 12:27:15 +03:00
|
|
|
type handler struct {
|
2022-07-05 06:14:21 +03:00
|
|
|
networkID uint64
|
|
|
|
forkFilter forkid.Filter // Fork ID filter, constant across the lifetime of the node
|
|
|
|
disablePeerTxBroadcast bool
|
2015-10-27 16:10:30 +03:00
|
|
|
|
2023-08-23 12:46:08 +03:00
|
|
|
snapSync atomic.Bool // Flag whether snap sync is enabled (gets disabled if we already have blocks)
|
|
|
|
acceptTxs atomic.Bool // Flag whether we're considered synchronised (enables transaction processing)
|
2021-03-03 12:50:30 +03:00
|
|
|
directBroadcast bool
|
2016-06-02 15:54:07 +03:00
|
|
|
|
2023-05-11 09:45:15 +03:00
|
|
|
database ethdb.Database
|
|
|
|
txpool txPool
|
|
|
|
votepool votePool
|
|
|
|
maliciousVoteMonitor *monitor.MaliciousVoteMonitor
|
|
|
|
chain *core.BlockChain
|
|
|
|
maxPeers int
|
2023-05-25 03:57:02 +03:00
|
|
|
maxPeersPerIP int
|
|
|
|
peersPerIP map[string]int
|
|
|
|
peerPerIPLock sync.Mutex
|
2015-07-02 19:55:18 +03:00
|
|
|
|
2019-10-28 14:59:07 +03:00
|
|
|
downloader *downloader.Downloader
|
|
|
|
blockFetcher *fetcher.BlockFetcher
|
|
|
|
txFetcher *fetcher.TxFetcher
|
|
|
|
peers *peerSet
|
all: core rework for the merge transition (#23761)
* all: work for eth1/2 transtition
* consensus/beacon, eth: change beacon difficulty to 0
* eth: updates
* all: add terminalBlockDifficulty config, fix rebasing issues
* eth: implemented merge interop spec
* internal/ethapi: update to v1.0.0.alpha.2
This commit updates the code to the new spec, moving payloadId into
it's own object. It also fixes an issue with finalizing an empty blockhash.
It also properly sets the basefee
* all: sync polishes, other fixes + refactors
* core, eth: correct semantics for LeavePoW, EnterPoS
* core: fixed rebasing artifacts
* core: light: performance improvements
* core: use keyed field (f)
* core: eth: fix compilation issues + tests
* eth/catalyst: dbetter error codes
* all: move Merger to consensus/, remove reliance on it in bc
* all: renamed EnterPoS and LeavePoW to ReachTDD and FinalizePoS
* core: make mergelogs a function
* core: use InsertChain instead of InsertBlock
* les: drop merger from lightchain object
* consensus: add merger
* core: recoverAncestors in catalyst mode
* core: fix nitpick
* all: removed merger from beacon, use TTD, nitpicks
* consensus: eth: add docstring, removed unnecessary code duplication
* consensus/beacon: better comment
* all: easy to fix nitpicks by karalabe
* consensus/beacon: verify known headers to be sure
* core: comments
* core: eth: don't drop peers who advertise blocks, nitpicks
* core: never add beacon blocks to the future queue
* core: fixed nitpicks
* consensus/beacon: simplify IsTTDReached check
* consensus/beacon: correct IsTTDReached check
Co-authored-by: rjl493456442 <garyrong0905@gmail.com>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2021-11-26 14:23:02 +03:00
|
|
|
merger *consensus.Merger
|
2015-04-18 03:21:07 +03:00
|
|
|
|
2023-05-31 14:35:10 +03:00
|
|
|
eventMux *event.TypeMux
|
|
|
|
txsCh chan core.NewTxsEvent
|
|
|
|
txsSub event.Subscription
|
|
|
|
reannoTxsCh chan core.ReannoTxsEvent
|
|
|
|
reannoTxsSub event.Subscription
|
|
|
|
minedBlockSub *event.TypeMuxSubscription
|
|
|
|
voteCh chan core.NewVoteEvent
|
|
|
|
votesSub event.Subscription
|
|
|
|
voteMonitorSub event.Subscription
|
2015-04-24 15:40:32 +03:00
|
|
|
|
2022-05-04 19:55:17 +03:00
|
|
|
requiredBlocks map[uint64]common.Hash
|
2018-11-02 23:26:45 +03:00
|
|
|
|
2015-06-09 13:03:14 +03:00
|
|
|
// channels for fetcher, syncer, txsyncLoop
|
2020-03-27 16:03:20 +03:00
|
|
|
quitSync chan struct{}
|
2023-09-24 07:21:00 +03:00
|
|
|
stopCh chan struct{}
|
2015-06-08 20:38:39 +03:00
|
|
|
|
2020-03-27 16:03:20 +03:00
|
|
|
chainSync *chainSyncer
|
|
|
|
wg sync.WaitGroup
|
2023-07-11 10:57:42 +03:00
|
|
|
|
|
|
|
handlerStartCh chan struct{}
|
|
|
|
handlerDoneCh chan struct{}
|
2015-04-18 02:11:09 +03:00
|
|
|
}
|
|
|
|
|
2020-12-14 12:27:15 +03:00
|
|
|
// newHandler returns a handler for all Ethereum chain management protocol.
|
|
|
|
func newHandler(config *handlerConfig) (*handler, error) {
|
2015-06-26 16:54:27 +03:00
|
|
|
// Create the protocol manager with the base fields
|
2020-12-14 12:27:15 +03:00
|
|
|
if config.EventMux == nil {
|
|
|
|
config.EventMux = new(event.TypeMux) // Nicety initialization for tests
|
|
|
|
}
|
2022-07-05 06:14:21 +03:00
|
|
|
if config.PeerSet == nil {
|
|
|
|
config.PeerSet = newPeerSet() // Nicety initialization for tests
|
|
|
|
}
|
2020-12-14 12:27:15 +03:00
|
|
|
h := &handler{
|
2022-07-05 06:14:21 +03:00
|
|
|
networkID: config.Network,
|
|
|
|
forkFilter: forkid.NewFilter(config.Chain),
|
|
|
|
disablePeerTxBroadcast: config.DisablePeerTxBroadcast,
|
|
|
|
eventMux: config.EventMux,
|
|
|
|
database: config.Database,
|
|
|
|
txpool: config.TxPool,
|
2023-04-10 13:36:45 +03:00
|
|
|
votepool: config.VotePool,
|
2022-07-05 06:14:21 +03:00
|
|
|
chain: config.Chain,
|
|
|
|
peers: config.PeerSet,
|
|
|
|
merger: config.Merger,
|
2023-05-25 03:57:02 +03:00
|
|
|
peersPerIP: make(map[string]int),
|
2023-08-23 12:46:08 +03:00
|
|
|
requiredBlocks: config.RequiredBlocks,
|
2022-07-05 06:14:21 +03:00
|
|
|
directBroadcast: config.DirectBroadcast,
|
|
|
|
quitSync: make(chan struct{}),
|
2023-08-23 12:46:08 +03:00
|
|
|
handlerDoneCh: make(chan struct{}),
|
|
|
|
handlerStartCh: make(chan struct{}),
|
2023-09-24 07:21:00 +03:00
|
|
|
stopCh: make(chan struct{}),
|
2015-04-18 02:11:09 +03:00
|
|
|
}
|
2020-12-14 12:27:15 +03:00
|
|
|
if config.Sync == downloader.FullSync {
|
2021-11-26 14:26:03 +03:00
|
|
|
// The database seems empty as the current block is the genesis. Yet the snap
|
|
|
|
// block is ahead, so snap sync was enabled for this node at a certain point.
|
2019-06-26 11:00:21 +03:00
|
|
|
// The scenarios where this can happen is
|
2021-11-26 14:26:03 +03:00
|
|
|
// * if the user manually (or via a bad block) rolled back a snap sync node
|
2019-06-26 11:00:21 +03:00
|
|
|
// below the sync point.
|
2021-11-26 14:26:03 +03:00
|
|
|
// * the last snap sync is not finished while user specifies a full sync this
|
2019-06-26 11:00:21 +03:00
|
|
|
// time. But we don't have any recent state for full sync.
|
2021-11-26 14:26:03 +03:00
|
|
|
// In these cases however it's safe to reenable snap sync.
|
2023-03-02 09:29:15 +03:00
|
|
|
fullBlock, snapBlock := h.chain.CurrentBlock(), h.chain.CurrentSnapBlock()
|
|
|
|
if fullBlock.Number.Uint64() == 0 && snapBlock.Number.Uint64() > 0 {
|
2022-07-05 06:14:21 +03:00
|
|
|
if rawdb.ReadAncientType(h.database) == rawdb.PruneFreezerType {
|
|
|
|
log.Crit("Fast Sync not finish, can't enable pruneancient mode")
|
|
|
|
}
|
2023-04-25 13:06:50 +03:00
|
|
|
h.snapSync.Store(true)
|
2021-11-26 14:26:03 +03:00
|
|
|
log.Warn("Switch sync mode from full sync to snap sync")
|
2019-06-26 11:00:21 +03:00
|
|
|
}
|
|
|
|
} else {
|
2023-03-02 09:29:15 +03:00
|
|
|
if h.chain.CurrentBlock().Number.Uint64() > 0 {
|
2021-11-26 14:26:03 +03:00
|
|
|
// Print warning log if database is not empty to run snap sync.
|
|
|
|
log.Warn("Switch sync mode from snap sync to full sync")
|
2019-06-26 11:00:21 +03:00
|
|
|
} else {
|
2021-11-26 14:26:03 +03:00
|
|
|
// If snap sync was requested and our database is empty, grant it
|
2023-04-25 13:06:50 +03:00
|
|
|
h.snapSync.Store(true)
|
2019-06-26 11:00:21 +03:00
|
|
|
}
|
2016-05-17 14:17:20 +03:00
|
|
|
}
|
2021-11-26 14:26:03 +03:00
|
|
|
// Construct the downloader (long sync) and its backing state bloom if snap
|
2019-05-13 15:28:01 +03:00
|
|
|
// sync is requested. The downloader is responsible for deallocating the state
|
|
|
|
// bloom when it's done.
|
2022-07-05 06:14:21 +03:00
|
|
|
var downloadOptions []downloader.DownloadOption
|
2022-03-11 15:14:45 +03:00
|
|
|
// If sync succeeds, pass a callback to potentially disable snap sync mode
|
|
|
|
// and enable transaction propagation.
|
2023-09-07 11:39:29 +03:00
|
|
|
// it was for beacon sync, bsc do not need it.
|
|
|
|
/*
|
|
|
|
success := func(dl *downloader.Downloader) *downloader.Downloader {
|
|
|
|
// If we were running snap sync and it finished, disable doing another
|
|
|
|
// round on next sync cycle
|
|
|
|
if h.snapSync.Load() {
|
|
|
|
log.Info("Snap sync complete, auto disabling")
|
all: activate pbss as experimental feature from eth (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 22:21:36 +03:00
|
|
|
h.snapSync.Store(false) }
|
2023-09-07 11:39:29 +03:00
|
|
|
// If we've successfully finished a sync cycle, accept transactions from
|
|
|
|
// the network
|
|
|
|
h.acceptTxs.Store(true)
|
|
|
|
return dl
|
2022-03-11 15:14:45 +03:00
|
|
|
}
|
2023-09-07 11:39:29 +03:00
|
|
|
downloadOptions = append(downloadOptions, success)
|
|
|
|
*/
|
|
|
|
|
2023-08-23 12:46:08 +03:00
|
|
|
h.downloader = downloader.New(config.Database, h.eventMux, h.chain, nil, h.removePeer, downloadOptions...)
|
2015-06-16 11:58:32 +03:00
|
|
|
|
2019-05-13 15:28:01 +03:00
|
|
|
// Construct the fetcher (short sync)
|
2017-04-05 01:16:29 +03:00
|
|
|
validator := func(header *types.Header) error {
|
all: core rework for the merge transition (#23761)
* all: work for eth1/2 transtition
* consensus/beacon, eth: change beacon difficulty to 0
* eth: updates
* all: add terminalBlockDifficulty config, fix rebasing issues
* eth: implemented merge interop spec
* internal/ethapi: update to v1.0.0.alpha.2
This commit updates the code to the new spec, moving payloadId into
it's own object. It also fixes an issue with finalizing an empty blockhash.
It also properly sets the basefee
* all: sync polishes, other fixes + refactors
* core, eth: correct semantics for LeavePoW, EnterPoS
* core: fixed rebasing artifacts
* core: light: performance improvements
* core: use keyed field (f)
* core: eth: fix compilation issues + tests
* eth/catalyst: dbetter error codes
* all: move Merger to consensus/, remove reliance on it in bc
* all: renamed EnterPoS and LeavePoW to ReachTDD and FinalizePoS
* core: make mergelogs a function
* core: use InsertChain instead of InsertBlock
* les: drop merger from lightchain object
* consensus: add merger
* core: recoverAncestors in catalyst mode
* core: fix nitpick
* all: removed merger from beacon, use TTD, nitpicks
* consensus: eth: add docstring, removed unnecessary code duplication
* consensus/beacon: better comment
* all: easy to fix nitpicks by karalabe
* consensus/beacon: verify known headers to be sure
* core: comments
* core: eth: don't drop peers who advertise blocks, nitpicks
* core: never add beacon blocks to the future queue
* core: fixed nitpicks
* consensus/beacon: simplify IsTTDReached check
* consensus/beacon: correct IsTTDReached check
Co-authored-by: rjl493456442 <garyrong0905@gmail.com>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2021-11-26 14:23:02 +03:00
|
|
|
// All the block fetcher activities should be disabled
|
|
|
|
// after the transition. Print the warning log.
|
|
|
|
if h.merger.PoSFinalized() {
|
|
|
|
log.Warn("Unexpected validation activity", "hash", header.Hash(), "number", header.Number)
|
|
|
|
return errors.New("unexpected behavior after transition")
|
|
|
|
}
|
|
|
|
// Reject all the PoS style headers in the first place. No matter
|
|
|
|
// the chain has finished the transition or not, the PoS headers
|
|
|
|
// should only come from the trusted consensus layer instead of
|
|
|
|
// p2p network.
|
|
|
|
if beacon, ok := h.chain.Engine().(*beacon.Beacon); ok {
|
|
|
|
if beacon.IsPoSHeader(header) {
|
|
|
|
return errors.New("unexpected post-merge header")
|
|
|
|
}
|
|
|
|
}
|
2023-05-03 12:58:39 +03:00
|
|
|
return h.chain.Engine().VerifyHeader(h.chain, header)
|
2015-06-18 18:00:19 +03:00
|
|
|
}
|
2015-06-16 17:39:04 +03:00
|
|
|
heighter := func() uint64 {
|
2023-03-02 09:29:15 +03:00
|
|
|
return h.chain.CurrentBlock().Number.Uint64()
|
2015-06-16 17:39:04 +03:00
|
|
|
}
|
2023-09-06 16:53:47 +03:00
|
|
|
finalizeHeighter := func() uint64 {
|
|
|
|
fblock := h.chain.CurrentFinalBlock()
|
|
|
|
if fblock == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return fblock.Number.Uint64()
|
|
|
|
}
|
2016-06-02 15:54:07 +03:00
|
|
|
inserter := func(blocks types.Blocks) (int, error) {
|
all: core rework for the merge transition (#23761)
* all: work for eth1/2 transtition
* consensus/beacon, eth: change beacon difficulty to 0
* eth: updates
* all: add terminalBlockDifficulty config, fix rebasing issues
* eth: implemented merge interop spec
* internal/ethapi: update to v1.0.0.alpha.2
This commit updates the code to the new spec, moving payloadId into
it's own object. It also fixes an issue with finalizing an empty blockhash.
It also properly sets the basefee
* all: sync polishes, other fixes + refactors
* core, eth: correct semantics for LeavePoW, EnterPoS
* core: fixed rebasing artifacts
* core: light: performance improvements
* core: use keyed field (f)
* core: eth: fix compilation issues + tests
* eth/catalyst: dbetter error codes
* all: move Merger to consensus/, remove reliance on it in bc
* all: renamed EnterPoS and LeavePoW to ReachTDD and FinalizePoS
* core: make mergelogs a function
* core: use InsertChain instead of InsertBlock
* les: drop merger from lightchain object
* consensus: add merger
* core: recoverAncestors in catalyst mode
* core: fix nitpick
* all: removed merger from beacon, use TTD, nitpicks
* consensus: eth: add docstring, removed unnecessary code duplication
* consensus/beacon: better comment
* all: easy to fix nitpicks by karalabe
* consensus/beacon: verify known headers to be sure
* core: comments
* core: eth: don't drop peers who advertise blocks, nitpicks
* core: never add beacon blocks to the future queue
* core: fixed nitpicks
* consensus/beacon: simplify IsTTDReached check
* consensus/beacon: correct IsTTDReached check
Co-authored-by: rjl493456442 <garyrong0905@gmail.com>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2021-11-26 14:23:02 +03:00
|
|
|
// All the block fetcher activities should be disabled
|
|
|
|
// after the transition. Print the warning log.
|
|
|
|
if h.merger.PoSFinalized() {
|
|
|
|
var ctx []interface{}
|
|
|
|
ctx = append(ctx, "blocks", len(blocks))
|
|
|
|
if len(blocks) > 0 {
|
|
|
|
ctx = append(ctx, "firsthash", blocks[0].Hash())
|
|
|
|
ctx = append(ctx, "firstnumber", blocks[0].Number())
|
|
|
|
ctx = append(ctx, "lasthash", blocks[len(blocks)-1].Hash())
|
|
|
|
ctx = append(ctx, "lastnumber", blocks[len(blocks)-1].Number())
|
|
|
|
}
|
|
|
|
log.Warn("Unexpected insertion activity", ctx...)
|
|
|
|
return 0, errors.New("unexpected behavior after transition")
|
|
|
|
}
|
2021-11-26 14:26:03 +03:00
|
|
|
// If snap sync is running, deny importing weird blocks. This is a problematic
|
|
|
|
// clause when starting up a new network, because snap-syncing miners might not
|
2019-04-26 12:11:22 +03:00
|
|
|
// accept each others' blocks until a restart. Unfortunately we haven't figured
|
|
|
|
// out a way yet where nodes can decide unilaterally whether the network is new
|
|
|
|
// or not. This should be fixed if we figure out a solution.
|
2023-04-25 13:06:50 +03:00
|
|
|
if h.snapSync.Load() {
|
2022-07-01 14:38:26 +03:00
|
|
|
log.Warn("Snap syncing, discarded propagated block", "number", blocks[0].Number(), "hash", blocks[0].Hash())
|
2017-05-26 16:04:12 +03:00
|
|
|
return 0, nil
|
|
|
|
}
|
all: core rework for the merge transition (#23761)
* all: work for eth1/2 transtition
* consensus/beacon, eth: change beacon difficulty to 0
* eth: updates
* all: add terminalBlockDifficulty config, fix rebasing issues
* eth: implemented merge interop spec
* internal/ethapi: update to v1.0.0.alpha.2
This commit updates the code to the new spec, moving payloadId into
it's own object. It also fixes an issue with finalizing an empty blockhash.
It also properly sets the basefee
* all: sync polishes, other fixes + refactors
* core, eth: correct semantics for LeavePoW, EnterPoS
* core: fixed rebasing artifacts
* core: light: performance improvements
* core: use keyed field (f)
* core: eth: fix compilation issues + tests
* eth/catalyst: dbetter error codes
* all: move Merger to consensus/, remove reliance on it in bc
* all: renamed EnterPoS and LeavePoW to ReachTDD and FinalizePoS
* core: make mergelogs a function
* core: use InsertChain instead of InsertBlock
* les: drop merger from lightchain object
* consensus: add merger
* core: recoverAncestors in catalyst mode
* core: fix nitpick
* all: removed merger from beacon, use TTD, nitpicks
* consensus: eth: add docstring, removed unnecessary code duplication
* consensus/beacon: better comment
* all: easy to fix nitpicks by karalabe
* consensus/beacon: verify known headers to be sure
* core: comments
* core: eth: don't drop peers who advertise blocks, nitpicks
* core: never add beacon blocks to the future queue
* core: fixed nitpicks
* consensus/beacon: simplify IsTTDReached check
* consensus/beacon: correct IsTTDReached check
Co-authored-by: rjl493456442 <garyrong0905@gmail.com>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2021-11-26 14:23:02 +03:00
|
|
|
if h.merger.TDDReached() {
|
|
|
|
// The blocks from the p2p network is regarded as untrusted
|
|
|
|
// after the transition. In theory block gossip should be disabled
|
|
|
|
// entirely whenever the transition is started. But in order to
|
|
|
|
// handle the transition boundary reorg in the consensus-layer,
|
|
|
|
// the legacy blocks are still accepted, but only for the terminal
|
|
|
|
// pow blocks. Spec: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-3675.md#halt-the-importing-of-pow-blocks
|
|
|
|
for i, block := range blocks {
|
|
|
|
ptd := h.chain.GetTd(block.ParentHash(), block.NumberU64()-1)
|
|
|
|
if ptd == nil {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
td := new(big.Int).Add(ptd, block.Difficulty())
|
|
|
|
if !h.chain.Config().IsTerminalPoWBlock(ptd, td) {
|
|
|
|
log.Info("Filtered out non-termimal pow block", "number", block.NumberU64(), "hash", block.Hash())
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
if err := h.chain.InsertBlockWithoutSetHead(block); err != nil {
|
|
|
|
return i, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0, nil
|
|
|
|
}
|
2020-12-14 12:27:15 +03:00
|
|
|
n, err := h.chain.InsertChain(blocks)
|
2019-04-26 12:11:22 +03:00
|
|
|
if err == nil {
|
all: activate pbss as experimental feature from eth (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 22:21:36 +03:00
|
|
|
h.enableSyncedFeatures() // Mark initial sync done on any fetcher import
|
2019-04-26 12:11:22 +03:00
|
|
|
}
|
|
|
|
return n, err
|
2016-06-02 15:54:07 +03:00
|
|
|
}
|
2023-09-06 16:53:47 +03:00
|
|
|
h.blockFetcher = fetcher.NewBlockFetcher(false, nil, h.chain.GetBlockByHash, validator, h.BroadcastBlock,
|
|
|
|
heighter, finalizeHeighter, nil, inserter, h.removePeer)
|
2020-01-22 17:39:43 +03:00
|
|
|
|
|
|
|
fetchTx := func(peer string, hashes []common.Hash) error {
|
2021-02-02 11:44:36 +03:00
|
|
|
p := h.peers.peer(peer)
|
2020-01-22 17:39:43 +03:00
|
|
|
if p == nil {
|
|
|
|
return errors.New("unknown peer")
|
|
|
|
}
|
|
|
|
return p.RequestTxs(hashes)
|
|
|
|
}
|
2023-12-07 11:07:48 +03:00
|
|
|
addTxs := func(peer string, txs []*txpool.Transaction) []error {
|
|
|
|
errors := h.txpool.Add(txs, false, false)
|
|
|
|
for _, err := range errors {
|
|
|
|
if err == legacypool.ErrInBlackList {
|
|
|
|
accountBlacklistPeerCounter.Inc(1)
|
|
|
|
p := h.peers.peer(peer)
|
|
|
|
if p != nil {
|
|
|
|
remoteAddr := p.remoteAddr()
|
|
|
|
if remoteAddr != nil {
|
|
|
|
log.Warn("blacklist account detected from other peer", "remoteAddr", remoteAddr, "ID", p.ID())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return errors
|
2023-06-16 15:29:40 +03:00
|
|
|
}
|
|
|
|
h.txFetcher = fetcher.NewTxFetcher(h.txpool.Has, addTxs, fetchTx)
|
2020-12-14 12:27:15 +03:00
|
|
|
h.chainSync = newChainSyncer(h)
|
|
|
|
return h, nil
|
2019-07-08 18:53:47 +03:00
|
|
|
}
|
|
|
|
|
2023-07-11 10:57:42 +03:00
|
|
|
// protoTracker tracks the number of active protocol handlers.
|
|
|
|
func (h *handler) protoTracker() {
|
|
|
|
defer h.wg.Done()
|
|
|
|
var active int
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-h.handlerStartCh:
|
|
|
|
active++
|
|
|
|
case <-h.handlerDoneCh:
|
|
|
|
active--
|
|
|
|
case <-h.quitSync:
|
|
|
|
// Wait for all active handlers to finish.
|
|
|
|
for ; active > 0; active-- {
|
|
|
|
<-h.handlerDoneCh
|
|
|
|
}
|
|
|
|
return
|
2023-09-24 07:21:00 +03:00
|
|
|
case <-h.stopCh:
|
|
|
|
return
|
2023-07-11 10:57:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// incHandlers signals to increment the number of active handlers if not
|
|
|
|
// quitting.
|
|
|
|
func (h *handler) incHandlers() bool {
|
|
|
|
select {
|
|
|
|
case h.handlerStartCh <- struct{}{}:
|
|
|
|
return true
|
|
|
|
case <-h.quitSync:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// decHandlers signals to decrement the number of active handlers.
|
|
|
|
func (h *handler) decHandlers() {
|
|
|
|
h.handlerDoneCh <- struct{}{}
|
|
|
|
}
|
|
|
|
|
2021-02-02 11:44:36 +03:00
|
|
|
// runEthPeer registers an eth peer into the joint eth/snap peerset, adds it to
|
2023-04-10 13:36:45 +03:00
|
|
|
// various subsystems and starts handling messages.
|
2020-12-14 12:27:15 +03:00
|
|
|
func (h *handler) runEthPeer(peer *eth.Peer, handler eth.Handler) error {
|
2023-07-11 10:57:42 +03:00
|
|
|
if !h.incHandlers() {
|
|
|
|
return p2p.DiscQuitting
|
|
|
|
}
|
|
|
|
defer h.decHandlers()
|
|
|
|
|
2021-02-02 11:44:36 +03:00
|
|
|
// If the peer has a `snap` extension, wait for it to connect so we can have
|
|
|
|
// a uniform initialization/teardown mechanism
|
|
|
|
snap, err := h.peers.waitSnapExtension(peer)
|
|
|
|
if err != nil {
|
|
|
|
peer.Log().Error("Snapshot extension barrier failed", "err", err)
|
|
|
|
return err
|
2015-05-27 18:58:51 +03:00
|
|
|
}
|
2022-07-05 06:14:21 +03:00
|
|
|
trust, err := h.peers.waitTrustExtension(peer)
|
|
|
|
if err != nil {
|
|
|
|
peer.Log().Error("Trust extension barrier failed", "err", err)
|
|
|
|
return err
|
|
|
|
}
|
2023-04-10 13:36:45 +03:00
|
|
|
bsc, err := h.peers.waitBscExtension(peer)
|
|
|
|
if err != nil {
|
|
|
|
peer.Log().Error("Bsc extension barrier failed", "err", err)
|
|
|
|
return err
|
|
|
|
}
|
2015-06-26 20:42:27 +03:00
|
|
|
|
|
|
|
// Execute the Ethereum handshake
|
2018-01-30 19:39:32 +03:00
|
|
|
var (
|
2020-12-14 12:27:15 +03:00
|
|
|
genesis = h.chain.Genesis()
|
|
|
|
head = h.chain.CurrentHeader()
|
2018-01-30 19:39:32 +03:00
|
|
|
hash = head.Hash()
|
|
|
|
number = head.Number.Uint64()
|
2020-12-14 12:27:15 +03:00
|
|
|
td = h.chain.GetTd(hash, number)
|
2018-01-30 19:39:32 +03:00
|
|
|
)
|
2023-01-03 14:06:32 +03:00
|
|
|
forkID := forkid.NewID(h.chain.Config(), genesis.Hash(), number, head.Time)
|
2022-07-05 06:14:21 +03:00
|
|
|
if err := peer.Handshake(h.networkID, td, hash, genesis.Hash(), forkID, h.forkFilter, ð.UpgradeStatusExtension{DisablePeerTxBroadcast: h.disablePeerTxBroadcast}); err != nil {
|
2020-12-14 12:27:15 +03:00
|
|
|
peer.Log().Debug("Ethereum handshake failed", "err", err)
|
2015-04-18 02:11:09 +03:00
|
|
|
return err
|
|
|
|
}
|
2021-01-25 21:06:52 +03:00
|
|
|
reject := false // reserved peer slots
|
2023-04-25 13:06:50 +03:00
|
|
|
if h.snapSync.Load() {
|
2021-02-02 11:44:36 +03:00
|
|
|
if snap == nil {
|
|
|
|
// If we are running snap-sync, we want to reserve roughly half the peer
|
|
|
|
// slots for peers supporting the snap protocol.
|
|
|
|
// The logic here is; we only allow up to 5 more non-snap peers than snap-peers.
|
|
|
|
if all, snp := h.peers.len(), h.peers.snapLen(); all-snp > snp+5 {
|
|
|
|
reject = true
|
|
|
|
}
|
2021-01-25 21:06:52 +03:00
|
|
|
}
|
|
|
|
}
|
2020-12-14 12:27:15 +03:00
|
|
|
// Ignore maxPeers if this is a trusted peer
|
2023-05-25 03:57:02 +03:00
|
|
|
peerInfo := peer.Peer.Info()
|
|
|
|
if !peerInfo.Network.Trusted {
|
2021-02-02 11:44:36 +03:00
|
|
|
if reject || h.peers.len() >= h.maxPeers {
|
2021-01-25 21:06:52 +03:00
|
|
|
return p2p.DiscTooManyPeers
|
|
|
|
}
|
2020-12-14 12:27:15 +03:00
|
|
|
}
|
2023-05-25 03:57:02 +03:00
|
|
|
|
|
|
|
remoteAddr := peerInfo.Network.RemoteAddress
|
|
|
|
indexIP := strings.LastIndex(remoteAddr, ":")
|
|
|
|
if indexIP == -1 {
|
|
|
|
// there could be no IP address, such as a pipe
|
|
|
|
peer.Log().Debug("runEthPeer", "no ip address, remoteAddress", remoteAddr)
|
|
|
|
} else if !peerInfo.Network.Trusted {
|
|
|
|
remoteIP := remoteAddr[:indexIP]
|
|
|
|
h.peerPerIPLock.Lock()
|
|
|
|
if num, ok := h.peersPerIP[remoteIP]; ok && num >= h.maxPeersPerIP {
|
|
|
|
h.peerPerIPLock.Unlock()
|
|
|
|
peer.Log().Info("The IP has too many peers", "ip", remoteIP, "maxPeersPerIP", h.maxPeersPerIP,
|
|
|
|
"name", peerInfo.Name, "Enode", peerInfo.Enode)
|
|
|
|
return p2p.DiscTooManyPeers
|
|
|
|
}
|
|
|
|
h.peersPerIP[remoteIP] = h.peersPerIP[remoteIP] + 1
|
|
|
|
h.peerPerIPLock.Unlock()
|
|
|
|
}
|
2020-12-14 12:27:15 +03:00
|
|
|
peer.Log().Debug("Ethereum peer connected", "name", peer.Name())
|
2020-03-27 16:03:20 +03:00
|
|
|
|
2015-06-26 20:42:27 +03:00
|
|
|
// Register the peer locally
|
2023-09-07 11:39:29 +03:00
|
|
|
if err := h.peers.registerPeer(peer, snap, trust, bsc); err != nil {
|
2020-12-14 12:27:15 +03:00
|
|
|
peer.Log().Error("Ethereum peer registration failed", "err", err)
|
2015-05-18 21:33:37 +03:00
|
|
|
return err
|
|
|
|
}
|
2021-05-25 23:20:36 +03:00
|
|
|
defer h.unregisterPeer(peer.ID())
|
2015-04-18 02:11:09 +03:00
|
|
|
|
2021-02-02 11:44:36 +03:00
|
|
|
p := h.peers.peer(peer.ID())
|
2020-12-14 12:27:15 +03:00
|
|
|
if p == nil {
|
|
|
|
return errors.New("peer dropped during handling")
|
|
|
|
}
|
2015-06-26 20:42:27 +03:00
|
|
|
// Register the peer in the downloader. If the downloader considers it banned, we disconnect
|
2020-12-14 12:27:15 +03:00
|
|
|
if err := h.downloader.RegisterPeer(peer.ID(), peer.Version(), peer); err != nil {
|
2021-02-02 11:44:36 +03:00
|
|
|
peer.Log().Error("Failed to register peer in eth syncer", "err", err)
|
2015-05-18 21:33:37 +03:00
|
|
|
return err
|
|
|
|
}
|
2021-02-02 11:44:36 +03:00
|
|
|
if snap != nil {
|
|
|
|
if err := h.downloader.SnapSyncer.Register(snap); err != nil {
|
|
|
|
peer.Log().Error("Failed to register peer in snap syncer", "err", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-07-11 10:57:42 +03:00
|
|
|
h.chainSync.handlePeerEvent()
|
2020-03-27 16:03:20 +03:00
|
|
|
|
2023-04-10 13:36:45 +03:00
|
|
|
// Propagate existing transactions and votes. new transactions and votes appearing
|
2015-04-18 02:11:09 +03:00
|
|
|
// after this will be sent via broadcasts.
|
2020-12-14 12:27:15 +03:00
|
|
|
h.syncTransactions(peer)
|
2023-04-10 13:36:45 +03:00
|
|
|
if h.votepool != nil && p.bscExt != nil {
|
|
|
|
h.syncVotes(p.bscExt)
|
|
|
|
}
|
2015-06-09 13:03:14 +03:00
|
|
|
|
2021-11-26 14:26:03 +03:00
|
|
|
// Create a notification channel for pending requests if the peer goes down
|
|
|
|
dead := make(chan struct{})
|
|
|
|
defer close(dead)
|
|
|
|
|
2022-03-15 14:20:03 +03:00
|
|
|
// If we have any explicit peer required block hashes, request them
|
2022-05-04 19:55:17 +03:00
|
|
|
for number, hash := range h.requiredBlocks {
|
2021-11-26 14:26:03 +03:00
|
|
|
resCh := make(chan *eth.Response)
|
2022-12-13 12:16:14 +03:00
|
|
|
|
|
|
|
req, err := peer.RequestHeadersByNumber(number, 1, 0, false, resCh)
|
|
|
|
if err != nil {
|
2018-11-02 23:26:45 +03:00
|
|
|
return err
|
|
|
|
}
|
2022-12-13 12:16:14 +03:00
|
|
|
go func(number uint64, hash common.Hash, req *eth.Request) {
|
|
|
|
// Ensure the request gets cancelled in case of error/drop
|
|
|
|
defer req.Close()
|
|
|
|
|
2021-11-26 14:26:03 +03:00
|
|
|
timeout := time.NewTimer(syncChallengeTimeout)
|
|
|
|
defer timeout.Stop()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case res := <-resCh:
|
|
|
|
headers := ([]*types.Header)(*res.Res.(*eth.BlockHeadersPacket))
|
|
|
|
if len(headers) == 0 {
|
2022-03-15 14:20:03 +03:00
|
|
|
// Required blocks are allowed to be missing if the remote
|
2021-11-26 14:26:03 +03:00
|
|
|
// node is not yet synced
|
|
|
|
res.Done <- nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Validate the header and either drop the peer or continue
|
|
|
|
if len(headers) > 1 {
|
2022-03-15 14:20:03 +03:00
|
|
|
res.Done <- errors.New("too many headers in required block response")
|
2021-11-26 14:26:03 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if headers[0].Number.Uint64() != number || headers[0].Hash() != hash {
|
2022-03-15 14:20:03 +03:00
|
|
|
peer.Log().Info("Required block mismatch, dropping peer", "number", number, "hash", headers[0].Hash(), "want", hash)
|
|
|
|
res.Done <- errors.New("required block mismatch")
|
2021-11-26 14:26:03 +03:00
|
|
|
return
|
|
|
|
}
|
2022-03-15 14:20:03 +03:00
|
|
|
peer.Log().Debug("Peer required block verified", "number", number, "hash", hash)
|
2022-01-07 16:12:43 +03:00
|
|
|
res.Done <- nil
|
2021-11-26 14:26:03 +03:00
|
|
|
case <-timeout.C:
|
2022-03-15 14:20:03 +03:00
|
|
|
peer.Log().Warn("Required block challenge timed out, dropping", "addr", peer.RemoteAddr(), "type", peer.Name())
|
2021-11-26 14:26:03 +03:00
|
|
|
h.removePeer(peer.ID())
|
|
|
|
}
|
2022-12-13 12:16:14 +03:00
|
|
|
}(number, hash, req)
|
2018-11-02 23:26:45 +03:00
|
|
|
}
|
2018-12-10 15:47:01 +03:00
|
|
|
// Handle incoming messages until the connection is torn down
|
2020-12-14 12:27:15 +03:00
|
|
|
return handler(peer)
|
2015-04-18 02:11:09 +03:00
|
|
|
}
|
|
|
|
|
2021-02-02 11:44:36 +03:00
|
|
|
// runSnapExtension registers a `snap` peer into the joint eth/snap peerset and
|
|
|
|
// starts handling inbound messages. As `snap` is only a satellite protocol to
|
|
|
|
// `eth`, all subsystem registrations and lifecycle management will be done by
|
|
|
|
// the main `eth` handler to prevent strange races.
|
|
|
|
func (h *handler) runSnapExtension(peer *snap.Peer, handler snap.Handler) error {
|
2023-07-11 10:57:42 +03:00
|
|
|
if !h.incHandlers() {
|
|
|
|
return p2p.DiscQuitting
|
|
|
|
}
|
|
|
|
defer h.decHandlers()
|
2020-12-14 12:27:15 +03:00
|
|
|
|
2021-02-02 11:44:36 +03:00
|
|
|
if err := h.peers.registerSnapExtension(peer); err != nil {
|
2023-07-06 17:20:31 +03:00
|
|
|
if metrics.Enabled {
|
|
|
|
if peer.Inbound() {
|
|
|
|
snap.IngressRegistrationErrorMeter.Mark(1)
|
|
|
|
} else {
|
|
|
|
snap.EgressRegistrationErrorMeter.Mark(1)
|
|
|
|
}
|
|
|
|
}
|
2022-03-30 11:28:27 +03:00
|
|
|
peer.Log().Warn("Snapshot extension registration failed", "err", err)
|
2015-04-18 02:11:09 +03:00
|
|
|
return err
|
|
|
|
}
|
2020-12-14 12:27:15 +03:00
|
|
|
return handler(peer)
|
|
|
|
}
|
2015-09-30 19:23:31 +03:00
|
|
|
|
2022-07-05 06:14:21 +03:00
|
|
|
// runTrustExtension registers a `trust` peer into the joint eth/trust peerset and
|
|
|
|
// starts handling inbound messages. As `trust` is only a satellite protocol to
|
|
|
|
// `eth`, all subsystem registrations and lifecycle management will be done by
|
|
|
|
// the main `eth` handler to prevent strange races.
|
|
|
|
func (h *handler) runTrustExtension(peer *trust.Peer, handler trust.Handler) error {
|
2023-08-23 12:46:08 +03:00
|
|
|
if !h.incHandlers() {
|
|
|
|
return p2p.DiscQuitting
|
|
|
|
}
|
2023-09-07 11:39:29 +03:00
|
|
|
defer h.decHandlers()
|
2022-07-05 06:14:21 +03:00
|
|
|
|
|
|
|
if err := h.peers.registerTrustExtension(peer); err != nil {
|
2023-08-23 12:46:08 +03:00
|
|
|
if metrics.Enabled {
|
|
|
|
if peer.Inbound() {
|
|
|
|
trust.IngressRegistrationErrorMeter.Mark(1)
|
|
|
|
} else {
|
|
|
|
trust.EgressRegistrationErrorMeter.Mark(1)
|
|
|
|
}
|
|
|
|
}
|
2022-07-05 06:14:21 +03:00
|
|
|
peer.Log().Error("Trust extension registration failed", "err", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return handler(peer)
|
|
|
|
}
|
|
|
|
|
2023-04-10 13:36:45 +03:00
|
|
|
// runBscExtension registers a `bsc` peer into the joint eth/bsc peerset and
|
|
|
|
// starts handling inbound messages. As `bsc` is only a satellite protocol to
|
|
|
|
// `eth`, all subsystem registrations and lifecycle management will be done by
|
|
|
|
// the main `eth` handler to prevent strange races.
|
|
|
|
func (h *handler) runBscExtension(peer *bsc.Peer, handler bsc.Handler) error {
|
2023-08-23 12:46:08 +03:00
|
|
|
if !h.incHandlers() {
|
|
|
|
return p2p.DiscQuitting
|
|
|
|
}
|
2023-09-07 11:39:29 +03:00
|
|
|
defer h.decHandlers()
|
2023-04-10 13:36:45 +03:00
|
|
|
|
|
|
|
if err := h.peers.registerBscExtension(peer); err != nil {
|
2023-08-23 12:46:08 +03:00
|
|
|
if metrics.Enabled {
|
|
|
|
if peer.Inbound() {
|
|
|
|
bsc.IngressRegistrationErrorMeter.Mark(1)
|
|
|
|
} else {
|
|
|
|
bsc.EgressRegistrationErrorMeter.Mark(1)
|
|
|
|
}
|
|
|
|
}
|
2023-04-10 13:36:45 +03:00
|
|
|
peer.Log().Error("Bsc extension registration failed", "err", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return handler(peer)
|
|
|
|
}
|
|
|
|
|
2021-05-25 23:20:36 +03:00
|
|
|
// removePeer requests disconnection of a peer.
|
2020-12-14 12:27:15 +03:00
|
|
|
func (h *handler) removePeer(id string) {
|
2021-05-25 23:20:36 +03:00
|
|
|
peer := h.peers.peer(id)
|
|
|
|
if peer != nil {
|
2022-07-05 06:14:21 +03:00
|
|
|
// Hard disconnect at the networking layer. Handler will get an EOF and terminate the peer. defer unregisterPeer will do the cleanup task after then.
|
2021-05-25 23:20:36 +03:00
|
|
|
peer.Peer.Disconnect(p2p.DiscUselessPeer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// unregisterPeer removes a peer from the downloader, fetchers and main peer set.
|
|
|
|
func (h *handler) unregisterPeer(id string) {
|
2021-01-25 09:17:05 +03:00
|
|
|
// Create a custom logger to avoid printing the entire id
|
|
|
|
var logger log.Logger
|
|
|
|
if len(id) < 16 {
|
|
|
|
// Tests use short IDs, don't choke on them
|
|
|
|
logger = log.New("peer", id)
|
|
|
|
} else {
|
|
|
|
logger = log.New("peer", id[:8])
|
|
|
|
}
|
2021-02-02 11:44:36 +03:00
|
|
|
// Abort if the peer does not exist
|
|
|
|
peer := h.peers.peer(id)
|
|
|
|
if peer == nil {
|
|
|
|
logger.Error("Ethereum peer removal failed", "err", errPeerNotRegistered)
|
|
|
|
return
|
2020-12-14 12:27:15 +03:00
|
|
|
}
|
2021-02-02 11:44:36 +03:00
|
|
|
// Remove the `eth` peer if it exists
|
|
|
|
logger.Debug("Removing Ethereum peer", "snap", peer.snapExt != nil)
|
2015-09-30 19:23:31 +03:00
|
|
|
|
2021-02-02 11:44:36 +03:00
|
|
|
// Remove the `snap` extension if it exists
|
|
|
|
if peer.snapExt != nil {
|
2020-12-14 12:27:15 +03:00
|
|
|
h.downloader.SnapSyncer.Unregister(id)
|
|
|
|
}
|
2021-02-02 11:44:36 +03:00
|
|
|
h.downloader.UnregisterPeer(id)
|
|
|
|
h.txFetcher.Drop(id)
|
2015-06-26 20:42:27 +03:00
|
|
|
|
2021-02-02 11:44:36 +03:00
|
|
|
if err := h.peers.unregisterPeer(id); err != nil {
|
|
|
|
logger.Error("Ethereum peer removal failed", "err", err)
|
2020-12-14 12:27:15 +03:00
|
|
|
}
|
2023-05-25 03:57:02 +03:00
|
|
|
|
|
|
|
peerInfo := peer.Peer.Info()
|
|
|
|
remoteAddr := peerInfo.Network.RemoteAddress
|
|
|
|
indexIP := strings.LastIndex(remoteAddr, ":")
|
|
|
|
if indexIP == -1 {
|
|
|
|
// there could be no IP address, such as a pipe
|
|
|
|
peer.Log().Debug("unregisterPeer", "name", peerInfo.Name, "no ip address, remoteAddress", remoteAddr)
|
|
|
|
} else if !peerInfo.Network.Trusted {
|
|
|
|
remoteIP := remoteAddr[:indexIP]
|
|
|
|
h.peerPerIPLock.Lock()
|
|
|
|
if h.peersPerIP[remoteIP] <= 0 {
|
|
|
|
peer.Log().Error("unregisterPeer without record", "name", peerInfo.Name, "remoteAddress", remoteAddr)
|
|
|
|
} else {
|
|
|
|
h.peersPerIP[remoteIP] = h.peersPerIP[remoteIP] - 1
|
|
|
|
logger.Debug("unregisterPeer", "name", peerInfo.Name, "connectNum", h.peersPerIP[remoteIP])
|
|
|
|
if h.peersPerIP[remoteIP] == 0 {
|
|
|
|
delete(h.peersPerIP, remoteIP)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
h.peerPerIPLock.Unlock()
|
|
|
|
}
|
2020-12-14 12:27:15 +03:00
|
|
|
}
|
2015-10-05 19:37:56 +03:00
|
|
|
|
2023-05-25 03:57:02 +03:00
|
|
|
func (h *handler) Start(maxPeers int, maxPeersPerIP int) {
|
2020-12-14 12:27:15 +03:00
|
|
|
h.maxPeers = maxPeers
|
2023-05-25 03:57:02 +03:00
|
|
|
h.maxPeersPerIP = maxPeersPerIP
|
2020-12-14 12:27:15 +03:00
|
|
|
// broadcast transactions
|
|
|
|
h.wg.Add(1)
|
|
|
|
h.txsCh = make(chan core.NewTxsEvent, txChanSize)
|
|
|
|
h.txsSub = h.txpool.SubscribeNewTxsEvent(h.txsCh)
|
|
|
|
go h.txBroadcastLoop()
|
2015-09-30 19:23:31 +03:00
|
|
|
|
2023-04-10 13:36:45 +03:00
|
|
|
// broadcast votes
|
|
|
|
if h.votepool != nil {
|
|
|
|
h.wg.Add(1)
|
|
|
|
h.voteCh = make(chan core.NewVoteEvent, voteChanSize)
|
|
|
|
h.votesSub = h.votepool.SubscribeNewVoteEvent(h.voteCh)
|
|
|
|
go h.voteBroadcastLoop()
|
2023-05-11 09:45:15 +03:00
|
|
|
|
|
|
|
if h.maliciousVoteMonitor != nil {
|
|
|
|
h.wg.Add(1)
|
|
|
|
go h.startMaliciousVoteMonitor()
|
|
|
|
}
|
2023-04-10 13:36:45 +03:00
|
|
|
}
|
|
|
|
|
2022-07-05 06:14:21 +03:00
|
|
|
// announce local pending transactions again
|
|
|
|
h.wg.Add(1)
|
|
|
|
h.reannoTxsCh = make(chan core.ReannoTxsEvent, txChanSize)
|
|
|
|
h.reannoTxsSub = h.txpool.SubscribeReannoTxsEvent(h.reannoTxsCh)
|
|
|
|
go h.txReannounceLoop()
|
|
|
|
|
2020-12-14 12:27:15 +03:00
|
|
|
// broadcast mined blocks
|
|
|
|
h.wg.Add(1)
|
|
|
|
h.minedBlockSub = h.eventMux.Subscribe(core.NewMinedBlockEvent{})
|
|
|
|
go h.minedBroadcastLoop()
|
2015-04-18 02:11:09 +03:00
|
|
|
|
2020-12-14 12:27:15 +03:00
|
|
|
// start sync handlers
|
2021-08-24 21:52:58 +03:00
|
|
|
h.wg.Add(1)
|
2020-12-14 12:27:15 +03:00
|
|
|
go h.chainSync.loop()
|
2023-07-11 10:57:42 +03:00
|
|
|
|
|
|
|
// start peer handler tracker
|
|
|
|
h.wg.Add(1)
|
|
|
|
go h.protoTracker()
|
2020-12-14 12:27:15 +03:00
|
|
|
}
|
2015-04-18 03:24:24 +03:00
|
|
|
|
2023-05-11 09:45:15 +03:00
|
|
|
func (h *handler) startMaliciousVoteMonitor() {
|
|
|
|
defer h.wg.Done()
|
|
|
|
voteCh := make(chan core.NewVoteEvent, voteChanSize)
|
2023-05-31 14:35:10 +03:00
|
|
|
h.voteMonitorSub = h.votepool.SubscribeNewVoteEvent(voteCh)
|
2023-05-11 09:45:15 +03:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case event := <-voteCh:
|
|
|
|
pendingBlockNumber := h.chain.CurrentHeader().Number.Uint64() + 1
|
|
|
|
h.maliciousVoteMonitor.ConflictDetect(event.Vote, pendingBlockNumber)
|
2023-05-31 14:35:10 +03:00
|
|
|
case <-h.voteMonitorSub.Err():
|
2023-05-11 09:45:15 +03:00
|
|
|
return
|
2023-09-24 07:21:00 +03:00
|
|
|
case <-h.stopCh:
|
|
|
|
return
|
2023-05-11 09:45:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-14 12:27:15 +03:00
|
|
|
func (h *handler) Stop() {
|
|
|
|
h.txsSub.Unsubscribe() // quits txBroadcastLoop
|
2022-07-05 06:14:21 +03:00
|
|
|
h.reannoTxsSub.Unsubscribe() // quits txReannounceLoop
|
2020-12-14 12:27:15 +03:00
|
|
|
h.minedBlockSub.Unsubscribe() // quits blockBroadcastLoop
|
2023-04-10 13:36:45 +03:00
|
|
|
if h.votepool != nil {
|
|
|
|
h.votesSub.Unsubscribe() // quits voteBroadcastLoop
|
2023-05-31 14:35:10 +03:00
|
|
|
if h.maliciousVoteMonitor != nil {
|
|
|
|
h.voteMonitorSub.Unsubscribe()
|
|
|
|
}
|
2023-04-10 13:36:45 +03:00
|
|
|
}
|
2023-09-24 07:21:00 +03:00
|
|
|
close(h.stopCh)
|
2020-12-14 12:27:15 +03:00
|
|
|
// Quit chainSync and txsync64.
|
|
|
|
// After this is done, no new peers will be accepted.
|
|
|
|
close(h.quitSync)
|
2019-10-28 14:59:07 +03:00
|
|
|
|
2020-12-14 12:27:15 +03:00
|
|
|
// Disconnect existing sessions.
|
|
|
|
// This also closes the gate for any new registrations on the peer set.
|
|
|
|
// sessions which are already established but not added to h.peers yet
|
|
|
|
// will exit when they try to register.
|
|
|
|
h.peers.close()
|
2023-07-11 10:57:42 +03:00
|
|
|
h.wg.Wait()
|
2015-06-30 19:05:06 +03:00
|
|
|
|
2020-12-14 12:27:15 +03:00
|
|
|
log.Info("Ethereum protocol stopped")
|
2015-04-18 02:11:09 +03:00
|
|
|
}
|
2015-04-18 03:21:07 +03:00
|
|
|
|
2020-01-23 18:08:06 +03:00
|
|
|
// BroadcastBlock will either propagate a block to a subset of its peers, or
|
|
|
|
// will only announce its availability (depending what's requested).
|
2020-12-14 12:27:15 +03:00
|
|
|
func (h *handler) BroadcastBlock(block *types.Block, propagate bool) {
|
all: core rework for the merge transition (#23761)
* all: work for eth1/2 transtition
* consensus/beacon, eth: change beacon difficulty to 0
* eth: updates
* all: add terminalBlockDifficulty config, fix rebasing issues
* eth: implemented merge interop spec
* internal/ethapi: update to v1.0.0.alpha.2
This commit updates the code to the new spec, moving payloadId into
it's own object. It also fixes an issue with finalizing an empty blockhash.
It also properly sets the basefee
* all: sync polishes, other fixes + refactors
* core, eth: correct semantics for LeavePoW, EnterPoS
* core: fixed rebasing artifacts
* core: light: performance improvements
* core: use keyed field (f)
* core: eth: fix compilation issues + tests
* eth/catalyst: dbetter error codes
* all: move Merger to consensus/, remove reliance on it in bc
* all: renamed EnterPoS and LeavePoW to ReachTDD and FinalizePoS
* core: make mergelogs a function
* core: use InsertChain instead of InsertBlock
* les: drop merger from lightchain object
* consensus: add merger
* core: recoverAncestors in catalyst mode
* core: fix nitpick
* all: removed merger from beacon, use TTD, nitpicks
* consensus: eth: add docstring, removed unnecessary code duplication
* consensus/beacon: better comment
* all: easy to fix nitpicks by karalabe
* consensus/beacon: verify known headers to be sure
* core: comments
* core: eth: don't drop peers who advertise blocks, nitpicks
* core: never add beacon blocks to the future queue
* core: fixed nitpicks
* consensus/beacon: simplify IsTTDReached check
* consensus/beacon: correct IsTTDReached check
Co-authored-by: rjl493456442 <garyrong0905@gmail.com>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2021-11-26 14:23:02 +03:00
|
|
|
// Disable the block propagation if the chain has already entered the PoS
|
|
|
|
// stage. The block propagation is delegated to the consensus layer.
|
|
|
|
if h.merger.PoSFinalized() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Disable the block propagation if it's the post-merge block.
|
|
|
|
if beacon, ok := h.chain.Engine().(*beacon.Beacon); ok {
|
|
|
|
if beacon.IsPoSHeader(block.Header()) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2015-06-18 16:09:34 +03:00
|
|
|
hash := block.Hash()
|
2021-02-02 11:44:36 +03:00
|
|
|
peers := h.peers.peersWithoutBlock(hash)
|
2015-06-04 18:46:07 +03:00
|
|
|
|
2015-06-18 18:00:19 +03:00
|
|
|
// If propagation is requested, send to a subset of the peer
|
|
|
|
if propagate {
|
2015-07-09 13:55:06 +03:00
|
|
|
// Calculate the TD of the block (it's not imported yet, so block.Td is not valid)
|
|
|
|
var td *big.Int
|
2020-12-14 12:27:15 +03:00
|
|
|
if parent := h.chain.GetBlock(block.ParentHash(), block.NumberU64()-1); parent != nil {
|
|
|
|
td = new(big.Int).Add(block.Difficulty(), h.chain.GetTd(block.ParentHash(), block.NumberU64()-1))
|
2015-07-09 13:55:06 +03:00
|
|
|
} else {
|
2017-03-02 16:06:16 +03:00
|
|
|
log.Error("Propagating dangling block", "number", block.Number(), "hash", hash)
|
2015-07-09 13:55:06 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// Send the block to a subset of our peers
|
2021-04-16 07:45:26 +03:00
|
|
|
var transfer []*ethPeer
|
|
|
|
if h.directBroadcast {
|
2022-07-05 06:14:21 +03:00
|
|
|
transfer = peers[:]
|
2021-03-03 12:50:30 +03:00
|
|
|
} else {
|
|
|
|
transfer = peers[:int(math.Sqrt(float64(len(peers))))]
|
|
|
|
}
|
2015-06-18 18:00:19 +03:00
|
|
|
for _, peer := range transfer {
|
2018-05-21 11:32:42 +03:00
|
|
|
peer.AsyncSendNewBlock(block, td)
|
2015-06-18 18:00:19 +03:00
|
|
|
}
|
2022-07-05 06:14:21 +03:00
|
|
|
|
2017-03-02 16:06:16 +03:00
|
|
|
log.Trace("Propagated block", "hash", hash, "recipients", len(transfer), "duration", common.PrettyDuration(time.Since(block.ReceivedAt)))
|
2017-08-18 08:52:16 +03:00
|
|
|
return
|
2015-06-04 18:46:07 +03:00
|
|
|
}
|
2023-04-10 13:36:45 +03:00
|
|
|
// Otherwise if the block is indeed in our own chain, announce it
|
2020-12-14 12:27:15 +03:00
|
|
|
if h.chain.HasBlock(hash, block.NumberU64()) {
|
2015-06-18 18:00:19 +03:00
|
|
|
for _, peer := range peers {
|
2018-05-21 11:32:42 +03:00
|
|
|
peer.AsyncSendNewBlockHash(block)
|
2015-06-18 18:00:19 +03:00
|
|
|
}
|
2017-03-02 16:06:16 +03:00
|
|
|
log.Trace("Announced block", "hash", hash, "recipients", len(peers), "duration", common.PrettyDuration(time.Since(block.ReceivedAt)))
|
2015-04-18 03:21:07 +03:00
|
|
|
}
|
|
|
|
}
|
2015-04-22 18:56:06 +03:00
|
|
|
|
2021-02-17 16:59:00 +03:00
|
|
|
// BroadcastTransactions will propagate a batch of transactions
|
|
|
|
// - To a square root of all peers
|
|
|
|
// - And, separately, as announcements to all peers which are not known to
|
2015-06-26 20:42:27 +03:00
|
|
|
// already have the given transaction.
|
2021-02-17 16:59:00 +03:00
|
|
|
func (h *handler) BroadcastTransactions(txs types.Transactions) {
|
2019-10-28 14:59:07 +03:00
|
|
|
var (
|
2021-02-17 16:59:00 +03:00
|
|
|
annoCount int // Count of announcements made
|
|
|
|
annoPeers int
|
|
|
|
directCount int // Count of the txs sent directly to peers
|
|
|
|
directPeers int // Count of the peers that were sent transactions directly
|
|
|
|
|
|
|
|
txset = make(map[*ethPeer][]common.Hash) // Set peer->hash to transfer directly
|
|
|
|
annos = make(map[*ethPeer][]common.Hash) // Set peer->hash to announce
|
|
|
|
|
2019-10-28 14:59:07 +03:00
|
|
|
)
|
2018-05-10 10:04:45 +03:00
|
|
|
// Broadcast transactions to a batch of peers not knowing about it
|
|
|
|
for _, tx := range txs {
|
2021-02-02 11:44:36 +03:00
|
|
|
peers := h.peers.peersWithoutTransaction(tx.Hash())
|
2023-06-28 12:06:20 +03:00
|
|
|
|
|
|
|
var numDirect int
|
|
|
|
if tx.Size() <= txMaxBroadcastSize {
|
|
|
|
numDirect = int(math.Sqrt(float64(len(peers))))
|
|
|
|
}
|
2021-02-17 16:59:00 +03:00
|
|
|
// Send the tx unconditionally to a subset of our peers
|
|
|
|
for _, peer := range peers[:numDirect] {
|
|
|
|
txset[peer] = append(txset[peer], tx.Hash())
|
|
|
|
}
|
|
|
|
// For the remaining peers, send announcement only
|
|
|
|
for _, peer := range peers[numDirect:] {
|
2019-10-28 14:59:07 +03:00
|
|
|
annos[peer] = append(annos[peer], tx.Hash())
|
2018-05-10 10:04:45 +03:00
|
|
|
}
|
|
|
|
}
|
2021-02-17 16:59:00 +03:00
|
|
|
for peer, hashes := range txset {
|
|
|
|
directPeers++
|
|
|
|
directCount += len(hashes)
|
|
|
|
peer.AsyncSendTransactions(hashes)
|
|
|
|
}
|
2019-10-28 14:59:07 +03:00
|
|
|
for peer, hashes := range annos {
|
2021-02-17 16:59:00 +03:00
|
|
|
annoPeers++
|
|
|
|
annoCount += len(hashes)
|
2021-04-08 18:06:03 +03:00
|
|
|
peer.AsyncSendPooledTransactionHashes(hashes)
|
2015-04-22 18:56:06 +03:00
|
|
|
}
|
2021-02-17 16:59:00 +03:00
|
|
|
log.Debug("Transaction broadcast", "txs", len(txs),
|
|
|
|
"announce packs", annoPeers, "announced hashes", annoCount,
|
|
|
|
"tx packs", directPeers, "broadcast txs", directCount)
|
2015-04-22 18:56:06 +03:00
|
|
|
}
|
|
|
|
|
2022-07-05 06:14:21 +03:00
|
|
|
// ReannounceTransactions will announce a batch of local pending transactions
|
|
|
|
// to a square root of all peers.
|
|
|
|
func (h *handler) ReannounceTransactions(txs types.Transactions) {
|
|
|
|
hashes := make([]common.Hash, 0, txs.Len())
|
|
|
|
for _, tx := range txs {
|
|
|
|
hashes = append(hashes, tx.Hash())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Announce transactions hash to a batch of peers
|
|
|
|
peersCount := uint(math.Sqrt(float64(h.peers.len())))
|
|
|
|
peers := h.peers.headPeers(peersCount)
|
|
|
|
for _, peer := range peers {
|
|
|
|
peer.AsyncSendPooledTransactionHashes(hashes)
|
|
|
|
}
|
|
|
|
log.Debug("Transaction reannounce", "txs", len(txs),
|
|
|
|
"announce packs", peersCount, "announced hashes", peersCount*uint(len(hashes)))
|
|
|
|
}
|
|
|
|
|
2023-04-10 13:36:45 +03:00
|
|
|
// BroadcastVote will propagate a batch of votes to all peers
|
|
|
|
// which are not known to already have the given vote.
|
|
|
|
func (h *handler) BroadcastVote(vote *types.VoteEnvelope) {
|
|
|
|
var (
|
|
|
|
directCount int // Count of announcements made
|
|
|
|
directPeers int
|
|
|
|
|
|
|
|
voteMap = make(map[*ethPeer]*types.VoteEnvelope) // Set peer->hash to transfer directly
|
|
|
|
)
|
|
|
|
|
|
|
|
// Broadcast vote to a batch of peers not knowing about it
|
|
|
|
peers := h.peers.peersWithoutVote(vote.Hash())
|
|
|
|
headBlock := h.chain.CurrentBlock()
|
2023-08-23 12:46:08 +03:00
|
|
|
currentTD := h.chain.GetTd(headBlock.Hash(), headBlock.Number.Uint64())
|
2023-04-10 13:36:45 +03:00
|
|
|
for _, peer := range peers {
|
|
|
|
_, peerTD := peer.Head()
|
|
|
|
deltaTD := new(big.Int).Abs(new(big.Int).Sub(currentTD, peerTD))
|
|
|
|
if deltaTD.Cmp(big.NewInt(deltaTdThreshold)) < 1 && peer.bscExt != nil {
|
|
|
|
voteMap[peer] = vote
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for peer, _vote := range voteMap {
|
|
|
|
directPeers++
|
|
|
|
directCount += 1
|
|
|
|
votes := []*types.VoteEnvelope{_vote}
|
|
|
|
peer.bscExt.AsyncSendVotes(votes)
|
|
|
|
}
|
|
|
|
log.Debug("Vote broadcast", "vote packs", directPeers, "broadcast vote", directCount)
|
|
|
|
}
|
|
|
|
|
2020-03-27 16:03:20 +03:00
|
|
|
// minedBroadcastLoop sends mined blocks to connected peers.
|
2020-12-14 12:27:15 +03:00
|
|
|
func (h *handler) minedBroadcastLoop() {
|
|
|
|
defer h.wg.Done()
|
2020-03-27 16:03:20 +03:00
|
|
|
|
2023-09-24 07:21:00 +03:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case obj := <-h.minedBlockSub.Chan():
|
|
|
|
if obj == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if ev, ok := obj.Data.(core.NewMinedBlockEvent); ok {
|
|
|
|
h.BroadcastBlock(ev.Block, true) // First propagate block to peers
|
|
|
|
h.BroadcastBlock(ev.Block, false) // Only then announce to the rest
|
|
|
|
}
|
|
|
|
case <-h.stopCh:
|
|
|
|
return
|
2015-04-22 18:56:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-27 16:03:20 +03:00
|
|
|
// txBroadcastLoop announces new transactions to connected peers.
|
2020-12-14 12:27:15 +03:00
|
|
|
func (h *handler) txBroadcastLoop() {
|
|
|
|
defer h.wg.Done()
|
2017-08-18 13:58:36 +03:00
|
|
|
for {
|
|
|
|
select {
|
2020-12-14 12:27:15 +03:00
|
|
|
case event := <-h.txsCh:
|
2021-02-17 16:59:00 +03:00
|
|
|
h.BroadcastTransactions(event.Txs)
|
2020-12-14 12:27:15 +03:00
|
|
|
case <-h.txsSub.Err():
|
2017-08-18 13:58:36 +03:00
|
|
|
return
|
2023-09-24 07:21:00 +03:00
|
|
|
case <-h.stopCh:
|
|
|
|
return
|
2017-08-18 13:58:36 +03:00
|
|
|
}
|
2015-04-22 18:56:06 +03:00
|
|
|
}
|
|
|
|
}
|
2022-07-05 06:14:21 +03:00
|
|
|
|
|
|
|
// txReannounceLoop announces local pending transactions to connected peers again.
|
|
|
|
func (h *handler) txReannounceLoop() {
|
|
|
|
defer h.wg.Done()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case event := <-h.reannoTxsCh:
|
|
|
|
h.ReannounceTransactions(event.Txs)
|
|
|
|
case <-h.reannoTxsSub.Err():
|
|
|
|
return
|
2023-09-24 07:21:00 +03:00
|
|
|
case <-h.stopCh:
|
|
|
|
return
|
2022-07-05 06:14:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-10 13:36:45 +03:00
|
|
|
|
|
|
|
// voteBroadcastLoop announces new vote to connected peers.
|
|
|
|
func (h *handler) voteBroadcastLoop() {
|
|
|
|
defer h.wg.Done()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case event := <-h.voteCh:
|
2023-07-11 05:13:08 +03:00
|
|
|
// The timeliness of votes is very important,
|
|
|
|
// so one vote will be sent instantly without waiting for other votes for batch sending by design.
|
2023-04-10 13:36:45 +03:00
|
|
|
h.BroadcastVote(event.Vote)
|
|
|
|
case <-h.votesSub.Err():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
all: activate pbss as experimental feature from eth (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 22:21:36 +03:00
|
|
|
|
|
|
|
// enableSyncedFeatures enables the post-sync functionalities when the initial
|
|
|
|
// sync is finished.
|
|
|
|
func (h *handler) enableSyncedFeatures() {
|
|
|
|
h.acceptTxs.Store(true)
|
2023-11-06 09:11:17 +03:00
|
|
|
// In the bsc scenario, pathdb.MaxDirtyBufferSize (256MB) will be used.
|
|
|
|
// The performance is better than DefaultDirtyBufferSize (64MB).
|
|
|
|
//if h.chain.TrieDB().Scheme() == rawdb.PathScheme {
|
|
|
|
// h.chain.TrieDB().SetBufferSize(pathdb.DefaultDirtyBufferSize)
|
|
|
|
//}
|
all: activate pbss as experimental feature from eth (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 22:21:36 +03:00
|
|
|
}
|