Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chaingen: Track expected blk heights separately. #1101

Merged
merged 1 commit into from
Mar 2, 2018

Conversation

dnldd
Copy link
Member

@dnldd dnldd commented Mar 1, 2018

No description provided.

@dnldd dnldd changed the title chaingen: use updated block height in NextBlock & SetTip reorg. chaingen: use updated block height in NextBlock & SetTip reorg. Mar 1, 2018
blockHeight, ok := g.blockHeights[blockHash]
if !ok {
panic(fmt.Sprintf("No block height found for block hash %s",
blockHash.String()))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to call String, it's already a fmt.Stringer.

@@ -1666,18 +1668,23 @@ func (g *Generator) connectBlockTickets(b *wire.MsgBlock) {
}

// Extract the ticket purchases (sstx) from the block.
height := b.Header.Height
blockHash := b.BlockHash()
blockHeight, ok := g.blockHeights[blockHash]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than repeating this everywhere, please just create a function that panics.

func (g *Generator) blockHeight(hash chainhash.Hash) uint32 {
	height, ok := g.blockHeights[hash]
	if !ok {
		panic(fmt.Sprintf("no block height found for block %s", hash)
	}
}

Then, you can simply do:

blockHeight := g.blockHeight(blockHash)

Also, notice the lowercase panic message to be consistent with everything else.

@@ -1691,8 +1698,13 @@ func (g *Generator) disconnectBlockTickets(b *wire.MsgBlock) {
"the current tip %s", b.BlockHash(), g.tip.BlockHash()))
}

blockHash := b.BlockHash()
blockHeight, ok := g.blockHeights[blockHash]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With aforementioned suggestion, this reduces to:

blockHeight := g.blockHeight(blockHash)

@@ -1707,7 +1719,13 @@ func (g *Generator) disconnectBlockTickets(b *wire.MsgBlock) {

// Move tickets that are no longer mature from the live ticket pool to
// the immature ticket pool.
prevBlockHeight := blockHeight - 1

prevBlockHeight := g.blockHeights[b.Header.PrevBlock]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And

prevBlockHeight := g.blockHeight(b.Header.PrevBlock)

@@ -1785,11 +1802,25 @@ func (g *Generator) SetTip(blockName string) {
// As long as the two branches are not at the same height, add
// the tip of the longest one to the appropriate connect or
// disconnect list and move its tip back to its previous block.
if oldBranch.Header.Height > newBranch.Header.Height {
oldBranchHeight, ok := g.blockHeights[oldBranch.BlockHash()]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oldBranchHeight := g.blockHeight(oldBranch.BlockHash())
newBranchHeight := g.blockHeight(newBranch.BlockHash())

@@ -2085,9 +2116,10 @@ func (g *Generator) NextBlock(blockName string, spend *SpendableOut, ticketSpend
// it.
g.originalParents[blockHash] = prevHash
}
g.connectLiveTickets(&blockHash, nextHeight, ticketWinners,
g.connectLiveTickets(&blockHash, block.Header.Height, ticketWinners,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fairly certain this shouldn't be changed. Just because the block header height changed making it invalid does not mean the rest of the state should be made invalid to match.

ticketPurchases)
g.blocks[blockHash] = &block
g.blockHeights[blockHash] = block.Header.Height
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be nextHeight, not the potentially invalid value from the header.

delete(g.blocksByName, oldBlockName)
delete(g.wonTickets, oldBlockHash)

// Add new entries.
newBlockHash := newBlock.BlockHash()
g.blocks[newBlockHash] = newBlock
g.blockHeights[newBlockHash] = newBlock.Header.Height
Copy link
Member

@davecgh davecgh Mar 1, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The existing height in should be reinserted for the new hash, not the now potentially invalid one.

wonTickets := g.wonTickets[oldBlockHash]
existingHeight := g.blockHeight(oldBlockHash)
...
g.blockHeights[newBlockHash] = existingHeight

@@ -176,6 +177,7 @@ func MakeGenerator(params *chaincfg.Params) (Generator, error) {
tip: genesis,
tipName: "genesis",
blocks: map[chainhash.Hash]*wire.MsgBlock{genesisHash: genesis},
blockHeights: map[chainhash.Hash]uint32{genesis.BlockHash(): genesis.Header.Height},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The genesis height is always zero. Better to hard code that to assert it.

map[chainhash.Hash]uint32{genesis.BlockHash(): 0},

@davecgh davecgh changed the title chaingen: use updated block height in NextBlock & SetTip reorg. chaingen: Track expected blk heights separately. Mar 1, 2018
@davecgh davecgh merged commit 365a0db into decred:master Mar 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants