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

Only call NotifyBlockTip when chainActive changes #12431

Merged

Conversation

jamesob
Copy link
Member

@jamesob jamesob commented Feb 14, 2018

This is a subset of the more controversial #12407, but this also adds a test demonstrating the bug.

In InvalidateBlock, we're calling NotifyBlockTip with the now-invalid block's prev regardless of what chain the ancestor block is on. This could create numerous issues, but it at least screws up waitforblockheight (or anything else relying on rpc/blockchain.cpp:latestblock) when InvalidateBlock is called on a block not in chainActive, which can happen via RPC.

Only call NotifyBlockTip when the block being marked invalid is on the active chain.

@jamesob jamesob force-pushed the jamesob/2018-02-prevent-bad-latestblock branch 2 times, most recently from 0eb52a0 to 8354565 Compare February 14, 2018 03:54

assert_waitforheight(0)
assert_waitforheight(current_height - 3)
assert_waitforheight(current_height)
Copy link
Contributor

@conscott conscott Feb 15, 2018

Choose a reason for hiding this comment

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

Might also want to add a timeout case

Copy link
Member Author

Choose a reason for hiding this comment

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

Played around with doing this, but found that the return from RPC is (unfortunately) no different in the timeout case, meaning we'd have to spin up a separate thread in-test, run a timer, and then assert that the RPC thread hadn't yet stopped execution -- probably overkill.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've added a testcase for current_height + 1, though.

Demonstrates the presence of a bug in in `validation.cpp:InvalidateBlock`
which will update `rpc/blockchain.cpp:latestblock` erroneously.
Previously, if `invalidateblock` was called on a block in a branch,
NotifyBlockTip would be called on that block's predecessor, creating an
incorrect `rpc/blockchain.cpp:latestblock` value.

Only call NotifyBlockTip if the chain being modified is activeChain.
@jamesob jamesob force-pushed the jamesob/2018-02-prevent-bad-latestblock branch from 8354565 to f98b543 Compare February 16, 2018 16:51
@jamesob
Copy link
Member Author

jamesob commented Feb 16, 2018

Thanks for the look, @conscott. Rebased.

@conscott
Copy link
Contributor

utACK f98b543

@maflcko maflcko self-requested a review February 16, 2018 17:00
@ryanofsky
Copy link
Contributor

ryanofsky commented Mar 6, 2018

@jamesob, I'm curious how this might interact with #11856 with gets rid of the uiInterface.NotifyBlockTip signal, replacing it with CValidationInterface::UpdatedBlockTip listeners.

EDIT: Looking into this more, this bugfix makes sense in combination with #11856, and would prevent spurious UpdatedBlockTip notifications in that PR.

Copy link
Contributor

@ryanofsky ryanofsky left a comment

Choose a reason for hiding this comment

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

utACK f98b543. This fixes a pretty clear waitforblockheight bug and adds a nice test case.

@sdaftuar
Copy link
Member

sdaftuar commented Mar 7, 2018

utACK f98b543

@ryanofsky
Copy link
Contributor

@TheBlueMatt, this is a simple, one line change fixing the NotifyBlockTip notification in InvalidateBlock. Could you confirm that it looks ok and doesn't mess up your plans for #11856?

@TheBlueMatt
Copy link
Contributor

Without thinking about it too hard, this seems fine. I'd obviously like to move towards getting rid of uiInterface calls in validation, ala #11856, but this should likely be applied there as well.

@ryanofsky
Copy link
Contributor

@laanwj, I think this looks good to merge

@laanwj laanwj merged commit f98b543 into bitcoin:master Mar 15, 2018
laanwj added a commit that referenced this pull request Mar 15, 2018
f98b543 Only call NotifyBlockTip when the active chain changes (James O'Beirne)
152b7fb [tests] Add a (failing) test for waitforblockheight (James O'Beirne)

Pull request description:

  This is a subset of the more controversial #12407, but this also adds a test demonstrating the bug.

  In InvalidateBlock, we're calling NotifyBlockTip with the now-invalid block's prev regardless of what chain the ancestor block is on. This could create numerous issues, but it at least screws up `waitforblockheight` (or anything else relying on `rpc/blockchain.cpp:latestblock`) when InvalidateBlock is called on a block not in chainActive, which can happen via RPC.

  Only call NotifyBlockTip when the block being marked invalid is on the active chain.

Tree-SHA512: 9a54fe5e8c7eb489daf5df4483c0986129e871e2ca931a456ba869ecb5d5a8d4f7bd27ccc9e711e9292c9ed79ddef896c85d0e81fc76883503e327995b0e914f
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jun 10, 2020
f98b543 Only call NotifyBlockTip when the active chain changes (James O'Beirne)
152b7fb [tests] Add a (failing) test for waitforblockheight (James O'Beirne)

Pull request description:

  This is a subset of the more controversial bitcoin#12407, but this also adds a test demonstrating the bug.

  In InvalidateBlock, we're calling NotifyBlockTip with the now-invalid block's prev regardless of what chain the ancestor block is on. This could create numerous issues, but it at least screws up `waitforblockheight` (or anything else relying on `rpc/blockchain.cpp:latestblock`) when InvalidateBlock is called on a block not in chainActive, which can happen via RPC.

  Only call NotifyBlockTip when the block being marked invalid is on the active chain.

Tree-SHA512: 9a54fe5e8c7eb489daf5df4483c0986129e871e2ca931a456ba869ecb5d5a8d4f7bd27ccc9e711e9292c9ed79ddef896c85d0e81fc76883503e327995b0e914f
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jun 13, 2020
f98b543 Only call NotifyBlockTip when the active chain changes (James O'Beirne)
152b7fb [tests] Add a (failing) test for waitforblockheight (James O'Beirne)

Pull request description:

  This is a subset of the more controversial bitcoin#12407, but this also adds a test demonstrating the bug.

  In InvalidateBlock, we're calling NotifyBlockTip with the now-invalid block's prev regardless of what chain the ancestor block is on. This could create numerous issues, but it at least screws up `waitforblockheight` (or anything else relying on `rpc/blockchain.cpp:latestblock`) when InvalidateBlock is called on a block not in chainActive, which can happen via RPC.

  Only call NotifyBlockTip when the block being marked invalid is on the active chain.

Tree-SHA512: 9a54fe5e8c7eb489daf5df4483c0986129e871e2ca931a456ba869ecb5d5a8d4f7bd27ccc9e711e9292c9ed79ddef896c85d0e81fc76883503e327995b0e914f
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jun 13, 2020
f98b543 Only call NotifyBlockTip when the active chain changes (James O'Beirne)
152b7fb [tests] Add a (failing) test for waitforblockheight (James O'Beirne)

Pull request description:

  This is a subset of the more controversial bitcoin#12407, but this also adds a test demonstrating the bug.

  In InvalidateBlock, we're calling NotifyBlockTip with the now-invalid block's prev regardless of what chain the ancestor block is on. This could create numerous issues, but it at least screws up `waitforblockheight` (or anything else relying on `rpc/blockchain.cpp:latestblock`) when InvalidateBlock is called on a block not in chainActive, which can happen via RPC.

  Only call NotifyBlockTip when the block being marked invalid is on the active chain.

Tree-SHA512: 9a54fe5e8c7eb489daf5df4483c0986129e871e2ca931a456ba869ecb5d5a8d4f7bd27ccc9e711e9292c9ed79ddef896c85d0e81fc76883503e327995b0e914f
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jun 13, 2020
f98b543 Only call NotifyBlockTip when the active chain changes (James O'Beirne)
152b7fb [tests] Add a (failing) test for waitforblockheight (James O'Beirne)

Pull request description:

  This is a subset of the more controversial bitcoin#12407, but this also adds a test demonstrating the bug.

  In InvalidateBlock, we're calling NotifyBlockTip with the now-invalid block's prev regardless of what chain the ancestor block is on. This could create numerous issues, but it at least screws up `waitforblockheight` (or anything else relying on `rpc/blockchain.cpp:latestblock`) when InvalidateBlock is called on a block not in chainActive, which can happen via RPC.

  Only call NotifyBlockTip when the block being marked invalid is on the active chain.

Tree-SHA512: 9a54fe5e8c7eb489daf5df4483c0986129e871e2ca931a456ba869ecb5d5a8d4f7bd27ccc9e711e9292c9ed79ddef896c85d0e81fc76883503e327995b0e914f
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jun 17, 2020
f98b543 Only call NotifyBlockTip when the active chain changes (James O'Beirne)
152b7fb [tests] Add a (failing) test for waitforblockheight (James O'Beirne)

Pull request description:

  This is a subset of the more controversial bitcoin#12407, but this also adds a test demonstrating the bug.

  In InvalidateBlock, we're calling NotifyBlockTip with the now-invalid block's prev regardless of what chain the ancestor block is on. This could create numerous issues, but it at least screws up `waitforblockheight` (or anything else relying on `rpc/blockchain.cpp:latestblock`) when InvalidateBlock is called on a block not in chainActive, which can happen via RPC.

  Only call NotifyBlockTip when the block being marked invalid is on the active chain.

Tree-SHA512: 9a54fe5e8c7eb489daf5df4483c0986129e871e2ca931a456ba869ecb5d5a8d4f7bd27ccc9e711e9292c9ed79ddef896c85d0e81fc76883503e327995b0e914f
@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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants