Skip to content

Conversation

@mzumsande
Copy link
Contributor

@mzumsande mzumsande commented Jun 24, 2022

In the gettxoutsetinfo RPC, if we set use_index to false but specify hash_or_height, we currently hit a nonfatal error, e.g. gettxoutsetinfo "muhash" "1" "false" results in:

Internal bug detected: "!pindex || pindex->GetBlockHash() == view->GetBestBlock()"
rpc/blockchain.cpp:836 (GetUTXOStats) 

The failing check was added in #24410, but the previous behavior, returning the specified height together with data corresponding to the tip's height, was very confusing too in my opinion.
Fix this by disallowing the interaction of use_index=false and hash_or_height and add a RPC help example with -named because users might ask themselves how to use the use_index flag witout hitting an error.

An alternative way would be to allow the interaction if the specified hash_or_height happens to correspond to the tip (which should then also be applied to the HASH_SERIALIZED check before). If reviewers would prefer that, please say so.

@DrahtBot
Copy link
Contributor

DrahtBot commented Jun 25, 2022

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

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #19949 (cli: Parse and allow hash value by fjahr)

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.

Copy link
Contributor

@shaavan shaavan 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

  • I agree with allowing displaying an RPC error instead of an Internal bug error because:
    1. AFAIK. This will enable the incorrect command to fail faster
    2. This will allow users to understand better the problem's cause and how to solve it.

Now, as for the alternative that you suggested:

An alternative way would be to allow the interaction if the specified hash_or_height happens to correspond to the tip...

I want to explain why I think it is better not to switch to the alternative.

  1. This will potentially increase the code complexity.
  2. Running the command bitcoin-cli -named gettxoutsetinfo hash_type='muhash' use_index='false' prints the info for the current best block. So technically, the alternative is not needed.
  3. Since it is explicitly mentioned in the gettxoutsetinfo help message, the hash_or_height option is available only with the coinstatsindex. So ideally, it shouldn't be allowed to provide hash_or_height value with use_index=0.
Arguments:
1. hash_type         (string, optional, default="hash_serialized_2") Which UTXO set hash should be calculated. Options: 'hash_serialized_2' (the legacy algorithm), 'muhash', 'none'.
2. hash_or_height    (string or numeric, optional) The block hash or height of the target height (only available with coinstatsindex).
3. use_index         (boolean, optional, default=true) Use coinstatsindex, if available.

However, not using the alternative makes the gettxoutsetinfo command quite verbose, as there is no way to make use_index = false without using the --named option.

Screenshot:

Screenshot from 2022-06-25 14-59-50

So I would suggest reordering the arguments of this RPC to

1. `hash_type`
2. `use_index`
3. `hash_or_height`

Because, first, it would eliminate the need to use --named for specific cases, and second, this makes the ordering of argument logically sounder.

And I would also suggest mentioning in the description of hash_or_height, that not using this command would print info for the best block.

@mzumsande
Copy link
Contributor Author

Thanks for the review!

So I would suggest reordering the arguments of this RPC

That would be nice, but it would break compatibility with older versions, which I think should really be avoided unless absolutely necessary. For other RPCs, we sometimes even keep dysfunctional dummy-arguments around to avoid this. (prioritisetransaction)

And I would also suggest mentioning in the description of hash_or_height, that not using this command would print info for the best block.

I'll do this with my next push!

@Riahiamirreza
Copy link
Contributor

Riahiamirreza commented Jun 26, 2022

I tried to produce the same error and have difficulty with it.
Where the use_index and hash_or_height are given to the program? Are they api args? or should be set while running bitcoind? I checked the api docs but not found something useful.

@mzumsande
Copy link
Contributor Author

Where the use_index and hash_or_height are given to the program? Are they api args? or should be set while running bitcoind? I checked the api docs but not found something useful.

You need to start bitcoind with the coinstatsindex to reproduce this, e.g. ./bitcoind -signet -coinstatsindex, and wait until the index is synced. After that, you can query ./bitcoin-cli -signet gettxoutsetinfo "muhash" "1" "false", which should give you this internal error on master.

Doing the same locally with -regtest instead of -signet works too, but then you need to mine a couple of blocks yourself (./bitcoin-cli -regtest -generate 10 before querying gettxoutsetinfo ).

…ndex=false

by returning an RPC error where previously a NonFatalError
would be thrown.
@mzumsande mzumsande force-pushed the 202206_gettxoutsetinfo_check branch from 2e9c043 to 27c8056 Compare June 28, 2022 22:33
@mzumsande
Copy link
Contributor Author

2e9c043 to 27c8056:
added explanation for the default of hash_or_height parameter (thanks @shaavan)

Copy link
Contributor

@shaavan shaavan left a comment

Choose a reason for hiding this comment

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

ACK 27c8056

Changes since my last review:

  • The help message for the default behavior of the hash_or_height argument has been added.

I was able to successfully run the ./bitcoin-cli -signet help gettxoutsetinfo command, and verify the help message has been displayed correctly.

Screenshot:
image

@maflcko maflcko requested review from dongcarl and fjahr June 29, 2022 14:16
@fjahr
Copy link
Contributor

fjahr commented Jun 29, 2022

utACK 27c8056

@dongcarl
Copy link
Contributor

Concept ACK for fixing the internal error that's exposed to RPC


In the longer term, I'd like to see CreateUTXOSnapshot use kernel::ComputeUTXOStats directly, and GetUTXOStats inlined into its only user gettxoutsetinfo and a lot of the if else checking can be removed.

@maflcko maflcko merged commit 53b1a24 into bitcoin:master Jul 1, 2022
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Jul 1, 2022
…ific block with `use_index=false`

27c8056 rpc: Disallow gettxoutsetinfo queries for a specific block with use_index=false (Martin Zumsande)

Pull request description:

  In the `gettxoutsetinfo` RPC, if we set `use_index` to false but specify `hash_or_height`, we currently hit a nonfatal error, e.g. `gettxoutsetinfo "muhash" "1" "false"` results in:
  ```
  Internal bug detected: "!pindex || pindex->GetBlockHash() == view->GetBestBlock()"
  rpc/blockchain.cpp:836 (GetUTXOStats)
  ```
  The failing check was added in [bitcoin#24410](bitcoin@664a14b), but the previous behavior, returning the specified height together with data corresponding to the tip's height, was very confusing too in my opinion.
  Fix this by disallowing the interaction  of `use_index=false` and `hash_or_height` and add a RPC help example with `-named` because users might ask themselves how to use the `use_index` flag witout hitting an error.

  An alternative way would be to allow the interaction if the specified `hash_or_height` happens to correspond to the tip (which should then also be applied to the `HASH_SERIALIZED` check before). If reviewers would prefer that, please say so.

ACKs for top commit:
  fjahr:
    utACK 27c8056
  shaavan:
    ACK 27c8056

Tree-SHA512: 1d81c34eaa48c86134a2cf7193246d5de6bfd819d413c3b3fae9cb9290e0297a336111eeaecede2f0f020b0f9a181d240de0da4493e1b387fe63b8189154442b
@mzumsande mzumsande deleted the 202206_gettxoutsetinfo_check branch November 3, 2022 01:11
@bitcoin bitcoin locked and limited conversation to collaborators Nov 3, 2023
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.

8 participants