eth: reject mining request if node is not synced

This commit is contained in:
rjl493456442 2019-06-12 20:57:41 +08:00
parent c8c3ebd593
commit 363e49b590
4 changed files with 11 additions and 11 deletions

View file

@ -411,6 +411,10 @@ func (s *Ethereum) SetEtherbase(etherbase common.Address) {
// is already running, this method adjust the number of threads allowed to use
// and updates the minimum price required by the transaction pool.
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
type threaded interface {
SetThreads(threads int)
@ -444,10 +448,6 @@ func (s *Ethereum) StartMining(threads int) error {
}
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)
}
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) NetVersion() uint64 { return s.networkID }
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 }
// Protocols implements node.Service, returning all the currently configured

View file

@ -69,8 +69,8 @@ func errResp(code errCode, format string, v ...interface{}) error {
type ProtocolManager struct {
networkID uint64
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)
synced uint32 // Flag whether we're considered synchronised (enables transaction processing)
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
@ -207,7 +207,7 @@ func NewProtocolManager(config *params.ChainConfig, mode downloader.SyncMode, ne
}
n, err := manager.blockchain.InsertChain(blocks)
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
}
@ -706,7 +706,7 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
case msg.Code == TxMsg:
// 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
}
// Transactions can be processed, parse all of them and deliver to the pool

View file

@ -99,7 +99,7 @@ func TestRecvTransactions63(t *testing.T) { testRecvTransactions(t, 63) }
func testRecvTransactions(t *testing.T, protocol int) {
txAdded := make(chan []*types.Transaction)
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)
defer pm.Stop()
defer p.close()

View file

@ -209,7 +209,7 @@ func (pm *ProtocolManager) synchronise(peer *peer) {
// Checkpoint passed, sanity check the timestamp to have a fallback mechanism
// for non-checkpointed (number = 0) private networks.
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 {