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

add: support for getblocktemplate in template mode #163

Merged
merged 1 commit into from
May 3, 2021

Conversation

0xB10C
Copy link
Contributor

@0xB10C 0xB10C commented Feb 12, 2021

Partly addresses #133

This adds basic support for the getblocktemplate RCP to the client. The default 'template' mode, in which Bitcoin Core builds and returns a block template to the RPC client, is implemented. The 'proposal' mode, where the client submits a block to Bitcoin Core for validation (before it's mined; proof-of-work is not checked) as well as longpolling for changes to a given block template are not implemented yet. However, the basic implementation proposed here can be extended to support both of these features without changing the API.

I've focused on the subset of features (also called "capabilities") that Bitcoin Core actually implements from BIP 22 and BIP 23. I've tested this against mainnet, testnet, regtest and signet with the Rust code provided below.

Reviewers might find the following resources useful:

Code for playing around with this
use bitcoincore_rpc::json::{GetBlockTemplateOptionsModes, GetBlockTemplateOptionsRules};
use bitcoincore_rpc::{Auth, Client, RpcApi};

fn main() {
    // mainnet rpc port: 8332
    // testnet rpc port: 18332
    // regtest rpc port: 18444
    // signet rpc port: 38332
    let rpc = Client::new(
        "http://127.0.0.1:8332".to_string(),
        Auth::UserPass("your user".to_string(), "your pass".to_string()),
    )
    .unwrap();

    let get_block_template = rpc
        .get_block_template(
            GetBlockTemplateOptionsModes::Template,
            vec![
                GetBlockTemplateOptionsRules::SegWit,
                // GetBlockTemplateOptionsRules::Signet, for signet
            ],
            vec![],
        )
        .unwrap();

    println!("template: {:#?}", get_block_template);
}

@0xB10C
Copy link
Contributor Author

0xB10C commented Feb 12, 2021

The one CI job failing seems to be unrelated. Happens on master as well.

@0xB10C 0xB10C force-pushed the 2021-02-getblocktemplate branch from 0803d7f to edcb7e3 Compare March 3, 2021 19:29
Copy link
Collaborator

@stevenroose stevenroose left a comment

Choose a reason for hiding this comment

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

Left some comments, looks quite good!

@stevenroose
Copy link
Collaborator

Could you also rebase to make CI happy?

@0xB10C 0xB10C force-pushed the 2021-02-getblocktemplate branch 2 times, most recently from c0e1ddb to 9c2ac81 Compare March 15, 2021 10:00
@0xB10C
Copy link
Contributor Author

0xB10C commented Mar 15, 2021

Thanks! Addressed your review and rebased.

Copy link
Collaborator

@stevenroose stevenroose left a comment

Choose a reason for hiding this comment

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

LGTM

@stevenroose
Copy link
Collaborator

Perhaps @shesek or @sgeisler could review too. One small nit that could be improved would be changing the Vec function arguments with &[ ] slices.

@shesek
Copy link
Contributor

shesek commented Mar 16, 2021

The PR looks great! Thanks for including doc comments for all the fields, the resource links in the PR's description and the example code.

My only nit is what @stevenroose already brought up regarding slice arguments.

@0xB10C
Copy link
Contributor Author

0xB10C commented Mar 20, 2021

Pushed f36e4e5 which has slice arguments for the get_block_template function. Wasn't to sure about to_vec(), but happy to squash if that's what you meant.

edit: forgot to adapt the tests in previously pushed commit

@0xB10C 0xB10C force-pushed the 2021-02-getblocktemplate branch from e8bfd72 to f36e4e5 Compare March 20, 2021 12:54
Copy link
Collaborator

@stevenroose stevenroose left a comment

Choose a reason for hiding this comment

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

One more nit :)

@0xB10C 0xB10C force-pushed the 2021-02-getblocktemplate branch 2 times, most recently from c63c258 to a2d6bc1 Compare April 30, 2021 16:24
Proposal mode and longpolling on the template mode are not yet
included.
@0xB10C 0xB10C force-pushed the 2021-02-getblocktemplate branch from a2d6bc1 to 3106381 Compare April 30, 2021 16:31
@0xB10C
Copy link
Contributor Author

0xB10C commented Apr 30, 2021

One more nit :)

Done!

@stevenroose
Copy link
Collaborator

ACK, let's await CI

@0xB10C
Copy link
Contributor Author

0xB10C commented Apr 30, 2021

The failing job seems to be unrelated

Copy link
Collaborator

@stevenroose stevenroose left a comment

Choose a reason for hiding this comment

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

thanks!

@stevenroose stevenroose merged commit 1520b83 into rust-bitcoin:master May 3, 2021
@0xB10C 0xB10C deleted the 2021-02-getblocktemplate branch May 3, 2021 09:36
0xB10C added a commit to 0xB10C/rust-bitcoincore-rpc that referenced this pull request May 4, 2021
PR rust-bitcoin#163 implemented support for getblocktemplate. However, during the
implementation the version_bits_available HashMap the map fields were
switched.

This became apparent during testing against a v0.21.1 Bitcoin
Core node, which includes support for the Taproot softfork via
the 2nd version bit. Previously, no softfork was defined.

```
$ bitcoin-cli getblocktemplate "{\"rules\": [\"segwit\"]}"
...
  "vbavailable": {
    "taproot": 2
  },
...

```
apoelstra added a commit that referenced this pull request Nov 29, 2022
1f5c57a fix: version_bits_available maps String to u32 (0xb10c)

Pull request description:

  PR #163 implemented support for `getblocktemplate`. However, during the implementation the `version_bits_available` HashMap the map fields were switched.

  This became apparent during testing against a v0.21.1 Bitcoin Core node, which includes support for the Taproot softfork via the 2nd version bit. Previously, no softfork was defined.

  ```
  $ bitcoin-cli getblocktemplate "{\"rules\": [\"segwit\"]}"
  ...
    "vbavailable": {
      "taproot": 2
    },
  ...
  ```

ACKs for top commit:
  sgeisler:
    utACK 1f5c57a
  tcharding:
    utACK 1f5c57a

Tree-SHA512: ebae449b93ca8d428e8c0e6dcbeca20765966500e3582368b1a6dc3a60e3205c507121b06a22b3e0e4091efc4828cc17dd65049afebf09695d9a5abc50b1dc5c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants