Skip to content

Commit f5ccb5b

Browse files
unclezorovietthang207
authored andcommitted
add directbroadcast flag (bnb-chain#99)
1 parent d1ebbf5 commit f5ccb5b

10 files changed

+47
-24
lines changed

cmd/geth/main.go

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ var (
6868
utils.KeyStoreDirFlag,
6969
utils.ExternalSignerFlag,
7070
utils.NoUSBFlag,
71+
utils.DirectBroadcastFlag,
7172
utils.SmartCardDaemonPathFlag,
7273
utils.OverrideIstanbulFlag,
7374
utils.OverrideMuirGlacierFlag,

cmd/geth/usage.go

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ var AppHelpFlagGroups = []flagGroup{
7070
utils.AncientFlag,
7171
utils.KeyStoreDirFlag,
7272
utils.NoUSBFlag,
73+
utils.DirectBroadcastFlag,
7374
utils.SmartCardDaemonPathFlag,
7475
utils.NetworkIdFlag,
7576
utils.GoerliFlag,

cmd/utils/flags.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ var (
143143
Usage: "Data directory for the databases and keystore",
144144
Value: DirectoryString(node.DefaultDataDir()),
145145
}
146+
DirectBroadcastFlag = cli.BoolFlag{
147+
Name: "directbroadcast",
148+
Usage: "Enable directly broadcast mined block to all peers",
149+
}
146150
AncientFlag = DirectoryFlag{
147151
Name: "datadir.ancient",
148152
Usage: "Data directory for ancient chain segments (default = inside chaindata)",
@@ -787,13 +791,13 @@ var (
787791
Value: "",
788792
}
789793

790-
InitNetworkIps= cli.StringFlag{
794+
InitNetworkIps = cli.StringFlag{
791795
Name: "init.ips",
792796
Usage: "the ips of each node in the network, example '192.168.0.1,192.168.0.2'",
793797
Value: "",
794798
}
795799

796-
InitNetworkPort= cli.IntFlag{
800+
InitNetworkPort = cli.IntFlag{
797801
Name: "init.p2p-port",
798802
Usage: "the p2p port of the nodes in the network",
799803
Value: 30311,
@@ -1243,6 +1247,9 @@ func SetNodeConfig(ctx *cli.Context, cfg *node.Config) {
12431247
if ctx.GlobalIsSet(NoUSBFlag.Name) {
12441248
cfg.NoUSB = ctx.GlobalBool(NoUSBFlag.Name)
12451249
}
1250+
if ctx.GlobalIsSet(DirectBroadcastFlag.Name) {
1251+
cfg.DirectBroadcast = ctx.GlobalBool(DirectBroadcastFlag.Name)
1252+
}
12461253
if ctx.GlobalIsSet(InsecureUnlockAllowedFlag.Name) {
12471254
cfg.InsecureUnlockAllowed = ctx.GlobalBool(InsecureUnlockAllowedFlag.Name)
12481255
}
@@ -1519,6 +1526,9 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *eth.Config) {
15191526
if ctx.GlobalIsSet(GCModeFlag.Name) {
15201527
cfg.NoPruning = ctx.GlobalString(GCModeFlag.Name) == "archive"
15211528
}
1529+
if ctx.GlobalIsSet(DirectBroadcastFlag.Name) {
1530+
cfg.DirectBroadcast = ctx.GlobalBool(DirectBroadcastFlag.Name)
1531+
}
15221532
if ctx.GlobalIsSet(CacheNoPrefetchFlag.Name) {
15231533
cfg.NoPrefetch = ctx.GlobalBool(CacheNoPrefetchFlag.Name)
15241534
}

eth/backend.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
213213
if checkpoint == nil {
214214
checkpoint = params.TrustedCheckpoints[genesisHash]
215215
}
216-
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 {
216+
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 {
217217
return nil, err
218218
}
219219

eth/config.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,9 @@ type Config struct {
103103
// for nodes to connect to.
104104
DiscoveryURLs []string
105105

106-
NoPruning bool // Whether to disable pruning and flush everything to disk
107-
NoPrefetch bool // Whether to disable prefetching and only load state on demand
106+
NoPruning bool // Whether to disable pruning and flush everything to disk
107+
NoPrefetch bool // Whether to disable prefetching and only load state on demand
108+
DirectBroadcast bool
108109

109110
// Whitelist of required block number -> hash values to accept
110111
Whitelist map[uint64]common.Hash `toml:"-"`

eth/handler.go

+20-13
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ type ProtocolManager struct {
6464
networkID uint64
6565
forkFilter forkid.Filter // Fork ID filter, constant across the lifetime of the node
6666

67-
fastSync uint32 // Flag whether fast sync is enabled (gets disabled if we already have blocks)
68-
acceptTxs uint32 // Flag whether we're considered synchronised (enables transaction processing)
67+
fastSync uint32 // Flag whether fast sync is enabled (gets disabled if we already have blocks)
68+
acceptTxs uint32 // Flag whether we're considered synchronised (enables transaction processing)
69+
directBroadcast bool
6970

7071
checkpointNumber uint64 // Block number for the sync progress validator to cross reference
7172
checkpointHash common.Hash // Block hash for the sync progress validator to cross reference
@@ -100,18 +101,19 @@ type ProtocolManager struct {
100101

101102
// NewProtocolManager returns a new Ethereum sub protocol manager. The Ethereum sub protocol manages peers capable
102103
// with the Ethereum network.
103-
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) {
104+
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) {
104105
// Create the protocol manager with the base fields
105106
manager := &ProtocolManager{
106-
networkID: networkID,
107-
forkFilter: forkid.NewFilter(blockchain),
108-
eventMux: mux,
109-
txpool: txpool,
110-
blockchain: blockchain,
111-
peers: newPeerSet(),
112-
whitelist: whitelist,
113-
txsyncCh: make(chan *txsync),
114-
quitSync: make(chan struct{}),
107+
directBroadcast: directBroadcast,
108+
networkID: networkID,
109+
forkFilter: forkid.NewFilter(blockchain),
110+
eventMux: mux,
111+
txpool: txpool,
112+
blockchain: blockchain,
113+
peers: newPeerSet(),
114+
whitelist: whitelist,
115+
txsyncCh: make(chan *txsync),
116+
quitSync: make(chan struct{}),
115117
}
116118

117119
if mode == downloader.FullSync {
@@ -821,7 +823,12 @@ func (pm *ProtocolManager) BroadcastBlock(block *types.Block, propagate bool) {
821823
return
822824
}
823825
// Send the block to a subset of our peers
824-
transfer := peers[:int(math.Sqrt(float64(len(peers))))]
826+
var transfer []*peer
827+
if pm.directBroadcast {
828+
transfer = peers[:int(len(peers))]
829+
} else {
830+
transfer = peers[:int(math.Sqrt(float64(len(peers))))]
831+
}
825832
for _, peer := range transfer {
826833
peer.AsyncSendNewBlock(block, td)
827834
}

eth/handler_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ func testCheckpointChallenge(t *testing.T, syncmode downloader.SyncMode, checkpo
495495
if err != nil {
496496
t.Fatalf("failed to create new blockchain: %v", err)
497497
}
498-
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)
498+
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)
499499
if err != nil {
500500
t.Fatalf("failed to start test protocol manager: %v", err)
501501
}
@@ -582,7 +582,7 @@ func testBroadcastBlock(t *testing.T, totalPeers, broadcastExpected int) {
582582
if err != nil {
583583
t.Fatalf("failed to create new blockchain: %v", err)
584584
}
585-
pm, err := NewProtocolManager(config, nil, downloader.FullSync, DefaultConfig.NetworkId, evmux, &testTxPool{pool: make(map[common.Hash]*types.Transaction)}, pow, blockchain, db, 1, nil)
585+
pm, err := NewProtocolManager(config, nil, downloader.FullSync, DefaultConfig.NetworkId, evmux, &testTxPool{pool: make(map[common.Hash]*types.Transaction)}, pow, blockchain, db, 1, nil, false)
586586
if err != nil {
587587
t.Fatalf("failed to start test protocol manager: %v", err)
588588
}
@@ -643,7 +643,7 @@ func TestBroadcastMalformedBlock(t *testing.T) {
643643
if err != nil {
644644
t.Fatalf("failed to create new blockchain: %v", err)
645645
}
646-
pm, err := NewProtocolManager(config, nil, downloader.FullSync, DefaultConfig.NetworkId, new(event.TypeMux), new(testTxPool), engine, blockchain, db, 1, nil)
646+
pm, err := NewProtocolManager(config, nil, downloader.FullSync, DefaultConfig.NetworkId, new(event.TypeMux), new(testTxPool), engine, blockchain, db, 1, nil, false)
647647
if err != nil {
648648
t.Fatalf("failed to start test protocol manager: %v", err)
649649
}

eth/helper_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func newTestProtocolManager(mode downloader.SyncMode, blocks int, generator func
6868
if _, err := blockchain.InsertChain(chain); err != nil {
6969
panic(err)
7070
}
71-
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)
71+
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)
7272
if err != nil {
7373
return nil, nil, err
7474
}

eth/protocol_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,8 @@ func TestForkIDSplit(t *testing.T) {
181181
blocksNoFork, _ = core.GenerateChain(configNoFork, genesisNoFork, engine, dbNoFork, 2, nil)
182182
blocksProFork, _ = core.GenerateChain(configProFork, genesisProFork, engine, dbProFork, 2, nil)
183183

184-
ethNoFork, _ = NewProtocolManager(configNoFork, nil, downloader.FullSync, 1, new(event.TypeMux), &testTxPool{pool: make(map[common.Hash]*types.Transaction)}, engine, chainNoFork, dbNoFork, 1, nil)
185-
ethProFork, _ = NewProtocolManager(configProFork, nil, downloader.FullSync, 1, new(event.TypeMux), &testTxPool{pool: make(map[common.Hash]*types.Transaction)}, engine, chainProFork, dbProFork, 1, nil)
184+
ethNoFork, _ = NewProtocolManager(configNoFork, nil, downloader.FullSync, 1, new(event.TypeMux), &testTxPool{pool: make(map[common.Hash]*types.Transaction)}, engine, chainNoFork, dbNoFork, 1, nil, false)
185+
ethProFork, _ = NewProtocolManager(configProFork, nil, downloader.FullSync, 1, new(event.TypeMux), &testTxPool{pool: make(map[common.Hash]*types.Transaction)}, engine, chainProFork, dbProFork, 1, nil, false)
186186
)
187187
ethNoFork.Start(1000)
188188
ethProFork.Start(1000)

node/config.go

+3
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ type Config struct {
9595
// NoUSB disables hardware wallet monitoring and connectivity.
9696
NoUSB bool `toml:",omitempty"`
9797

98+
// DirectBroadcast enable directly broadcast mined block to all peers
99+
DirectBroadcast bool `toml:",omitempty"`
100+
98101
// SmartCardDaemonPath is the path to the smartcard daemon's socket
99102
SmartCardDaemonPath string `toml:",omitempty"`
100103

0 commit comments

Comments
 (0)