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 { type Chainstats struct {
currentBlockNumber atomic.Value currentBlockNumber uint64
currentFastBlockNumber atomic.Value currentFastBlockNumber uint64
currentTd atomic.Value currentTd atomic.Value
} }
func NewChainstats() *Chainstats { func NewChainstats() *Chainstats {
stats := &Chainstats{} stats := &Chainstats{}
stats.currentBlockNumber.Store(big.NewInt(0))
stats.currentFastBlockNumber.Store(big.NewInt(0))
stats.currentTd.Store(big.NewInt(0)) stats.currentTd.Store(big.NewInt(0))
return stats return stats
} }
// GetNumber returns the latest block number // 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
} }
// UpdateNumbers is a convenience method to set both latest number and fast number // Update is a convenience method to set all values
func (stats *Chainstats) UpdateNumbers(currentBlock, currentFastBlock *types.Block) { func (stats *Chainstats) Update(currentBlock, currentFastBlock *types.Block, totalDifficulty *big.Int) {
stats.currentBlockNumber.Store(currentBlock.Number()) stats.SetNumber(currentBlock.NumberU64())
stats.currentFastBlockNumber.Store(currentFastBlock.Number()) 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 // SetNumber stores latest block number
func (stats *Chainstats) SetNumber(number *big.Int) { func (stats *Chainstats) SetNumber(number uint64) {
stats.currentBlockNumber.Store(number) atomic.StoreUint64(&stats.currentBlockNumber, number)
} }
// GetFastNumber return latest fast block 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
}
// 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 // SetFastNumber stores latest fast block number
func (stats *Chainstats) SetFastNumber(number *big.Int) { func (stats *Chainstats) SetFastNumber(number uint64) {
stats.currentFastBlockNumber.Store(number) atomic.StoreUint64(&stats.currentFastBlockNumber, number)
} }
// GetTotalDifficulty return latest total difficulty // 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()) blockTd := bc.GetTd(bc.currentBlock.Hash(), bc.currentBlock.NumberU64())
fastTd := bc.GetTd(bc.currentFastBlock.Hash(), bc.currentFastBlock.NumberU64()) fastTd := bc.GetTd(bc.currentFastBlock.Hash(), bc.currentFastBlock.NumberU64())
bc.chainStats.SetTotalDifficulty(blockTd) bc.chainStats.Update(bc.currentBlock, bc.currentFastBlock, blockTd)
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)
log.Info("Loaded most recent local full block", "number", bc.currentBlock.Number(), "hash", bc.currentBlock.Hash(), "td", blockTd) 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) log.Crit("Failed to reset head fast block", "err", err)
} }
bc.chainStats.UpdateNumbers(bc.currentBlock, bc.currentFastBlock) bc.chainStats.Update(bc.currentBlock, bc.currentFastBlock, bc.GetTd(bc.currentBlock.Hash(), bc.currentBlock.NumberU64()))
bc.chainStats.SetTotalDifficulty(bc.GetTd(bc.currentBlock.Hash(), bc.currentBlock.NumberU64()))
return bc.loadLastState() return bc.loadLastState()
} }
@ -332,7 +330,7 @@ func (bc *BlockChain) FastSyncCommitHead(hash common.Hash) error {
// If all checks out, manually set the head block // If all checks out, manually set the head block
bc.mu.Lock() bc.mu.Lock()
bc.currentBlock = block bc.currentBlock = block
bc.chainStats.SetNumber(block.Number()) bc.chainStats.SetNumber(block.Number().Uint64())
bc.chainStats.SetTotalDifficulty(bc.GetTd(block.Hash(), block.NumberU64())) bc.chainStats.SetTotalDifficulty(bc.GetTd(block.Hash(), block.NumberU64()))
bc.mu.Unlock() bc.mu.Unlock()
@ -433,8 +431,7 @@ func (bc *BlockChain) ResetWithGenesisBlock(genesis *types.Block) error {
bc.hc.SetCurrentHeader(bc.genesisBlock.Header()) bc.hc.SetCurrentHeader(bc.genesisBlock.Header())
bc.currentFastBlock = bc.genesisBlock bc.currentFastBlock = bc.genesisBlock
bc.chainStats.UpdateNumbers(bc.currentBlock, bc.currentFastBlock) bc.chainStats.Update(bc.currentBlock, bc.currentFastBlock, bc.genesisBlock.Difficulty())
bc.chainStats.SetTotalDifficulty(bc.genesisBlock.Difficulty())
return nil return nil
} }
@ -514,8 +511,7 @@ func (bc *BlockChain) insert(block *types.Block) {
} }
bc.currentFastBlock = block bc.currentFastBlock = block
} }
bc.chainStats.UpdateNumbers(bc.currentBlock, bc.currentFastBlock) bc.chainStats.Update(bc.currentBlock, bc.currentFastBlock, bc.GetTd(block.Hash(), block.NumberU64()))
bc.chainStats.SetTotalDifficulty(bc.GetTd(block.Hash(), block.NumberU64()))
} }
// Genesis retrieves the chain's genesis block. // Genesis retrieves the chain's genesis block.
@ -759,8 +755,7 @@ func (bc *BlockChain) Rollback(chain []common.Hash) {
WriteHeadBlockHash(bc.db, bc.currentBlock.Hash()) WriteHeadBlockHash(bc.db, bc.currentBlock.Hash())
} }
} }
bc.chainStats.UpdateNumbers(bc.currentBlock, bc.currentFastBlock) bc.chainStats.Update(bc.currentBlock, bc.currentFastBlock, bc.GetTd(bc.currentBlock.Hash(), bc.currentBlock.NumberU64()))
bc.chainStats.SetTotalDifficulty(bc.GetTd(bc.currentBlock.Hash(), bc.currentBlock.NumberU64()))
} }
// SetReceiptsData computes all the non-consensus fields of the receipts // 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) log.Crit("Failed to update head fast block hash", "err", err)
} }
bc.currentFastBlock = head bc.currentFastBlock = head
bc.chainStats.SetFastNumber(bc.currentFastBlock.Number()) bc.chainStats.SetFastNumber(bc.currentFastBlock.NumberU64())
} }
} }
bc.mu.Unlock() bc.mu.Unlock()

View file

@ -167,8 +167,7 @@ func (pm *ProtocolManager) synchronise(peer *peer) {
return return
} }
// Make sure the peer's TD is higher than our own // Make sure the peer's TD is higher than our own
currentNumber, currentFastNumber := pm.blockchain.Stats().GetNumbers() currentNumber, currentFastNumber, td := pm.blockchain.Stats().Get()
td := pm.blockchain.Stats().GetTotalDifficulty()
pHead, pTd := peer.Head() pHead, pTd := peer.Head()
if pTd.Cmp(td) <= 0 { if pTd.Cmp(td) <= 0 {