Skip to content
This repository has been archived by the owner on Nov 26, 2024. It is now read-only.

Commit

Permalink
Implement JSONRPC method generate
Browse files Browse the repository at this point in the history
Implement the last JSONRPC  method from the `Generating` section of the
docs.
  • Loading branch information
tcharding committed Sep 6, 2024
1 parent 9d71d32 commit 7c0cf3a
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 5 deletions.
12 changes: 12 additions & 0 deletions client/src/client_sync/v17/generating.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,15 @@ macro_rules! impl_client_v17__generatetoaddress {
}
};
}

/// Implements bitcoind JSON-RPC API method `generate`
#[macro_export]
macro_rules! impl_client_v17__generate {
() => {
impl Client {
pub fn generate(&self, nblocks: usize) -> Result<Generate> {
self.call("generate", &[nblocks.into()])
}
}
};
}
1 change: 1 addition & 0 deletions client/src/client_sync/v17/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ crate::impl_client_v17__stop!();

// == Generating ==
crate::impl_client_v17__generatetoaddress!();
crate::impl_client_v17__generate!();

// == Network ==
crate::impl_client_v17__getnetworkinfo!();
Expand Down
21 changes: 19 additions & 2 deletions integration_test/src/v17/generating.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,32 @@
//! Specifically this is methods found under the `== Generating ==` section of the
//! API docs of `bitcoind v0.17.1`.
/// Requires `Client` to be in scope and to implement `get_blockchain_info`.
/// Requires `Client` to be in scope and to implement `generate_to_address`.
#[macro_export]
macro_rules! impl_test_v17__generatetoaddress {
() => {
#[test]
fn generate_to_address() {
const NBLOCKS: usize = 1;

let bitcoind = $crate::bitcoind_with_default_wallet();
let address = bitcoind.client.new_address().expect("failed to get new address");
let json = bitcoind.client.generate_to_address(1, &address).expect("generatetoaddress");
let json = bitcoind.client.generate_to_address(NBLOCKS, &address).expect("generatetoaddress");
json.into_model().unwrap();
}
};
}

/// Requires `Client` to be in scope and to implement `generate`.
#[macro_export]
macro_rules! impl_test_v17__generate {
() => {
#[test]
fn generate() {
const NBLOCKS: usize = 100;

let bitcoind = $crate::bitcoind_with_default_wallet();
let json = bitcoind.client.generate(NBLOCKS).expect("generate");
json.into_model().unwrap();
}
};
Expand Down
1 change: 1 addition & 0 deletions integration_test/tests/v17_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ mod generating {
use super::*;

impl_test_v17__generatetoaddress!();
impl_test_v17__generate!();
}

// == Network ==
Expand Down
12 changes: 12 additions & 0 deletions json/src/model/generating.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,15 @@ impl GenerateToAddress {
/// Returns true if 0 blocks were generated.
pub fn is_empty(&self) -> bool { self.0.is_empty() }
}

/// Models the result of JSON-RPC method `generate`.
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct Generate(pub Vec<BlockHash>);

impl Generate {
/// Returns the number of blocks generated.
pub fn len(&self) -> usize { self.0.len() }

/// Returns true if 0 blocks were generated.
pub fn is_empty(&self) -> bool { self.0.is_empty() }
}
2 changes: 1 addition & 1 deletion json/src/model/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub use self::{
GetBlockVerbosityOne, GetBlockVerbosityZero, GetBlockchainInfo, GetTxOut, Softfork,
SoftforkType,
},
generating::GenerateToAddress,
generating::{Generate, GenerateToAddress},
network::{GetNetworkInfo, GetNetworkInfoAddress, GetNetworkInfoNetwork},
raw_transactions::SendRawTransaction,
wallet::{
Expand Down
21 changes: 21 additions & 0 deletions json/src/v17/generating.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use serde::{Deserialize, Serialize};
use crate::model;

/// Result of JSON-RPC method `generatetoaddress`.
///
/// > generatetoaddress nblocks "address" ( maxtries )
/// >
/// > Mine blocks immediately to a specified address (before the RPC call returns)
Expand All @@ -31,3 +32,23 @@ impl GenerateToAddress {
Ok(model::GenerateToAddress(v))
}
}

/// Result of JSON-RPC method `generate`.
///
/// > generate nblocks ( maxtries )
/// >
/// > Mine up to nblocks blocks immediately (before the RPC call returns) to an address in the wallet.
/// >
/// > Arguments:
/// > 1. nblocks (numeric, required) How many blocks are generated immediately.
/// > 2. maxtries (numeric, optional) How many iterations to try (default = 1000000).
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct Generate(Vec<String>);

impl Generate {
/// Converts version specific type to a version in-specific, more strongly typed type.
pub fn into_model(self) -> Result<model::Generate, hex::HexToArrayError> {
let v = self.0.iter().map(|s| s.parse::<BlockHash>()).collect::<Result<Vec<_>, _>>()?;
Ok(model::Generate(v))
}
}
4 changes: 2 additions & 2 deletions json/src/v17/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
//! - [ ] `uptime`
//!
//! **== Generating ==**
//! - [ ] `generate nblocks ( maxtries )`
//! - [x] `generate nblocks ( maxtries )`
//! - [x] `generatetoaddress nblocks address (maxtries)`
//!
//! **== Mining ==**
Expand Down Expand Up @@ -169,7 +169,7 @@ pub use self::{
Bip9Softfork, Bip9SoftforkStatus, GetBestBlockHash, GetBlockVerbosityOne,
GetBlockVerbosityZero, GetBlockchainInfo, GetTxOut, ScriptPubkey, Softfork, SoftforkReject,
},
generating::GenerateToAddress,
generating::{Generate, GenerateToAddress},
network::{GetNetworkInfo, GetNetworkInfoAddress, GetNetworkInfoNetwork},
raw_transactions::SendRawTransaction,
wallet::{
Expand Down

0 comments on commit 7c0cf3a

Please sign in to comment.