diff --git a/client/src/client_sync/v17/generating.rs b/client/src/client_sync/v17/generating.rs index b941602..7f38465 100644 --- a/client/src/client_sync/v17/generating.rs +++ b/client/src/client_sync/v17/generating.rs @@ -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 { + self.call("generate", &[nblocks.into()]) + } + } + }; +} diff --git a/client/src/client_sync/v17/mod.rs b/client/src/client_sync/v17/mod.rs index 8d8438c..acb1ee6 100644 --- a/client/src/client_sync/v17/mod.rs +++ b/client/src/client_sync/v17/mod.rs @@ -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!(); diff --git a/integration_test/src/v17/generating.rs b/integration_test/src/v17/generating.rs index 06848a3..e04afce 100644 --- a/integration_test/src/v17/generating.rs +++ b/integration_test/src/v17/generating.rs @@ -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(); } }; diff --git a/integration_test/tests/v17_api.rs b/integration_test/tests/v17_api.rs index 2ee2106..9d659d7 100644 --- a/integration_test/tests/v17_api.rs +++ b/integration_test/tests/v17_api.rs @@ -26,6 +26,7 @@ mod generating { use super::*; impl_test_v17__generatetoaddress!(); + impl_test_v17__generate!(); } // == Network == diff --git a/json/src/model/generating.rs b/json/src/model/generating.rs index 724b690..501d356 100644 --- a/json/src/model/generating.rs +++ b/json/src/model/generating.rs @@ -8,6 +8,18 @@ use bitcoin::BlockHash; use serde::{Deserialize, Serialize}; +/// Models the result of JSON-RPC method `generate`. +#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] +pub struct Generate(pub Vec); + +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() } +} + /// Models the result of JSON-RPC method `generatetoaddress`. #[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] pub struct GenerateToAddress(pub Vec); diff --git a/json/src/model/mod.rs b/json/src/model/mod.rs index f724434..13bfd12 100644 --- a/json/src/model/mod.rs +++ b/json/src/model/mod.rs @@ -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::{ diff --git a/json/src/v17/generating.rs b/json/src/v17/generating.rs index 28c24f9..4713bde 100644 --- a/json/src/v17/generating.rs +++ b/json/src/v17/generating.rs @@ -9,7 +9,28 @@ use serde::{Deserialize, Serialize}; use crate::model; +/// 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); + +impl Generate { + /// Converts version specific type to a version in-specific, more strongly typed type. + pub fn into_model(self) -> Result { + let v = self.0.iter().map(|s| s.parse::()).collect::, _>>()?; + Ok(model::Generate(v)) + } +} + /// Result of JSON-RPC method `generatetoaddress`. +/// /// > generatetoaddress nblocks "address" ( maxtries ) /// > /// > Mine blocks immediately to a specified address (before the RPC call returns) diff --git a/json/src/v17/mod.rs b/json/src/v17/mod.rs index 0f600cf..c819bb1 100644 --- a/json/src/v17/mod.rs +++ b/json/src/v17/mod.rs @@ -41,7 +41,7 @@ //! - [ ] `uptime` //! //! **== Generating ==** -//! - [ ] `generate nblocks ( maxtries )` +//! - [x] `generate nblocks ( maxtries )` //! - [x] `generatetoaddress nblocks address (maxtries)` //! //! **== Mining ==** @@ -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::{