refactor and cleanup

This commit is contained in:
Alfredo Garcia 2024-06-16 18:27:23 -03:00
parent f082ed1a72
commit b75ad69275
5 changed files with 761 additions and 961 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1417,20 +1417,12 @@ impl AddressStrings {
}
/// The transparent balance of a set of addresses.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, serde::Serialize)]
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Hash, serde::Serialize)]
pub struct AddressBalance {
/// The total transparent balance.
balance: u64,
}
impl Default for AddressBalance {
fn default() -> Self {
Self {
balance: u64::default(),
}
}
}
/// A hex-encoded [`ConsensusBranchId`] string.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, serde::Serialize, serde::Deserialize)]
struct ConsensusBranchIdHex(#[serde(with = "hex")] ConsensusBranchId);

View File

@ -3,7 +3,7 @@
use zebra_chain::parameters::Network;
/// Response to a `getmininginfo` RPC request.
#[derive(Debug, PartialEq, Eq, serde::Serialize)]
#[derive(Debug, Default, PartialEq, Eq, serde::Serialize)]
pub struct Response {
/// The estimated network solution rate in Sol/s.
networksolps: u64,
@ -29,14 +29,3 @@ impl Response {
}
}
}
impl Default for Response {
fn default() -> Self {
Self {
networksolps: u64::default(),
networkhashps: u64::default(),
chain: String::default(),
testnet: bool::default(),
}
}
}

View File

@ -32,6 +32,16 @@ pub struct GetSubtrees {
pub subtrees: Vec<SubtreeRpcData>,
}
impl Default for GetSubtrees {
fn default() -> Self {
Self {
pool: String::default(),
start_index: NoteCommitmentSubtreeIndex(u16::default()),
subtrees: vec![],
}
}
}
/// Response to a `z_gettreestate` RPC request.
///
/// Contains hex-encoded Sapling & Orchard note commitment trees and their corresponding

View File

@ -453,294 +453,103 @@ fn add_params_to_description(description: &str, params_description: &str) -> Str
new_description
}
fn default_property<T: serde::Serialize>(
type_: &str,
items: Option<ArrayItems>,
default_value: T,
) -> Result<Property, Box<dyn Error>> {
Ok(Property {
type_: type_.to_string(),
items,
default: serde_json::to_string(&default_value)?,
})
}
// Get requests examples by using defaults from the Zebra RPC methods
fn get_default_properties(method_name: &str) -> Result<HashMap<String, Property>, Box<dyn Error>> {
// TODO: Complete the list of methods
let type_ = "object".to_string();
let type_ = "object";
let items = None;
let mut props = HashMap::new();
let properties = match method_name {
"getinfo" => {
props.insert(
"result".to_string(),
Property {
type_,
items,
default: serde_json::to_string(&GetInfo::default())?,
},
);
props
}
"getbestblockhash" => {
props.insert(
"result".to_string(),
Property {
type_,
items,
default: serde_json::to_string(&GetBlockHash::default())?,
},
);
props
}
// TODO: An entry has to be added here manually for each new RPC method introduced, can we automate?
let default_result = match method_name {
// mining
// TODO: missing `getblocktemplate`. It's a complex method that requires a lot of parameters.
"getnetworkhashps" => default_property(type_, items.clone(), u64::default())?,
"getblocksubsidy" => default_property(
type_,
items.clone(),
get_block_template_rpcs::types::subsidy::BlockSubsidy::default(),
)?,
"getmininginfo" => default_property(
type_,
items.clone(),
get_block_template_rpcs::types::get_mining_info::Response::default(),
)?,
"getnetworksolps" => default_property(type_, items.clone(), u64::default())?,
"submitblock" => default_property(
type_,
items.clone(),
get_block_template_rpcs::types::submit_block::Response::default(),
)?,
// util
"validateaddress" => default_property(
type_,
items.clone(),
get_block_template_rpcs::types::validate_address::Response::default(),
)?,
"z_validateaddress" => default_property(
type_,
items.clone(),
get_block_template_rpcs::types::z_validate_address::Response::default(),
)?,
// address
"getaddressbalance" => default_property(type_, items.clone(), AddressBalance::default())?,
"getaddressutxos" => default_property(type_, items.clone(), GetAddressUtxos::default())?,
"getaddresstxids" => default_property(type_, items.clone(), Vec::<String>::default())?,
// network
"getpeerinfo" => default_property(
type_,
items.clone(),
get_block_template_rpcs::types::peer_info::PeerInfo::default(),
)?,
// blockchain
"getdifficulty" => default_property(type_, items.clone(), f64::default())?,
"getblockchaininfo" => {
props.insert(
"result".to_string(),
Property {
type_,
items,
default: serde_json::to_string(&GetBlockChainInfo::default())?,
},
);
props
default_property(type_, items.clone(), GetBlockChainInfo::default())?
}
"getblock" => {
props.insert(
"result".to_string(),
Property {
type_,
items,
default: serde_json::to_string(&GetBlock::default())?,
},
);
props
}
"getblockhash" => {
props.insert(
"result".to_string(),
Property {
type_,
items,
default: serde_json::to_string(&GetBlockHash::default())?,
},
);
props
}
"z_gettreestate" => {
props.insert(
"result".to_string(),
Property {
type_,
items,
default: serde_json::to_string(&GetTreestate::default())?,
},
);
props
}
"getpeerinfo" => {
props.insert(
"result".to_string(),
Property {
type_,
items,
default: serde_json::to_string(
&get_block_template_rpcs::types::peer_info::PeerInfo::default(),
)?,
},
);
props
}
"getblocksubsidy" => {
props.insert(
"result".to_string(),
Property {
type_,
items,
default: serde_json::to_string(
&get_block_template_rpcs::types::subsidy::BlockSubsidy::default(),
)?,
},
);
props
}
"getmininginfo" => {
props.insert(
"result".to_string(),
Property {
type_,
items,
default: serde_json::to_string(
&get_block_template_rpcs::types::get_mining_info::Response::default(),
)?,
},
);
props
}
"getblockcount" => {
props.insert(
"result".to_string(),
Property {
type_,
items,
default: serde_json::to_string(&u32::default())?,
},
);
props
}
"getaddressbalance" => {
props.insert(
"result".to_string(),
Property {
type_,
items,
default: serde_json::to_string(&AddressBalance::default())?,
},
);
props
}
"getaddressutxos" => {
props.insert(
"result".to_string(),
Property {
type_,
items,
default: serde_json::to_string(&GetAddressUtxos::default())?,
},
);
props
}
"getaddresstxids" => {
props.insert(
"result".to_string(),
Property {
type_,
items,
default: serde_json::to_string(&Vec::<String>::default())?,
},
);
props
}
"validateaddress" => {
props.insert(
"result".to_string(),
Property {
type_,
items,
default: serde_json::to_string(
&get_block_template_rpcs::types::validate_address::Response::default(),
)?,
},
);
props
}
"z_validateaddress" => {
props.insert(
"result".to_string(),
Property {
type_,
items,
default: serde_json::to_string(
&get_block_template_rpcs::types::z_validate_address::Response::default(),
)?,
},
);
props
}
"getrawmempool" => {
props.insert(
"result".to_string(),
Property {
type_,
items,
default: serde_json::to_string(&Vec::<String>::default())?,
},
);
props
}
"getdifficulty" => {
props.insert(
"result".to_string(),
Property {
type_,
items,
default: serde_json::to_string(&f64::default())?,
},
);
props
"getrawmempool" => default_property(type_, items.clone(), Vec::<String>::default())?,
"getblockhash" => default_property(type_, items.clone(), GetBlockHash::default())?,
"z_getsubtreesbyindex" => {
default_property(type_, items.clone(), trees::GetSubtrees::default())?
}
"z_gettreestate" => default_property(type_, items.clone(), GetTreestate::default())?,
"getblockcount" => default_property(type_, items.clone(), u32::default())?,
"getbestblockhash" => default_property(type_, items.clone(), GetBlockHash::default())?,
"getblock" => default_property(type_, items.clone(), GetBlock::default())?,
// wallet
"z_listunifiedreceivers" => default_property(
type_,
items.clone(),
get_block_template_rpcs::types::unified_address::Response::default(),
)?,
// control
"getinfo" => default_property(type_, items.clone(), GetInfo::default())?,
// transaction
"sendrawtransaction" => {
props.insert(
"result".to_string(),
Property {
type_,
items,
default: serde_json::to_string(&SentTransactionHash::default())?,
},
);
props
default_property(type_, items.clone(), SentTransactionHash::default())?
}
"getrawtransaction" => {
props.insert(
"result".to_string(),
Property {
type_,
items,
default: serde_json::to_string(&GetRawTransaction::default())?,
},
);
props
}
"getnetworksolps" => {
props.insert(
"result".to_string(),
Property {
type_,
items,
default: serde_json::to_string(&u64::default())?,
},
);
props
}
"getnetworkhashps" => {
props.insert(
"result".to_string(),
Property {
type_,
items,
default: serde_json::to_string(&u64::default())?,
},
);
props
}
"submitblock" => {
props.insert(
"result".to_string(),
Property {
type_,
items,
default: serde_json::to_string(
&get_block_template_rpcs::types::submit_block::Response::default(),
)?,
},
);
props
}
"z_listunifiedreceivers" => {
props.insert(
"result".to_string(),
Property {
type_,
items,
default: serde_json::to_string(
&get_block_template_rpcs::types::unified_address::Response::default(),
)?,
},
);
props
}
_ => {
props.insert(
"result".to_string(),
Property {
type_,
items: None,
default: "{}".to_string(),
},
);
props
default_property(type_, items.clone(), GetRawTransaction::default())?
}
// default
_ => Property {
type_: type_.to_string(),
items: None,
default: "{}".to_string(),
},
};
Ok(properties)
props.insert("result".to_string(), default_result);
Ok(props)
}