Skip to content

rpc: Stamp getblock OpenRPC result discriminator - #36175

Draft
nervana21 wants to merge 3 commits into
bitcoin:masterfrom
nervana21:openrpc-discriminated-result
Draft

nervana21 wants to merge 3 commits into
bitcoin:masterfrom
nervana21:openrpc-discriminated-result

Conversation

@nervana21

@nervana21 nervana21 commented Sep 5, 2026 •

Copy link
Copy Markdown
Contributor

OpenRPC documents getblock as a four-way oneOf on verbosity, but the dump does not say which integer selects which arm. Therefore, clients that expose a type or method per verbosity (e.g. corepc, btcd) have to hardcode that map.

This patch adds optional RPCResultDiscriminator metadata, emits it as x-bitcoin-discriminated-result, and stamps getblock. getblock request handling and returned block data are unchanged. Now, help getblock notes that verbosity 2 and 3 cannot be told apart by validating a response alone.

Also updates getopenrpcinfo schema text to document x-bitcoin-discriminated-result and x-bitcoin-default-hint.

The change to the schema's format is:

x-bitcoin-discriminated-result on getblock
diff dump.json dump_new.json
5590c5590,5600
<           ]
---
>           ],
>           "x-bitcoin-discriminated-result": {
>             "parameter": "verbosity",
>             "parameterIndex": 1,
>             "values": [
>               0,
>               1,
>               2,
>               3
>             ]
>           }
getblock description notes verbosity 2 and 3 overlap
diff openrpc.json openrpc_new.json
4696c4696
<       "description": "If verbosity is 0, returns a string that is serialized, hex-encoded data for block 'hash'.\nIf verbosity is 1, returns an Object with information about block <hash>.\nIf verbosity is 2, returns an Object with information about block <hash> and information about each transaction.\nIf verbosity is 3, returns an Object with information about block <hash> and information about each transaction, including prevout information for inputs (only for unpruned blocks in the current best chain).",
---
>       "description": "If verbosity is 0, returns a string that is serialized, hex-encoded data for block 'hash'.\nIf verbosity is 1, returns an Object with information about block <hash>.\nIf verbosity is 2, returns an Object with information about block <hash> and information about each transaction.\nIf verbosity is 3, returns an Object with information about block <hash> and information about each transaction, including prevout information for inputs (only for unpruned blocks in the current best chain).\nThe verbosity = 2 and verbosity = 3 result schemas cannot be distinguished by validating a response alone because a block with no spent inputs returns byte-identical JSON at both levels (a coinbase input has no prevout to add), and the verbosity = 3 schema lists prevout as optional, so both object arms accept both responses.",
schema text for x-bitcoin-default-hint and x-bitcoin-discriminated-result
diff openrpc.json openrpc_new.json
8610c8610
<                           "description": "JSON Schema for the parameter."
---
>                           "description": "JSON Schema for the parameter. May include \"x-bitcoin-default-hint\" when the default cannot be expressed as a JSON value."
8649c8649
<                         "description": "JSON Schema for the result. Numeric schemas may include \"x-bitcoin-unit\" property: \"amount\" which denotes a Bitcoin amount in BTC."
---
>                         "description": "JSON Schema for the result. Numeric schemas may include \"x-bitcoin-unit\" property: \"amount\" which denotes a Bitcoin amount in BTC. Multi-result schemas may include \"x-bitcoin-discriminated-result\" naming the request parameter or parameters that select among the top-level oneOf branches."
13319c13319
<                           "description": "JSON Schema for the parameter."
---
>                           "description": "JSON Schema for the parameter. May include \"x-bitcoin-default-hint\" when the default cannot be expressed as a JSON value."
13358c13358
<                         "description": "JSON Schema for the result. Numeric schemas may include \"x-bitcoin-unit\" property: \"amount\" which denotes a Bitcoin amount in BTC."
---
>                         "description": "JSON Schema for the result. Numeric schemas may include \"x-bitcoin-unit\" property: \"amount\" which denotes a Bitcoin amount in BTC. Multi-result schemas may include \"x-bitcoin-discriminated-result\" naming the request parameter or parameters that select among the top-level oneOf branches."

Other results can also use the same discriminator pattern.

@DrahtBot

DrahtBot commented Sep 5, 2026 •

Copy link
Copy Markdown
Contributor

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

External sites

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
ACK willcl-ark
Concept ACK jeanpablojp

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

@sedited
sedited requested a review from willcl-ark September 5, 2026 14:32
@nervana21
nervana21 force-pushed the openrpc-discriminated-result branch from 10f69e5 to 270ecac Compare September 6, 2026 21:52

@jeanpablojp jeanpablojp left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Concept ACK

One thing I could not settle by reading. The test asserts the discriminator's literals rather than the two relations behind them.

Inserting an argument before verbosity in getblock leaves parameterIndex 1 addressing the new argument, and swapping the verbosity 2 and 3 arms points value 2 at the arm with prevout. rpc_openrpc.py passes on both builds, and none of the three new CHECK_NONFATALs looks at either relation. Worth pinning them?

Comparing getblock["params"][discriminator["parameterIndex"]]["name"] to discriminator["parameter"], and asserting the two object arms differ in prevout in the order values claims, fails on both and passes on this head.

Comment thread src/rpc/util.h Resolved
Comment thread src/rpc/server.cpp Outdated
UniValue vals{UniValue::VARR};
vals.push_backV(discriminator->values);
disc_meta.pushKV("values", std::move(vals));
result_schema.pushKV("x-bitcoin-discriminatedResult", std::move(disc_meta));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The schema field in OpenRPCDocResult already explains x-bitcoin-unit to whoever reads the help. Worth a line for x-bitcoin-discriminatedResult too? The parameter-side schema field next to it has the same gap for x-bitcoin-default-hint, if you would rather cover both at once.

nit: the other x-bitcoin- keys in the document are all lowercase and hyphenated, so x-bitcoin-discriminated-result would match.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread src/rpc/blockchain.cpp Resolved
@nervana21

Copy link
Copy Markdown
Contributor Author

Comparing getblock["params"][discriminator["parameterIndex"]]["name"] to discriminator["parameter"], and asserting the two object arms differ in prevout in the order values claims, fails on both and passes on this head.

Good catch, thanks! I've updated the tests according to your suggestion

@nervana21
nervana21 force-pushed the openrpc-discriminated-result branch from 270ecac to 213b2ed Compare September 7, 2026 22:49
@nervana21
nervana21 force-pushed the openrpc-discriminated-result branch from 213b2ed to 3b02b9b Compare September 16, 2026 23:23
@nervana21

Copy link
Copy Markdown
Contributor Author

Rebased off master

Comment thread src/rpc/server.cpp Resolved
OpenRPC documents `getblock` as a four-way `oneOf` on `verbosity`, but
nothing in the schema maps `verbosity` values onto those branches.

Add optional `RPCResultDiscriminator` on `RPCResults`, emit it as
`x-bitcoin-discriminated-result`, and stamp `getblock` so `verbosity`
selects the existing oneOf arms via {0, 1, 2, 3} in that order.

No runtime behavior change.
Parameter schemas may include `x-bitcoin-default-hint` when the default
cannot be expressed as a JSON value.

Multi-result schemas may include `x-bitcoin-discriminated-result`
naming the request parameter or parameters that select among the
top-level oneOf branches.
@nervana21
nervana21 force-pushed the openrpc-discriminated-result branch from 3b02b9b to 85f2993 Compare September 17, 2026 15:20
@nervana21

Copy link
Copy Markdown
Contributor Author

Updates:

@willcl-ark willcl-ark left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ACK 85f2993

RPCResultDiscriminator seems like a fair concept to use here.

I tried to think of other ways to implement, such as attaching each verbosity value directly to its RPCResult arm, which would remove the parallel ordering between values and oneOf, but it would be overkill for the one current use IMO.

@sedited
sedited requested a review from maflcko October 20, 2026 07:19
@maflcko

maflcko commented Oct 20, 2026

Copy link
Copy Markdown
Member

I tried to think of other ways to implement, such as attaching each verbosity value directly to its RPCResult arm, which would remove the parallel ordering between values and oneOf, but it would be overkill for the one current use IMO.

Same, I find it a bit ugly that we can't discriminate all results. The reason is due to legacy code such as:

src/rpc/blockchain.cpp:            RPCResult{"If the UTXO was not found", RPCResult::Type::NONE, "", ""},
src/rpc/blockchain.cpp:            RPCResult{"Otherwise", RPCResult::Type::OBJ, "", "", {

src/rpc/mining.cpp:            RPCResult{"If the proposal was accepted with mode=='proposal'", RPCResult::Type::NONE, "", ""},
src/rpc/mining.cpp:            RPCResult{"If the proposal was not accepted with mode=='proposal'", RPCResult::Type::STR, "", "According to BIP22"},

src/rpc/mining.cpp:            RPCResult{"If the block was accepted", RPCResult::Type::NONE, "", ""},
src/rpc/mining.cpp:            RPCResult{"Otherwise", RPCResult::Type::STR, "", "According to BIP22"},

Which leads to issues such as #18476.

I don't think this will be fixed any time soon, or ever, so maybe a dedicated RPCResult{Undiscriminated{"text"}, ... could be introduced. This way, the docs and the schema could be derived from the same ground truth.

I am asking because clients would either care to have all of them discriminated, or none? What is so special about getblock?

Also, while reviewing I found a likely unrelated bug:

src/rpc/mempool.cpp:            RPCResult{"mempool chunks",

"mempool chunks" is not a discriminator, but should be the description of the array?

@nervana21

Copy link
Copy Markdown
Contributor Author

I don't think this will be fixed any time soon, or ever, so maybe a dedicated RPCResult{Undiscriminated{"text"}, ... could be introduced. This way, the docs and the schema could be derived from the same ground truth.

This sounds reasonable. I can work on this once I get some more time, or happy to review a PR by someone else.

I am asking because clients would either care to have all of them discriminated, or none? What is so special about getblock?

I had planned to go one by one for the RPCs that are selected by a request parameter. getblock seems to be a more commonly used and complex RPC, so I figured it was the best one to start with.

Also, while reviewing I found a likely unrelated bug:

src/rpc/mempool.cpp:            RPCResult{"mempool chunks",

"mempool chunks" is not a discriminator, but should be the description of the array?

Good catch. I will open a PR for a fix, unless someone else does before me.

@fanquake

Copy link
Copy Markdown
Member

Good catch. I will open a PR for a fix, unless someone else does before me.

@nervana21 do you still plan on opening this?

@nervana21

Copy link
Copy Markdown
Contributor Author

@nervana21 do you still plan on opening this?

Yes, PR opened. Thanks for the reminder

@maflcko

maflcko commented Nov 30, 2026

Copy link
Copy Markdown
Member

I don't think this will be fixed any time soon, or ever, so maybe a dedicated RPCResult{Undiscriminated{"text"}, ... could be introduced. This way, the docs and the schema could be derived from the same ground truth.

This sounds reasonable. I can work on this once I get some more time, or happy to review a PR by someone else.

Maybe turn into draft for now?

@nervana21
nervana21 marked this pull request as draft November 30, 2026 14:36
@nervana21

Copy link
Copy Markdown
Contributor Author

Maybe turn into draft for now?

Done!

fanquake added a commit that referenced this pull request Nov 30, 2026
0b17fa0 rpc: fix getmempoolfeeratediagram result description (nervana21)

Pull request description:

  In #36175, maflcko [found](#36175 (comment)) that `"mempool chunks"` was incorrectly rendered as a result branch label.

  Fix it to annotate the array instead.

  <details>
  <summary><code>help getmempoolfeeratediagram</code> diff</summary>

  ```diff
  -Result (mempool chunks):
  -[                    (json array)
  +Result:
  +[                    (json array) mempool chunks
     {                  (json object)
       "weight" : n,    (numeric) cumulative sigops-adjusted weight
       "fee" : n        (numeric) cumulative fee
     },
     ...
   ]
  ```

  </details>

ACKs for top commit:
  maflcko:
    lgtm ACK 0b17fa0

Tree-SHA512: 7dba3fb9f9b493a9e5f6192456b83bd872e312f70aeb2cc06ee737e4fbb194480aa387391482ea7dda8b283c930873f043cfde7d9665944907af6c84e490d87e
BTC4Peace added a commit to BTC4Peace/bitcoin-agentic-workflow that referenced this pull request Nov 30, 2026
…iption

0b17fa0 rpc: fix getmempoolfeeratediagram result description (nervana21)

Pull request description:

  In bitcoin#36175, maflcko [found](bitcoin#36175 (comment)) that `"mempool chunks"` was incorrectly rendered as a result branch label.

  Fix it to annotate the array instead.

  <details>
  <summary><code>help getmempoolfeeratediagram</code> diff</summary>

  ```diff
  -Result (mempool chunks):
  -[                    (json array)
  +Result:
  +[                    (json array) mempool chunks
     {                  (json object)
       "weight" : n,    (numeric) cumulative sigops-adjusted weight
       "fee" : n        (numeric) cumulative fee
     },
     ...
   ]
  ```

  </details>

ACKs for top commit:
  maflcko:
    lgtm ACK 0b17fa0

Tree-SHA512: 7dba3fb9f9b493a9e5f6192456b83bd872e312f70aeb2cc06ee737e4fbb194480aa387391482ea7dda8b283c930873f043cfde7d9665944907af6c84e490d87e

Cherry-picked-from: bitcoin/bitcoin@1ea904d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants