Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved formatting for bitcoin'd rpc error messages #133

Merged
merged 4 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,13 @@ fn parse_jsonrpc_reply(mut reply: Value, method: &str, expected_id: u64) -> Resu
if let Some(err) = reply_obj.get_mut("error") {
if !err.is_null() {
if let Some(code) = parse_error_code(&err) {
let msg = err["message"]
.as_str()
.map_or_else(|| err.to_string(), |s| s.to_string());
match code {
// RPC_IN_WARMUP -> retry by later reconnection
-28 => bail!(ErrorKind::Connection(err.to_string())),
code => bail!(ErrorKind::RpcError(code, err.take(), method.to_string())),
code => bail!(ErrorKind::RpcError(code, msg, method.to_string())),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ error_chain! {
display("Connection error: {}", msg)
}

RpcError(code: i64, error: serde_json::Value, method: String) {
RpcError(code: i64, error: String, method: String) {
description("RPC error")
display("{} RPC error {}: {}", method, code, error)
}
Expand Down
4 changes: 1 addition & 3 deletions src/rest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -987,9 +987,7 @@ fn handle_request(
.ok_or_else(|| HttpError::from("Missing tx".to_string()))?,
_ => return http_message(StatusCode::METHOD_NOT_ALLOWED, "Invalid method", 0),
};
let txid = query
.broadcast_raw(&txhex)
.map_err(|err| HttpError::from(err.description().to_string()))?;
let txid = query.broadcast_raw(&txhex)?;
http_message(StatusCode::OK, txid.to_string(), 0)
}

Expand Down
54 changes: 42 additions & 12 deletions tests/rest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,23 @@ fn test_rest() -> Result<()> {
tester.mine()?;
assert_eq!(get_json("/mempool")?["count"].as_u64(), Some(0));

// Test POST /tx
let txid = tester.send(&addr1, "9.9 BTC".parse().unwrap())?;
let tx_hex = get_plain(&format!("/tx/{}/hex", txid))?;
// Re-send the tx created by send(). It'll be accepted again since its still in the mempool.
let broadcast1_resp = ureq::post(&format!("http://{}/tx", rest_addr)).send_string(&tx_hex)?;
assert_eq!(broadcast1_resp.status(), 200);
assert_eq!(broadcast1_resp.into_string()?, txid.to_string());
// Mine the tx then submit it again. Should now fail.
tester.mine()?;
let broadcast2_res = ureq::post(&format!("http://{}/tx", rest_addr)).send_string(&tx_hex);
let broadcast2_resp = broadcast2_res.unwrap_err().into_response().unwrap();
assert_eq!(broadcast2_resp.status(), 400);
assert_eq!(
broadcast2_resp.into_string()?,
"sendrawtransaction RPC error -27: Transaction already in block chain"
);

// Elements-only tests
#[cfg(feature = "liquid")]
{
Expand Down Expand Up @@ -296,22 +313,35 @@ fn test_rest() -> Result<()> {

// Test GET /block/:hash
{
let bestblockhash = get_plain("/blocks/tip/hash")?;
let block = get_json(&format!("/block/{}", bestblockhash))?;
let block1_hash = get_plain("/block-height/1")?;
let block1 = get_json(&format!("/block/{}", block1_hash))?;

// No PoW-related stuff
assert!(block["bits"].is_null());
assert!(block["nonce"].is_null());
assert!(block["difficulty"].is_null());
assert!(block1["bits"].is_null());
assert!(block1["nonce"].is_null());
assert!(block1["difficulty"].is_null());

// Dynamic Federations (dynafed) fields
assert!(block["ext"]["current"]["signblockscript"].is_string());
assert!(block["ext"]["current"]["fedpegscript"].is_string());
assert!(block["ext"]["current"]["fedpeg_program"].is_string());
assert!(block["ext"]["current"]["signblock_witness_limit"].is_u64());
assert!(block["ext"]["current"]["extension_space"].is_array());
assert!(block["ext"]["proposed"].is_object());
assert!(block["ext"]["signblock_witness"].is_array());
// Block #1 should have the Full dynafed params
// See https://docs.rs/elements/latest/elements/dynafed/enum.Params.html
assert!(block1["ext"]["current"]["signblockscript"].is_string());
assert!(block1["ext"]["current"]["fedpegscript"].is_string());
assert!(block1["ext"]["current"]["fedpeg_program"].is_string());
assert!(block1["ext"]["current"]["signblock_witness_limit"].is_u64());
assert!(block1["ext"]["current"]["extension_space"].is_array());
assert!(block1["ext"]["proposed"].is_object());
assert!(block1["ext"]["signblock_witness"].is_array());

// Block #2 should have the Compact params
let block2_hash = get_plain("/block-height/2")?;
let block2 = get_json(&format!("/block/{}", block2_hash))?;
assert!(block2["ext"]["current"]["signblockscript"].is_string());
assert!(block2["ext"]["current"]["signblock_witness_limit"].is_u64());
// With the `elided_root` in place of `fedpegscript`/`fedpeg_program`/`extension_space``
assert!(block2["ext"]["current"]["elided_root"].is_string());
assert!(block2["ext"]["current"]["fedpegscript"].is_null());
assert!(block2["ext"]["current"]["fedpeg_program"].is_null());
assert!(block2["ext"]["current"]["extension_space"].is_null());
}
}

Expand Down
Loading