From 89eb1702c8b7d46761cb9e2929ef6dcacbd7db21 Mon Sep 17 00:00:00 2001 From: fudongbai <296179868@qq.com> Date: Mon, 1 Mar 2021 19:27:29 +0800 Subject: [PATCH] add directbroadcast flag --- cmd/geth/main.go | 1 + cmd/geth/usage.go | 1 + cmd/utils/flags.go | 14 ++++++++++++-- eth/backend.go | 2 +- eth/config.go | 5 +++-- eth/handler.go | 33 ++++++++++++++++++++------------- eth/handler_test.go | 6 +++--- eth/helper_test.go | 2 +- eth/protocol_test.go | 4 ++-- node/config.go | 3 +++ 10 files changed, 47 insertions(+), 24 deletions(-) diff --git a/cmd/geth/main.go b/cmd/geth/main.go index d3a8108fa7..7ad300a112 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -68,6 +68,7 @@ var ( utils.KeyStoreDirFlag, utils.ExternalSignerFlag, utils.NoUSBFlag, + utils.DirectBroadcastFlag, utils.SmartCardDaemonPathFlag, utils.OverrideIstanbulFlag, utils.OverrideMuirGlacierFlag, diff --git a/cmd/geth/usage.go b/cmd/geth/usage.go index c5bfd295d0..e91be898c8 100644 --- a/cmd/geth/usage.go +++ b/cmd/geth/usage.go @@ -70,6 +70,7 @@ var AppHelpFlagGroups = []flagGroup{ utils.AncientFlag, utils.KeyStoreDirFlag, utils.NoUSBFlag, + utils.DirectBroadcastFlag, utils.SmartCardDaemonPathFlag, utils.NetworkIdFlag, utils.GoerliFlag, diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index ef81f5e95c..c1b1ae94af 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -143,6 +143,10 @@ var ( Usage: "Data directory for the databases and keystore", Value: DirectoryString(node.DefaultDataDir()), } + DirectBroadcastFlag = cli.BoolFlag{ + Name: "directbroadcast", + Usage: "Enable directly broadcast mined block to all peers", + } AncientFlag = DirectoryFlag{ Name: "datadir.ancient", Usage: "Data directory for ancient chain segments (default = inside chaindata)", @@ -787,13 +791,13 @@ var ( Value: "", } - InitNetworkIps= cli.StringFlag{ + InitNetworkIps = cli.StringFlag{ Name: "init.ips", Usage: "the ips of each node in the network, example '192.168.0.1,192.168.0.2'", Value: "", } - InitNetworkPort= cli.IntFlag{ + InitNetworkPort = cli.IntFlag{ Name: "init.p2p-port", Usage: "the p2p port of the nodes in the network", Value: 30311, @@ -1243,6 +1247,9 @@ func SetNodeConfig(ctx *cli.Context, cfg *node.Config) { if ctx.GlobalIsSet(NoUSBFlag.Name) { cfg.NoUSB = ctx.GlobalBool(NoUSBFlag.Name) } + if ctx.GlobalIsSet(DirectBroadcastFlag.Name) { + cfg.DirectBroadcast = ctx.GlobalBool(DirectBroadcastFlag.Name) + } if ctx.GlobalIsSet(InsecureUnlockAllowedFlag.Name) { cfg.InsecureUnlockAllowed = ctx.GlobalBool(InsecureUnlockAllowedFlag.Name) } @@ -1519,6 +1526,9 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *eth.Config) { if ctx.GlobalIsSet(GCModeFlag.Name) { cfg.NoPruning = ctx.GlobalString(GCModeFlag.Name) == "archive" } + if ctx.GlobalIsSet(DirectBroadcastFlag.Name) { + cfg.DirectBroadcast = ctx.GlobalBool(DirectBroadcastFlag.Name) + } if ctx.GlobalIsSet(CacheNoPrefetchFlag.Name) { cfg.NoPrefetch = ctx.GlobalBool(CacheNoPrefetchFlag.Name) } diff --git a/eth/backend.go b/eth/backend.go index 961603bd65..4bfc0d097c 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -213,7 +213,7 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) { if checkpoint == nil { checkpoint = params.TrustedCheckpoints[genesisHash] } - if eth.protocolManager, err = NewProtocolManager(chainConfig, checkpoint, config.SyncMode, config.NetworkId, eth.eventMux, eth.txPool, eth.engine, eth.blockchain, chainDb, cacheLimit, config.Whitelist); err != nil { + if eth.protocolManager, err = NewProtocolManager(chainConfig, checkpoint, config.SyncMode, config.NetworkId, eth.eventMux, eth.txPool, eth.engine, eth.blockchain, chainDb, cacheLimit, config.Whitelist, config.DirectBroadcast); err != nil { return nil, err } diff --git a/eth/config.go b/eth/config.go index 898d7f5bbe..6cf3e4db36 100644 --- a/eth/config.go +++ b/eth/config.go @@ -103,8 +103,9 @@ type Config struct { // for nodes to connect to. DiscoveryURLs []string - NoPruning bool // Whether to disable pruning and flush everything to disk - NoPrefetch bool // Whether to disable prefetching and only load state on demand + NoPruning bool // Whether to disable pruning and flush everything to disk + NoPrefetch bool // Whether to disable prefetching and only load state on demand + DirectBroadcast bool // Whitelist of required block number -> hash values to accept Whitelist map[uint64]common.Hash `toml:"-"` diff --git a/eth/handler.go b/eth/handler.go index 9a02f1f20f..6a1bc369a4 100644 --- a/eth/handler.go +++ b/eth/handler.go @@ -64,8 +64,9 @@ type ProtocolManager struct { networkID uint64 forkFilter forkid.Filter // Fork ID filter, constant across the lifetime of the node - fastSync uint32 // Flag whether fast sync is enabled (gets disabled if we already have blocks) - acceptTxs uint32 // Flag whether we're considered synchronised (enables transaction processing) + fastSync uint32 // Flag whether fast sync is enabled (gets disabled if we already have blocks) + acceptTxs uint32 // Flag whether we're considered synchronised (enables transaction processing) + directBroadcast bool checkpointNumber uint64 // Block number for the sync progress validator to cross reference checkpointHash common.Hash // Block hash for the sync progress validator to cross reference @@ -100,18 +101,19 @@ type ProtocolManager struct { // NewProtocolManager returns a new Ethereum sub protocol manager. The Ethereum sub protocol manages peers capable // with the Ethereum network. -func NewProtocolManager(config *params.ChainConfig, checkpoint *params.TrustedCheckpoint, mode downloader.SyncMode, networkID uint64, mux *event.TypeMux, txpool txPool, engine consensus.Engine, blockchain *core.BlockChain, chaindb ethdb.Database, cacheLimit int, whitelist map[uint64]common.Hash) (*ProtocolManager, error) { +func NewProtocolManager(config *params.ChainConfig, checkpoint *params.TrustedCheckpoint, mode downloader.SyncMode, networkID uint64, mux *event.TypeMux, txpool txPool, engine consensus.Engine, blockchain *core.BlockChain, chaindb ethdb.Database, cacheLimit int, whitelist map[uint64]common.Hash, directBroadcast bool) (*ProtocolManager, error) { // Create the protocol manager with the base fields manager := &ProtocolManager{ - networkID: networkID, - forkFilter: forkid.NewFilter(blockchain), - eventMux: mux, - txpool: txpool, - blockchain: blockchain, - peers: newPeerSet(), - whitelist: whitelist, - txsyncCh: make(chan *txsync), - quitSync: make(chan struct{}), + directBroadcast: directBroadcast, + networkID: networkID, + forkFilter: forkid.NewFilter(blockchain), + eventMux: mux, + txpool: txpool, + blockchain: blockchain, + peers: newPeerSet(), + whitelist: whitelist, + txsyncCh: make(chan *txsync), + quitSync: make(chan struct{}), } if mode == downloader.FullSync { @@ -821,7 +823,12 @@ func (pm *ProtocolManager) BroadcastBlock(block *types.Block, propagate bool) { return } // Send the block to a subset of our peers - transfer := peers[:int(math.Sqrt(float64(len(peers))))] + var transfer []*peer + if pm.directBroadcast { + transfer = peers[:int(len(peers))] + } else { + transfer = peers[:int(math.Sqrt(float64(len(peers))))] + } for _, peer := range transfer { peer.AsyncSendNewBlock(block, td) } diff --git a/eth/handler_test.go b/eth/handler_test.go index 1b398a8c08..fd5898d6e6 100644 --- a/eth/handler_test.go +++ b/eth/handler_test.go @@ -495,7 +495,7 @@ func testCheckpointChallenge(t *testing.T, syncmode downloader.SyncMode, checkpo if err != nil { t.Fatalf("failed to create new blockchain: %v", err) } - pm, err := NewProtocolManager(config, cht, syncmode, DefaultConfig.NetworkId, new(event.TypeMux), &testTxPool{pool: make(map[common.Hash]*types.Transaction)}, ethash.NewFaker(), blockchain, db, 1, nil) + pm, err := NewProtocolManager(config, cht, syncmode, DefaultConfig.NetworkId, new(event.TypeMux), &testTxPool{pool: make(map[common.Hash]*types.Transaction)}, ethash.NewFaker(), blockchain, db, 1, nil, false) if err != nil { t.Fatalf("failed to start test protocol manager: %v", err) } @@ -582,7 +582,7 @@ func testBroadcastBlock(t *testing.T, totalPeers, broadcastExpected int) { if err != nil { t.Fatalf("failed to create new blockchain: %v", err) } - pm, err := NewProtocolManager(config, nil, downloader.FullSync, DefaultConfig.NetworkId, evmux, &testTxPool{pool: make(map[common.Hash]*types.Transaction)}, pow, blockchain, db, 1, nil) + pm, err := NewProtocolManager(config, nil, downloader.FullSync, DefaultConfig.NetworkId, evmux, &testTxPool{pool: make(map[common.Hash]*types.Transaction)}, pow, blockchain, db, 1, nil, false) if err != nil { t.Fatalf("failed to start test protocol manager: %v", err) } @@ -643,7 +643,7 @@ func TestBroadcastMalformedBlock(t *testing.T) { if err != nil { t.Fatalf("failed to create new blockchain: %v", err) } - pm, err := NewProtocolManager(config, nil, downloader.FullSync, DefaultConfig.NetworkId, new(event.TypeMux), new(testTxPool), engine, blockchain, db, 1, nil) + pm, err := NewProtocolManager(config, nil, downloader.FullSync, DefaultConfig.NetworkId, new(event.TypeMux), new(testTxPool), engine, blockchain, db, 1, nil, false) if err != nil { t.Fatalf("failed to start test protocol manager: %v", err) } diff --git a/eth/helper_test.go b/eth/helper_test.go index 3338af71d5..83ba7245a1 100644 --- a/eth/helper_test.go +++ b/eth/helper_test.go @@ -68,7 +68,7 @@ func newTestProtocolManager(mode downloader.SyncMode, blocks int, generator func if _, err := blockchain.InsertChain(chain); err != nil { panic(err) } - pm, err := NewProtocolManager(gspec.Config, nil, mode, DefaultConfig.NetworkId, evmux, &testTxPool{added: newtx, pool: make(map[common.Hash]*types.Transaction)}, engine, blockchain, db, 1, nil) + pm, err := NewProtocolManager(gspec.Config, nil, mode, DefaultConfig.NetworkId, evmux, &testTxPool{added: newtx, pool: make(map[common.Hash]*types.Transaction)}, engine, blockchain, db, 1, nil, false) if err != nil { return nil, nil, err } diff --git a/eth/protocol_test.go b/eth/protocol_test.go index a313e4e6cf..5f57c87175 100644 --- a/eth/protocol_test.go +++ b/eth/protocol_test.go @@ -181,8 +181,8 @@ func TestForkIDSplit(t *testing.T) { blocksNoFork, _ = core.GenerateChain(configNoFork, genesisNoFork, engine, dbNoFork, 2, nil) blocksProFork, _ = core.GenerateChain(configProFork, genesisProFork, engine, dbProFork, 2, nil) - ethNoFork, _ = NewProtocolManager(configNoFork, nil, downloader.FullSync, 1, new(event.TypeMux), &testTxPool{pool: make(map[common.Hash]*types.Transaction)}, engine, chainNoFork, dbNoFork, 1, nil) - ethProFork, _ = NewProtocolManager(configProFork, nil, downloader.FullSync, 1, new(event.TypeMux), &testTxPool{pool: make(map[common.Hash]*types.Transaction)}, engine, chainProFork, dbProFork, 1, nil) + ethNoFork, _ = NewProtocolManager(configNoFork, nil, downloader.FullSync, 1, new(event.TypeMux), &testTxPool{pool: make(map[common.Hash]*types.Transaction)}, engine, chainNoFork, dbNoFork, 1, nil, false) + ethProFork, _ = NewProtocolManager(configProFork, nil, downloader.FullSync, 1, new(event.TypeMux), &testTxPool{pool: make(map[common.Hash]*types.Transaction)}, engine, chainProFork, dbProFork, 1, nil, false) ) ethNoFork.Start(1000) ethProFork.Start(1000) diff --git a/node/config.go b/node/config.go index 612042fdef..e4c7af9675 100644 --- a/node/config.go +++ b/node/config.go @@ -95,6 +95,9 @@ type Config struct { // NoUSB disables hardware wallet monitoring and connectivity. NoUSB bool `toml:",omitempty"` + // DirectBroadcast enable directly broadcast mined block to all peers + DirectBroadcast bool `toml:",omitempty"` + // SmartCardDaemonPath is the path to the smartcard daemon's socket SmartCardDaemonPath string `toml:",omitempty"`