chainstats, core: modify chainstats to use uint64, merge setters

This commit is contained in:
Martin Holst Swende 2018-02-02 13:08:58 +01:00
parent 5e6dc9fc06
commit d90c7c1378
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0
3 changed files with 29 additions and 34 deletions

View file

@ -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

View file

@ -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()

View file

@ -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 {