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

Commit 2eea858

Browse files
committed
v17: Implement JSONRPC method generate
Implement the last JSONRPC method from the `Generating` section of the docs.
1 parent 9d71d32 commit 2eea858

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
@@ -19,3 +19,15 @@ impl GenerateToAddress {
1919
/// Returns true if 0 blocks were generated.
2020
pub fn is_empty(&self) -> bool { self.0.is_empty() }
2121
}
22+
23+
/// Models the result of JSON-RPC method `generate`.
24+
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
25+
pub struct Generate(pub Vec<BlockHash>);
26+
27+
impl Generate {
28+
/// Returns the number of blocks generated.
29+
pub fn len(&self) -> usize { self.0.len() }
30+
31+
/// Returns true if 0 blocks were generated.
32+
pub fn is_empty(&self) -> bool { self.0.is_empty() }
33+
}

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
@@ -10,6 +10,7 @@ use serde::{Deserialize, Serialize};
1010
use crate::model;
1111

1212
/// Result of JSON-RPC method `generatetoaddress`.
13+
///
1314
/// > generatetoaddress nblocks "address" ( maxtries )
1415
/// >
1516
/// > Mine blocks immediately to a specified address (before the RPC call returns)
@@ -31,3 +32,23 @@ impl GenerateToAddress {
3132
Ok(model::GenerateToAddress(v))
3233
}
3334
}
35+
36+
/// Result of JSON-RPC method `generate`.
37+
///
38+
/// > generate nblocks ( maxtries )
39+
/// >
40+
/// > Mine up to nblocks blocks immediately (before the RPC call returns) to an address in the wallet.
41+
/// >
42+
/// > Arguments:
43+
/// > 1. nblocks (numeric, required) How many blocks are generated immediately.
44+
/// > 2. maxtries (numeric, optional) How many iterations to try (default = 1000000).
45+
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
46+
pub struct Generate(Vec<String>);
47+
48+
impl Generate {
49+
/// Converts version specific type to a version in-specific, more strongly typed type.
50+
pub fn into_model(self) -> Result<model::Generate, hex::HexToArrayError> {
51+
let v = self.0.iter().map(|s| s.parse::<BlockHash>()).collect::<Result<Vec<_>, _>>()?;
52+
Ok(model::Generate(v))
53+
}
54+
}

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)