core, chainstats: remove unnecessary code, add docs

This commit is contained in:
Martin Holst Swende 2018-01-27 15:45:41 +01:00
parent 373f600c7b
commit 5e6dc9fc06
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0
2 changed files with 23 additions and 12 deletions

View file

@ -19,47 +19,64 @@
package chainstats package chainstats
import ( import (
"github.com/ethereum/go-ethereum/core/types"
"math/big" "math/big"
"sync/atomic" "sync/atomic"
"github.com/ethereum/go-ethereum/core/types"
) )
type Chainstats struct { type Chainstats struct {
currentBlockNumber atomic.Value currentBlockNumber atomic.Value
currentFastBlockNumber atomic.Value currentFastBlockNumber atomic.Value
currentTd atomic.Value currentTd atomic.Value
currentFastTd atomic.Value
} }
func NewChainstats() *Chainstats { 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 { func (stats *Chainstats) GetNumber() uint64 {
return stats.currentBlockNumber.Load().(*big.Int).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) { func (stats *Chainstats) UpdateNumbers(currentBlock, currentFastBlock *types.Block) {
stats.currentBlockNumber.Store(currentBlock.Number()) stats.currentBlockNumber.Store(currentBlock.Number())
stats.currentFastBlockNumber.Store(currentFastBlock.Number()) stats.currentFastBlockNumber.Store(currentFastBlock.Number())
} }
// SetNumber stores latest block number
func (stats *Chainstats) SetNumber(number *big.Int) { func (stats *Chainstats) SetNumber(number *big.Int) {
stats.currentBlockNumber.Store(number) stats.currentBlockNumber.Store(number)
} }
// GetFastNumber return latest fast block number
func (stats *Chainstats) GetFastNumber() uint64 { func (stats *Chainstats) GetFastNumber() uint64 {
return stats.currentFastBlockNumber.Load().(*big.Int).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) { func (stats *Chainstats) GetNumbers() (uint64, uint64) {
return stats.currentBlockNumber.Load().(*big.Int).Uint64(), return stats.currentBlockNumber.Load().(*big.Int).Uint64(),
stats.currentFastBlockNumber.Load().(*big.Int).Uint64() stats.currentFastBlockNumber.Load().(*big.Int).Uint64()
} }
// SetFastNumber stores latest fast block number
func (stats *Chainstats) SetFastNumber(number *big.Int) { func (stats *Chainstats) SetFastNumber(number *big.Int) {
stats.currentFastBlockNumber.Store(number) stats.currentFastBlockNumber.Store(number)
} }
// GetTotalDifficulty return latest total difficulty
func (stats *Chainstats) GetTotalDifficulty() *big.Int { func (stats *Chainstats) GetTotalDifficulty() *big.Int {
return new(big.Int).Set(stats.currentTd.Load().(*big.Int)) return new(big.Int).Set(stats.currentTd.Load().(*big.Int))
} }
// SetTotalDifficulty sets latest total difficulty
func (stats *Chainstats) SetTotalDifficulty(newTd *big.Int) { func (stats *Chainstats) SetTotalDifficulty(newTd *big.Int) {
stats.currentTd.Store(newTd) stats.currentTd.Store(newTd)
} }
func (stats *Chainstats) SetTotalFastDifficulty(newTd *big.Int) {
stats.currentFastTd.Store(newTd)
}

View file

@ -252,7 +252,6 @@ func (bc *BlockChain) loadLastState() error {
fastTd := bc.GetTd(bc.currentFastBlock.Hash(), bc.currentFastBlock.NumberU64()) fastTd := bc.GetTd(bc.currentFastBlock.Hash(), bc.currentFastBlock.NumberU64())
bc.chainStats.SetTotalDifficulty(blockTd) bc.chainStats.SetTotalDifficulty(blockTd)
bc.chainStats.SetTotalFastDifficulty(fastTd)
bc.chainStats.UpdateNumbers(bc.currentBlock, bc.currentFastBlock) bc.chainStats.UpdateNumbers(bc.currentBlock, bc.currentFastBlock)
log.Info("Loaded most recent local header", "number", currentHeader.Number, "hash", currentHeader.Hash(), "td", headerTd) 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.UpdateNumbers(bc.currentBlock, bc.currentFastBlock)
bc.chainStats.SetTotalDifficulty(bc.GetTd(bc.currentBlock.Hash(), bc.currentBlock.NumberU64())) 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() return bc.loadLastState()
} }
@ -437,7 +435,6 @@ func (bc *BlockChain) ResetWithGenesisBlock(genesis *types.Block) error {
bc.chainStats.UpdateNumbers(bc.currentBlock, bc.currentFastBlock) bc.chainStats.UpdateNumbers(bc.currentBlock, bc.currentFastBlock)
bc.chainStats.SetTotalDifficulty(bc.genesisBlock.Difficulty()) bc.chainStats.SetTotalDifficulty(bc.genesisBlock.Difficulty())
bc.chainStats.SetTotalFastDifficulty(bc.genesisBlock.Difficulty())
return nil return nil
} }
@ -519,7 +516,6 @@ func (bc *BlockChain) insert(block *types.Block) {
} }
bc.chainStats.UpdateNumbers(bc.currentBlock, bc.currentFastBlock) bc.chainStats.UpdateNumbers(bc.currentBlock, bc.currentFastBlock)
bc.chainStats.SetTotalDifficulty(bc.GetTd(block.Hash(), block.NumberU64())) 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. // 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.UpdateNumbers(bc.currentBlock, bc.currentFastBlock)
bc.chainStats.SetTotalDifficulty(bc.GetTd(bc.currentBlock.Hash(), bc.currentBlock.NumberU64())) 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 // 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.currentFastBlock = head
bc.chainStats.SetFastNumber(bc.currentFastBlock.Number()) bc.chainStats.SetFastNumber(bc.currentFastBlock.Number())
bc.chainStats.SetTotalFastDifficulty(td)
} }
} }
bc.mu.Unlock() bc.mu.Unlock()