mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
eth: reject mining request if node is not synced
This commit is contained in:
parent
c8c3ebd593
commit
363e49b590
4 changed files with 11 additions and 11 deletions
|
|
@ -411,6 +411,10 @@ func (s *Ethereum) SetEtherbase(etherbase common.Address) {
|
||||||
// is already running, this method adjust the number of threads allowed to use
|
// is already running, this method adjust the number of threads allowed to use
|
||||||
// and updates the minimum price required by the transaction pool.
|
// and updates the minimum price required by the transaction pool.
|
||||||
func (s *Ethereum) StartMining(threads int) error {
|
func (s *Ethereum) StartMining(threads int) error {
|
||||||
|
// Reject mining request if the node is not synced.
|
||||||
|
if atomic.LoadUint32(&s.protocolManager.synced) == 0 {
|
||||||
|
return errors.New("node is not synced")
|
||||||
|
}
|
||||||
// Update the thread count within the consensus engine
|
// Update the thread count within the consensus engine
|
||||||
type threaded interface {
|
type threaded interface {
|
||||||
SetThreads(threads int)
|
SetThreads(threads int)
|
||||||
|
|
@ -444,10 +448,6 @@ func (s *Ethereum) StartMining(threads int) error {
|
||||||
}
|
}
|
||||||
clique.Authorize(eb, wallet.SignData)
|
clique.Authorize(eb, wallet.SignData)
|
||||||
}
|
}
|
||||||
// If mining is started, we can disable the transaction rejection mechanism
|
|
||||||
// introduced to speed sync times.
|
|
||||||
atomic.StoreUint32(&s.protocolManager.acceptTxs, 1)
|
|
||||||
|
|
||||||
go s.miner.Start(eb)
|
go s.miner.Start(eb)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
@ -480,7 +480,7 @@ func (s *Ethereum) IsListening() bool { return true } // Always
|
||||||
func (s *Ethereum) EthVersion() int { return int(s.protocolManager.SubProtocols[0].Version) }
|
func (s *Ethereum) EthVersion() int { return int(s.protocolManager.SubProtocols[0].Version) }
|
||||||
func (s *Ethereum) NetVersion() uint64 { return s.networkID }
|
func (s *Ethereum) NetVersion() uint64 { return s.networkID }
|
||||||
func (s *Ethereum) Downloader() *downloader.Downloader { return s.protocolManager.downloader }
|
func (s *Ethereum) Downloader() *downloader.Downloader { return s.protocolManager.downloader }
|
||||||
func (s *Ethereum) Synced() bool { return atomic.LoadUint32(&s.protocolManager.acceptTxs) == 1 }
|
func (s *Ethereum) Synced() bool { return atomic.LoadUint32(&s.protocolManager.synced) == 1 }
|
||||||
func (s *Ethereum) ArchiveMode() bool { return s.config.NoPruning }
|
func (s *Ethereum) ArchiveMode() bool { return s.config.NoPruning }
|
||||||
|
|
||||||
// Protocols implements node.Service, returning all the currently configured
|
// Protocols implements node.Service, returning all the currently configured
|
||||||
|
|
|
||||||
|
|
@ -69,8 +69,8 @@ func errResp(code errCode, format string, v ...interface{}) error {
|
||||||
type ProtocolManager struct {
|
type ProtocolManager struct {
|
||||||
networkID uint64
|
networkID uint64
|
||||||
|
|
||||||
fastSync uint32 // Flag whether fast sync is enabled (gets disabled if we already have blocks)
|
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)
|
synced uint32 // Flag whether we're considered synchronised (enables transaction processing)
|
||||||
|
|
||||||
checkpointNumber uint64 // Block number for the sync progress validator to cross reference
|
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
|
checkpointHash common.Hash // Block hash for the sync progress validator to cross reference
|
||||||
|
|
@ -207,7 +207,7 @@ func NewProtocolManager(config *params.ChainConfig, mode downloader.SyncMode, ne
|
||||||
}
|
}
|
||||||
n, err := manager.blockchain.InsertChain(blocks)
|
n, err := manager.blockchain.InsertChain(blocks)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
atomic.StoreUint32(&manager.acceptTxs, 1) // Mark initial sync done on any fetcher import
|
atomic.StoreUint32(&manager.synced, 1) // Mark initial sync done on any fetcher import
|
||||||
}
|
}
|
||||||
return n, err
|
return n, err
|
||||||
}
|
}
|
||||||
|
|
@ -706,7 +706,7 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
|
||||||
|
|
||||||
case msg.Code == TxMsg:
|
case msg.Code == TxMsg:
|
||||||
// Transactions arrived, make sure we have a valid and fresh chain to handle them
|
// Transactions arrived, make sure we have a valid and fresh chain to handle them
|
||||||
if atomic.LoadUint32(&pm.acceptTxs) == 0 {
|
if atomic.LoadUint32(&pm.synced) == 0 {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
// Transactions can be processed, parse all of them and deliver to the pool
|
// Transactions can be processed, parse all of them and deliver to the pool
|
||||||
|
|
|
||||||
|
|
@ -99,7 +99,7 @@ func TestRecvTransactions63(t *testing.T) { testRecvTransactions(t, 63) }
|
||||||
func testRecvTransactions(t *testing.T, protocol int) {
|
func testRecvTransactions(t *testing.T, protocol int) {
|
||||||
txAdded := make(chan []*types.Transaction)
|
txAdded := make(chan []*types.Transaction)
|
||||||
pm, _ := newTestProtocolManagerMust(t, downloader.FullSync, 0, nil, txAdded)
|
pm, _ := newTestProtocolManagerMust(t, downloader.FullSync, 0, nil, txAdded)
|
||||||
pm.acceptTxs = 1 // mark synced to accept transactions
|
pm.synced = 1 // mark synced to accept transactions
|
||||||
p, _ := newTestPeer("peer", protocol, pm, true)
|
p, _ := newTestPeer("peer", protocol, pm, true)
|
||||||
defer pm.Stop()
|
defer pm.Stop()
|
||||||
defer p.close()
|
defer p.close()
|
||||||
|
|
|
||||||
|
|
@ -209,7 +209,7 @@ func (pm *ProtocolManager) synchronise(peer *peer) {
|
||||||
// Checkpoint passed, sanity check the timestamp to have a fallback mechanism
|
// Checkpoint passed, sanity check the timestamp to have a fallback mechanism
|
||||||
// for non-checkpointed (number = 0) private networks.
|
// for non-checkpointed (number = 0) private networks.
|
||||||
if head.Time() >= uint64(time.Now().AddDate(0, -1, 0).Unix()) {
|
if head.Time() >= uint64(time.Now().AddDate(0, -1, 0).Unix()) {
|
||||||
atomic.StoreUint32(&pm.acceptTxs, 1)
|
atomic.StoreUint32(&pm.synced, 1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if head.NumberU64() > 0 {
|
if head.NumberU64() > 0 {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue