Skip to content

Commit 6ffbf1d

Browse files
committed
Add getblockfrompeer macro and test
The types table said version + model but an empty json object is returned. Update the table to `returns nothing`. Add a client macro that returns `Ok` if an empty json object is returned. Add a test where the RPC succeeds and returns an empty object.
1 parent 24607b7 commit 6ffbf1d

File tree

16 files changed

+61
-7
lines changed

16 files changed

+61
-7
lines changed

client/src/client_sync/v23/blockchain.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,22 @@
99
//!
1010
//! See or use the `define_jsonrpc_minreq_client!` macro to define a `Client`.
1111
12+
/// Implements Bitcoin Core JSON-RPC API method `getblockfrompeer`
13+
#[macro_export]
14+
macro_rules! impl_client_v23__get_block_from_peer {
15+
() => {
16+
impl Client {
17+
pub fn get_block_from_peer(&self, blockhash: BlockHash, peer_id: u32) -> Result<()> {
18+
match self.call("getblockfrompeer", &[into_json(blockhash)?, into_json(peer_id)?]) {
19+
Ok(serde_json::Value::Object(ref map)) if map.is_empty() => Ok(()),
20+
Ok(res) => Err(Error::Returned(res.to_string())),
21+
Err(err) => Err(err.into()),
22+
}
23+
}
24+
}
25+
};
26+
}
27+
1228
/// Implements Bitcoin Core JSON-RPC API method `savemempool`
1329
#[macro_export]
1430
macro_rules! impl_client_v23__save_mempool {

client/src/client_sync/v23/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ crate::impl_client_v17__get_block!();
3535
crate::impl_client_v17__get_blockchain_info!();
3636
crate::impl_client_v17__get_block_count!();
3737
crate::impl_client_v19__get_block_filter!();
38+
crate::impl_client_v23__get_block_from_peer!();
3839
crate::impl_client_v17__get_block_hash!();
3940
crate::impl_client_v17__get_block_header!();
4041
crate::impl_client_v17__get_block_stats!();

client/src/client_sync/v24/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ crate::impl_client_v17__get_block!();
3232
crate::impl_client_v17__get_blockchain_info!();
3333
crate::impl_client_v17__get_block_count!();
3434
crate::impl_client_v19__get_block_filter!();
35+
crate::impl_client_v23__get_block_from_peer!();
3536
crate::impl_client_v17__get_block_hash!();
3637
crate::impl_client_v17__get_block_header!();
3738
crate::impl_client_v17__get_block_stats!();

client/src/client_sync/v25/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ crate::impl_client_v17__get_block!();
3434
crate::impl_client_v17__get_blockchain_info!();
3535
crate::impl_client_v17__get_block_count!();
3636
crate::impl_client_v19__get_block_filter!();
37+
crate::impl_client_v23__get_block_from_peer!();
3738
crate::impl_client_v17__get_block_hash!();
3839
crate::impl_client_v17__get_block_header!();
3940
crate::impl_client_v17__get_block_stats!();

client/src/client_sync/v26/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ crate::impl_client_v17__get_block!();
3636
crate::impl_client_v17__get_blockchain_info!();
3737
crate::impl_client_v17__get_block_count!();
3838
crate::impl_client_v19__get_block_filter!();
39+
crate::impl_client_v23__get_block_from_peer!();
3940
crate::impl_client_v17__get_block_hash!();
4041
crate::impl_client_v17__get_block_header!();
4142
crate::impl_client_v17__get_block_stats!();

client/src/client_sync/v27/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ crate::impl_client_v17__get_block!();
3232
crate::impl_client_v17__get_blockchain_info!();
3333
crate::impl_client_v17__get_block_count!();
3434
crate::impl_client_v19__get_block_filter!();
35+
crate::impl_client_v23__get_block_from_peer!();
3536
crate::impl_client_v17__get_block_hash!();
3637
crate::impl_client_v17__get_block_header!();
3738
crate::impl_client_v17__get_block_stats!();

client/src/client_sync/v28/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ crate::impl_client_v17__get_block!();
3434
crate::impl_client_v17__get_blockchain_info!();
3535
crate::impl_client_v17__get_block_count!();
3636
crate::impl_client_v19__get_block_filter!();
37+
crate::impl_client_v23__get_block_from_peer!();
3738
crate::impl_client_v17__get_block_hash!();
3839
crate::impl_client_v17__get_block_header!();
3940
crate::impl_client_v17__get_block_stats!();

client/src/client_sync/v29/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ crate::impl_client_v17__get_block!();
3333
crate::impl_client_v17__get_blockchain_info!();
3434
crate::impl_client_v17__get_block_count!();
3535
crate::impl_client_v19__get_block_filter!();
36+
crate::impl_client_v23__get_block_from_peer!();
3637
crate::impl_client_v17__get_block_hash!();
3738
crate::impl_client_v17__get_block_header!();
3839
crate::impl_client_v17__get_block_stats!();

integration_test/tests/blockchain.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,37 @@ fn blockchain__get_block_filter__modelled() {
6565
model.unwrap();
6666
}
6767

68+
#[test]
69+
#[cfg(not(feature = "v22_and_below"))]
70+
fn blockchain__get_block_from_peer() {
71+
use bitcoin::hashes::Hash;
72+
let (node1, _node2, _node3) = integration_test::three_node_network();
73+
74+
let now = std::time::SystemTime::now()
75+
.duration_since(std::time::UNIX_EPOCH)
76+
.expect("Time went backwards")
77+
.as_secs() as u32;
78+
79+
// Create a dummy header and submit it
80+
let mut header = bitcoin::block::Header {
81+
version: bitcoin::block::Version::from_consensus(0x20000000),
82+
prev_blockhash: node1.client.best_block_hash().expect("best_block_hash failed"),
83+
merkle_root: bitcoin::TxMerkleNode::all_zeros(),
84+
time: now,
85+
bits: bitcoin::CompactTarget::from_consensus(0x207fffff),
86+
nonce: 0,
87+
};
88+
let target = header.target();
89+
while header.validate_pow(target).is_err() {
90+
header.nonce += 1;
91+
}
92+
node1.client.submit_header(&header).expect("submit_header failed");
93+
94+
let hash = header.block_hash();
95+
let peer_id = node1.client.get_peer_info().expect("getpeerinfo").0[0].id;
96+
let _: () = node1.client.get_block_from_peer(hash, peer_id).expect("getblockfrompeer");
97+
}
98+
6899
#[test]
69100
fn blockchain__get_block_hash__modelled() {
70101
let node = Node::with_wallet(Wallet::None, &[]);

types/src/v23/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
//! | getblockchaininfo | version + model | |
3232
//! | getblockcount | version + model | |
3333
//! | getblockfilter | version | |
34-
//! | getblockfrompeer | version + model | TODO |
34+
//! | getblockfrompeer | returns nothing | |
3535
//! | getblockhash | version + model | |
3636
//! | getblockheader | version + model | Includes additional 'verbose' type |
3737
//! | getblockstats | version + model | |

0 commit comments

Comments
 (0)