[DEV-34] Added SelectedPrevBlock() to BlockHeader.

This commit is contained in:
Stas Boutenko 2018-06-26 12:44:38 +03:00
parent a289b72980
commit fdeb87bb99
9 changed files with 31 additions and 22 deletions

View File

@ -235,7 +235,7 @@ func (b *BlockChain) GetOrphanRoot(hash *daghash.Hash) *daghash.Hash {
break
}
orphanRoot = prevHash
prevHash = &orphan.block.MsgBlock().Header.PrevBlocks[0] // TODO: (Stas) This is wrong. Modified only to satisfy compilation.
prevHash = orphan.block.MsgBlock().Header.SelectedPrevBlock()
}
return orphanRoot
@ -256,7 +256,7 @@ func (b *BlockChain) removeOrphanBlock(orphan *orphanBlock) {
// for loop is intentionally used over a range here as range does not
// reevaluate the slice on each iteration nor does it adjust the index
// for the modified slice.
prevHash := &orphan.block.MsgBlock().Header.PrevBlocks[0] // TODO: (Stas) This is wrong. Modified only to satisfy compilation.
prevHash := orphan.block.MsgBlock().Header.SelectedPrevBlock()
orphans := b.prevOrphans[*prevHash]
for i := 0; i < len(orphans); i++ {
hash := orphans[i].block.Hash()
@ -320,7 +320,7 @@ func (b *BlockChain) addOrphanBlock(block *btcutil.Block) {
b.orphans[*block.Hash()] = oBlock
// Add to previous hash lookup index for faster dependency lookups.
prevHash := &block.MsgBlock().Header.PrevBlocks[0] // TODO: (Stas) This is wrong. Modified only to satisfy compilation.
prevHash := block.MsgBlock().Header.SelectedPrevBlock()
b.prevOrphans[*prevHash] = append(b.prevOrphans[*prevHash], oBlock)
}
@ -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.SelectedPrevBlock()
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")
@ -1029,7 +1029,7 @@ func (b *BlockChain) connectBestChain(node *blockNode, block *btcutil.Block, fla
// We are extending the main (best) chain with a new block. This is the
// most common case.
parentHash := &block.MsgBlock().Header.PrevBlocks[0] // TODO: (Stas) This is wrong. Modified only to satisfy compilation.
parentHash := block.MsgBlock().Header.SelectedPrevBlock()
if parentHash.IsEqual(&b.bestChain.Tips()[0].hash) { // TODO: (Stas) This is wrong. Modified only to satisfy compilation.
// Skip checks if node has already been fully validated.
fastAdd = fastAdd || b.index.NodeStatus(node).KnownValid()

View File

@ -1146,13 +1146,13 @@ func (b *BlockChain) initChainState() error {
"first entry in block index to be genesis block, "+
"found %s", blockHash))
}
} else if header.PrevBlocks[0] == lastNode.hash { // TODO: (Stas) This is wrong. Modified only to satisfy compilation.
} else if *header.SelectedPrevBlock() == lastNode.hash {
// Since we iterate block headers in order of height, if the
// blocks are mostly linear there is a very good chance the
// previous header processed is the parent.
parent = lastNode
} else {
parent = b.index.LookupNode(&header.PrevBlocks[0]) // TODO: (Stas) This is wrong. Modified only to satisfy compilation.
parent = b.index.LookupNode(header.SelectedPrevBlock())
if parent == nil {
return AssertError(fmt.Sprintf("initChainState: Could "+
"not find parent for block %s", header.BlockHash()))

View File

@ -608,7 +608,7 @@ func (g *testGenerator) saveSpendableCoinbaseOuts() {
// reaching the block that has already had the coinbase outputs
// collected.
var collectBlocks []*wire.MsgBlock
for b := g.tip; b != nil; b = g.blocks[b.Header.PrevBlocks[0]] { // TODO: (Stas) This is wrong. Modified only to satisfy compilation.
for b := g.tip; b != nil; b = g.blocks[*b.Header.SelectedPrevBlock()] {
if b.BlockHash() == g.prevCollectedHash {
break
}
@ -1556,9 +1556,9 @@ func Generate(includeLargeReorg bool) (tests [][]TestInstance, err error) {
// ... -> b33(9) -> b35(10) -> b39(11) -> b42(12) -> b43(13) -> b53(14)
// \-> b54(15)
g.nextBlock("b54", outs[15], func(b *wire.MsgBlock) {
medianBlock := g.blocks[b.Header.PrevBlocks[0]] // TODO: (Stas) This is wrong. Modified only to satisfy compilation.
medianBlock := g.blocks[*b.Header.SelectedPrevBlock()]
for i := 0; i < medianTimeBlocks/2; i++ {
medianBlock = g.blocks[medianBlock.Header.PrevBlocks[0]] // TODO: (Stas) This is wrong. Modified only to satisfy compilation.
medianBlock = g.blocks[*medianBlock.Header.SelectedPrevBlock()]
}
b.Header.Timestamp = medianBlock.Header.Timestamp
})
@ -1570,9 +1570,9 @@ func Generate(includeLargeReorg bool) (tests [][]TestInstance, err error) {
// ... -> b33(9) -> b35(10) -> b39(11) -> b42(12) -> b43(13) -> b53(14) -> b55(15)
g.setTip("b53")
g.nextBlock("b55", outs[15], func(b *wire.MsgBlock) {
medianBlock := g.blocks[b.Header.PrevBlocks[0]] // TODO: (Stas) This is wrong. Modified only to satisfy compilation.
medianBlock := g.blocks[*b.Header.SelectedPrevBlock()]
for i := 0; i < medianTimeBlocks/2; i++ {
medianBlock = g.blocks[medianBlock.Header.PrevBlocks[0]] // TODO: (Stas) This is wrong. Modified only to satisfy compilation.
medianBlock = g.blocks[*medianBlock.Header.SelectedPrevBlock()]
}
medianBlockTime := medianBlock.Header.Timestamp
b.Header.Timestamp = medianBlockTime.Add(time.Second)
@ -1720,7 +1720,7 @@ func Generate(includeLargeReorg bool) (tests [][]TestInstance, err error) {
g.nextBlock("b61", outs[18], func(b *wire.MsgBlock) {
// Duplicate the coinbase of the parent block to force the
// condition.
parent := g.blocks[b.Header.PrevBlocks[0]] // TODO: (Stas) This is wrong. Modified only to satisfy compilation.
parent := g.blocks[*b.Header.SelectedPrevBlock()]
b.Transactions[0] = parent.Transactions[0]
})
rejected(blockdag.ErrOverwriteTx)

View File

@ -175,7 +175,7 @@ func storeFilter(dbTx database.Tx, block *btcutil.Block, f *gcs.Filter,
// Then fetch the previous block's filter header.
var prevHeader *daghash.Hash
ph := &block.MsgBlock().Header.PrevBlocks[0] // TODO: (Stas) This is wrong. Modified only to satisfy compilation.
ph := block.MsgBlock().Header.SelectedPrevBlock()
if ph.IsEqual(&zeroHash) {
prevHeader = &zeroHash
} else {

View File

@ -76,7 +76,7 @@ func dbIndexConnectBlock(dbTx database.Tx, indexer Indexer, block *btcutil.Block
if err != nil {
return err
}
if !curTipHash.IsEqual(&block.MsgBlock().Header.PrevBlocks[0]) { // TODO: (Stas) This is wrong. Modified only to satisfy compilation.
if !curTipHash.IsEqual(block.MsgBlock().Header.SelectedPrevBlock()) {
return AssertError(fmt.Sprintf("dbIndexConnectBlock must be "+
"called with a block that extends the current index "+
"tip (%s, tip %s, block %s)", indexer.Name(),
@ -118,7 +118,7 @@ func dbIndexDisconnectBlock(dbTx database.Tx, indexer Indexer, block *btcutil.Bl
}
// Update the current index tip.
prevHash := &block.MsgBlock().Header.PrevBlocks[0] // TODO: (Stas) This is wrong. Modified only to satisfy compilation.
prevHash := block.MsgBlock().Header.SelectedPrevBlock()
return dbPutIndexerTip(dbTx, idxKey, prevHash, block.Height()-1)
}
@ -332,7 +332,7 @@ func (m *Manager) Init(chain *blockdag.BlockChain, interrupt <-chan struct{}) er
}
// Update the tip to the previous block.
hash = &block.MsgBlock().Header.PrevBlocks[0] // TODO: (Stas) This is wrong. Modified only to satisfy compilation.
hash = block.MsgBlock().Header.SelectedPrevBlock()
height--
return nil

View File

@ -161,7 +161,7 @@ func readBlockTree(v1BlockIdxBucket database.Bucket) (map[daghash.Hash]*blockCha
}
blockHash := header.BlockHash()
prevHash := header.PrevBlocks[0] // TODO: (Stas) This is wrong. Modified only to satisfy compilation.
prevHash := *header.SelectedPrevBlock()
if blocksMap[blockHash] == nil {
blocksMap[blockHash] = &blockChainContext{height: -1}

View File

@ -434,7 +434,7 @@ func (view *UtxoViewpoint) disconnectTransactions(db database.DB, block *btcutil
// Update the best hash for view to the previous block since all of the
// transactions for the current block have been disconnected.
view.SetBestHash(&block.MsgBlock().Header.PrevBlocks[0]) // TODO: (Stas) This is wrong. Modified only to satisfy compilation.
view.SetBestHash(block.MsgBlock().Header.SelectedPrevBlock())
return nil
}

View File

@ -973,7 +973,7 @@ func (b *BlockChain) checkConnectBlock(node *blockNode, block *btcutil.Block, vi
}
// Ensure the view is for the node being checked.
parentHash := &block.MsgBlock().Header.PrevBlocks[0]
parentHash := block.MsgBlock().Header.SelectedPrevBlock()
if !view.BestHash().IsEqual(parentHash) {
return AssertError(fmt.Sprintf("inconsistent view when "+
"checking block connection: best hash is %v instead "+
@ -1212,9 +1212,9 @@ func (b *BlockChain) CheckConnectBlockTemplate(block *btcutil.Block) error {
// current chain.
tip := b.bestChain.Tips()[0] // TODO: (Stas) This is wrong. Modified only to satisfy compilation.
header := block.MsgBlock().Header
if tip.hash != header.PrevBlocks[0] { // TODO: (Stas) This is wrong. Modified only to satisfy compilation.
if tip.hash != *header.SelectedPrevBlock() {
str := fmt.Sprintf("previous block must be the current chain tip %v, "+
"instead got %v", tip.hash, header.PrevBlocks[0]) // TODO: (Stas) This is wrong. Modified only to satisfy compilation.
"instead got %v", tip.hash, header.SelectedPrevBlock())
return ruleError(ErrPrevBlockNotBest, str)
}

View File

@ -66,6 +66,15 @@ func (h *BlockHeader) BlockHash() daghash.Hash {
return daghash.DoubleHashH(buf.Bytes())
}
// BestPrevBlock returns the hash of the selected block header.
func (header *BlockHeader) SelectedPrevBlock() *daghash.Hash {
if header.NumPrevBlocks == 0 {
return nil
}
return &header.PrevBlocks[0]
}
// BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
// This is part of the Message interface implementation.
// See Deserialize for decoding block headers stored to disk, such as in a