Skip to content
This repository has been archived by the owner on Aug 7, 2023. It is now read-only.

Commit

Permalink
chainstate only needs the previous block hash
Browse files Browse the repository at this point in the history
... not the entire block header.
  • Loading branch information
dajohi committed Jun 21, 2017
1 parent 0d406ff commit 2ffbad1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 42 deletions.
29 changes: 7 additions & 22 deletions blockchain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1029,29 +1029,14 @@ func (b *BlockChain) pruneNodes() error {
return b.pruneBlockNodes()
}

// BestBlockHeader returns a copy of the block header of the block at HEAD.
// BestPrevHash returns the hash of the previous block of the block at HEAD.
//
// This function is NOT safe for concurrent access.
func (b *BlockChain) BestBlockHeader() *wire.BlockHeader {
return wire.NewBlockHeader(
b.bestNode.header.Version,
&b.bestNode.header.PrevBlock,
&b.bestNode.header.MerkleRoot,
&b.bestNode.header.StakeRoot,
b.bestNode.header.VoteBits,
b.bestNode.header.FinalState,
b.bestNode.header.Voters,
b.bestNode.header.FreshStake,
b.bestNode.header.Revocations,
b.bestNode.header.PoolSize,
b.bestNode.header.Bits,
b.bestNode.header.SBits,
b.bestNode.header.Height,
b.bestNode.header.Size,
b.bestNode.header.Nonce,
b.bestNode.header.ExtraData,
b.bestNode.header.StakeVersion,
)
// This function is safe for concurrent access.
func (b *BlockChain) BestPrevHash() chainhash.Hash {
b.chainLock.Lock()
defer b.chainLock.Unlock()

return b.bestNode.header.PrevBlock
}

// isMajorityVersion determines if a previous number of blocks in the chain
Expand Down
33 changes: 16 additions & 17 deletions blockmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ type chainState struct {
nextStakeDifficulty int64
winningTickets []chainhash.Hash
missedTickets []chainhash.Hash
curBlockHeader wire.BlockHeader
curPrevHash chainhash.Hash
pastMedianTime time.Time
pastMedianTimeErr error
stakeVersion uint32
Expand Down Expand Up @@ -415,15 +415,14 @@ func (c *chainState) CurrentlyMissed() []chainhash.Hash {
return c.missedTickets
}

// CurrentlyMissed returns the eligible SStx hashes to vote on the
// next block as inputs for SSGen.
// GetTopPrevHash returns the current previous block hash.
//
// This function is safe for concurrent access.
func (c *chainState) GetTopBlockHeader() wire.BlockHeader {
func (c *chainState) GetTopPrevHash() chainhash.Hash {
c.Lock()
defer c.Unlock()

return c.curBlockHeader
return c.curPrevHash
}

// blockManager provides a concurrency safe block manager for handling all
Expand Down Expand Up @@ -490,7 +489,7 @@ func (b *blockManager) resetHeaderState(newestHash *chainhash.Hash, newestHeight
func (b *blockManager) updateChainState(newestHash *chainhash.Hash,
newestHeight int64, finalState [6]byte, poolSize uint32,
nextStakeDiff int64, winningTickets []chainhash.Hash,
missedTickets []chainhash.Hash, curBlockHeader wire.BlockHeader) {
missedTickets []chainhash.Hash, curPrevHash chainhash.Hash) {

b.chainState.Lock()
defer b.chainState.Unlock()
Expand All @@ -509,7 +508,7 @@ func (b *blockManager) updateChainState(newestHash *chainhash.Hash,
b.chainState.nextStakeDifficulty = nextStakeDiff
b.chainState.winningTickets = winningTickets
b.chainState.missedTickets = missedTickets
b.chainState.curBlockHeader = curBlockHeader
b.chainState.curPrevHash = curPrevHash
}

// findNextHeaderCheckpoint returns the next checkpoint after the passed height.
Expand Down Expand Up @@ -1228,8 +1227,8 @@ func (b *blockManager) handleBlockMsg(bmsg *blockMsg) {
"for best block %v: %v", best.Hash, err)
}

// Retrieve the current block header.
curBlockHeader := b.chain.BestBlockHeader()
// Retrieve the current previous block hash.
curPrevHash := b.chain.BestPrevHash()

nextStakeDiff, errSDiff :=
b.chain.CalcNextRequiredStakeDifficulty()
Expand Down Expand Up @@ -1260,7 +1259,7 @@ func (b *blockManager) handleBlockMsg(bmsg *blockMsg) {

b.updateChainState(best.Hash, best.Height, finalState,
uint32(poolSize), nextStakeDiff, winningTickets,
missedTickets, *curBlockHeader)
missedTickets, curPrevHash)

// Update this peer's latest block height, for future
// potential sync node candidancy.
Expand Down Expand Up @@ -1829,7 +1828,7 @@ out:
// The blockchain should be updated, so fetch the
// latest snapshot.
best = b.chain.BestSnapshot()
curBlockHeader := b.chain.BestBlockHeader()
curPrevHash := b.chain.BestPrevHash()

b.updateChainState(best.Hash,
best.Height,
Expand All @@ -1838,7 +1837,7 @@ out:
nextStakeDiff,
winningTickets,
missedTickets,
*curBlockHeader)
curPrevHash)
}

msg.reply <- forceReorganizationResponse{
Expand Down Expand Up @@ -1961,7 +1960,7 @@ out:
bmgrLog.Warnf("Failed to get missing tickets for "+
"incoming block %v: %v", best.Hash, err)
}
curBlockHeader := b.chain.BestBlockHeader()
curPrevHash := b.chain.BestPrevHash()

winningTickets, poolSize, finalState, err :=
b.chain.LotteryDataForBlock(msg.block.Hash())
Expand All @@ -1978,7 +1977,7 @@ out:
nextStakeDiff,
winningTickets,
missedTickets,
*curBlockHeader)
curPrevHash)
}

// Allow any clients performing long polling via the
Expand Down Expand Up @@ -2726,8 +2725,8 @@ func newBlockManager(s *server, indexManager blockchain.IndexManager) (*blockMan
return nil, err
}

// Retrieve the current block header and next stake difficulty.
curBlockHeader := bm.chain.BestBlockHeader()
// Retrieve the current previous block hash and next stake difficulty.
curPrevHash := bm.chain.BestPrevHash()
nextStakeDiff, err := bm.chain.CalcNextRequiredStakeDifficulty()
if err != nil {
return nil, err
Expand All @@ -2740,7 +2739,7 @@ func newBlockManager(s *server, indexManager blockchain.IndexManager) (*blockMan
nextStakeDiff,
wt,
missedTickets,
*curBlockHeader)
curPrevHash)
bm.lotteryDataBroadcast = make(map[chainhash.Hash]struct{})

return &bm, nil
Expand Down
2 changes: 1 addition & 1 deletion dcrec/edwards/ecdsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"crypto/sha512"
"fmt"
"hash"
"io"
"math/big"
"crypto/sha512"

"github.com/agl/ed25519"
"github.com/agl/ed25519/edwards25519"
Expand Down
4 changes: 2 additions & 2 deletions mining.go
Original file line number Diff line number Diff line change
Expand Up @@ -871,9 +871,9 @@ func handleTooFewVoters(subsidyCache *blockchain.SubsidyCache,
// Check to see if we've fallen off the chain, for example if a
// reorganization had recently occurred. If this is the case,
// nuke the templates.
bestHeader := chainState.GetTopBlockHeader()
prevBlockHash := chainState.GetTopPrevHash()
if curTemplate != nil {
if !bestHeader.PrevBlock.IsEqual(
if !prevBlockHash.IsEqual(
&curTemplate.Block.Header.PrevBlock) {
minrLog.Debugf("Cached mining templates are no longer current, " +
"resetting")
Expand Down

0 comments on commit 2ffbad1

Please sign in to comment.