Skip to content

Conversation

luke-jr
Copy link
Member

@luke-jr luke-jr commented Oct 12, 2014

Unlikely, but might as well check it...

@TheBlueMatt
Copy link
Contributor

I would just assert this one?

@luke-jr
Copy link
Member Author

luke-jr commented Oct 12, 2014

@TheBlueMatt I'm not sure under what conditions this might occur. Tip() explicitly returns NULL in some cases... Don't want running a miner to crash your Bitcoin node.

@TheBlueMatt
Copy link
Contributor

It should never return null unless you havent loaded even the genesis block, I think

@sipa
Copy link
Member

sipa commented Oct 12, 2014

Correct. chainActive.Tip should never be NULL after AppInit. Either LoadBlockIndex or InitBlockIndex fixed that.

@luke-jr
Copy link
Member Author

luke-jr commented Oct 12, 2014

@sipa Would you object to making Tip() return a reference rather than a pointer, then?

@sipa
Copy link
Member

sipa commented Oct 12, 2014

We use CBlockIndex* variables everywhere as identifiers for blocks, so yes, I would rather not change that. Also, it is effectively useful inside the initialization code to detect the we're-not-yet-initialized case.

@laanwj
Copy link
Member

laanwj commented Oct 13, 2014

Correct. chainActive.Tip should never be NULL after AppInit. Either LoadBlockIndex or InitBlockIndex fixed that.

This post #4995 (comment) in #4995 claims that in the case of -reindex it can be NULL (note: haven't checked this myself).

@jgarzik
Copy link
Contributor

jgarzik commented Oct 13, 2014

This is an assert, not a normal one. Should be impossible for this condition to occur, even with reindex.

reindex always hand-builds the genesis block.

@sipa
Copy link
Member

sipa commented Oct 13, 2014

Oh, no, indeed. If you reindex, InitBlockIndex bypassing rebuilding the
genesis block, as the one already on disk will be used.

@TheBlueMatt
Copy link
Contributor

@laanwj Hmm, would it not make more sense to fix that by making sure Tip() always points to at least genesis, instead of letting it be NULL?

@sipa
Copy link
Member

sipa commented Oct 13, 2014

When you're reindexing, there is no genesis CBlockIndex before you start
the reindex (as you don't know where it is on disk yet).

@TheBlueMatt
Copy link
Contributor

OK, so we should wait to return from AppInit before we at least have the genesis block reindexed?

@laanwj
Copy link
Member

laanwj commented Oct 14, 2014

Now that we've established that chainActive.Tip() can return NULL, I suppose the most straightforward way forward is document this behaviour and make the code robust against it where necessary.

Just delaying the return won't solve anything. @TheBlueMatt To be precise we'd have to wait after starting ThreadImport, before starting the node and RPC threads and internal miner. It would be another possible solution, although a bit hackier than just considering "no chain yet" a legit state, it may have less code impact overall...

@TheBlueMatt
Copy link
Contributor

@laanwj Yes, I was going for less impact. It certainly seems rather strange to start import in init, and then finish initializing everything else before we have even initialized the genesis block (which should be one of the first blocks imported, no?).

@laanwj
Copy link
Member

laanwj commented Oct 15, 2014

OK. Then cleanest would be to split up reindex into a 'locate genesis block' and 'reindex chain' part, the first part being executed from AppInit2. This avoids ugly constructions with waiting for ThreadImport.

@laanwj
Copy link
Member

laanwj commented Oct 15, 2014

Correct me if wrong: when reindexing, even with headers-first, the genesis block is always the first block in the first block file (blk00000.dat). If not we are reindexing the wrong chain, for example a testnet block chain on a mainnet node or dealing with a corrupted block file. In that case it acceptable to throw an initialization error.

I can't think of any exceptions to this.

@sipa
Copy link
Member

sipa commented Oct 15, 2014

The genesis block should be the first one yes, as it's not received from network but put there in InitBlockIndex, If it is not, there's a problem.

@sipa
Copy link
Member

sipa commented Nov 6, 2014

utACK

@TheBlueMatt
Copy link
Contributor

Ive always considered the chainActive.Tip() never returning NULL to be a rule, so I'd prefer to wait in init for it to be the case (ie #5243)

@laanwj
Copy link
Member

laanwj commented Dec 5, 2014

Closing in favor of #5243.

@laanwj laanwj closed this Dec 5, 2014
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Sep 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants