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

feature(rpc): add real data to getinfo method #3660

Merged
merged 7 commits into from
Mar 1, 2022
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
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions zebra-rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ edition = "2021"

[dependencies]

zebra-network = { path = "../zebra-network" }

futures = "0.3"

# lightwalletd sends JSON-RPC requests over HTTP 1.1
Expand All @@ -24,3 +26,7 @@ tracing = "0.1"
tracing-futures = "0.2"

serde = { version = "1", features = ["serde_derive"] }

[dev-dependencies]

zebra-test = { path = "../zebra-test/" }
38 changes: 30 additions & 8 deletions zebra-rpc/src/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,34 @@
use jsonrpc_core::{self, Result};
use jsonrpc_derive::rpc;

use zebra_network::constants::USER_AGENT;

#[cfg(test)]
mod tests;

#[rpc(server)]
/// RPC method signatures.
pub trait Rpc {
/// getinfo
///
/// TODO: explain what the method does
/// link to the zcashd RPC reference
/// list the arguments and fields that lightwalletd uses
/// note any other lightwalletd changes
/// Returns software information from the RPC server running Zebra.
///
/// zcashd reference: <https://zcash.github.io/rpc/getinfo.html>
///
/// Result:
/// {
/// "build": String, // Full application version
/// "subversion", String, // Zebra user agent
/// }
///
/// Note 1: We only expose 2 fields as they are the only ones needed for
/// lightwalletd: <https://github.com/zcash/lightwalletd/blob/v0.4.9/common/common.go#L91-L95>
///
/// Note 2: <https://zcash.github.io/rpc/getinfo.html> is outdated so it does not
/// show the fields we are exposing. However, this fields are part of the output
/// as shown in the following zcashd code:
/// <https://github.com/zcash/zcash/blob/v4.6.0-1/src/rpc/misc.cpp#L86-L87>
/// Zcash open ticket to add this fields to the docs: <https://github.com/zcash/zcash/issues/5606>
#[rpc(name = "getinfo")]
fn get_info(&self) -> Result<GetInfo>;

Expand All @@ -32,13 +51,16 @@ pub trait Rpc {
}

/// RPC method implementations.
pub struct RpcImpl;

pub struct RpcImpl {
/// Zebra's application version.
pub app_version: String,
}
impl Rpc for RpcImpl {
fn get_info(&self) -> Result<GetInfo> {
// TODO: dummy output data, fix in the context of #3142
let response = GetInfo {
build: "TODO: Zebra v1.0.0 ...".into(),
subversion: "TODO: /Zebra:1.0.0-beta.../".into(),
build: self.app_version.clone(),
subversion: USER_AGENT.into(),
};

Ok(response)
Expand Down
2 changes: 2 additions & 0 deletions zebra-rpc/src/methods/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
//! Test code for RPC methods
mod vectors;
23 changes: 23 additions & 0 deletions zebra-rpc/src/methods/tests/vectors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//! Fixed test vectors for RPC methods.

use super::super::*;
use zebra_network::constants::USER_AGENT;

#[test]
fn rpc_getinfo() {
zebra_test::init();

let rpc = RpcImpl {
app_version: "Zebra version test".to_string(),
};

let get_info = rpc.get_info().expect("We should have a GetInfo struct");

// make sure there is a `build` field in the response,
// and that is equal to the provided string.
assert_eq!(get_info.build, "Zebra version test");

// make sure there is a `subversion` field,
// and that is equal to the Zebra user agent.
assert_eq!(get_info.subversion, USER_AGENT);
}
8 changes: 6 additions & 2 deletions zebra-rpc/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@ pub struct RpcServer;

impl RpcServer {
/// Start a new RPC server endpoint
pub fn spawn(config: Config) -> tokio::task::JoinHandle<()> {
pub fn spawn(config: Config, app_version: String) -> tokio::task::JoinHandle<()> {
if let Some(listen_addr) = config.listen_addr {
info!("Trying to open RPC endpoint at {}...", listen_addr,);

// Initialize the rpc methods with the zebra version
let rpc_impl = RpcImpl { app_version };

// Create handler compatible with V1 and V2 RPC protocols
let mut io =
jsonrpc_core::IoHandler::with_compatibility(jsonrpc_core::Compatibility::Both);
io.extend_with(RpcImpl.to_delegate());
io.extend_with(rpc_impl.to_delegate());

let server = ServerBuilder::new(io)
// use the same tokio executor as the rest of Zebra
Expand All @@ -54,6 +57,7 @@ impl RpcServer {
info!("Stopping RPC endpoint");
})
};

tokio::task::spawn_blocking(server)
} else {
// There is no RPC port, so the RPC task does nothing.
Expand Down
3 changes: 2 additions & 1 deletion zebrad/src/commands/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ use zebra_consensus::CheckpointList;
use zebra_rpc::server::RpcServer;

use crate::{
application::app_version,
components::{
inbound::{self, InboundSetupData},
mempool::{self, Mempool},
Expand Down Expand Up @@ -196,7 +197,7 @@ impl StartCmd {
.in_current_span(),
);

let rpc_task_handle = RpcServer::spawn(config.rpc);
let rpc_task_handle = RpcServer::spawn(config.rpc, app_version().to_string());

info!("spawned initial Zebra tasks");

Expand Down