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

refactor(app): De-duplicate and fix version handling code #6996

Merged
merged 5 commits into from
Jun 20, 2023
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
7 changes: 6 additions & 1 deletion zebra-chain/src/parameters/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub enum Network {
#[default]
Mainnet,

/// The testnet.
/// The oldest public test network.
Testnet,
}

Expand Down Expand Up @@ -124,6 +124,11 @@ impl Network {
pub fn lowercase_name(&self) -> String {
self.to_string().to_ascii_lowercase()
}

/// Returns `true` if this network is a testing network.
pub fn is_a_test_network(&self) -> bool {
*self != Network::Mainnet
}
}

impl FromStr for Network {
Expand Down
48 changes: 22 additions & 26 deletions zebra-rpc/src/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,11 @@ where
{
// Configuration
//
/// Zebra's application version.
app_version: String,
/// Zebra's application version, with build metadata.
build_version: String,

/// Zebra's RPC user agent.
user_agent: String,

/// The configured network for this RPC service.
network: Network,
Expand Down Expand Up @@ -300,8 +303,13 @@ where
Tip: ChainTip + Clone + Send + Sync + 'static,
{
/// Create a new instance of the RPC handler.
pub fn new<Version>(
app_version: Version,
//
// TODO:
// - put some of the configs or services in their own struct?
#[allow(clippy::too_many_arguments)]
pub fn new<VersionString, UserAgentString>(
build_version: VersionString,
user_agent: UserAgentString,
network: Network,
debug_force_finished_sync: bool,
debug_like_zcashd: bool,
Expand All @@ -310,21 +318,24 @@ where
latest_chain_tip: Tip,
) -> (Self, JoinHandle<()>)
where
Version: ToString,
VersionString: ToString + Clone + Send + 'static,
UserAgentString: ToString + Clone + Send + 'static,
<Mempool as Service<mempool::Request>>::Future: Send,
<State as Service<zebra_state::ReadRequest>>::Future: Send,
{
let (runner, queue_sender) = Queue::start();

let mut app_version = app_version.to_string();
let mut build_version = build_version.to_string();
let user_agent = user_agent.to_string();

// Match zcashd's version format, if the version string has anything in it
if !app_version.is_empty() && !app_version.starts_with('v') {
app_version.insert(0, 'v');
if !build_version.is_empty() && !build_version.starts_with('v') {
build_version.insert(0, 'v');
}

let rpc_impl = RpcImpl {
app_version,
build_version,
user_agent,
network,
debug_force_finished_sync,
debug_like_zcashd,
Expand Down Expand Up @@ -364,25 +375,10 @@ where
State::Future: Send,
Tip: ChainTip + Clone + Send + Sync + 'static,
{
#[allow(clippy::unwrap_in_result)]
fn get_info(&self) -> Result<GetInfo> {
// Build a [BIP 14] valid user agent with release info.
//
// [BIP 14]: https://github.com/bitcoin/bips/blob/master/bip-0014.mediawiki
let release_version = self
.app_version
// remove everything after the `+` character if any
.split('+')
.next()
.expect("always at least 1 slice");
// Remove the previously added `v` character at the start since it's not a part of the user agent.
let release_version = release_version.strip_prefix('v').unwrap_or(release_version);

let user_agent = format!("/Zebra:{release_version}/");

let response = GetInfo {
build: self.app_version.clone(),
subversion: user_agent,
build: self.build_version.clone(),
subversion: self.user_agent.clone(),
};

Ok(response)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl Response {
networksolps,
networkhashps: networksolps,
chain: network.bip70_network_name(),
testnet: network == Network::Testnet,
testnet: network.is_a_test_network(),
}
}
}
14 changes: 14 additions & 0 deletions zebra-rpc/src/methods/tests/prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ proptest! {
let mut mempool = MockService::build().for_prop_tests();
let mut state: MockService<_, _, _, BoxError> = MockService::build().for_prop_tests();
let (rpc, rpc_tx_queue_task_handle) = RpcImpl::new(
"RPC test",
"RPC test",
Mainnet,
false,
Expand Down Expand Up @@ -94,6 +95,7 @@ proptest! {
let mut state: MockService<_, _, _, BoxError> = MockService::build().for_prop_tests();

let (rpc, rpc_tx_queue_task_handle) = RpcImpl::new(
"RPC test",
"RPC test",
Mainnet,
false,
Expand Down Expand Up @@ -153,6 +155,7 @@ proptest! {
let mut state: MockService<_, _, _, BoxError> = MockService::build().for_prop_tests();

let (rpc, rpc_tx_queue_task_handle) = RpcImpl::new(
"RPC test",
"RPC test",
Mainnet,
false,
Expand Down Expand Up @@ -220,6 +223,7 @@ proptest! {
let mut state: MockService<_, _, _, BoxError> = MockService::build().for_prop_tests();

let (rpc, rpc_tx_queue_task_handle) = RpcImpl::new(
"RPC test",
"RPC test",
Mainnet,
false,
Expand Down Expand Up @@ -276,6 +280,7 @@ proptest! {
let mut state: MockService<_, _, _, BoxError> = MockService::build().for_prop_tests();

let (rpc, rpc_tx_queue_task_handle) = RpcImpl::new(
"RPC test",
"RPC test",
Mainnet,
false,
Expand Down Expand Up @@ -330,6 +335,7 @@ proptest! {
let mut state: MockService<_, _, _, BoxError> = MockService::build().for_prop_tests();

let (rpc, rpc_tx_queue_task_handle) = RpcImpl::new(
"RPC test",
"RPC test",
Mainnet,
false,
Expand Down Expand Up @@ -430,6 +436,7 @@ proptest! {
let mut state: MockService<_, _, _, BoxError> = MockService::build().for_prop_tests();

let (rpc, rpc_tx_queue_task_handle) = RpcImpl::new(
"RPC test",
"RPC test",
Mainnet,
false,
Expand Down Expand Up @@ -488,6 +495,7 @@ proptest! {
let mut state: MockService<_, _, _, BoxError> = MockService::build().for_prop_tests();

let (rpc, rpc_tx_queue_task_handle) = RpcImpl::new(
"RPC test",
"RPC test",
Mainnet,
false,
Expand Down Expand Up @@ -535,6 +543,7 @@ proptest! {

// look for an error with a `NoChainTip`
let (rpc, rpc_tx_queue_task_handle) = RpcImpl::new(
"RPC test",
"RPC test",
network,
false,
Expand Down Expand Up @@ -585,6 +594,7 @@ proptest! {

// Start RPC with the mocked `ChainTip`
let (rpc, rpc_tx_queue_task_handle) = RpcImpl::new(
"RPC test",
"RPC test",
network,
false,
Expand Down Expand Up @@ -671,6 +681,7 @@ proptest! {
// Start RPC with the mocked `ChainTip`
runtime.block_on(async move {
let (rpc, _rpc_tx_queue_task_handle) = RpcImpl::new(
"RPC test",
"RPC test",
network,
false,
Expand Down Expand Up @@ -734,6 +745,7 @@ proptest! {
// Start RPC with the mocked `ChainTip`
runtime.block_on(async move {
let (rpc, _rpc_tx_queue_task_handle) = RpcImpl::new(
"RPC test",
"RPC test",
network,
false,
Expand Down Expand Up @@ -785,6 +797,7 @@ proptest! {
let mut state: MockService<_, _, _, BoxError> = MockService::build().for_prop_tests();

let (rpc, rpc_tx_queue_task_handle) = RpcImpl::new(
"RPC test",
"RPC test",
Mainnet,
false,
Expand Down Expand Up @@ -874,6 +887,7 @@ proptest! {
let mut state: MockService<_, _, _, BoxError> = MockService::build().for_prop_tests();

let (rpc, rpc_tx_queue_task_handle) = RpcImpl::new(
"RPC test",
"RPC test",
Mainnet,
false,
Expand Down
1 change: 1 addition & 0 deletions zebra-rpc/src/methods/tests/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ async fn test_rpc_response_data_for_network(network: Network) {
// Init RPC
let (rpc, _rpc_tx_queue_task_handle) = RpcImpl::new(
"RPC test",
"/Zebra:RPC test/",
network,
false,
true,
Expand Down
10 changes: 10 additions & 0 deletions zebra-rpc/src/methods/tests/vectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ async fn rpc_getinfo() {

let (rpc, rpc_tx_queue_task_handle) = RpcImpl::new(
"RPC test",
"/Zebra:RPC test/",
Mainnet,
false,
true,
Expand Down Expand Up @@ -72,6 +73,7 @@ async fn rpc_getblock() {

// Init RPC
let (rpc, rpc_tx_queue_task_handle) = RpcImpl::new(
"RPC test",
"RPC test",
Mainnet,
false,
Expand Down Expand Up @@ -223,6 +225,7 @@ async fn rpc_getblock_parse_error() {

// Init RPC
let (rpc, rpc_tx_queue_task_handle) = RpcImpl::new(
"RPC test",
"RPC test",
Mainnet,
false,
Expand Down Expand Up @@ -265,6 +268,7 @@ async fn rpc_getblock_missing_error() {

// Init RPC
let (rpc, rpc_tx_queue_task_handle) = RpcImpl::new(
"RPC test",
"RPC test",
Mainnet,
false,
Expand Down Expand Up @@ -333,6 +337,7 @@ async fn rpc_getbestblockhash() {

// Init RPC
let (rpc, rpc_tx_queue_task_handle) = RpcImpl::new(
"RPC test",
"RPC test",
Mainnet,
false,
Expand Down Expand Up @@ -378,6 +383,7 @@ async fn rpc_getrawtransaction() {

// Init RPC
let (rpc, rpc_tx_queue_task_handle) = RpcImpl::new(
"RPC test",
"RPC test",
Mainnet,
false,
Expand Down Expand Up @@ -539,6 +545,7 @@ async fn rpc_getaddresstxids_invalid_arguments() {
zebra_state::populated_state(blocks.clone(), Mainnet).await;

let (rpc, rpc_tx_queue_task_handle) = RpcImpl::new(
"RPC test",
"RPC test",
Mainnet,
false,
Expand Down Expand Up @@ -682,6 +689,7 @@ async fn rpc_getaddresstxids_response_with(
zebra_state::populated_state(blocks.to_owned(), network).await;

let (rpc, rpc_tx_queue_task_handle) = RpcImpl::new(
"RPC test",
"RPC test",
network,
false,
Expand Down Expand Up @@ -733,6 +741,7 @@ async fn rpc_getaddressutxos_invalid_arguments() {
let mut state: MockService<_, _, _, BoxError> = MockService::build().for_unit_tests();

let rpc = RpcImpl::new(
"RPC test",
"RPC test",
Mainnet,
false,
Expand Down Expand Up @@ -781,6 +790,7 @@ async fn rpc_getaddressutxos_response() {
zebra_state::populated_state(blocks.clone(), Mainnet).await;

let rpc = RpcImpl::new(
"RPC test",
"RPC test",
Mainnet,
false,
Expand Down
Loading