From d90c7c137809dfe03cc71d51892facba4ba4caae Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Fri, 2 Feb 2018 13:08:58 +0100 Subject: [PATCH] chainstats, core: modify chainstats to use uint64, merge setters --- common/chainstats/chainstats.go | 41 +++++++++++++++++---------------- core/blockchain.go | 19 ++++++--------- eth/sync.go | 3 +-- 3 files changed, 29 insertions(+), 34 deletions(-) diff --git a/common/chainstats/chainstats.go b/common/chainstats/chainstats.go index e0aba99711..c562879af5 100644 --- a/common/chainstats/chainstats.go +++ b/common/chainstats/chainstats.go @@ -26,49 +26,50 @@ import ( ) type Chainstats struct { - currentBlockNumber atomic.Value - currentFastBlockNumber atomic.Value + currentBlockNumber uint64 + currentFastBlockNumber uint64 currentTd atomic.Value } func NewChainstats() *Chainstats { stats := &Chainstats{} - stats.currentBlockNumber.Store(big.NewInt(0)) - stats.currentFastBlockNumber.Store(big.NewInt(0)) stats.currentTd.Store(big.NewInt(0)) return stats } // GetNumber returns the latest block number func (stats *Chainstats) GetNumber() uint64 { - return stats.currentBlockNumber.Load().(*big.Int).Uint64() + return stats.currentBlockNumber } -// UpdateNumbers is a convenience method to set both latest number and fast number -func (stats *Chainstats) UpdateNumbers(currentBlock, currentFastBlock *types.Block) { - stats.currentBlockNumber.Store(currentBlock.Number()) - stats.currentFastBlockNumber.Store(currentFastBlock.Number()) +// Update is a convenience method to set all values +func (stats *Chainstats) Update(currentBlock, currentFastBlock *types.Block, totalDifficulty *big.Int) { + stats.SetNumber(currentBlock.NumberU64()) + stats.SetFastNumber(currentFastBlock.NumberU64()) + stats.SetTotalDifficulty(totalDifficulty) + +} + +// GetNumbers convenience-method to get all values +func (stats *Chainstats) Get() (uint64, uint64, *big.Int) { + return stats.currentBlockNumber, + stats.currentFastBlockNumber, + new(big.Int).Set(stats.currentTd.Load().(*big.Int)) } // SetNumber stores latest block number -func (stats *Chainstats) SetNumber(number *big.Int) { - stats.currentBlockNumber.Store(number) +func (stats *Chainstats) SetNumber(number uint64) { + atomic.StoreUint64(&stats.currentBlockNumber, number) } // GetFastNumber return latest fast block number func (stats *Chainstats) GetFastNumber() uint64 { - return stats.currentFastBlockNumber.Load().(*big.Int).Uint64() -} - -// GetNumbers convenience-method to get both last number and last fast number -func (stats *Chainstats) GetNumbers() (uint64, uint64) { - return stats.currentBlockNumber.Load().(*big.Int).Uint64(), - stats.currentFastBlockNumber.Load().(*big.Int).Uint64() + return stats.currentFastBlockNumber } // SetFastNumber stores latest fast block number -func (stats *Chainstats) SetFastNumber(number *big.Int) { - stats.currentFastBlockNumber.Store(number) +func (stats *Chainstats) SetFastNumber(number uint64) { + atomic.StoreUint64(&stats.currentFastBlockNumber, number) } // GetTotalDifficulty return latest total difficulty diff --git a/core/blockchain.go b/core/blockchain.go index 1fbba38ae0..6ec68c8315 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -251,8 +251,7 @@ func (bc *BlockChain) loadLastState() error { blockTd := bc.GetTd(bc.currentBlock.Hash(), bc.currentBlock.NumberU64()) fastTd := bc.GetTd(bc.currentFastBlock.Hash(), bc.currentFastBlock.NumberU64()) - bc.chainStats.SetTotalDifficulty(blockTd) - bc.chainStats.UpdateNumbers(bc.currentBlock, bc.currentFastBlock) + bc.chainStats.Update(bc.currentBlock, bc.currentFastBlock, blockTd) log.Info("Loaded most recent local header", "number", currentHeader.Number, "hash", currentHeader.Hash(), "td", headerTd) log.Info("Loaded most recent local full block", "number", bc.currentBlock.Number(), "hash", bc.currentBlock.Hash(), "td", blockTd) @@ -312,8 +311,7 @@ func (bc *BlockChain) SetHead(head uint64) error { log.Crit("Failed to reset head fast block", "err", err) } - bc.chainStats.UpdateNumbers(bc.currentBlock, bc.currentFastBlock) - bc.chainStats.SetTotalDifficulty(bc.GetTd(bc.currentBlock.Hash(), bc.currentBlock.NumberU64())) + bc.chainStats.Update(bc.currentBlock, bc.currentFastBlock, bc.GetTd(bc.currentBlock.Hash(), bc.currentBlock.NumberU64())) return bc.loadLastState() } @@ -332,7 +330,7 @@ func (bc *BlockChain) FastSyncCommitHead(hash common.Hash) error { // If all checks out, manually set the head block bc.mu.Lock() bc.currentBlock = block - bc.chainStats.SetNumber(block.Number()) + bc.chainStats.SetNumber(block.Number().Uint64()) bc.chainStats.SetTotalDifficulty(bc.GetTd(block.Hash(), block.NumberU64())) bc.mu.Unlock() @@ -433,8 +431,7 @@ func (bc *BlockChain) ResetWithGenesisBlock(genesis *types.Block) error { bc.hc.SetCurrentHeader(bc.genesisBlock.Header()) bc.currentFastBlock = bc.genesisBlock - bc.chainStats.UpdateNumbers(bc.currentBlock, bc.currentFastBlock) - bc.chainStats.SetTotalDifficulty(bc.genesisBlock.Difficulty()) + bc.chainStats.Update(bc.currentBlock, bc.currentFastBlock, bc.genesisBlock.Difficulty()) return nil } @@ -514,8 +511,7 @@ func (bc *BlockChain) insert(block *types.Block) { } bc.currentFastBlock = block } - bc.chainStats.UpdateNumbers(bc.currentBlock, bc.currentFastBlock) - bc.chainStats.SetTotalDifficulty(bc.GetTd(block.Hash(), block.NumberU64())) + bc.chainStats.Update(bc.currentBlock, bc.currentFastBlock, bc.GetTd(block.Hash(), block.NumberU64())) } // Genesis retrieves the chain's genesis block. @@ -759,8 +755,7 @@ func (bc *BlockChain) Rollback(chain []common.Hash) { WriteHeadBlockHash(bc.db, bc.currentBlock.Hash()) } } - bc.chainStats.UpdateNumbers(bc.currentBlock, bc.currentFastBlock) - bc.chainStats.SetTotalDifficulty(bc.GetTd(bc.currentBlock.Hash(), bc.currentBlock.NumberU64())) + bc.chainStats.Update(bc.currentBlock, bc.currentFastBlock, bc.GetTd(bc.currentBlock.Hash(), bc.currentBlock.NumberU64())) } // SetReceiptsData computes all the non-consensus fields of the receipts @@ -872,7 +867,7 @@ func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain [ log.Crit("Failed to update head fast block hash", "err", err) } bc.currentFastBlock = head - bc.chainStats.SetFastNumber(bc.currentFastBlock.Number()) + bc.chainStats.SetFastNumber(bc.currentFastBlock.NumberU64()) } } bc.mu.Unlock() diff --git a/eth/sync.go b/eth/sync.go index f20fbe55c4..0ca57943b0 100644 --- a/eth/sync.go +++ b/eth/sync.go @@ -167,8 +167,7 @@ func (pm *ProtocolManager) synchronise(peer *peer) { return } // Make sure the peer's TD is higher than our own - currentNumber, currentFastNumber := pm.blockchain.Stats().GetNumbers() - td := pm.blockchain.Stats().GetTotalDifficulty() + currentNumber, currentFastNumber, td := pm.blockchain.Stats().Get() pHead, pTd := peer.Head() if pTd.Cmp(td) <= 0 {