Skip to content

Conversation

@jnewbery
Copy link
Contributor

Since at least #17693, the extra nonce logic has been unused.

Prior to that PR, if the nNonce field rolled, then the while loop inside generateBlocks() would iterate, a new block would be generated in CreateNewBlock(), and the extra nonce could be incremented. See dcc8332#diff-ccc24453c13307f815879738d3bf00eec351417537fbf10dde1468180cacd2f1L132-L133.

After that PR, the extra_nonce can only be set to 0 in the call to IncrementExtraNonce().

I think this is fine and we should just remove the unused code. The generate RPCs are only used for regtest and signet. For regtest, the difficulty is always minimum, so the nNonce field won't roll. If the nNonce field rolls in signet and the rpc fails, then the rpc can just be called again and a new candidate block will be generated.

@DrahtBot
Copy link
Contributor

DrahtBot commented Mar 27, 2021

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

Reviewers, this pull request conflicts with the following ones:

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@Empact
Copy link
Contributor

Empact commented Mar 30, 2021

Concept ACK

@jnewbery jnewbery force-pushed the 2021-03-extra-nonce branch from 4740100 to 28f052f Compare March 30, 2021 08:06
@jnewbery
Copy link
Contributor Author

Takes the height of the desired block instead of the *CBlockIndex of the
previous block.
It's redundant with the block in/out param.
@jnewbery
Copy link
Contributor Author

Thanks for the review @Empact! I've addressed your comment.

@jnewbery jnewbery force-pushed the 2021-03-extra-nonce branch from 28f052f to fcc0b52 Compare March 30, 2021 08:30
@jnewbery jnewbery changed the title [mining] Remove unused extranonce logic mining: Remove unused extranonce logic Mar 30, 2021
Copy link
Contributor

@theStack theStack left a comment

Choose a reason for hiding this comment

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

Concept ACK

Reviewed commits 05d053c ... b97a35b, changes LGTM
However, I think the last commit (fcc0b52) changes the logic unintentionally, as reaching the maximum nNonce then leads to a silent fail, i.e. the generateblock RPC call would succeed without having generated a block.
Before this change, GenerateBlock used kind of a tri-state logic to show if it's been successful:

  • returns false: generation of block failed
  • returns true with valid block_hash out-param: generation of block succeeded
  • returns true with invalid (nullified) block_hash out-param: generation of block didn't succeed, maximum nNonce reached

The third case is not needed anymore. The introduced check in GenerateBlock for block.GetHash().IsNull() can be removed (I don't think the condition can ever be true) and if nNonce is about to wrap, simply false rather than true should be returned, IMHO.

block_hash = block.GetHash();
if (block.GetHash().IsNull()) {
throw JSONRPCError(RPC_MISC_ERROR, "Failed to make block.");
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I tend to agree with @theStack that throwing here is a change of failure modes for generateBlocks, impacting generatetodescriptor and generatetoaddress.

However, I'm not clear on when this condition occurs. CBlock::GetHash() resolves to SerializeHash(*this), and I didn't see a failure possible in the serialization logic (through a cursory glance).

Options seem to be:

  • retain existing behavior by returning false in this case
  • fail more noisily, given that this is not an expected error, as here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's a very strange and unintuitive interface. The passed out hash will be null if block.nNonce == std::numeric_limits<uint32_t>::max(). The nullness of that out-param is what is used to determine whether the outer while loop should increment the height or try again at the same height.

@jnewbery
Copy link
Contributor Author

Thanks for the reviews @theStack and @Empact. I realize now that I've misread the new GenerateBlock() function and that the effective tri-state return means that the extranonce can be incremented if we exit from GenerateBlock() with block_hash unset.

I think it's very confusing that state is being passed around and shared between generateBlocks(), GenerateBlock(), and IncrementExtraNonce() (which is storing a static variable of the hash of the previous block). I think that could be clarified/cleaned up, but for now I'm just going to close this PR.

@jnewbery jnewbery closed this Apr 14, 2021
@DrahtBot DrahtBot mentioned this pull request Apr 14, 2021
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Aug 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants