diff --git a/common/chainstats/chainstats.go b/common/chainstats/chainstats.go index 7df0a3f884..e0aba99711 100644 --- a/common/chainstats/chainstats.go +++ b/common/chainstats/chainstats.go @@ -19,47 +19,64 @@ package chainstats import ( - "github.com/ethereum/go-ethereum/core/types" "math/big" "sync/atomic" + + "github.com/ethereum/go-ethereum/core/types" ) type Chainstats struct { currentBlockNumber atomic.Value currentFastBlockNumber atomic.Value currentTd atomic.Value - currentFastTd atomic.Value } func NewChainstats() *Chainstats { - return &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() } + +// 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()) } + +// SetNumber stores latest block number func (stats *Chainstats) SetNumber(number *big.Int) { stats.currentBlockNumber.Store(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() } + +// SetFastNumber stores latest fast block number func (stats *Chainstats) SetFastNumber(number *big.Int) { stats.currentFastBlockNumber.Store(number) } + +// GetTotalDifficulty return latest total difficulty func (stats *Chainstats) GetTotalDifficulty() *big.Int { return new(big.Int).Set(stats.currentTd.Load().(*big.Int)) } + +// SetTotalDifficulty sets latest total difficulty func (stats *Chainstats) SetTotalDifficulty(newTd *big.Int) { stats.currentTd.Store(newTd) } -func (stats *Chainstats) SetTotalFastDifficulty(newTd *big.Int) { - stats.currentFastTd.Store(newTd) -} diff --git a/core/blockchain.go b/core/blockchain.go index c7ce127368..1fbba38ae0 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -252,7 +252,6 @@ func (bc *BlockChain) loadLastState() error { fastTd := bc.GetTd(bc.currentFastBlock.Hash(), bc.currentFastBlock.NumberU64()) bc.chainStats.SetTotalDifficulty(blockTd) - bc.chainStats.SetTotalFastDifficulty(fastTd) bc.chainStats.UpdateNumbers(bc.currentBlock, bc.currentFastBlock) log.Info("Loaded most recent local header", "number", currentHeader.Number, "hash", currentHeader.Hash(), "td", headerTd) @@ -315,7 +314,6 @@ func (bc *BlockChain) SetHead(head uint64) error { bc.chainStats.UpdateNumbers(bc.currentBlock, bc.currentFastBlock) bc.chainStats.SetTotalDifficulty(bc.GetTd(bc.currentBlock.Hash(), bc.currentBlock.NumberU64())) - bc.chainStats.SetTotalFastDifficulty(bc.GetTd(bc.currentFastBlock.Hash(), bc.currentFastBlock.NumberU64())) return bc.loadLastState() } @@ -437,7 +435,6 @@ func (bc *BlockChain) ResetWithGenesisBlock(genesis *types.Block) error { bc.chainStats.UpdateNumbers(bc.currentBlock, bc.currentFastBlock) bc.chainStats.SetTotalDifficulty(bc.genesisBlock.Difficulty()) - bc.chainStats.SetTotalFastDifficulty(bc.genesisBlock.Difficulty()) return nil } @@ -519,7 +516,6 @@ func (bc *BlockChain) insert(block *types.Block) { } bc.chainStats.UpdateNumbers(bc.currentBlock, bc.currentFastBlock) bc.chainStats.SetTotalDifficulty(bc.GetTd(block.Hash(), block.NumberU64())) - bc.chainStats.SetTotalFastDifficulty(bc.GetTd(bc.currentFastBlock.Hash(), bc.currentFastBlock.NumberU64())) } // Genesis retrieves the chain's genesis block. @@ -765,7 +761,6 @@ func (bc *BlockChain) Rollback(chain []common.Hash) { } bc.chainStats.UpdateNumbers(bc.currentBlock, bc.currentFastBlock) bc.chainStats.SetTotalDifficulty(bc.GetTd(bc.currentBlock.Hash(), bc.currentBlock.NumberU64())) - bc.chainStats.SetTotalFastDifficulty(bc.GetTd(bc.currentFastBlock.Hash(), bc.currentFastBlock.NumberU64())) } // SetReceiptsData computes all the non-consensus fields of the receipts @@ -878,7 +873,6 @@ func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain [ } bc.currentFastBlock = head bc.chainStats.SetFastNumber(bc.currentFastBlock.Number()) - bc.chainStats.SetTotalFastDifficulty(td) } } bc.mu.Unlock()