core: cache fresh headers and tds to avoid db trashing

This commit is contained in:
Péter Szilágyi 2016-03-10 20:19:09 +02:00
parent 8b58cd0190
commit daad2b2559
2 changed files with 52 additions and 20 deletions

View File

@ -368,7 +368,7 @@ func (bc *BlockChain) ResetWithGenesisBlock(genesis *types.Block) {
defer bc.mu.Unlock() defer bc.mu.Unlock()
// Prepare the genesis block and reinitialise the chain // Prepare the genesis block and reinitialise the chain
if err := WriteTd(bc.chainDb, genesis.Hash(), genesis.Difficulty()); err != nil { if err := bc.hc.WriteTd(genesis.Hash(), genesis.Difficulty()); err != nil {
glog.Fatalf("failed to write genesis block TD: %v", err) glog.Fatalf("failed to write genesis block TD: %v", err)
} }
if err := WriteBlock(bc.chainDb, genesis); err != nil { if err := WriteBlock(bc.chainDb, genesis); err != nil {
@ -788,7 +788,7 @@ func (self *BlockChain) WriteBlock(block *types.Block) (status WriteStatus, err
status = SideStatTy status = SideStatTy
} }
// Irrelevant of the canonical status, write the block itself to the database // Irrelevant of the canonical status, write the block itself to the database
if err := WriteTd(self.chainDb, block.Hash(), externTd); err != nil { if err := self.hc.WriteTd(block.Hash(), externTd); err != nil {
glog.Fatalf("failed to write block total difficulty: %v", err) glog.Fatalf("failed to write block total difficulty: %v", err)
} }
if err := WriteBlock(self.chainDb, block); err != nil { if err := WriteBlock(self.chainDb, block); err != nil {

View File

@ -44,6 +44,8 @@ type HeaderChain struct {
genesisHeader *types.Header genesisHeader *types.Header
currentHeader *types.Header // Current head of the header chain (may be above the block chain!) currentHeader *types.Header // Current head of the header chain (may be above the block chain!)
currentHeaderHash common.Hash // Hash of the current head of the header chain (prevent recomputing all the time)
headerCache *lru.Cache // Cache for the most recent block headers headerCache *lru.Cache // Cache for the most recent block headers
tdCache *lru.Cache // Cache for the most recent block total difficulties tdCache *lru.Cache // Cache for the most recent block total difficulties
@ -95,6 +97,7 @@ func NewHeaderChain(chainDb ethdb.Database, getValidator getHeaderValidatorFn, p
hc.currentHeader = chead hc.currentHeader = chead
} }
} }
hc.currentHeaderHash = hc.currentHeader.Hash()
return hc, nil return hc, nil
} }
@ -109,12 +112,17 @@ func NewHeaderChain(chainDb ethdb.Database, getValidator getHeaderValidatorFn, p
// in two scenarios: pure-header mode of operation (light clients), or properly // in two scenarios: pure-header mode of operation (light clients), or properly
// separated header/block phases (non-archive clients). // separated header/block phases (non-archive clients).
func (hc *HeaderChain) WriteHeader(header *types.Header) (status WriteStatus, err error) { func (hc *HeaderChain) WriteHeader(header *types.Header) (status WriteStatus, err error) {
// Cache some values to prevent constant recalculation
var (
hash = header.Hash()
number = header.Number.Uint64()
)
// Calculate the total difficulty of the header // Calculate the total difficulty of the header
ptd := hc.GetTd(header.ParentHash) ptd := hc.GetTd(header.ParentHash)
if ptd == nil { if ptd == nil {
return NonStatTy, ParentError(header.ParentHash) return NonStatTy, ParentError(header.ParentHash)
} }
localTd := hc.GetTd(hc.currentHeader.Hash()) localTd := hc.GetTd(hc.currentHeaderHash)
externTd := new(big.Int).Add(header.Difficulty, ptd) externTd := new(big.Int).Add(header.Difficulty, ptd)
// If the total difficulty is higher than our known, add it to the canonical chain // If the total difficulty is higher than our known, add it to the canonical chain
@ -122,34 +130,44 @@ func (hc *HeaderChain) WriteHeader(header *types.Header) (status WriteStatus, er
// Please refer to http://www.cs.cornell.edu/~ie53/publications/btcProcFC.pdf // Please refer to http://www.cs.cornell.edu/~ie53/publications/btcProcFC.pdf
if externTd.Cmp(localTd) > 0 || (externTd.Cmp(localTd) == 0 && mrand.Float64() < 0.5) { if externTd.Cmp(localTd) > 0 || (externTd.Cmp(localTd) == 0 && mrand.Float64() < 0.5) {
// Delete any canonical number assignments above the new head // Delete any canonical number assignments above the new head
for i := header.Number.Uint64() + 1; GetCanonicalHash(hc.chainDb, i) != (common.Hash{}); i++ { for i := number + 1; GetCanonicalHash(hc.chainDb, i) != (common.Hash{}); i++ {
DeleteCanonicalHash(hc.chainDb, i) DeleteCanonicalHash(hc.chainDb, i)
} }
// Overwrite any stale canonical number assignments // Overwrite any stale canonical number assignments
head := hc.GetHeader(header.ParentHash) var (
for GetCanonicalHash(hc.chainDb, head.Number.Uint64()) != head.Hash() { headHash = header.ParentHash
WriteCanonicalHash(hc.chainDb, head.Hash(), head.Number.Uint64()) headHeader = hc.GetHeader(headHash)
head = hc.GetHeader(head.ParentHash) headNumber = headHeader.Number.Uint64()
)
for GetCanonicalHash(hc.chainDb, headNumber) != headHash {
WriteCanonicalHash(hc.chainDb, headHash, headNumber)
headHash = headHeader.ParentHash
headHeader = hc.GetHeader(headHash)
headNumber = headHeader.Number.Uint64()
} }
// Extend the canonical chain with the new header // Extend the canonical chain with the new header
if err := WriteCanonicalHash(hc.chainDb, header.Hash(), header.Number.Uint64()); err != nil { if err := WriteCanonicalHash(hc.chainDb, hash, number); err != nil {
glog.Fatalf("failed to insert header number: %v", err) glog.Fatalf("failed to insert header number: %v", err)
} }
if err := WriteHeadHeaderHash(hc.chainDb, header.Hash()); err != nil { if err := WriteHeadHeaderHash(hc.chainDb, hash); err != nil {
glog.Fatalf("failed to insert head header hash: %v", err) glog.Fatalf("failed to insert head header hash: %v", err)
} }
hc.currentHeader = types.CopyHeader(header) hc.currentHeaderHash, hc.currentHeader = hash, types.CopyHeader(header)
status = CanonStatTy status = CanonStatTy
} else { } else {
status = SideStatTy status = SideStatTy
} }
// Irrelevant of the canonical status, write the header itself to the database // Irrelevant of the canonical status, write the header itself to the database
if err := WriteTd(hc.chainDb, header.Hash(), externTd); err != nil { if err := hc.WriteTd(hash, externTd); err != nil {
glog.Fatalf("failed to write header total difficulty: %v", err) glog.Fatalf("failed to write header total difficulty: %v", err)
} }
if err := WriteHeader(hc.chainDb, header); err != nil { if err := WriteHeader(hc.chainDb, header); err != nil {
glog.Fatalf("failed to write header contents: %v", err) glog.Fatalf("failed to write header contents: %v", err)
} }
hc.headerCache.Add(hash, header)
return return
} }
@ -285,10 +303,11 @@ func (hc *HeaderChain) GetBlockHashesFromHash(hash common.Hash, max uint64) []co
// Iterate the headers until enough is collected or the genesis reached // Iterate the headers until enough is collected or the genesis reached
chain := make([]common.Hash, 0, max) chain := make([]common.Hash, 0, max)
for i := uint64(0); i < max; i++ { for i := uint64(0); i < max; i++ {
if header = hc.GetHeader(header.ParentHash); header == nil { next := header.ParentHash
if header = hc.GetHeader(next); header == nil {
break break
} }
chain = append(chain, header.Hash()) chain = append(chain, next)
if header.Number.Cmp(common.Big0) == 0 { if header.Number.Cmp(common.Big0) == 0 {
break break
} }
@ -312,6 +331,16 @@ func (hc *HeaderChain) GetTd(hash common.Hash) *big.Int {
return td return td
} }
// WriteTd stores a block's total difficulty into the database, also caching it
// along the way.
func (hc *HeaderChain) WriteTd(hash common.Hash, td *big.Int) error {
if err := WriteTd(hc.chainDb, hash, td); err != nil {
return err
}
hc.tdCache.Add(hash, new(big.Int).Set(td))
return nil
}
// GetHeader retrieves a block header from the database by hash, caching it if // GetHeader retrieves a block header from the database by hash, caching it if
// found. // found.
func (hc *HeaderChain) GetHeader(hash common.Hash) *types.Header { func (hc *HeaderChain) GetHeader(hash common.Hash) *types.Header {
@ -324,7 +353,7 @@ func (hc *HeaderChain) GetHeader(hash common.Hash) *types.Header {
return nil return nil
} }
// Cache the found header for next time and return // Cache the found header for next time and return
hc.headerCache.Add(header.Hash(), header) hc.headerCache.Add(hash, header)
return header return header
} }
@ -356,6 +385,7 @@ func (hc *HeaderChain) SetCurrentHeader(head *types.Header) {
glog.Fatalf("failed to insert head header hash: %v", err) glog.Fatalf("failed to insert head header hash: %v", err)
} }
hc.currentHeader = head hc.currentHeader = head
hc.currentHeaderHash = head.Hash()
} }
// DeleteCallback is a callback function that is called by SetHead before // DeleteCallback is a callback function that is called by SetHead before
@ -390,7 +420,9 @@ func (hc *HeaderChain) SetHead(head uint64, delFn DeleteCallback) {
if hc.currentHeader == nil { if hc.currentHeader == nil {
hc.currentHeader = hc.genesisHeader hc.currentHeader = hc.genesisHeader
} }
if err := WriteHeadHeaderHash(hc.chainDb, hc.currentHeader.Hash()); err != nil { hc.currentHeaderHash = hc.currentHeader.Hash()
if err := WriteHeadHeaderHash(hc.chainDb, hc.currentHeaderHash); err != nil {
glog.Fatalf("failed to reset head header hash: %v", err) glog.Fatalf("failed to reset head header hash: %v", err)
} }
} }