Skip to content

Commit 94a91eb

Browse files
committed
fix(tests): adapt test infrastructure to corepc-node and ureq v3
After cherry-picking PR Blockstream#177 (202601-deps), fix test compilation issues arising from API differences between our refactored tests and the new dependencies: - Replace `ureq::Response` (now private) with `ureq::Body` in get_body() - Replace `impl bitcoincore_rpc::RpcApi for TestRunner` with a plain `call()` method (bitcoincore_rpc not available in non-liquid mode) - Add TestRunner wrappers: invalidate_block, generate_to_address, get_block_hash, get_raw_transaction (all previously via RpcApi) - Fix get_block_count() call to go through wrapper (returns u64 not corepc_node::vtype::GetBlockCount)
1 parent 34de949 commit 94a91eb

2 files changed

Lines changed: 41 additions & 12 deletions

File tree

tests/common.rs

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -295,15 +295,44 @@ impl TestRunner {
295295
}
296296
}
297297

298-
// Make the RpcApi methods available directly on TestRunner,
299-
// without having to go through the node_client() getter
300-
impl bitcoincore_rpc::RpcApi for TestRunner {
301-
fn call<T: for<'a> serde::de::Deserialize<'a>>(
298+
impl TestRunner {
299+
// Make the node_client's call() available directly on TestRunner
300+
pub fn call<T: for<'a> serde::de::Deserialize<'a>>(
302301
&self,
303302
cmd: &str,
304303
args: &[serde_json::Value],
305-
) -> bitcoincore_rpc::Result<T> {
306-
self.node_client().call(cmd, args)
304+
) -> Result<T> {
305+
Ok(self.node_client().call(cmd, args)?)
306+
}
307+
308+
pub fn invalidate_block(&self, block_hash: &BlockHash) -> Result<()> {
309+
self.call("invalidateblock", &[block_hash.to_string().into()])
310+
}
311+
312+
pub fn generate_to_address(&self, count: u64, address: &Address) -> Result<Vec<BlockHash>> {
313+
let hashes: Vec<String> =
314+
self.call("generatetoaddress", &[count.into(), address.to_string().into()])?;
315+
Ok(hashes
316+
.into_iter()
317+
.map(|h| h.parse().expect("valid block hash"))
318+
.collect())
319+
}
320+
321+
pub fn get_block_hash(&self, height: u64) -> Result<BlockHash> {
322+
let hash: String = self.call("getblockhash", &[height.into()])?;
323+
Ok(hash.parse().expect("valid block hash"))
324+
}
325+
326+
#[cfg(not(feature = "liquid"))]
327+
pub fn get_raw_transaction(
328+
&self,
329+
txid: &Txid,
330+
_block_hash: Option<&BlockHash>,
331+
) -> Result<bitcoin::Transaction> {
332+
use bitcoin::hex::FromHex;
333+
let tx_hex: String = self.call("getrawtransaction", &[txid.to_string().into()])?;
334+
let tx_bytes = Vec::<u8>::from_hex(&tx_hex).expect("valid hex");
335+
Ok(bitcoin::consensus::deserialize(&tx_bytes).expect("valid transaction"))
307336
}
308337
}
309338

tests/rest.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ pub mod common;
1717

1818
use common::Result;
1919

20-
fn get(rest_addr: net::SocketAddr, path: &str) -> std::result::Result<ureq::Response, ureq::Error> {
21-
ureq::get(&format!("http://{}{}", rest_addr, path)).call()
20+
fn get_body(rest_addr: net::SocketAddr, path: &str) -> std::result::Result<ureq::Body, ureq::Error> {
21+
Ok(ureq::get(&format!("http://{}{}", rest_addr, path)).call()?.into_body())
2222
}
2323

2424
fn get_json(rest_addr: net::SocketAddr, path: &str) -> Result<Value> {
25-
Ok(get(rest_addr, path)?.into_body().read_json()?)
25+
Ok(get_body(rest_addr, path)?.read_json()?)
2626
}
2727

2828
fn get_plain(rest_addr: net::SocketAddr, path: &str) -> Result<String> {
29-
Ok(get(rest_addr, path)?.into_body().read_to_string()?)
29+
Ok(get_body(rest_addr, path)?.read_to_string()?)
3030
}
3131

3232
#[test]
@@ -318,7 +318,7 @@ fn test_rest_block() -> Result<()> {
318318
}
319319

320320
// Test GET /block/:hash/raw
321-
let mut res = get(rest_addr, &format!("/block/{}/raw", blockhash))?.into_reader();
321+
let mut res = get_body(rest_addr, &format!("/block/{}/raw", blockhash))?.into_reader();
322322
let mut rest_rawblock = Vec::new();
323323
res.read_to_end(&mut rest_rawblock).unwrap();
324324
let node_hexblock = // uses low-level call() to support Elements
@@ -826,7 +826,7 @@ fn test_rest_reorg() -> Result<()> {
826826
)
827827
};
828828

829-
let init_height = tester.node_client().get_block_count()?;
829+
let init_height = tester.get_block_count()?;
830830

831831
let address = tester.newaddress()?;
832832
let miner_address = tester.newaddress()?;

0 commit comments

Comments
 (0)