Skip to content

Commit

Permalink
fix query bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Spark committed Mar 17, 2023
1 parent ac2a4ab commit a18eab6
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 52 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "btcdomain_resolver"
version = "0.1.2"
version = "0.1.3"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
2 changes: 1 addition & 1 deletion install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ docker start ${image_name}
export RUST_LOG=info
export database=mysql://root:$pwd@localhost:$port/domain_inscription_data

wget https://github.com/btcdomain/btcdomain_resolver/releases/download/v0.1.2/btcdomain_resolver
wget https://github.com/btcdomain/btcdomain_resolver/releases/download/v0.1.3/btcdomain_resolver

chmod +x btcdomain_resolver

Expand Down
3 changes: 3 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ use std::env;
use std::time::{SystemTime, UNIX_EPOCH};

pub const START_INSCRIPTION_NUMBER: u64 = 325000;
pub const SUCCESS: i32 = 0i32;
pub const ERROR_1: i32 = 1i32;
pub const ERROR_2: i32 = 2i32;

pub fn get_database_url() -> String{
get_env_str( "database", "None")
Expand Down
88 changes: 48 additions & 40 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,13 @@ async fn resolve_domain(Path(domain): Path<String>) -> Response {
let query_result = query_by_domain(&domain);
let mut resp_data = Vec::new();
for info in query_result.iter() {
let check = check_inscription(info.inscribe_num);
let (check, code) = check_inscription(info.inscribe_num);
if check {
resp_data.push(info);
}else {
let _ = delete_from_id(info.id);
if code == ERROR_1 {
let _ = delete_from_id(info.id);
}
}
}
let resp = Json(InscribeResponse {
Expand All @@ -73,11 +75,13 @@ async fn resolve_address(Path(address): Path<String>) -> Response {
let query_result = query_by_address(&address);
let mut resp_data = Vec::new();
for info in query_result.iter() {
let check = check_inscription(info.inscribe_num);
let (check, code) = check_inscription(info.inscribe_num);
if check {
resp_data.push(info);
}else {
let _ = delete_from_id(info.id);
if code == ERROR_1 {
let _ = delete_from_id(info.id);
}
}
}
let resp = Json(InscribeResponse {
Expand All @@ -88,46 +92,50 @@ async fn resolve_address(Path(address): Path<String>) -> Response {
resp.into_response()
}

fn check_inscription(number: u64) -> bool {
let inscribe_result = get_inscribe_by_number(number);
if inscribe_result.is_some() {
let content = inscribe_result.unwrap();
let content_data = content.content;
let length = content_data.len();
if length > 350 && length < 500 {
let format_data = serde_json::from_slice(&content_data);
if format_data.is_ok() {
let inscribe_data: InscribeData = format_data.unwrap();
info!("inscribe data: {:?}", inscribe_data);

let domain_name = inscribe_data.name;
let expire_date = inscribe_data.expire_date;
let now_date = get_now_time();
if expire_date < now_date {
warn!("domain: {}, is expired, now: {}, expire_time: {}", domain_name, now_date, expire_date);
return false;
}
fn check_inscription(number: u64) -> (bool, i32) {
let (inscribe_result, code) = get_inscribe_by_number(number);
if code == SUCCESS {
if inscribe_result.is_some() {
let content = inscribe_result.unwrap();
let content_data = content.content;
let length = content_data.len();
if length > 350 && length < 500 {
let format_data = serde_json::from_slice(&content_data);
if format_data.is_ok() {
let inscribe_data: InscribeData = format_data.unwrap();
info!("inscribe data: {:?}", inscribe_data);

let domain_name = inscribe_data.name;
let expire_date = inscribe_data.expire_date;
let now_date = get_now_time();
if expire_date < now_date {
warn!("domain: {}, is expired, now: {}, expire_time: {}", domain_name, now_date, expire_date);
return (false, ERROR_1);
}

let sign_info = InscribeSignData{
name: domain_name.clone(),
first_owner: inscribe_data.first_owner,
create_date: inscribe_data.create_date,
register_date: inscribe_data.register_date,
expire_date: inscribe_data.expire_date
};
let sign_data = serde_json::to_vec(&sign_info).unwrap();
if ecdsa::verify(&sign_data, &inscribe_data.sig) {
info!("ecds signature verify success");
return true;
let sign_info = InscribeSignData{
name: domain_name.clone(),
first_owner: inscribe_data.first_owner,
create_date: inscribe_data.create_date,
register_date: inscribe_data.register_date,
expire_date: inscribe_data.expire_date
};
let sign_data = serde_json::to_vec(&sign_info).unwrap();
if ecdsa::verify(&sign_data, &inscribe_data.sig) {
info!("ecds signature verify success");
return (true, SUCCESS);
}else {
warn!("ecds signature verify failed");
return (false, ERROR_1);
}

}else {
warn!("ecds signature verify failed");
return false;
return (false, ERROR_1);
}

}else {
return false;
}
}
}else {
return (false, code)
}
return false;
return (false, code);
}
16 changes: 12 additions & 4 deletions src/sched.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const FATAL_RCVTSK: &str = "error receiving task";

lazy_static::lazy_static! {
pub static ref SYNC_DATA: Arc<RwLock<VecDeque<String>>> = Arc::new(RwLock::new(VecDeque::new()));
pub static ref UPDATE_DATA: Arc<RwLock<VecDeque<String>>> = Arc::new(RwLock::new(VecDeque::new()));
pub static ref CUR_NUMBER: Arc<RwLock<VecDeque<u64>>> = Arc::new(RwLock::new(VecDeque::new()));
}

pub async fn sched_work() {
Expand Down Expand Up @@ -80,12 +80,19 @@ fn sync_data_task_inner() {
if &(*SYNC_DATA).read().unwrap().len() == &0 {
let _ =&(*SYNC_DATA).write().unwrap().push_back(String::from("sync data"));
let lastest = query_lastest_number();
let mut max_number = std::cmp::max(lastest, START_INSCRIPTION_NUMBER);
let mut max_number = if &(*CUR_NUMBER).read().unwrap().len() == &0 {
std::cmp::max(lastest, START_INSCRIPTION_NUMBER)
}else {
let cur_number = &(*CUR_NUMBER).write().unwrap().pop_front().unwrap();
info!("cur_number: {}, lastest: {}", cur_number, lastest);
std::cmp::max(*cur_number, lastest)
};

let mut break_count = 0;
loop {
max_number += 1;
info!("query number: {}", max_number);
let inscribe_result = get_inscribe_by_number(max_number);
let (inscribe_result, _) = get_inscribe_by_number(max_number);
// info!("inscribe_result: {:?}", inscribe_result);
if inscribe_result.is_some() {
let content = inscribe_result.unwrap();
Expand Down Expand Up @@ -154,6 +161,7 @@ fn sync_data_task_inner() {
}

}
let _ = &(*CUR_NUMBER).write().unwrap().push_back(max_number);
let _ = &(*SYNC_DATA).write().unwrap().pop_front().unwrap();
}else {
info!("Syncing data!");
Expand Down Expand Up @@ -218,7 +226,7 @@ fn update_task_inner() {
let _ =&(*SYNC_DATA).write().unwrap().push_back(String::from("update"));
let all_domains = query_all();
for info in all_domains.iter() {
let inscribe_result = get_inscribe_by_number(info.inscribe_num);
let (inscribe_result, _) = get_inscribe_by_number(info.inscribe_num);
if inscribe_result.is_some() {
let content = inscribe_result.unwrap();
let content_data = content.content;
Expand Down
10 changes: 5 additions & 5 deletions src/service.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
use std::process::Command;
use log::{warn};
use crate::{entity::InscribeContent};
use crate::{entity::InscribeContent, ERROR_1, ERROR_2, SUCCESS};

pub fn get_inscribe_by_number(number: u64) -> Option<InscribeContent> {
pub fn get_inscribe_by_number(number: u64) -> (Option<InscribeContent>, i32) {
let output = Command::new("ord")
.arg("find-number")
.arg(number.to_string()).output().unwrap();

if output.status.success() {
let resp = serde_json::from_slice(&output.stdout);
if resp.is_ok() {
Some(resp.unwrap())
(Some(resp.unwrap()), SUCCESS)
}else {
None
(None, ERROR_1)
}

}else {
warn!("get_inscribe_by_number failed number: {}, output: {:?}", number, String::from_utf8(output.stderr));
None
(None, ERROR_2)
}
}

0 comments on commit a18eab6

Please sign in to comment.