Skip to content

Commit

Permalink
Fixing shit
Browse files Browse the repository at this point in the history
  • Loading branch information
mjovanc committed May 20, 2023
1 parent d7887f3 commit 0e7e341
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 54 deletions.
14 changes: 1 addition & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ mod syncers;
mod types;

use env_logger::Env;
use rocket::tokio::runtime::Builder;

#[macro_use]
extern crate rocket;
Expand All @@ -22,18 +21,7 @@ fn internal_error() -> &'static str {
#[launch]
fn rocket() -> _ {
// initialize logger
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();

// initialize tokio runtime
let tokio_runtime = Builder::new_multi_thread()
.enable_all()
.build()
.expect("Failed to build Tokio runtime");

// spawn the syncers
tokio_runtime.spawn(async {
syncers::hugin::hugin_syncer().await;
});
// env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();

// launch the rocket server
rocket::build()
Expand Down
81 changes: 41 additions & 40 deletions src/syncers/hugin.rs
Original file line number Diff line number Diff line change
@@ -1,76 +1,77 @@
use crate::types::PoolChangesLiteRequest;
use log::{info, warn};
use reqwest::header::HeaderMap;
use reqwest::header::CONTENT_TYPE;
use rocket::http::hyper::HeaderValue;
use std::{thread, time};
use rocket::tokio::task::{self, spawn};
use serde_json::json;
use std::{sync::Arc, thread, time};

//async fn get_pool_changes_lite() -> String {}
async fn get_info() -> Result<(), reqwest::Error> {
let client = reqwest::Client::new();
let response = client
.get("http://privacymine.net:11898/info")
.send()
.await?;

println!("response status: {:?}", response.status());

async fn get_pool_changes_lite(body: &str) -> String {
Ok(())
}

async fn get_pool_changes_lite() {
let known = vec![""];
let body = serde_json::to_string(&json!({ "knownTxsIds": known })).unwrap();
let body = body.to_string();

// set headers
let mut headers = HeaderMap::new();
headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
headers.insert("Content-Type", "application/json".parse().unwrap());

// set node and request endpoint
let ip = String::from("privacymine.net");
let ip = String::from("blocksum.org");
let port = String::from("11898");
let url = format!("http://{}:{}/{}", ip, port, "get_pool_changes_lite");
let endpoint = String::from("get_pool_changes_lite");
let url = format!("http://{}:{}/{}", ip, port, endpoint);

// make the post request
let client = reqwest::Client::new();
let request = client
.post(&url)
.headers(headers.clone())
.body(body.clone());

let response = request.send().await;
let response_text = response.unwrap().text().await;

match response_text {
Ok(text) => {
info!("Response text: {}", text);
text
}
Err(e) => {
warn!("Error: {}", e);
String::from("")
}
}
let response = client
.post(url)
.headers(headers)
.body(body)
.send()
.await
.expect("Failed to get response")
.text()
.await
.expect("Failed to get payload");

println!("response: {:?}", response);
}

pub async fn hugin_syncer() {
info!(target: "sync_events", "Starting Hugin syncer...");

let delay = time::Duration::from_secs(2);
let delay = time::Duration::from_secs(5);
let known = vec!["37669a52df20daf42e74757d05547f9b151ec0383819b377be1c7df59d43e2f8"];
let pool_changes_lite_req = PoolChangesLiteRequest {
known_txs_ids: &known,
knownTxsIds: &known,
};

// run the hugin syncer loop
loop {
info!("Hugin syncer loop...");

// convert the PoolChangesLiteRequest struct to a json string
let json_body = serde_json::to_string(&pool_changes_lite_req);
// let json_body = serde_json::to_string(&pool_changes_lite_req);

println!("json_body: {:?}", json_body);
// println!("json_body: {:?}", body);

match json_body {
Ok(body) => {
let response = get_pool_changes_lite(&body).await;
info!("Response: {}", response);

// parse the deserialize the response into a PoolChangesLiteResponse
}
Err(e) => {
warn!("Error: {}", e);
}
if let Err(e) = get_info().await {
warn!("Error: {:?}", e);
}

thread::sleep(delay);
}

// get_info().await;
}
2 changes: 1 addition & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize};
#[derive(Serialize)]
#[serde(crate = "rocket::serde")]
pub struct PoolChangesLiteRequest<'r> {
pub known_txs_ids: &'r Vec<&'r str>,
pub knownTxsIds: &'r Vec<&'r str>,
}

#[derive(Deserialize)]
Expand Down

0 comments on commit 0e7e341

Please sign in to comment.