-
Notifications
You must be signed in to change notification settings - Fork 296
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
Conversation
NextBlock
& SetTip
reorg.
blockchain/chaingen/generator.go
Outdated
blockHeight, ok := g.blockHeights[blockHash] | ||
if !ok { | ||
panic(fmt.Sprintf("No block height found for block hash %s", | ||
blockHash.String())) |
There was a problem hiding this comment.
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
.
blockchain/chaingen/generator.go
Outdated
@@ -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] |
There was a problem hiding this comment.
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.
blockchain/chaingen/generator.go
Outdated
@@ -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] |
There was a problem hiding this comment.
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)
blockchain/chaingen/generator.go
Outdated
@@ -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] |
There was a problem hiding this comment.
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)
blockchain/chaingen/generator.go
Outdated
@@ -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()] |
There was a problem hiding this comment.
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())
blockchain/chaingen/generator.go
Outdated
@@ -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, |
There was a problem hiding this comment.
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.
blockchain/chaingen/generator.go
Outdated
ticketPurchases) | ||
g.blocks[blockHash] = &block | ||
g.blockHeights[blockHash] = block.Header.Height |
There was a problem hiding this comment.
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.
blockchain/chaingen/generator.go
Outdated
delete(g.blocksByName, oldBlockName) | ||
delete(g.wonTickets, oldBlockHash) | ||
|
||
// Add new entries. | ||
newBlockHash := newBlock.BlockHash() | ||
g.blocks[newBlockHash] = newBlock | ||
g.blockHeights[newBlockHash] = newBlock.Header.Height |
There was a problem hiding this comment.
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
blockchain/chaingen/generator.go
Outdated
@@ -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}, |
There was a problem hiding this comment.
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},
No description provided.