Skip to content

Commit

Permalink
Rename Web of Trust to Friends of Friends:
Browse files Browse the repository at this point in the history
for backwards compatibility:
* database name stays the same
* spam script handles either 'fof' or 'wot' the same
  • Loading branch information
mikedilger committed Sep 23, 2024
1 parent de584d1 commit 5c1834d
Show file tree
Hide file tree
Showing 13 changed files with 92 additions and 90 deletions.
8 changes: 4 additions & 4 deletions filter.example.rhai
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
// muted - if the author is in your mute list
// name - if we have it, the name of the author (or your
// petname), else an empty string
// wot - Web of Trust score. How many people that you
// follow happen to follow the author of the
// event.
// fof - Friends of friends: Among you, the people you
// follow, and the people they follow, how many
// follow the pubkey of the event?
// nip05valid - whether nip05 is valid for the author, as a
// boolean
// pow - the Proof of Work on the event
Expand Down Expand Up @@ -167,7 +167,7 @@ fn allow_proven() {
}

// Accept if anybody that you follow follows them
if wot > 0 {
if fof > 0 {
return ALLOW;
}

Expand Down
12 changes: 6 additions & 6 deletions gossip-bin/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,9 @@ const COMMANDS: [Command; 42] = [
desc: "Show statistics on reactions",
},
Command {
cmd: "rebuild_wot",
cmd: "rebuild_fof",
usage_params: "",
desc: "Rebuild web of trust (will rebuild next time gossip starts)",
desc: "Rebuild friends-of-friends (will rebuild next time gossip starts)",
},
Command {
cmd: "rebuild_indices",
Expand Down Expand Up @@ -291,7 +291,7 @@ pub fn handle_command(mut args: env::Args) -> Result<bool, Error> {
"print_seen_on" => print_seen_on(command, args)?,
"rapid" => {} // is handled early in main.rs
"reaction_stats" => reaction_stats(command, args)?,
"rebuild_wot" => rebuild_wot()?,
"rebuild_fof" => rebuild_fof()?,
"rebuild_indices" => rebuild_indices()?,
"rename_person_list" => rename_person_list(command, args)?,
"reprocess_recent" => reprocess_recent(command)?,
Expand Down Expand Up @@ -1005,9 +1005,9 @@ pub fn reaction_stats(_cmd: Command, mut _args: env::Args) -> Result<(), Error>
Ok(())
}

pub fn rebuild_wot() -> Result<(), Error> {
GLOBALS.db().set_flag_rebuild_wot_needed(true, None)?;
println!("Web of trust will be rebuilt next time gossip starts.");
pub fn rebuild_fof() -> Result<(), Error> {
GLOBALS.db().set_flag_rebuild_fof_needed(true, None)?;
println!("Friends of friends data will be rebuilt next time gossip starts.");
Ok(())
}

Expand Down
4 changes: 2 additions & 2 deletions gossip-bin/src/ui/help/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ pub(super) fn update(app: &mut GossipUi, _ctx: &Context, _frame: &mut eframe::Fr
ui.add_space(6.0);

ui.label(format!(
"WoT: {} records",
GLOBALS.db().get_wot_len().unwrap_or(0)
"FoF: {} records",
GLOBALS.db().get_fof_len().unwrap_or(0)
));
ui.add_space(6.0);
});
Expand Down
30 changes: 15 additions & 15 deletions gossip-bin/src/ui/widgets/avatar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub(crate) fn paint_avatar(
let muted = person.is_in_list(PersonList::Muted);
let on_list = person.is_in_list(PersonList::Custom(2)); // TODO: change to any list
let size = avatar_size.get_size();
let wot = GLOBALS.db().read_wot(person.pubkey).unwrap_or(0);
let fof = GLOBALS.db().read_fof(person.pubkey).unwrap_or(0);

let avatar_response = paint_avatar_only(ui, avatar, avatar_size.get_size());

Expand Down Expand Up @@ -113,13 +113,13 @@ pub(crate) fn paint_avatar(
});
}

// Paint WoT
let (wotstr, fsize) = if wot >= 100 {
// Paint fof
let (fofstr, fsize) = if fof >= 100 {
("★".to_owned(), 11.0)
} else if wot >= 10 {
(format!("{}", wot), 9.0) // 2-digits
} else if fof >= 10 {
(format!("{}", fof), 9.0) // 2-digits
} else {
(format!("{}", wot), 11.0) // 1-digit
(format!("{}", fof), 11.0) // 1-digit
};
let center = avatar_response.rect.right_bottom() + vec2(-0.139 * size.x, -0.139 * size.y);
let mut fontid = TextStyle::Body.resolve(ui.style());
Expand All @@ -136,7 +136,7 @@ pub(crate) fn paint_avatar(
ui.painter().text(
center,
egui::Align2::CENTER_CENTER,
wotstr,
fofstr,
fontid,
ui.visuals().hyperlink_color,
);
Expand All @@ -156,15 +156,15 @@ pub(crate) fn paint_avatar_only(ui: &mut Ui, avatar: &TextureHandle, size: Vec2)
}

/*
fn wot_to_unicode(wot: u64) -> char {
let unicode: u64 = if wot==0 {
fn fof_to_unicode(fof: u64) -> char {
let unicode: u64 = if fof==0 {
0x24EA
} else if (1..=20).contains(&wot) {
0x2460 + (wot-1)
} else if (21..=35).contains(&wot) {
0x3251 + (wot-21)
} else if (36..=50).contains(&wot) {
0x32B1 + (wot-36)
} else if (1..=20).contains(&fof) {
0x2460 + (fof-1)
} else if (21..=35).contains(&fof) {
0x3251 + (fof-21)
} else if (36..=50).contains(&fof) {
0x32B1 + (fof-36)
} else {
0x235F // or try 272A
};
Expand Down
2 changes: 1 addition & 1 deletion gossip-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ pub fn init(rapid: bool) -> Result<(), Error> {
if GLOBALS.db().get_flag_rebuild_relationships_needed()
|| GLOBALS.db().get_flag_rebuild_indexes_needed()
|| GLOBALS.db().get_flag_reprocess_relay_lists_needed()
|| GLOBALS.db().get_flag_rebuild_wot_needed()
|| GLOBALS.db().get_flag_rebuild_fof_needed()
{
GLOBALS.wait_for_login.store(true, Ordering::Relaxed);
GLOBALS
Expand Down
8 changes: 4 additions & 4 deletions gossip-lib/src/overlord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,10 @@ impl Overlord {
crate::process::reprocess_relay_lists()?;
}

// If we need to rebuild web of trust, do so now
if GLOBALS.db().get_flag_rebuild_wot_needed() {
tracing::info!("Rebuilding web of trust...");
GLOBALS.db().rebuild_wot(None)?;
// If we need to rebuild friends of friends data, do so now
if GLOBALS.db().get_flag_rebuild_fof_needed() {
tracing::info!("Rebuilding friends-of-friends data...");
GLOBALS.db().rebuild_fof(None)?;
}

// Data migrations complete
Expand Down
4 changes: 2 additions & 2 deletions gossip-lib/src/people.rs
Original file line number Diff line number Diff line change
Expand Up @@ -734,13 +734,13 @@ impl People {
// Don't remove from relay picker here. They might still be on other
// lists. Garbage collection will eventually clean it up.

// Update Web of Trust scores
// Update Friends of Friends data
if list == PersonList::Followed {
use crate::storage::{FollowingsTable, Table};
let mut txn = GLOBALS.db().get_write_txn()?;
if let Some(followings) = FollowingsTable::read_record(*pubkey, Some(&mut txn))? {
for followed in followings.followed.iter() {
GLOBALS.db().decr_wot(*followed, Some(&mut txn))?;
GLOBALS.db().decr_fof(*followed, Some(&mut txn))?;
}
}
txn.commit()?;
Expand Down
12 changes: 6 additions & 6 deletions gossip-lib/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,12 +441,12 @@ pub fn process_new_event(
}

fn process_somebody_elses_contact_list(event: &Event, force: bool) -> Result<(), Error> {
// Only if we follow them... update their followings record and the WoT
// Only if we follow them... update their followings record and the FoF
if GLOBALS
.people
.is_person_in_list(&event.pubkey, PersonList::Followed)
{
update_followings_and_wot_from_contact_list(event, None)?;
update_followings_and_fof_from_contact_list(event, None)?;
}

// Try to parse the contents as a SimpleRelayList (ignore if it is not)
Expand Down Expand Up @@ -966,7 +966,7 @@ fn update_or_allocate_person_list_from_event(
}

// Caller must ensure that the author is followed.
pub(crate) fn update_followings_and_wot_from_contact_list<'a>(
pub(crate) fn update_followings_and_fof_from_contact_list<'a>(
event: &Event,
rw_txn: Option<&mut RwTxn<'a>>,
) -> Result<(), Error> {
Expand All @@ -992,17 +992,17 @@ pub(crate) fn update_followings_and_wot_from_contact_list<'a>(
let old_followings = FollowingsTable::read_record(event.pubkey, Some(txn))?
.unwrap_or(Following::new(event.pubkey, vec![]));

// Compute difference and adjust Web of Trust data
// Compute difference and adjust Friends of Friends data
{
use std::collections::HashSet;

let old: HashSet<PublicKey> = old_followings.followed.iter().map(|p| *p).collect();
let new: HashSet<PublicKey> = new_followings.followed.iter().map(|p| *p).collect();
for added in new.difference(&old) {
GLOBALS.db().incr_wot(*added, Some(txn))?;
GLOBALS.db().incr_fof(*added, Some(txn))?;
}
for subtracted in old.difference(&new) {
GLOBALS.db().decr_wot(*subtracted, Some(txn))?;
GLOBALS.db().decr_fof(*subtracted, Some(txn))?;
}
}

Expand Down
4 changes: 3 additions & 1 deletion gossip-lib/src/spam_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ fn inner_filter(
.push_constant("kind", <EventKind as Into<u32>>::into(kind) as i64)
.push_constant("content", content)
.push_constant("tags", tags)
.push_constant("wot", GLOBALS.db().read_wot(pubkey).unwrap_or(0) as i64)
.push_constant("fof", GLOBALS.db().read_fof(pubkey).unwrap_or(0) as i64)
// for backwards compatibility:
.push_constant("wot", GLOBALS.db().read_fof(pubkey).unwrap_or(0) as i64)
.push_constant(
"nip05valid",
match &author {
Expand Down
68 changes: 34 additions & 34 deletions gossip-lib/src/storage/wot.rs → gossip-lib/src/storage/fof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ use std::sync::Mutex;
// key: key!(pubkey.as_bytes())
// val: u64.as_be_bytes();

static WOT_DB_CREATE_LOCK: Mutex<()> = Mutex::new(());
static mut WOT_DB: Option<RawDatabase> = None;
static FOF_DB_CREATE_LOCK: Mutex<()> = Mutex::new(());
static mut FOF_DB: Option<RawDatabase> = None;

impl Storage {
pub(super) fn db_wot(&self) -> Result<RawDatabase, Error> {
pub(super) fn db_fof(&self) -> Result<RawDatabase, Error> {
unsafe {
if let Some(db) = WOT_DB {
if let Some(db) = FOF_DB {
Ok(db)
} else {
// Lock. This drops when anything returns.
let _lock = WOT_DB_CREATE_LOCK.lock();
let _lock = FOF_DB_CREATE_LOCK.lock();

// In case of a race, check again
if let Some(db) = WOT_DB {
if let Some(db) = FOF_DB {
return Ok(db);
}

Expand All @@ -39,93 +39,93 @@ impl Storage {
.name("wot")
.create(&mut txn)?;
txn.commit()?;
WOT_DB = Some(db);
FOF_DB = Some(db);
Ok(db)
}
}
}

// Write wot
// Write fof
#[allow(dead_code)]
pub(crate) fn write_wot<'a>(
pub(crate) fn write_fof<'a>(
&'a self,
pubkey: PublicKey,
wot: u64,
fof: u64,
rw_txn: Option<&mut RwTxn<'a>>,
) -> Result<(), Error> {
let mut local_txn = None;
let txn = maybe_local_txn!(self, rw_txn, local_txn);

self.db_wot()?
.put(txn, pubkey.as_bytes(), wot.to_be_bytes().as_slice())?;
self.db_fof()?
.put(txn, pubkey.as_bytes(), fof.to_be_bytes().as_slice())?;

maybe_local_txn_commit!(local_txn);

Ok(())
}

// Read wot
pub fn read_wot<'a>(&'a self, pubkey: PublicKey) -> Result<u64, Error> {
// Read fof
pub fn read_fof<'a>(&'a self, pubkey: PublicKey) -> Result<u64, Error> {
let txn = self.get_read_txn()?;
let wot = match self.db_wot()?.get(&txn, pubkey.as_bytes())? {
let fof = match self.db_fof()?.get(&txn, pubkey.as_bytes())? {
Some(bytes) => u64::from_be_bytes(<[u8; 8]>::try_from(&bytes[..8]).unwrap()),
None => 0,
};
Ok(wot)
Ok(fof)
}

// Incr wot
pub(crate) fn incr_wot<'a>(
// Incr fof
pub(crate) fn incr_fof<'a>(
&'a self,
pubkey: PublicKey,
rw_txn: Option<&mut RwTxn<'a>>,
) -> Result<(), Error> {
let mut local_txn = None;
let txn = maybe_local_txn!(self, rw_txn, local_txn);

let mut wot = match self.db_wot()?.get(txn, pubkey.as_bytes())? {
let mut fof = match self.db_fof()?.get(txn, pubkey.as_bytes())? {
Some(bytes) => u64::from_be_bytes(<[u8; 8]>::try_from(&bytes[..8]).unwrap()),
None => 0,
};
wot += 1;
self.db_wot()?
.put(txn, pubkey.as_bytes(), wot.to_be_bytes().as_slice())?;
fof += 1;
self.db_fof()?
.put(txn, pubkey.as_bytes(), fof.to_be_bytes().as_slice())?;

maybe_local_txn_commit!(local_txn);

Ok(())
}

// Decr wot
pub(crate) fn decr_wot<'a>(
// Decr fof
pub(crate) fn decr_fof<'a>(
&'a self,
pubkey: PublicKey,
rw_txn: Option<&mut RwTxn<'a>>,
) -> Result<(), Error> {
let mut local_txn = None;
let txn = maybe_local_txn!(self, rw_txn, local_txn);

let mut wot = match self.db_wot()?.get(txn, pubkey.as_bytes())? {
let mut fof = match self.db_fof()?.get(txn, pubkey.as_bytes())? {
Some(bytes) => u64::from_be_bytes(<[u8; 8]>::try_from(&bytes[..8]).unwrap()),
None => 0,
};
if wot > 0 {
wot -= 1;
if fof > 0 {
fof -= 1;
}
self.db_wot()?
.put(txn, pubkey.as_bytes(), wot.to_be_bytes().as_slice())?;
self.db_fof()?
.put(txn, pubkey.as_bytes(), fof.to_be_bytes().as_slice())?;

maybe_local_txn_commit!(local_txn);

Ok(())
}

pub(crate) fn rebuild_wot<'a>(&'a self, rw_txn: Option<&mut RwTxn<'a>>) -> Result<(), Error> {
pub(crate) fn rebuild_fof<'a>(&'a self, rw_txn: Option<&mut RwTxn<'a>>) -> Result<(), Error> {
let mut local_txn = None;
let txn = maybe_local_txn!(self, rw_txn, local_txn);

// Clear WoT data
self.db_wot()?.clear(txn)?;
// Clear Fof data
self.db_fof()?.clear(txn)?;

// Clear following lists
FollowingsTable::clear(Some(txn))?;
Expand All @@ -145,10 +145,10 @@ impl Storage {
let contact_lists = self.find_events_by_filter(&filter, |_| true)?;

for event in &contact_lists {
crate::process::update_followings_and_wot_from_contact_list(event, Some(txn))?;
crate::process::update_followings_and_fof_from_contact_list(event, Some(txn))?;
}

self.set_flag_rebuild_wot_needed(false, Some(txn))?;
self.set_flag_rebuild_fof_needed(false, Some(txn))?;

maybe_local_txn_commit!(local_txn);

Expand Down
6 changes: 3 additions & 3 deletions gossip-lib/src/storage/migrations/m42.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ impl Storage {
txn: &mut RwTxn<'a>,
) -> Result<(), Error> {
// Info message
tracing::info!("{prefix}: rebuilding web of trust...");
tracing::info!("{prefix}: rebuilding friends-of-friends datat...");

// Rebuild WoT
self.set_flag_rebuild_wot_needed(true, Some(txn))?;
// Rebuild Fof
self.set_flag_rebuild_fof_needed(true, Some(txn))?;

Ok(())
}
Expand Down
Loading

0 comments on commit 5c1834d

Please sign in to comment.