Skip to content

Commit

Permalink
Merge branch 'scsibug:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
mroxso authored Apr 3, 2024
2 parents 9c71e87 + b04ab76 commit 0d97cac
Show file tree
Hide file tree
Showing 12 changed files with 456 additions and 489 deletions.
855 changes: 411 additions & 444 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/bin/bulkloader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ fn write_event(tx: &Transaction, e: Event) -> Result<usize> {
let event_id = tx.last_insert_rowid();
// look at each event, and each tag, creating new tag entries if appropriate.
for t in e.tags.iter().filter(|x| x.len() > 1) {
let tagname = t.get(0).unwrap();
let tagname = t.first().unwrap();
let tagnamechar_opt = single_char_tagname(tagname);
if tagnamechar_opt.is_none() {
continue;
Expand Down
4 changes: 2 additions & 2 deletions src/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,10 @@ impl ClientConn {
let mut relay: Option<&str> = None;

for tag in &event.tags {
if tag.len() == 2 && tag.get(0) == Some(&"challenge".into()) {
if tag.len() == 2 && tag.first() == Some(&"challenge".into()) {
challenge = tag.get(1).map(|x| x.as_str());
}
if tag.len() == 2 && tag.get(0) == Some(&"relay".into()) {
if tag.len() == 2 && tag.first() == Some(&"relay".into()) {
relay = tag.get(1).map(|x| x.as_str());
}
}
Expand Down
16 changes: 8 additions & 8 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,11 @@ impl Event {
.tags
.iter()
.filter(|x| !x.is_empty())
.filter(|x| x.get(0).unwrap() == "expiration")
.filter(|x| x.first().unwrap() == "expiration")
.map(|x| x.get(1).unwrap_or(&default))
.take(1)
.collect();
let val_first = dvals.get(0);
let val_first = dvals.first();
val_first.and_then(|t| t.parse::<u64>().ok())
}

Expand Down Expand Up @@ -192,11 +192,11 @@ impl Event {
.tags
.iter()
.filter(|x| !x.is_empty())
.filter(|x| x.get(0).unwrap() == "d")
.filter(|x| x.first().unwrap() == "d")
.map(|x| x.get(1).unwrap_or(&default))
.take(1)
.collect();
let dval_first = dvals.get(0);
let dval_first = dvals.first();
match dval_first {
Some(_) => dval_first.map(|x| x.to_string()),
None => Some(default),
Expand Down Expand Up @@ -232,7 +232,7 @@ impl Event {
.tags
.iter()
.filter(|x| x.len() == 4)
.filter(|x| x.get(0).unwrap() == "delegation")
.filter(|x| x.first().unwrap() == "delegation")
.take(1)
.next()?
.clone(); // get first tag
Expand Down Expand Up @@ -277,15 +277,15 @@ impl Event {
let mut idx: HashMap<char, HashSet<String>> = HashMap::new();
// iterate over tags that have at least 2 elements
for t in self.tags.iter().filter(|x| x.len() > 1) {
let tagname = t.get(0).unwrap();
let tagname = t.first().unwrap();
let tagnamechar_opt = single_char_tagname(tagname);
if tagnamechar_opt.is_none() {
continue;
}
let tagnamechar = tagnamechar_opt.unwrap();
let tagval = t.get(1).unwrap();
// ensure a vector exists for this tag
idx.entry(tagnamechar).or_insert_with(HashSet::new);
idx.entry(tagnamechar).or_default();
// get the tag vec and insert entry
let idx_tag_vec = idx.get_mut(&tagnamechar).expect("could not get tag vector");
idx_tag_vec.insert(tagval.clone());
Expand All @@ -310,7 +310,7 @@ impl Event {
self.tags
.iter()
.filter(|x| x.len() > 1)
.filter(|x| x.get(0).unwrap() == tag_name)
.filter(|x| x.first().unwrap() == tag_name)
.map(|x| x.get(1).unwrap().clone())
.collect()
}
Expand Down
4 changes: 2 additions & 2 deletions src/repo/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -812,13 +812,13 @@ fn query_from_filter(f: &ReqFilter) -> Option<QueryBuilder<Postgres>> {
.push_bind(key.to_string())
.push(" AND (");

let has_plain_values = val.iter().any(|v| !is_lower_hex(v));
let has_plain_values = val.iter().any(|v| (v.len() % 2 != 0 || !is_lower_hex(v)));
let has_hex_values = val.iter().any(|v| v.len() % 2 == 0 && is_lower_hex(v));
if has_plain_values {
query.push("value in (");
// plain value match first
let mut tag_query = query.separated(", ");
for v in val.iter().filter(|v| !is_lower_hex(v)) {
for v in val.iter().filter(|v| v.len() % 2 != 0 || !is_lower_hex(v)) {
tag_query.push_bind(v.as_bytes());
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/repo/postgres_migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ CREATE INDEX tag_value_hex_idx ON tag USING btree (value_hex);
let event: Event = serde_json::from_str(&String::from_utf8(event_bytes).unwrap())?;

for t in event.tags.iter().filter(|x| x.len() > 1) {
let tagname = t.get(0).unwrap();
let tagname = t.first().unwrap();
let tagnamechar_opt = single_char_tagname(tagname);
if tagnamechar_opt.is_none() {
continue;
Expand Down
4 changes: 3 additions & 1 deletion src/repo/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -993,7 +993,8 @@ fn query_from_filter(f: &ReqFilter) -> (String, Vec<Box<dyn ToSql>>, Option<Stri
let mut auth_searches: Vec<String> = vec![];
for auth in authvec {
auth_searches.push("author=?".to_owned());
params.push(Box::new(auth.clone()));
let auth_bin = hex::decode(auth).ok();
params.push(Box::new(auth_bin));
}
if !authvec.is_empty() {
let auth_clause = format!("({})", auth_searches.join(" OR "));
Expand Down Expand Up @@ -1281,6 +1282,7 @@ pub async fn db_checkpoint_task(
}

#[derive(Debug)]
#[allow(dead_code)]
enum SqliteStatus {
Ok,
Busy,
Expand Down
8 changes: 4 additions & 4 deletions src/repo/sqlite_migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ pub fn rebuild_tags(conn: &mut PooledConnection) -> Result<()> {
let event: Event = serde_json::from_str(&event_json)?;
// look at each event, and each tag, creating new tag entries if appropriate.
for t in event.tags.iter().filter(|x| x.len() > 1) {
let tagname = t.get(0).unwrap();
let tagname = t.first().unwrap();
let tagnamechar_opt = single_char_tagname(tagname);
if tagnamechar_opt.is_none() {
continue;
Expand Down Expand Up @@ -325,7 +325,7 @@ pub fn rebuild_tags(conn: &mut PooledConnection) -> Result<()> {
Ok(())
}

//// Migration Scripts
// Migration Scripts

fn mig_1_to_2(conn: &mut PooledConnection) -> Result<usize> {
// only change is adding a hidden column to events.
Expand Down Expand Up @@ -461,7 +461,7 @@ fn mig_5_to_6(conn: &mut PooledConnection) -> Result<usize> {
let event: Event = serde_json::from_str(&event_json)?;
// look at each event, and each tag, creating new tag entries if appropriate.
for t in event.tags.iter().filter(|x| x.len() > 1) {
let tagname = t.get(0).unwrap();
let tagname = t.first().unwrap();
let tagnamechar_opt = single_char_tagname(tagname);
if tagnamechar_opt.is_none() {
continue;
Expand Down Expand Up @@ -749,7 +749,7 @@ CREATE INDEX IF NOT EXISTS tag_covering_index ON tag(name,kind,value,created_at,
let event: Event = serde_json::from_str(&event_json)?;
// look at each event, and each tag, creating new tag entries if appropriate.
for t in event.tags.iter().filter(|x| x.len() > 1) {
let tagname = t.get(0).unwrap();
let tagname = t.first().unwrap();
let tagnamechar_opt = single_char_tagname(tagname);
if tagnamechar_opt.is_none() {
continue;
Expand Down
1 change: 1 addition & 0 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,7 @@ fn get_header_string(header: &str, headers: &HeaderMap) -> Option<String> {
async fn ctrl_c_or_signal(mut shutdown_signal: Receiver<()>) {
let mut term_signal = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())
.expect("could not define signal");
#[allow(clippy::never_loop)]
loop {
tokio::select! {
_ = shutdown_signal.recv() => {
Expand Down
18 changes: 9 additions & 9 deletions src/subscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ mod tests {
let s: Subscription = serde_json::from_str(raw_json)?;
assert_eq!(s.id, "some-id");
assert_eq!(s.filters.len(), 1);
assert_eq!(s.filters.get(0).unwrap().authors, None);
assert_eq!(s.filters.first().unwrap().authors, None);
Ok(())
}

Expand Down Expand Up @@ -425,7 +425,7 @@ mod tests {
let s: Subscription = serde_json::from_str(raw_json)?;
assert_eq!(s.id, "some-id");
assert_eq!(s.filters.len(), 1);
let first_filter = s.filters.get(0).unwrap();
let first_filter = s.filters.first().unwrap();
assert_eq!(
first_filter.authors,
Some(vec!("test-author-id".to_owned()))
Expand Down Expand Up @@ -656,11 +656,11 @@ mod tests {
let s: Subscription = serde_json::from_str(
r##"["REQ","xyz",{"authors":["abc", "bcd"], "since": 10, "until": 20, "limit":100, "#e": ["foo", "bar"], "#d": ["test"]}]"##,
)?;
let f = s.filters.get(0);
let f = s.filters.first();
let serialized = serde_json::to_string(&f)?;
let serialized_wrapped = format!(r##"["REQ", "xyz",{}]"##, serialized);
let parsed: Subscription = serde_json::from_str(&serialized_wrapped)?;
let parsed_filter = parsed.filters.get(0);
let parsed_filter = parsed.filters.first();
if let Some(pf) = parsed_filter {
assert_eq!(pf.since, Some(10));
assert_eq!(pf.until, Some(20));
Expand All @@ -673,11 +673,11 @@ mod tests {

#[test]
fn is_scraper() -> Result<()> {
assert_eq!(true, serde_json::from_str::<Subscription>(r#"["REQ","some-id",{"kinds": [1984],"since": 123,"limit":1}]"#)?.is_scraper());
assert_eq!(true, serde_json::from_str::<Subscription>(r#"["REQ","some-id",{"kinds": [1984]},{"kinds": [1984],"authors":["aaaa"]}]"#)?.is_scraper());
assert_eq!(false, serde_json::from_str::<Subscription>(r#"["REQ","some-id",{"kinds": [1984],"authors":["aaaa"]}]"#)?.is_scraper());
assert_eq!(false, serde_json::from_str::<Subscription>(r#"["REQ","some-id",{"ids": ["aaaa"]}]"#)?.is_scraper());
assert_eq!(false, serde_json::from_str::<Subscription>(r##"["REQ","some-id",{"#p": ["aaaa"],"kinds":[1,4]}]"##)?.is_scraper());
assert!(serde_json::from_str::<Subscription>(r#"["REQ","some-id",{"kinds": [1984],"since": 123,"limit":1}]"#)?.is_scraper());
assert!(serde_json::from_str::<Subscription>(r#"["REQ","some-id",{"kinds": [1984]},{"kinds": [1984],"authors":["aaaa"]}]"#)?.is_scraper());
assert!(!serde_json::from_str::<Subscription>(r#"["REQ","some-id",{"kinds": [1984],"authors":["aaaa"]}]"#)?.is_scraper());
assert!(!serde_json::from_str::<Subscription>(r#"["REQ","some-id",{"ids": ["aaaa"]}]"#)?.is_scraper());
assert!(!serde_json::from_str::<Subscription>(r##"["REQ","some-id",{"#p": ["aaaa"],"kinds":[1,4]}]"##)?.is_scraper());
Ok(())
}
}
5 changes: 1 addition & 4 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,5 @@ fn get_available_port() -> Option<u16> {
}
pub fn port_is_available(port: u16) -> bool {
info!("checking on port {}", port);
match TcpListener::bind(("127.0.0.1", port)) {
Ok(_) => true,
Err(_) => false,
}
TcpListener::bind(("127.0.0.1", port)).is_ok()
}
26 changes: 13 additions & 13 deletions tests/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ mod tests {
let challenge = client_conn.auth_challenge().unwrap();
let event = auth_event(challenge);

let result = client_conn.authenticate(&event, RELAY.into());
let result = client_conn.authenticate(&event, RELAY);

assert!(matches!(result, Ok(())));
assert_eq!(client_conn.auth_challenge(), None);
Expand All @@ -67,7 +67,7 @@ mod tests {
assert_eq!(client_conn.auth_pubkey(), None);

let event = auth_event(&"challenge".into());
let result = client_conn.authenticate(&event, RELAY.into());
let result = client_conn.authenticate(&event, RELAY);

assert!(matches!(result, Err(Error::AuthFailure)));
}
Expand All @@ -87,14 +87,14 @@ mod tests {
let challenge = client_conn.auth_challenge().unwrap().clone();

let event = auth_event(&challenge);
let result = client_conn.authenticate(&event, RELAY.into());
let result = client_conn.authenticate(&event, RELAY);

assert!(matches!(result, Ok(())));
assert_eq!(client_conn.auth_challenge(), None);
assert_eq!(client_conn.auth_pubkey(), Some(&event.pubkey));

let event1 = auth_event(&challenge);
let result1 = client_conn.authenticate(&event1, RELAY.into());
let result1 = client_conn.authenticate(&event1, RELAY);

assert!(matches!(result1, Ok(())));
assert_eq!(client_conn.auth_challenge(), None);
Expand All @@ -118,7 +118,7 @@ mod tests {
let mut event = auth_event(challenge);
event.sig = event.sig.chars().rev().collect::<String>();

let result = client_conn.authenticate(&event, RELAY.into());
let result = client_conn.authenticate(&event, RELAY);

assert!(matches!(result, Err(Error::AuthFailure)));
}
Expand All @@ -138,7 +138,7 @@ mod tests {
let challenge = client_conn.auth_challenge().unwrap();
let event = auth_event_with_kind(challenge, 9999999999999999);

let result = client_conn.authenticate(&event, RELAY.into());
let result = client_conn.authenticate(&event, RELAY);

assert!(matches!(result, Err(Error::AuthFailure)));
}
Expand All @@ -158,7 +158,7 @@ mod tests {
let challenge = client_conn.auth_challenge().unwrap();
let event = auth_event_with_created_at(challenge, unix_time() - 1200); // 20 minutes

let result = client_conn.authenticate(&event, RELAY.into());
let result = client_conn.authenticate(&event, RELAY);

assert!(matches!(result, Err(Error::AuthFailure)));
}
Expand All @@ -178,7 +178,7 @@ mod tests {
let challenge = client_conn.auth_challenge().unwrap();
let event = auth_event_with_created_at(challenge, unix_time() + 1200); // 20 minutes

let result = client_conn.authenticate(&event, RELAY.into());
let result = client_conn.authenticate(&event, RELAY);

assert!(matches!(result, Err(Error::AuthFailure)));
}
Expand All @@ -197,7 +197,7 @@ mod tests {

let event = auth_event_without_tags();

let result = client_conn.authenticate(&event, RELAY.into());
let result = client_conn.authenticate(&event, RELAY);

assert!(matches!(result, Err(Error::AuthFailure)));
}
Expand All @@ -216,7 +216,7 @@ mod tests {

let event = auth_event_without_challenge();

let result = client_conn.authenticate(&event, RELAY.into());
let result = client_conn.authenticate(&event, RELAY);

assert!(matches!(result, Err(Error::AuthFailure)));
}
Expand All @@ -236,7 +236,7 @@ mod tests {
let challenge = client_conn.auth_challenge().unwrap();
let event = auth_event_without_relay(challenge);

let result = client_conn.authenticate(&event, RELAY.into());
let result = client_conn.authenticate(&event, RELAY);

assert!(matches!(result, Err(Error::AuthFailure)));
}
Expand All @@ -255,7 +255,7 @@ mod tests {

let event = auth_event(&"invalid challenge".into());

let result = client_conn.authenticate(&event, RELAY.into());
let result = client_conn.authenticate(&event, RELAY);

assert!(matches!(result, Err(Error::AuthFailure)));
}
Expand All @@ -275,7 +275,7 @@ mod tests {
let challenge = client_conn.auth_challenge().unwrap();
let event = auth_event_with_relay(challenge, &"xyz".into());

let result = client_conn.authenticate(&event, RELAY.into());
let result = client_conn.authenticate(&event, RELAY);

assert!(matches!(result, Err(Error::AuthFailure)));
}
Expand Down

0 comments on commit 0d97cac

Please sign in to comment.