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

Commit e024d1b

Browse files
authored
v17: Add support for control methods (#36)
Add support for all the JSONRPC methods under the "control" section of the docs. Note, I'm unsure about the map returned by `getmemoryinfo` in default mode, integration tests show that the "locked" key is parsed correctly.
2 parents c923124 + 6e637e7 commit e024d1b

File tree

6 files changed

+164
-4
lines changed

6 files changed

+164
-4
lines changed

client/src/client_sync/v17/control.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,28 @@
99
//!
1010
//! See or use the `define_jsonrpc_minreq_client!` macro to define a `Client`.
1111
12+
/// Implements bitcoind JSON-RPC API method `getmemory`
13+
#[macro_export]
14+
macro_rules! impl_client_v17__getmemoryinfo {
15+
() => {
16+
impl Client {
17+
pub fn get_memory_info(&self) -> Result<GetMemoryInfoStats> {
18+
self.call("getmemoryinfo", &[])
19+
}
20+
}
21+
};
22+
}
23+
24+
/// Implements bitcoind JSON-RPC API method `logging`
25+
#[macro_export]
26+
macro_rules! impl_client_v17__logging {
27+
() => {
28+
impl Client {
29+
pub fn logging(&self) -> Result<Logging> { self.call("logging", &[]) }
30+
}
31+
};
32+
}
33+
1234
/// Implements bitcoind JSON-RPC API method `stop`
1335
#[macro_export]
1436
macro_rules! impl_client_v17__stop {
@@ -18,3 +40,13 @@ macro_rules! impl_client_v17__stop {
1840
}
1941
};
2042
}
43+
44+
/// Implements bitcoind JSON-RPC API method `uptime`
45+
#[macro_export]
46+
macro_rules! impl_client_v17__uptime {
47+
() => {
48+
impl Client {
49+
pub fn uptime(&self) -> Result<u32> { self.call("uptime", &[]) }
50+
}
51+
};
52+
}

client/src/client_sync/v17/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ crate::impl_client_v17__getblock!();
2727
crate::impl_client_v17__gettxout!();
2828

2929
// == Control ==
30+
crate::impl_client_v17__getmemoryinfo!();
31+
crate::impl_client_v17__logging!();
3032
crate::impl_client_v17__stop!();
33+
crate::impl_client_v17__uptime!();
3134

3235
// == Generating ==
3336
crate::impl_client_v17__generatetoaddress!();

integration_test/src/v17/control.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,32 @@
55
//! Specifically this is methods found under the `== Control ==` section of the
66
//! API docs of `bitcoind v0.17.1`.
77
8+
/// Requires `Client` to be in scope and to implement `getmemoryinfo`.
9+
#[macro_export]
10+
macro_rules! impl_test_v17__getmemoryinfo {
11+
() => {
12+
#[test]
13+
fn get_memory_info() {
14+
let bitcoind = $crate::bitcoind_no_wallet();
15+
// There is no model for `getmemoryinfo`, just check we can make the call.
16+
let _ = bitcoind.client.get_memory_info().expect("getmemoryinfo");
17+
}
18+
};
19+
}
20+
21+
/// Requires `Client` to be in scope and to implement `logging`.
22+
#[macro_export]
23+
macro_rules! impl_test_v17__logging {
24+
() => {
25+
#[test]
26+
fn logging() {
27+
let bitcoind = $crate::bitcoind_no_wallet();
28+
// There is no model for `logging`, just check we can make the call.
29+
let _ = bitcoind.client.logging().expect("logging");
30+
}
31+
};
32+
}
33+
834
/// Requires `Client` to be in scope and to implement `stop`.
935
#[macro_export]
1036
macro_rules! impl_test_v17__stop {
@@ -17,3 +43,16 @@ macro_rules! impl_test_v17__stop {
1743
}
1844
};
1945
}
46+
47+
/// Requires `Client` to be in scope and to implement `uptime`.
48+
#[macro_export]
49+
macro_rules! impl_test_v17__uptime {
50+
() => {
51+
#[test]
52+
fn uptime() {
53+
let bitcoind = $crate::bitcoind_no_wallet();
54+
// There is no json object for `stop`, we just return a int.
55+
let _ = bitcoind.client.uptime().expect("uptime");
56+
}
57+
};
58+
}

integration_test/tests/v17_api.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ mod blockchain {
1818
mod control {
1919
use super::*;
2020

21+
impl_test_v17__getmemoryinfo!();
22+
impl_test_v17__logging!();
2123
impl_test_v17__stop!();
24+
impl_test_v17__uptime!();
2225
}
2326

2427
// == Generating ==

json/src/v17/control.rs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,85 @@
33
//! The JSON-RPC API for Bitcoin Core v0.17.1 - control.
44
//!
55
//! Types for methods found under the `== Control ==` section of the API docs.
6+
7+
use std::collections::HashMap;
8+
9+
use serde::{Deserialize, Serialize};
10+
11+
/// Result of JSON-RPC method `getmemoryinfo`.
12+
///
13+
/// We only support the default "stats" mode.
14+
///
15+
/// > getmemoryinfo ("mode")
16+
///
17+
/// > Returns an object containing information about memory usage.
18+
///
19+
/// > Arguments:
20+
/// > 1. "mode" determines what kind of information is returned. This argument is optional, the default mode is "stats".
21+
/// > - "stats" returns general statistics about memory usage in the daemon.
22+
/// > - "mallocinfo" returns an XML string describing low-level heap state (only available if compiled with glibc 2.10+).
23+
// This just mimics the map returned by my instance of Core v0.17.1, I don't know how
24+
// to handle other map values or if they exist?
25+
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
26+
pub struct GetMemoryInfoStats(pub HashMap<String, Locked>);
27+
28+
/// Information about locked memory manager.
29+
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
30+
pub struct Locked {
31+
/// Number of bytes used.
32+
pub used: u64,
33+
/// Number of bytes available in current arenas.
34+
pub free: u64,
35+
/// Total number of bytes managed.
36+
pub total: u64,
37+
/// Amount of bytes that succeeded locking.
38+
///
39+
/// If this number is smaller than total, locking pages failed at some point and key data could
40+
/// be swapped to disk.
41+
pub locked: u64,
42+
/// Number allocated chunks.
43+
pub chunks_used: u64,
44+
/// Number unused chunks.
45+
pub chunks_free: u64,
46+
}
47+
48+
/// Result of JSON-RPC method `logging`.
49+
///
50+
/// > logging ( `<include>` `<exclude>` )
51+
///
52+
/// > Gets and sets the logging configuration.
53+
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
54+
pub struct Logging {
55+
pub net: bool,
56+
pub tor: bool,
57+
pub mempool: bool,
58+
pub http: bool,
59+
pub bench: bool,
60+
pub zmq: bool,
61+
pub db: bool,
62+
pub rpc: bool,
63+
pub estimatefee: bool,
64+
pub addrman: bool,
65+
pub selectcoins: bool,
66+
pub reindex: bool,
67+
pub cmpctblock: bool,
68+
pub rand: bool,
69+
pub prune: bool,
70+
pub proxy: bool,
71+
pub mempoolrej: bool,
72+
pub libevent: bool,
73+
pub coindb: bool,
74+
pub qt: bool,
75+
pub leveldb: bool,
76+
}
77+
78+
/// Result of JSON-RPC method `uptime`.
79+
///
80+
/// > uptime
81+
/// >
82+
/// > Returns the total uptime of the server.
83+
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
84+
pub struct Uptime {
85+
/// The number of seconds that the server has been running.
86+
ttt: u32,
87+
}

json/src/v17/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@
3434
//! - [ ] `verifytxoutproof "proof"`
3535
//!
3636
//! **== Control ==**
37-
//! - [ ] `getmemoryinfo ("mode")`
38-
//! - [ ] `help ( "command" )`
39-
//! - [ ] `logging ( <include> <exclude> )`
37+
//! - [x] `getmemoryinfo ("mode")`
38+
//! - [-] `help ( "command" )`
39+
//! - [x] `logging ( <include> <exclude> )`
4040
//! - [x] `stop`
41-
//! - [ ] `uptime`
41+
//! - [x] `uptime`
4242
//!
4343
//! **== Generating ==**
4444
//! - [x] `generate nblocks ( maxtries )`
@@ -169,6 +169,7 @@ pub use self::{
169169
Bip9Softfork, Bip9SoftforkStatus, GetBestBlockHash, GetBlockVerbosityOne,
170170
GetBlockVerbosityZero, GetBlockchainInfo, GetTxOut, ScriptPubkey, Softfork, SoftforkReject,
171171
},
172+
control::{GetMemoryInfoStats, Locked, Logging, Uptime},
172173
generating::{Generate, GenerateToAddress},
173174
network::{GetNetworkInfo, GetNetworkInfoAddress, GetNetworkInfoNetwork},
174175
raw_transactions::SendRawTransaction,

0 commit comments

Comments
 (0)