Skip to content

Commit c8788a6

Browse files
committed
test
1 parent f84dfff commit c8788a6

File tree

1 file changed

+112
-115
lines changed

1 file changed

+112
-115
lines changed

tests/rest.rs

Lines changed: 112 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -212,87 +212,85 @@ fn test_rest() -> Result<()> {
212212
{
213213
let network_info = tester.node_client().call::<Value>("getnetworkinfo", &[])?;
214214
let version = network_info["version"].as_u64().expect("network version");
215-
if version >= 280000 {
216-
// Test with a real transaction package - create parent-child transactions
217-
// submitpackage requires between 2 and 25 transactions with proper dependencies
218-
let package_addr1 = tester.newaddress()?;
219-
let package_addr2 = tester.newaddress()?;
220-
221-
// Create parent transaction
222-
let tx1_result = tester.node_client().call::<Value>(
223-
"createrawtransaction",
224-
&[
225-
serde_json::json!([]),
226-
serde_json::json!({package_addr1.to_string(): 0.5}),
227-
],
228-
)?;
229-
let tx1_unsigned_hex = tx1_result.as_str().expect("raw tx hex").to_string();
230-
231-
let tx1_fund_result = tester
232-
.node_client()
233-
.call::<Value>("fundrawtransaction", &[serde_json::json!(tx1_unsigned_hex)])?;
234-
let tx1_funded_hex = tx1_fund_result["hex"]
235-
.as_str()
236-
.expect("funded tx hex")
237-
.to_string();
238-
239-
let tx1_sign_result = tester.node_client().call::<Value>(
240-
"signrawtransactionwithwallet",
241-
&[serde_json::json!(tx1_funded_hex)],
242-
)?;
243-
let tx1_signed_hex = tx1_sign_result["hex"]
244-
.as_str()
245-
.expect("signed tx hex")
246-
.to_string();
247-
248-
// Decode parent transaction to get its txid and find the output to spend
249-
let tx1_decoded = tester
250-
.node_client()
251-
.call::<Value>("decoderawtransaction", &[serde_json::json!(tx1_signed_hex)])?;
252-
let tx1_txid = tx1_decoded["txid"].as_str().expect("parent txid");
253-
254-
// Find the output going to package_addr1 (the one we want to spend)
255-
let tx1_vouts = tx1_decoded["vout"].as_array().expect("parent vouts");
256-
let mut spend_vout_index = None;
257-
let mut spend_vout_value = 0u64;
258-
259-
for (i, vout) in tx1_vouts.iter().enumerate() {
260-
if let Some(script_pub_key) = vout.get("scriptPubKey") {
261-
if let Some(address) = script_pub_key.get("address") {
262-
if address.as_str() == Some(&package_addr1.to_string()) {
263-
spend_vout_index = Some(i);
264-
// Convert from BTC to satoshis
265-
spend_vout_value = (vout["value"].as_f64().expect("vout value")
266-
* 100_000_000.0)
267-
as u64;
268-
break;
269-
}
215+
// Test with a real transaction package - create parent-child transactions
216+
// submitpackage requires between 2 and 25 transactions with proper dependencies
217+
let package_addr1 = tester.newaddress()?;
218+
let package_addr2 = tester.newaddress()?;
219+
220+
// Create parent transaction
221+
let tx1_result = tester.node_client().call::<Value>(
222+
"createrawtransaction",
223+
&[
224+
serde_json::json!([]),
225+
serde_json::json!({package_addr1.to_string(): 0.5}),
226+
],
227+
)?;
228+
let tx1_unsigned_hex = tx1_result.as_str().expect("raw tx hex").to_string();
229+
230+
let tx1_fund_result = tester
231+
.node_client()
232+
.call::<Value>("fundrawtransaction", &[serde_json::json!(tx1_unsigned_hex)])?;
233+
let tx1_funded_hex = tx1_fund_result["hex"]
234+
.as_str()
235+
.expect("funded tx hex")
236+
.to_string();
237+
238+
let tx1_sign_result = tester.node_client().call::<Value>(
239+
"signrawtransactionwithwallet",
240+
&[serde_json::json!(tx1_funded_hex)],
241+
)?;
242+
let tx1_signed_hex = tx1_sign_result["hex"]
243+
.as_str()
244+
.expect("signed tx hex")
245+
.to_string();
246+
247+
// Decode parent transaction to get its txid and find the output to spend
248+
let tx1_decoded = tester
249+
.node_client()
250+
.call::<Value>("decoderawtransaction", &[serde_json::json!(tx1_signed_hex)])?;
251+
let tx1_txid = tx1_decoded["txid"].as_str().expect("parent txid");
252+
253+
// Find the output going to package_addr1 (the one we want to spend)
254+
let tx1_vouts = tx1_decoded["vout"].as_array().expect("parent vouts");
255+
let mut spend_vout_index = None;
256+
let mut spend_vout_value = 0u64;
257+
258+
for (i, vout) in tx1_vouts.iter().enumerate() {
259+
if let Some(script_pub_key) = vout.get("scriptPubKey") {
260+
if let Some(address) = script_pub_key.get("address") {
261+
if address.as_str() == Some(&package_addr1.to_string()) {
262+
spend_vout_index = Some(i);
263+
// Convert from BTC to satoshis
264+
spend_vout_value =
265+
(vout["value"].as_f64().expect("vout value") * 100_000_000.0) as u64;
266+
break;
270267
}
271268
}
272269
}
270+
}
273271

274-
let spend_vout_index = spend_vout_index.expect("Could not find output to spend");
275-
276-
// Create child transaction that spends from parent
277-
// Leave some satoshis for fee (e.g., 1000 sats)
278-
let child_output_value = spend_vout_value - 1000;
279-
let child_output_btc = child_output_value as f64 / 100_000_000.0;
280-
281-
let tx2_result = tester.node_client().call::<Value>(
282-
"createrawtransaction",
283-
&[
284-
serde_json::json!([{
285-
"txid": tx1_txid,
286-
"vout": spend_vout_index
287-
}]),
288-
serde_json::json!({package_addr2.to_string(): child_output_btc}),
289-
],
290-
)?;
291-
let tx2_unsigned_hex = tx2_result.as_str().expect("raw tx hex").to_string();
292-
293-
// Sign the child transaction
294-
// We need to provide the parent transaction's output details for signing
295-
let tx2_sign_result = tester.node_client().call::<Value>(
272+
let spend_vout_index = spend_vout_index.expect("Could not find output to spend");
273+
274+
// Create child transaction that spends from parent
275+
// Leave some satoshis for fee (e.g., 1000 sats)
276+
let child_output_value = spend_vout_value - 1000;
277+
let child_output_btc = child_output_value as f64 / 100_000_000.0;
278+
279+
let tx2_result = tester.node_client().call::<Value>(
280+
"createrawtransaction",
281+
&[
282+
serde_json::json!([{
283+
"txid": tx1_txid,
284+
"vout": spend_vout_index
285+
}]),
286+
serde_json::json!({package_addr2.to_string(): child_output_btc}),
287+
],
288+
)?;
289+
let tx2_unsigned_hex = tx2_result.as_str().expect("raw tx hex").to_string();
290+
291+
// Sign the child transaction
292+
// We need to provide the parent transaction's output details for signing
293+
let tx2_sign_result = tester.node_client().call::<Value>(
296294
"signrawtransactionwithwallet",
297295
&[
298296
serde_json::json!(tx2_unsigned_hex),
@@ -304,50 +302,49 @@ fn test_rest() -> Result<()> {
304302
}])
305303
],
306304
)?;
307-
let tx2_signed_hex = tx2_sign_result["hex"]
308-
.as_str()
309-
.expect("signed tx hex")
310-
.to_string();
311-
312-
// Debug: try calling submitpackage directly to see the result
313-
eprintln!("Trying submitpackage directly with parent-child transactions...");
314-
let direct_result = tester.node_client().call::<Value>(
315-
"submitpackage",
316-
&[serde_json::json!([
317-
tx1_signed_hex.clone(),
318-
tx2_signed_hex.clone()
319-
])],
320-
);
321-
match direct_result {
322-
Ok(result) => {
323-
eprintln!("Direct submitpackage succeeded: {:#?}", result);
324-
}
325-
Err(e) => {
326-
eprintln!("Direct submitpackage failed: {:?}", e);
327-
}
305+
let tx2_signed_hex = tx2_sign_result["hex"]
306+
.as_str()
307+
.expect("signed tx hex")
308+
.to_string();
309+
310+
// Debug: try calling submitpackage directly to see the result
311+
eprintln!("Trying submitpackage directly with parent-child transactions...");
312+
let direct_result = tester.node_client().call::<Value>(
313+
"submitpackage",
314+
&[serde_json::json!([
315+
tx1_signed_hex.clone(),
316+
tx2_signed_hex.clone()
317+
])],
318+
);
319+
match direct_result {
320+
Ok(result) => {
321+
eprintln!("Direct submitpackage succeeded: {:#?}", result);
328322
}
323+
Err(e) => {
324+
eprintln!("Direct submitpackage failed: {:?}", e);
325+
}
326+
}
329327

330-
// Now submit this transaction package via the package endpoint
331-
let package_json =
332-
serde_json::json!([tx1_signed_hex.clone(), tx2_signed_hex.clone()]).to_string();
333-
let package_result = ureq::post(&format!("http://{}/txs/package", rest_addr))
334-
.set("Content-Type", "application/json")
335-
.send_string(&package_json);
328+
// Now submit this transaction package via the package endpoint
329+
let package_json =
330+
serde_json::json!([tx1_signed_hex.clone(), tx2_signed_hex.clone()]).to_string();
331+
let package_result = ureq::post(&format!("http://{}/txs/package", rest_addr))
332+
.set("Content-Type", "application/json")
333+
.send_string(&package_json);
336334

337-
let package_resp = package_result.unwrap();
338-
assert_eq!(package_resp.status(), 200);
339-
let package_result = package_resp.into_json::<Value>()?;
335+
let package_resp = package_result.unwrap();
336+
assert_eq!(package_resp.status(), 200);
337+
let package_result = package_resp.into_json::<Value>()?;
340338

341-
// Verify the response structure
342-
assert!(package_result["tx-results"].is_object());
343-
assert!(package_result["package_msg"].is_string());
339+
// Verify the response structure
340+
assert!(package_result["tx-results"].is_object());
341+
assert!(package_result["package_msg"].is_string());
344342

345-
let tx_results = package_result["tx-results"].as_object().unwrap();
346-
assert_eq!(tx_results.len(), 2);
343+
let tx_results = package_result["tx-results"].as_object().unwrap();
344+
assert_eq!(tx_results.len(), 2);
347345

348-
// The transactions should be processed (whether accepted or rejected)
349-
assert!(!tx_results.is_empty());
350-
}
346+
// The transactions should be processed (whether accepted or rejected)
347+
assert!(!tx_results.is_empty());
351348
}
352349

353350
// Elements-only tests

0 commit comments

Comments
 (0)