From 2ffbad1e88455c294811c2ff9ff19518d8a08e2e Mon Sep 17 00:00:00 2001 From: David Hill Date: Wed, 24 May 2017 15:15:07 -0400 Subject: [PATCH] chainstate only needs the previous block hash ... not the entire block header. --- blockchain/chain.go | 29 +++++++---------------------- blockmanager.go | 33 ++++++++++++++++----------------- dcrec/edwards/ecdsa.go | 2 +- mining.go | 4 ++-- 4 files changed, 26 insertions(+), 42 deletions(-) diff --git a/blockchain/chain.go b/blockchain/chain.go index cdd8809b..e1600d77 100644 --- a/blockchain/chain.go +++ b/blockchain/chain.go @@ -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 diff --git a/blockmanager.go b/blockmanager.go index 312655a5..1e041f74 100644 --- a/blockmanager.go +++ b/blockmanager.go @@ -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 @@ -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 @@ -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() @@ -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. @@ -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() @@ -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. @@ -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, @@ -1838,7 +1837,7 @@ out: nextStakeDiff, winningTickets, missedTickets, - *curBlockHeader) + curPrevHash) } msg.reply <- forceReorganizationResponse{ @@ -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()) @@ -1978,7 +1977,7 @@ out: nextStakeDiff, winningTickets, missedTickets, - *curBlockHeader) + curPrevHash) } // Allow any clients performing long polling via the @@ -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 @@ -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 diff --git a/dcrec/edwards/ecdsa.go b/dcrec/edwards/ecdsa.go index d4c65044..bf9b1184 100644 --- a/dcrec/edwards/ecdsa.go +++ b/dcrec/edwards/ecdsa.go @@ -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" diff --git a/mining.go b/mining.go index 26e1094e..6cd240d2 100644 --- a/mining.go +++ b/mining.go @@ -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")