diff --git a/eth/backend.go b/eth/backend.go index 37cd23f366..93004cc196 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -351,6 +351,10 @@ func makeExtraData(extra []byte) []byte { return extra } +func (s *Ethereum) PeerCount() int { + return s.p2pServer.PeerCount() +} + // APIs return the collection of RPC services the ethereum package offers. // NOTE, some of these services probably need to be moved to somewhere else. func (s *Ethereum) APIs() []rpc.API { diff --git a/miner/fake_miner.go b/miner/fake_miner.go index b0501c11f8..b3b58c0dc2 100644 --- a/miner/fake_miner.go +++ b/miner/fake_miner.go @@ -172,6 +172,11 @@ type mockBackend struct { txPool *txpool.TxPool } +// PeerCount implements Backend. +func (*mockBackend) PeerCount() int { + panic("unimplemented") +} + func NewMockBackend(bc *core.BlockChain, txPool *txpool.TxPool) *mockBackend { return &mockBackend{ bc: bc, diff --git a/miner/miner.go b/miner/miner.go index dbec7a4717..3735f0ac6d 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -41,6 +41,7 @@ import ( type Backend interface { BlockChain() *core.BlockChain TxPool() *txpool.TxPool + PeerCount() int } // Config is the configuration parameters of mining. diff --git a/miner/test_backend.go b/miner/test_backend.go index e8acf42edf..67e9f53e94 100644 --- a/miner/test_backend.go +++ b/miner/test_backend.go @@ -95,6 +95,11 @@ type testWorkerBackend struct { uncleBlock *types.Block } +// PeerCount implements Backend. +func (*testWorkerBackend) PeerCount() int { + panic("unimplemented") +} + func newTestWorkerBackend(t TensingObject, chainConfig *params.ChainConfig, engine consensus.Engine, db ethdb.Database, n int) *testWorkerBackend { var gspec = core.Genesis{ Config: chainConfig, diff --git a/miner/worker.go b/miner/worker.go index bc2523a3c3..c74a0aff62 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -657,8 +657,15 @@ func (w *worker) mainLoop(ctx context.Context) { for { select { case req := <-w.newWorkCh: - //nolint:contextcheck - w.commitWork(req.ctx, req.interrupt, req.noempty, req.timestamp) + if w.chainConfig.ChainID.Cmp(params.BorMainnetChainConfig.ChainID) == 0 || w.chainConfig.ChainID.Cmp(params.MumbaiChainConfig.ChainID) == 0 { + if w.eth.PeerCount() > 0 { + //nolint:contextcheck + w.commitWork(req.ctx, req.interrupt, req.noempty, req.timestamp) + } + } else { + //nolint:contextcheck + w.commitWork(req.ctx, req.interrupt, req.noempty, req.timestamp) + } case req := <-w.getWorkCh: block, fees, err := w.generateWork(req.ctx, req.params) diff --git a/p2p/server.go b/p2p/server.go index dd1cb327fc..3a013458fc 100644 --- a/p2p/server.go +++ b/p2p/server.go @@ -356,6 +356,10 @@ func (srv *Server) SetMaxPeers(maxPeers int) { // PeerCount returns the number of connected peers. func (srv *Server) PeerCount() int { + if !srv.running { + return 0 + } + var count int srv.doPeerOp(func(ps map[enode.ID]*Peer) { diff --git a/params/config.go b/params/config.go index 5c82c7b848..8f54f1cb30 100644 --- a/params/config.go +++ b/params/config.go @@ -229,7 +229,7 @@ var ( // BorTestChainConfig contains the chain parameters to run a node on the Test network. BorTestChainConfig = &ChainConfig{ - ChainID: big.NewInt(80001), + ChainID: big.NewInt(80002), HomesteadBlock: big.NewInt(0), DAOForkBlock: nil, DAOForkSupport: true, @@ -264,7 +264,7 @@ var ( }, } BorUnittestChainConfig = &ChainConfig{ - ChainID: big.NewInt(80001), + ChainID: big.NewInt(80003), HomesteadBlock: big.NewInt(0), DAOForkBlock: nil, DAOForkSupport: true,