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

Commit 5ad70e6

Browse files
authored
v17: Implement JSONRPC method generate (#34)
Implement the last JSONRPC method from the `Generating` section of the docs.
2 parents 249bb01 + 279e0f9 commit 5ad70e6

File tree

8 files changed

+69
-5
lines changed

8 files changed

+69
-5
lines changed

client/src/client_sync/v17/generating.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,15 @@ macro_rules! impl_client_v17__generatetoaddress {
2424
}
2525
};
2626
}
27+
28+
/// Implements bitcoind JSON-RPC API method `generate`
29+
#[macro_export]
30+
macro_rules! impl_client_v17__generate {
31+
() => {
32+
impl Client {
33+
pub fn generate(&self, nblocks: usize) -> Result<Generate> {
34+
self.call("generate", &[nblocks.into()])
35+
}
36+
}
37+
};
38+
}

client/src/client_sync/v17/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ crate::impl_client_v17__stop!();
3131

3232
// == Generating ==
3333
crate::impl_client_v17__generatetoaddress!();
34+
crate::impl_client_v17__generate!();
3435

3536
// == Network ==
3637
crate::impl_client_v17__getnetworkinfo!();

integration_test/src/v17/generating.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,32 @@
55
//! Specifically this is methods found under the `== Generating ==` section of the
66
//! API docs of `bitcoind v0.17.1`.
77
8-
/// Requires `Client` to be in scope and to implement `get_blockchain_info`.
8+
/// Requires `Client` to be in scope and to implement `generate_to_address`.
99
#[macro_export]
1010
macro_rules! impl_test_v17__generatetoaddress {
1111
() => {
1212
#[test]
1313
fn generate_to_address() {
14+
const NBLOCKS: usize = 1;
15+
1416
let bitcoind = $crate::bitcoind_with_default_wallet();
1517
let address = bitcoind.client.new_address().expect("failed to get new address");
16-
let json = bitcoind.client.generate_to_address(1, &address).expect("generatetoaddress");
18+
let json = bitcoind.client.generate_to_address(NBLOCKS, &address).expect("generatetoaddress");
19+
json.into_model().unwrap();
20+
}
21+
};
22+
}
23+
24+
/// Requires `Client` to be in scope and to implement `generate`.
25+
#[macro_export]
26+
macro_rules! impl_test_v17__generate {
27+
() => {
28+
#[test]
29+
fn generate() {
30+
const NBLOCKS: usize = 100;
31+
32+
let bitcoind = $crate::bitcoind_with_default_wallet();
33+
let json = bitcoind.client.generate(NBLOCKS).expect("generate");
1734
json.into_model().unwrap();
1835
}
1936
};

integration_test/tests/v17_api.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ mod generating {
2626
use super::*;
2727

2828
impl_test_v17__generatetoaddress!();
29+
impl_test_v17__generate!();
2930
}
3031

3132
// == Network ==

json/src/model/generating.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@
88
use bitcoin::BlockHash;
99
use serde::{Deserialize, Serialize};
1010

11+
/// Models the result of JSON-RPC method `generate`.
12+
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
13+
pub struct Generate(pub Vec<BlockHash>);
14+
15+
impl Generate {
16+
/// Returns the number of blocks generated.
17+
pub fn len(&self) -> usize { self.0.len() }
18+
19+
/// Returns true if 0 blocks were generated.
20+
pub fn is_empty(&self) -> bool { self.0.is_empty() }
21+
}
22+
1123
/// Models the result of JSON-RPC method `generatetoaddress`.
1224
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
1325
pub struct GenerateToAddress(pub Vec<BlockHash>);

json/src/model/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub use self::{
3030
GetBlockVerbosityOne, GetBlockVerbosityZero, GetBlockchainInfo, GetTxOut, Softfork,
3131
SoftforkType,
3232
},
33-
generating::GenerateToAddress,
33+
generating::{Generate, GenerateToAddress},
3434
network::{GetNetworkInfo, GetNetworkInfoAddress, GetNetworkInfoNetwork},
3535
raw_transactions::SendRawTransaction,
3636
wallet::{

json/src/v17/generating.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,28 @@ use serde::{Deserialize, Serialize};
99

1010
use crate::model;
1111

12+
/// Result of JSON-RPC method `generate`.
13+
///
14+
/// > generate nblocks ( maxtries )
15+
/// >
16+
/// > Mine up to nblocks blocks immediately (before the RPC call returns) to an address in the wallet.
17+
/// >
18+
/// > Arguments:
19+
/// > 1. nblocks (numeric, required) How many blocks are generated immediately.
20+
/// > 2. maxtries (numeric, optional) How many iterations to try (default = 1000000).
21+
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
22+
pub struct Generate(Vec<String>);
23+
24+
impl Generate {
25+
/// Converts version specific type to a version in-specific, more strongly typed type.
26+
pub fn into_model(self) -> Result<model::Generate, hex::HexToArrayError> {
27+
let v = self.0.iter().map(|s| s.parse::<BlockHash>()).collect::<Result<Vec<_>, _>>()?;
28+
Ok(model::Generate(v))
29+
}
30+
}
31+
1232
/// Result of JSON-RPC method `generatetoaddress`.
33+
///
1334
/// > generatetoaddress nblocks "address" ( maxtries )
1435
/// >
1536
/// > Mine blocks immediately to a specified address (before the RPC call returns)

json/src/v17/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
//! - [ ] `uptime`
4242
//!
4343
//! **== Generating ==**
44-
//! - [ ] `generate nblocks ( maxtries )`
44+
//! - [x] `generate nblocks ( maxtries )`
4545
//! - [x] `generatetoaddress nblocks address (maxtries)`
4646
//!
4747
//! **== Mining ==**
@@ -169,7 +169,7 @@ pub use self::{
169169
Bip9Softfork, Bip9SoftforkStatus, GetBestBlockHash, GetBlockVerbosityOne,
170170
GetBlockVerbosityZero, GetBlockchainInfo, GetTxOut, ScriptPubkey, Softfork, SoftforkReject,
171171
},
172-
generating::GenerateToAddress,
172+
generating::{Generate, GenerateToAddress},
173173
network::{GetNetworkInfo, GetNetworkInfoAddress, GetNetworkInfoNetwork},
174174
raw_transactions::SendRawTransaction,
175175
wallet::{

0 commit comments

Comments
 (0)