diff --git a/src/api.rs b/src/api.rs index 78613f1..7cf0826 100644 --- a/src/api.rs +++ b/src/api.rs @@ -4,8 +4,8 @@ use crate::db::{ }; use crate::smt::db::db::RocksDB; use crate::smt::entry::generate_registry_smt; -use crate::utils::parse_request_param; -use jsonrpc_http_server::jsonrpc_core::serde_json::{Map, Number}; +use crate::utils::{parse_request_param, Inserter}; +use jsonrpc_http_server::jsonrpc_core::serde_json::Map; use jsonrpc_http_server::jsonrpc_core::{Error, Params, Value}; use log::info; @@ -18,19 +18,10 @@ pub async fn register_rpc(params: Params, db: &RocksDB) -> Result .map_err(|err| err.into())?; let block_number = get_syncer_tip_block_number().map_err(|err| err.into())?; let mut response = Map::new(); - response.insert("smt_root_hash".to_string(), Value::String(root_hash)); - response.insert( - "registry_smt_entry".to_string(), - Value::String(registry_entry), - ); - response.insert( - "output_account_num".to_string(), - Value::Number(Number::from(output_account_num)), - ); - response.insert( - "block_number".to_string(), - Value::Number(Number::from(block_number)), - ); + response.insert_str("smt_root_hash", root_hash); + response.insert_str("registry_smt_entry", registry_entry); + response.insert_u64("output_account_num", output_account_num); + response.insert_u64("block_number", block_number); Ok(Value::Object(response)) } @@ -41,18 +32,12 @@ pub async fn check_registered_rpc(params: Params) -> Result { let (registry_state, block_height) = check_lock_hashes_registered(lock_hashes).map_err(|err| err.into())?; let mut response = Map::new(); - response.insert( - "registered".to_string(), - Value::Bool(registry_state != RegistryState::Unregister), - ); - response.insert( - "block_number".to_string(), - Value::Number(Number::from(block_height)), - ); + response.insert_bool("registered", registry_state != RegistryState::Unregister); + response.insert_u64("block_number", block_height); Ok(Value::Object(response)) } -pub async fn update_registered_ccid_rpc(db: &RocksDB) -> Result { +pub async fn update_ccid_rpc(db: &RocksDB) -> Result { info!("Update registered ccid request"); let lock_hashes = get_50_registered_lock_hashes().map_err(|err| err.into())?; let (root_hash, registry_entry, output_account_num) = generate_registry_smt(db, lock_hashes) @@ -60,18 +45,9 @@ pub async fn update_registered_ccid_rpc(db: &RocksDB) -> Result { .map_err(|err| err.into())?; let block_number = get_syncer_tip_block_number().map_err(|err| err.into())?; let mut response = Map::new(); - response.insert("smt_root_hash".to_string(), Value::String(root_hash)); - response.insert( - "registry_smt_entry".to_string(), - Value::String(registry_entry), - ); - response.insert( - "output_account_num".to_string(), - Value::Number(Number::from(output_account_num)), - ); - response.insert( - "block_number".to_string(), - Value::Number(Number::from(block_number)), - ); + response.insert_str("smt_root_hash", root_hash); + response.insert_str("registry_smt_entry", registry_entry); + response.insert_u64("output_account_num", output_account_num); + response.insert_u64("block_number", block_number); Ok(Value::Object(response)) } diff --git a/src/main.rs b/src/main.rs index d56d034..cc802f2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,7 @@ extern crate diesel; extern crate dotenv; -use crate::api::{check_registered_rpc, register_rpc, update_registered_ccid_rpc}; +use crate::api::{check_registered_rpc, register_rpc, update_ccid_rpc}; use crate::db::{init_connection_pool, SqlConnectionPool}; use crate::smt::db::db::RocksDB; use dotenv::dotenv; @@ -32,9 +32,7 @@ fn main() { let mut io = IoHandler::default(); io.add_method("register_cota_cells", |req| register_rpc(req, &DB)); - io.add_method("update_registered_ccid", |_req| { - update_registered_ccid_rpc(&DB) - }); + io.add_method("update_registered_ccid", |_req| update_ccid_rpc(&DB)); io.add_method("check_registered_lock_hashes", check_registered_rpc); let server = ServerBuilder::new(io) diff --git a/src/utils.rs b/src/utils.rs index 28ffdda..efc0e83 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,5 +1,6 @@ use crate::error::Error; use hex; +use jsonrpc_http_server::jsonrpc_core::serde_json::{Map, Number}; use jsonrpc_http_server::jsonrpc_core::Value; use std::convert::TryInto; @@ -48,6 +49,34 @@ pub fn parse_bytes_n(value: String) -> Result<[u8; N], Error> { Ok(parse_n(vec)) } +pub trait Inserter { + fn insert_hex(&mut self, k: &str, v: &[u8]) -> Option; + fn insert_u64(&mut self, k: &str, v: u64) -> Option; + fn insert_str(&mut self, k: &str, v: String) -> Option; + fn insert_bool(&mut self, k: &str, v: bool) -> Option; +} + +impl Inserter for Map { + fn insert_hex(&mut self, k: &str, v: &[u8]) -> Option { + self.insert( + k.to_string(), + Value::String(format!("0x{}", hex::encode(v))), + ) + } + + fn insert_u64(&mut self, k: &str, v: u64) -> Option { + self.insert(k.to_string(), Value::Number(Number::from(v))) + } + + fn insert_str(&mut self, k: &str, v: String) -> Option { + self.insert(k.to_string(), Value::String(v)) + } + + fn insert_bool(&mut self, k: &str, v: bool) -> Option { + self.insert(k.to_string(), Value::Bool(v)) + } +} + #[cfg(test)] mod tests { use super::*;