Skip to content

Commit

Permalink
refactor: Update rpc request and method
Browse files Browse the repository at this point in the history
  • Loading branch information
duanyytop committed Sep 29, 2022
1 parent 5fe3fc4 commit 9eb7870
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 41 deletions.
50 changes: 13 additions & 37 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -18,19 +18,10 @@ pub async fn register_rpc(params: Params, db: &RocksDB) -> Result<Value, Error>
.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))
}

Expand All @@ -41,37 +32,22 @@ pub async fn check_registered_rpc(params: Params) -> Result<Value, Error> {
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<Value, Error> {
pub async fn update_ccid_rpc(db: &RocksDB) -> Result<Value, Error> {
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)
.await
.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))
}
6 changes: 2 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
29 changes: 29 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -48,6 +49,34 @@ pub fn parse_bytes_n<const N: usize>(value: String) -> Result<[u8; N], Error> {
Ok(parse_n(vec))
}

pub trait Inserter {
fn insert_hex(&mut self, k: &str, v: &[u8]) -> Option<Value>;
fn insert_u64(&mut self, k: &str, v: u64) -> Option<Value>;
fn insert_str(&mut self, k: &str, v: String) -> Option<Value>;
fn insert_bool(&mut self, k: &str, v: bool) -> Option<Value>;
}

impl Inserter for Map<String, Value> {
fn insert_hex(&mut self, k: &str, v: &[u8]) -> Option<Value> {
self.insert(
k.to_string(),
Value::String(format!("0x{}", hex::encode(v))),
)
}

fn insert_u64(&mut self, k: &str, v: u64) -> Option<Value> {
self.insert(k.to_string(), Value::Number(Number::from(v)))
}

fn insert_str(&mut self, k: &str, v: String) -> Option<Value> {
self.insert(k.to_string(), Value::String(v))
}

fn insert_bool(&mut self, k: &str, v: bool) -> Option<Value> {
self.insert(k.to_string(), Value::Bool(v))
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 9eb7870

Please sign in to comment.