Skip to content

Commit bfe5eb7

Browse files
karalabefjl
authored andcommitted
eth: accept transactions when starting CPU mining (#13882)
1 parent f32b72c commit bfe5eb7

6 files changed

Lines changed: 18 additions & 10 deletions

File tree

‎cmd/geth/main.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ func startNode(ctx *cli.Context, stack *node.Node) {
301301
th.SetThreads(threads)
302302
}
303303
}
304-
if err := ethereum.StartMining(); err != nil {
304+
if err := ethereum.StartMining(true); err != nil {
305305
utils.Fatalf("Failed to start mining: %v", err)
306306
}
307307
}

‎eth/api.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func (api *PublicMinerAPI) SubmitWork(nonce types.BlockNonce, solution, digest c
103103
// result[2], 32 bytes hex encoded boundary condition ("target"), 2^256/difficulty
104104
func (api *PublicMinerAPI) GetWork() ([3]string, error) {
105105
if !api.e.IsMining() {
106-
if err := api.e.StartMining(); err != nil {
106+
if err := api.e.StartMining(false); err != nil {
107107
return [3]string{}, err
108108
}
109109
}
@@ -153,7 +153,7 @@ func (api *PrivateMinerAPI) Start(threads *int) error {
153153
}
154154
// Start the miner and return
155155
if !api.e.IsMining() {
156-
return api.e.StartMining()
156+
return api.e.StartMining(true)
157157
}
158158
return nil
159159
}

‎eth/backend.go‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"math/big"
2323
"regexp"
2424
"sync"
25+
"sync/atomic"
2526

2627
"github.com/ethereum/go-ethereum/accounts"
2728
"github.com/ethereum/go-ethereum/common"
@@ -326,12 +327,19 @@ func (self *Ethereum) SetEtherbase(etherbase common.Address) {
326327
self.miner.SetEtherbase(etherbase)
327328
}
328329

329-
func (s *Ethereum) StartMining() error {
330+
func (s *Ethereum) StartMining(local bool) error {
330331
eb, err := s.Etherbase()
331332
if err != nil {
332333
log.Error("Cannot start mining without etherbase", "err", err)
333334
return fmt.Errorf("etherbase missing: %v", err)
334335
}
336+
if local {
337+
// If local (CPU) mining is started, we can disable the transaction rejection
338+
// mechanism introduced to speed sync times. CPU mining on mainnet is ludicrous
339+
// so noone will ever hit this path, whereas marking sync done on CPU mining
340+
// will ensure that private networks work in single miner mode too.
341+
atomic.StoreUint32(&s.protocolManager.acceptTxs, 1)
342+
}
335343
go s.miner.Start(eb)
336344
return nil
337345
}

‎eth/handler.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ func errResp(code errCode, format string, v ...interface{}) error {
6262
type ProtocolManager struct {
6363
networkId int
6464

65-
fastSync uint32 // Flag whether fast sync is enabled (gets disabled if we already have blocks)
66-
synced uint32 // Flag whether we're considered synchronised (enables transaction processing)
65+
fastSync uint32 // Flag whether fast sync is enabled (gets disabled if we already have blocks)
66+
acceptTxs uint32 // Flag whether we're considered synchronised (enables transaction processing)
6767

6868
txpool txPool
6969
blockchain *core.BlockChain
@@ -171,7 +171,7 @@ func NewProtocolManager(config *params.ChainConfig, fastSync bool, networkId int
171171
return blockchain.CurrentBlock().NumberU64()
172172
}
173173
inserter := func(blocks types.Blocks) (int, error) {
174-
atomic.StoreUint32(&manager.synced, 1) // Mark initial sync done on any fetcher import
174+
atomic.StoreUint32(&manager.acceptTxs, 1) // Mark initial sync done on any fetcher import
175175
return manager.blockchain.InsertChain(blocks)
176176
}
177177
manager.fetcher = fetcher.New(blockchain.GetBlockByHash, validator, manager.BroadcastBlock, heighter, inserter, manager.removePeer)
@@ -643,7 +643,7 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
643643

644644
case msg.Code == TxMsg:
645645
// Transactions arrived, make sure we have a valid and fresh chain to handle them
646-
if atomic.LoadUint32(&pm.synced) == 0 {
646+
if atomic.LoadUint32(&pm.acceptTxs) == 0 {
647647
break
648648
}
649649
// Transactions can be processed, parse all of them and deliver to the pool

‎eth/protocol_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func TestRecvTransactions63(t *testing.T) { testRecvTransactions(t, 63) }
9494
func testRecvTransactions(t *testing.T, protocol int) {
9595
txAdded := make(chan []*types.Transaction)
9696
pm := newTestProtocolManagerMust(t, false, 0, nil, txAdded)
97-
pm.synced = 1 // mark synced to accept transactions
97+
pm.acceptTxs = 1 // mark synced to accept transactions
9898
p, _ := newTestPeer("peer", protocol, pm, true)
9999
defer pm.Stop()
100100
defer p.close()

‎eth/sync.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func (pm *ProtocolManager) synchronise(peer *peer) {
188188
if err := pm.downloader.Synchronise(peer.id, pHead, pTd, mode); err != nil {
189189
return
190190
}
191-
atomic.StoreUint32(&pm.synced, 1) // Mark initial sync done
191+
atomic.StoreUint32(&pm.acceptTxs, 1) // Mark initial sync done
192192
if head := pm.blockchain.CurrentBlock(); head.NumberU64() > 0 {
193193
// We've completed a sync cycle, notify all peers of new state. This path is
194194
// essential in star-topology networks where a gateway node needs to notify

0 commit comments

Comments
 (0)