Skip to content

Commit

Permalink
chore: format the code
Browse files Browse the repository at this point in the history
  • Loading branch information
ppoliani committed Jan 28, 2024
1 parent da4ee6e commit 1f15224
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 72 deletions.
152 changes: 81 additions & 71 deletions src/address_verifier.rs
Original file line number Diff line number Diff line change
@@ -1,89 +1,99 @@
use std::{
collections::HashMap, sync::Arc, time::{Duration, UNIX_EPOCH}
};
use log::error;
use tokio::{
spawn, sync::RwLock, time::interval,
use std::{
collections::HashMap,
sync::Arc,
time::{Duration, UNIX_EPOCH},
};
use tokio::{spawn, sync::RwLock, time::interval};
use xml::reader::{EventReader, XmlEvent};

pub struct AddressVerifier {
sanctioned_addresses: Arc<RwLock<HashMap<String, bool>>>,
last_update: Arc<RwLock<u64>>,
sanctioned_addresses: Arc<RwLock<HashMap<String, bool>>>,
last_update: Arc<RwLock<u64>>,
}

impl AddressVerifier {
const BTC_ID: &'static str = "344";
const BTC_ID: &'static str = "344";

pub fn new() -> Self {
Self {
sanctioned_addresses: Arc::new(RwLock::new(HashMap::new())),
last_update: Arc::new(RwLock::new(Self::now())),
}
}

pub fn new() -> Self {
Self {
sanctioned_addresses: Arc::new(RwLock::new(HashMap::new())),
last_update: Arc::new(RwLock::new(Self::now())),
fn now() -> u64 {
UNIX_EPOCH.elapsed().unwrap().as_secs()
}
}

fn now() -> u64 {
UNIX_EPOCH.elapsed().unwrap().as_secs()
}
/// Periodically fetces the latest list from https://www.treasury.gov/ofac/downloads/sanctions/1.0/sdn_advanced.xml
/// and updates the list
pub async fn start(&self) {
let sanctioned_addresses = Arc::clone(&self.sanctioned_addresses);
let last_update = Arc::clone(&self.last_update);

/// Periodically fetces the latest list from https://www.treasury.gov/ofac/downloads/sanctions/1.0/sdn_advanced.xml
/// and updates the list
pub async fn start(&self) {
let sanctioned_addresses = Arc::clone(&self.sanctioned_addresses);
let last_update = Arc::clone(&self.last_update);
spawn(async move {
let mut interval = interval(Duration::from_secs(600));

spawn(async move {
let mut interval = interval(Duration::from_secs(600));

loop {
let mut sanctioned_addresses = sanctioned_addresses.write().await;
interval.tick().await;
// TODO:: read the first few bytes from the remote XML file and extract the last update date.
// If there is no fresh data we can skip the parsing of XML which is slow.
let mut _last_update = last_update.write().await;
loop {
let mut sanctioned_addresses = sanctioned_addresses.write().await;
interval.tick().await;
// TODO:: read the first few bytes from the remote XML file and extract the last update date.
// If there is no fresh data we can skip the parsing of XML which is slow.
let mut _last_update = last_update.write().await;

let Ok(res) = reqwest::get("https://www.treasury.gov/ofac/downloads/sanctions/1.0/sdn_advanced.xml").await else {
error!("couldn't fetch OFAC list");
continue;
};
let Ok(xml) = res.text().await else {
error!("couldn't parse OFAC list");
continue;
};
let parser: EventReader<&[u8]> = EventReader::new(xml.as_bytes());
let mut inside_feature_elem = false;
let mut inside_final_elem = false;
let Ok(res) = reqwest::get(
"https://www.treasury.gov/ofac/downloads/sanctions/1.0/sdn_advanced.xml",
)
.await
else {
error!("couldn't fetch OFAC list");
continue;
};
let Ok(xml) = res.text().await else {
error!("couldn't parse OFAC list");
continue;
};
let parser: EventReader<&[u8]> = EventReader::new(xml.as_bytes());
let mut inside_feature_elem = false;
let mut inside_final_elem = false;

for e in parser {
match e {
Ok(XmlEvent::StartElement { name, attributes, .. }) => {
if name.local_name == "Feature" {
if attributes.iter().any(|a| a.name.local_name == "FeatureTypeID" && a.value == Self::BTC_ID) {
inside_feature_elem = true;
for e in parser {
match e {
Ok(XmlEvent::StartElement {
name, attributes, ..
}) => {
if name.local_name == "Feature" {
if attributes.iter().any(|a| {
a.name.local_name == "FeatureTypeID" && a.value == Self::BTC_ID
}) {
inside_feature_elem = true;
}
} else if name.local_name == "VersionDetail" && inside_feature_elem {
inside_final_elem = true;
}
}
Ok(XmlEvent::Characters(value)) => {
if inside_final_elem {
sanctioned_addresses.insert(value, true);
}
}
Ok(XmlEvent::EndElement { name, .. }) => {
if name.local_name == "VersionDetail" && inside_feature_elem {
inside_feature_elem = false;
inside_final_elem = false;
}
}
Err(e) => {
error!("Error parsing xml: {e}");
break;
}
_ => {}
}
}
} else if name.local_name == "VersionDetail" && inside_feature_elem {
inside_final_elem = true;
}
}
Ok(XmlEvent::Characters(value)) => {
if inside_final_elem {
sanctioned_addresses.insert(value, true);
}
}
Ok(XmlEvent::EndElement { name, .. }) => {
if name.local_name == "VersionDetail" && inside_feature_elem {
inside_feature_elem = false;
inside_final_elem = false;
}
}
Err(e) => {
error!("Error parsing xml: {e}");
break;
}
_ => {}
}
}
}
}).await.unwrap();
}
})
.await
.unwrap();
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
use anyhow::Context;
use secp256k1::hashes::Hash;

pub mod address_verifier;
pub mod committee;
pub mod constants;
pub mod frost;
pub mod json_rpc_stuff;
pub mod plonk;
pub mod snarkjs;
pub mod srs;
pub mod address_verifier;

/// 1. Alice signs a transaction to deploy a smart contract.
pub mod alice_sign_tx;
Expand Down

0 comments on commit 1f15224

Please sign in to comment.