Skip to content

Adds generate_block function to Client #189

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,16 @@ pub trait RpcApi: Sized {
self.call("generatetoaddress", &[block_num.into(), address.to_string().into()])
}

/// Mine a block with a set of ordered transactions immediately to a specified
/// address or descriptor (before the RPC call returns)
fn generate_block(
&self,
output: &Address,
transactions: &[bitcoin::Txid],
Copy link
Contributor

Choose a reason for hiding this comment

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

Note: As per linked documentation Bitcoin Core accepts both txids and raw transaction in hex here.

Not sure if it's worth going the extra mile to be able to pass raw transaction in hex here too.

Choose a reason for hiding this comment

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

One of the main use cases for the generateblock rpc is to perform a double spend of a tx that is in bitcoind's mempool. In that case a raw transaction is required since bitcoind won't be able to find the tx by txid.

) -> Result<json::GenerateBlockResult> {
self.call("generateblock", &[into_json(output)?, into_json(transactions)?])
}

/// Mine up to block_num blocks immediately (before the RPC call returns)
/// to an address in the wallet.
fn generate(&self, block_num: u64, maxtries: Option<u64>) -> Result<Vec<bitcoin::BlockHash>> {
Expand Down
6 changes: 6 additions & 0 deletions json/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1657,6 +1657,12 @@ pub struct Utxo {
pub height: u64,
}

#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Debug)]
#[serde(rename_all = "camelCase")]
pub struct GenerateBlockResult {
hash: bitcoin::BlockHash,
}

impl<'a> serde::Serialize for PubKeyOrAddress<'a> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
Expand Down