From f5043cf50dee14bb1281f49a4997f3cd0c369c41 Mon Sep 17 00:00:00 2001 From: Mike Dilger Date: Fri, 22 Mar 2024 09:39:25 +1300 Subject: [PATCH] dump approvals functions and binary --- chorus-bin/src/bin/chorus_dump_approvals.rs | 50 +++++++++++++++++++++ chorus-lib/src/store/mod.rs | 24 ++++++++++ docs/DEPLOYING.md | 2 + 3 files changed, 76 insertions(+) create mode 100644 chorus-bin/src/bin/chorus_dump_approvals.rs diff --git a/chorus-bin/src/bin/chorus_dump_approvals.rs b/chorus-bin/src/bin/chorus_dump_approvals.rs new file mode 100644 index 0000000..87d70b2 --- /dev/null +++ b/chorus-bin/src/bin/chorus_dump_approvals.rs @@ -0,0 +1,50 @@ +use chorus_lib::config::{Config, FriendlyConfig}; +use chorus_lib::error::Error; +use chorus_lib::store::Store; +use std::env; +use std::fs::OpenOptions; +use std::io::Read; + +fn main() -> Result<(), Error> { + // Get args (config path) + let mut args = env::args(); + if args.len() <= 1 { + panic!("USAGE: chorus_moderate "); + } + let _ = args.next(); // ignore program name + let config_path = args.next().unwrap(); + + // Read config file + let mut file = OpenOptions::new().read(true).open(config_path)?; + let mut contents = String::new(); + file.read_to_string(&mut contents)?; + let friendly_config: FriendlyConfig = toml::from_str(&contents)?; + let mut config: Config = friendly_config.into_config()?; + + env_logger::Builder::new() + .filter_level(config.library_log_level) + .filter(Some("Server"), config.server_log_level) + .filter(Some("Client"), config.client_log_level) + .format_target(true) + .format_module_path(false) + .format_timestamp_millis() + .init(); + + log::debug!(target: "Server", "Loaded config file."); + + // Force allow of scraping (this program is a scraper) + config.allow_scraping = true; + + // Setup store + let store = Store::new(&config)?; + + for (id, approved) in store.dump_event_approvals()? { + println!("ID {} = {}", id, approved); + } + + for (pubkey, approved) in store.dump_pubkey_approvals()? { + println!("PUBKEY {} = {}", pubkey, approved); + } + + Ok(()) +} diff --git a/chorus-lib/src/store/mod.rs b/chorus-lib/src/store/mod.rs index e491b36..2c86c36 100644 --- a/chorus-lib/src/store/mod.rs +++ b/chorus-lib/src/store/mod.rs @@ -981,6 +981,18 @@ impl Store { .map(|u| u != 0)) } + pub fn dump_event_approvals(&self) -> Result, Error> { + let mut output: Vec<(Id, bool)> = Vec::new(); + let txn = self.env.read_txn()?; + for i in self.approved_events.iter(&txn)? { + let (key, val) = i?; + let id = Id(key.try_into().unwrap()); + let approval: bool = val != 0; + output.push((id, approval)); + } + Ok(output) + } + pub fn mark_pubkey_approval(&self, pubkey: Pubkey, approval: bool) -> Result<(), Error> { let mut txn = self.env.write_txn()?; self.approved_pubkeys @@ -1005,6 +1017,18 @@ impl Store { .map(|u| u != 0)) } + pub fn dump_pubkey_approvals(&self) -> Result, Error> { + let mut output: Vec<(Pubkey, bool)> = Vec::new(); + let txn = self.env.read_txn()?; + for i in self.approved_pubkeys.iter(&txn)? { + let (key, val) = i?; + let pubkey = Pubkey(key.try_into().unwrap()); + let approval: bool = val != 0; + output.push((pubkey, approval)); + } + Ok(output) + } + fn key_ci_index(created_at: Time, id: Id) -> Vec { let mut key: Vec = Vec::with_capacity(std::mem::size_of::