[DEV-34] Inlined a couple of things, did some renaming, exported PrevHashes().

This commit is contained in:
Stas Boutenko 2018-06-24 14:38:15 +03:00
parent 328fab0231
commit 14287a1dd1
3 changed files with 13 additions and 16 deletions

View File

@ -106,7 +106,6 @@ type blockNode struct {
// This function is NOT safe for concurrent access. It must only be called when
// initially creating a node.
func initBlockNode(node *blockNode, blockHeader *wire.BlockHeader, parents []*blockNode) {
numParents := byte(len(parents))
*node = blockNode{
hash: blockHeader.BlockHash(),
parents: parents,
@ -117,8 +116,8 @@ func initBlockNode(node *blockNode, blockHeader *wire.BlockHeader, parents []*bl
timestamp: blockHeader.Timestamp.Unix(),
merkleRoot: blockHeader.MerkleRoot,
}
if numParents > 0 {
parent := parents[0]
if len(parents) > 0 {
parent := parents[0] // TODO: (Stas) This is wrong. Modified only to satisfy compilation.
node.height = parent.height + 1
node.workSum = node.workSum.Add(parent.workSum, node.workSum)
}
@ -138,12 +137,10 @@ func newBlockNode(blockHeader *wire.BlockHeader, parents []*blockNode) *blockNod
// This function is safe for concurrent access.
func (node *blockNode) Header() wire.BlockHeader {
// No lock is needed because all accessed fields are immutable.
prevHashes := node.prevHashes()
return wire.BlockHeader{
Version: node.version,
NumPrevBlocks: byte(len(node.parents)),
PrevBlocks: prevHashes,
PrevBlocks: node.PrevHashes(),
MerkleRoot: node.merkleRoot,
Timestamp: time.Unix(node.timestamp, 0),
Bits: node.bits,
@ -218,7 +215,7 @@ func (node *blockNode) CalcPastMedianTime() time.Time {
return time.Unix(medianTimestamp, 0)
}
func (node *blockNode) prevHashes() []daghash.Hash {
func (node *blockNode) PrevHashes() []daghash.Hash {
prevHashes := make([]daghash.Hash, len(node.parents))
for _, parent := range node.parents {
prevHashes = append(prevHashes, parent.hash)

View File

@ -69,7 +69,7 @@ type BestState struct {
// newBestState returns a new best stats instance for the given parameters.
func newBestState(node *blockNode, blockSize, numTxns,
totalTxns uint64, medianTime time.Time) *BestState {
totalTxns uint64, medianTime time.Time) *BestState {
return &BestState{
Hash: node.hash,
@ -556,7 +556,7 @@ func (b *BlockChain) getReorganizeNodes(node *blockNode) (*list.List, *list.List
// This function MUST be called with the chain state lock held (for writes).
func (b *BlockChain) connectBlock(node *blockNode, block *btcutil.Block, view *UtxoViewpoint, stxos []spentTxOut) error {
// Make sure it's extending the end of the best chain.
prevHash := &block.MsgBlock().Header.PrevBlocks[0] // TODO: (Stas) This is wrong. Modified only to satisfy compilation.
prevHash := &block.MsgBlock().Header.PrevBlocks[0] // TODO: (Stas) This is wrong. Modified only to satisfy compilation.
if !prevHash.IsEqual(&b.bestChain.Tips()[0].hash) { // TODO: (Stas) This is wrong. Modified only to satisfy compilation.
return AssertError("connectBlock must be called with a block " +
"that extends the main chain")

View File

@ -211,22 +211,22 @@ func (b *BlockChain) ProcessBlock(block *btcutil.Block, flags BehaviorFlags) (bo
}
// Handle orphan blocks.
allPrevHashesExist := true
for _, prevHash := range blockHeader.PrevBlocks {
prevHashExists, err := b.blockExists(&prevHash)
allPrevBlocksExist := true
for _, prevBlock := range blockHeader.PrevBlocks {
prevBlockExists, err := b.blockExists(&prevBlock)
if err != nil {
return false, false, err
}
if !prevHashExists {
log.Infof("Adding orphan block %v with parent %v", blockHash, prevHash)
if !prevBlockExists {
log.Infof("Adding orphan block %v with parent %v", blockHash, prevBlock)
b.addOrphanBlock(block)
allPrevHashesExist = false
allPrevBlocksExist = false
}
}
if !allPrevHashesExist {
if !allPrevBlocksExist {
return false, true, nil
}