From b327d5bb7de41f2c9c0f41b84b8e9bf5c045c7ba Mon Sep 17 00:00:00 2001 From: Alfredo Garcia Date: Wed, 1 Feb 2023 22:35:13 -0300 Subject: [PATCH] fix(getblocktemplate): change error format in proposals (#6044) * change error format in proposals * use trim_matches * make regex feature dependency * remove regex fully * remove char in comment to leave the file as it was before * remove assertion lines from snapshots --- .../types/get_block_template/proposal.rs | 32 +++++++++---------- ..._template_invalid-proposal@mainnet_10.snap | 2 +- ..._template_invalid-proposal@testnet_10.snap | 2 +- 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/zebra-rpc/src/methods/get_block_template_rpcs/types/get_block_template/proposal.rs b/zebra-rpc/src/methods/get_block_template_rpcs/types/get_block_template/proposal.rs index bbbca523..cf4471d5 100644 --- a/zebra-rpc/src/methods/get_block_template_rpcs/types/get_block_template/proposal.rs +++ b/zebra-rpc/src/methods/get_block_template_rpcs/types/get_block_template/proposal.rs @@ -2,7 +2,7 @@ //! //! `ProposalResponse` is the output of the `getblocktemplate` RPC method in 'proposal' mode. -use std::{error::Error, num::ParseIntError, str::FromStr, sync::Arc}; +use std::{num::ParseIntError, str::FromStr, sync::Arc}; use zebra_chain::{ block::{self, Block, Height}, @@ -42,23 +42,21 @@ pub enum ProposalResponse { impl ProposalResponse { /// Return a rejected response containing an error kind and detailed error info. - pub fn rejected(kind: S, error: BoxError) -> Self { - let kind = kind.to_string(); + /// + /// Note: Error kind is currently ignored to match zcashd error format (`kebab-case` string). + pub fn rejected(_kind: S, error: BoxError) -> Self { + // Make error `kebab-case` to match zcashd format. + let error_kebab1 = format!("{error:?}") + .replace(|c: char| !c.is_alphanumeric(), "-") + .to_ascii_lowercase(); + // Remove consecutive duplicated `-` characters. + let mut error_v: Vec = error_kebab1.chars().collect(); + error_v.dedup_by(|a, b| a == &'-' && b == &'-'); + let error_kebab2: String = error_v.into_iter().collect(); + // Trim any leading or trailing `-` characters. + let final_error = error_kebab2.trim_matches('-'); - // Pretty-print the detailed error for now - ProposalResponse::Rejected(format!("{kind}: {error:#?}")) - } - - /// Return a rejected response containing just the detailed error information. - pub fn error(error: BoxError) -> Self { - // Pretty-print the detailed error for now - ProposalResponse::Rejected(format!("{error:#?}")) - } -} - -impl From for ProposalResponse { - fn from(error: E) -> Self { - Self::error(error.into()) + ProposalResponse::Rejected(final_error.to_string()) } } diff --git a/zebra-rpc/src/methods/tests/snapshot/snapshots/get_block_template_invalid-proposal@mainnet_10.snap b/zebra-rpc/src/methods/tests/snapshot/snapshots/get_block_template_invalid-proposal@mainnet_10.snap index 6d80c427..a5f573ed 100644 --- a/zebra-rpc/src/methods/tests/snapshot/snapshots/get_block_template_invalid-proposal@mainnet_10.snap +++ b/zebra-rpc/src/methods/tests/snapshot/snapshots/get_block_template_invalid-proposal@mainnet_10.snap @@ -2,4 +2,4 @@ source: zebra-rpc/src/methods/tests/snapshot/get_block_template_rpcs.rs expression: block_template --- -"invalid proposal format: Io(\n Error {\n kind: UnexpectedEof,\n message: \"failed to fill whole buffer\",\n },\n)" +"io-error-kind-unexpectedeof-message-failed-to-fill-whole-buffer" diff --git a/zebra-rpc/src/methods/tests/snapshot/snapshots/get_block_template_invalid-proposal@testnet_10.snap b/zebra-rpc/src/methods/tests/snapshot/snapshots/get_block_template_invalid-proposal@testnet_10.snap index 6d80c427..a5f573ed 100644 --- a/zebra-rpc/src/methods/tests/snapshot/snapshots/get_block_template_invalid-proposal@testnet_10.snap +++ b/zebra-rpc/src/methods/tests/snapshot/snapshots/get_block_template_invalid-proposal@testnet_10.snap @@ -2,4 +2,4 @@ source: zebra-rpc/src/methods/tests/snapshot/get_block_template_rpcs.rs expression: block_template --- -"invalid proposal format: Io(\n Error {\n kind: UnexpectedEof,\n message: \"failed to fill whole buffer\",\n },\n)" +"io-error-kind-unexpectedeof-message-failed-to-fill-whole-buffer"