Skip to content

Commit

Permalink
Store unique_person_proof in User canisters
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles committed Jul 16, 2024
1 parent e868ea3 commit cbb5309
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 5 deletions.
2 changes: 1 addition & 1 deletion backend/canisters/local_user_index/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub enum Event {
UserPrincipalUpdated(UpdateUserPrincipalArgs),
DeleteUser(DeleteUser),
SecretKeySet(Vec<u8>),
NotifyUniqueHumanProof(UserId, UniquePersonProof),
NotifyUniquePersonProof(UserId, UniquePersonProof),
}

#[derive(Serialize, Deserialize, Clone, Debug)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ fn handle_event(event: Event, state: &mut RuntimeState) {
Event::SecretKeySet(sk_der) => {
state.data.oc_secret_key_der = Some(sk_der);
}
Event::NotifyUniqueHumanProof(user_id, proof) => {
Event::NotifyUniquePersonProof(user_id, proof) => {
state.data.global_users.insert_unique_person_proof(user_id, proof);
}
}
Expand Down
2 changes: 2 additions & 0 deletions backend/canisters/user/api/can.did
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,7 @@ type InitialStateResponse = variant {
streak : nat16;
streak_ends : TimestampMillis;
next_daily_claim : TimestampMillis;
is_unique_person : bool;
};
};

Expand Down Expand Up @@ -943,6 +944,7 @@ type UpdatesResponse = variant {
streak : nat16;
streak_ends : TimestampMillis;
next_daily_claim : TimestampMillis;
is_unique_person : opt bool;
};
SuccessNoUpdates;
};
Expand Down
3 changes: 2 additions & 1 deletion backend/canisters/user/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::collections::HashMap;
use types::{
CanisterId, ChannelId, ChannelLatestMessageIndex, Chat, ChatId, CommunityId, Cryptocurrency, DiamondMembershipPlanDuration,
EventIndex, MessageContent, MessageContentInitial, MessageId, MessageIndex, Milliseconds, P2PSwapStatus, PhoneNumber,
Reaction, SuspensionDuration, TimestampMillis, User, UserId,
Reaction, SuspensionDuration, TimestampMillis, UniquePersonProof, User, UserId,
};

mod lifecycle;
Expand Down Expand Up @@ -97,6 +97,7 @@ pub enum Event {
UserJoinedGroup(Box<UserJoinedGroup>),
UserJoinedCommunityOrChannel(Box<UserJoinedCommunityOrChannel>),
DiamondMembershipPaymentReceived(Box<DiamondMembershipPaymentReceived>),
NotifyUniquePersonProof(Box<UniquePersonProof>),
}

#[derive(Serialize, Deserialize, Clone, Debug)]
Expand Down
1 change: 1 addition & 0 deletions backend/canisters/user/api/src/queries/initial_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub struct SuccessResult {
pub streak: u16,
pub streak_ends: TimestampMillis,
pub next_daily_claim: TimestampMillis,
pub is_unique_person: bool,
}

#[derive(CandidType, Serialize, Deserialize, Debug)]
Expand Down
1 change: 1 addition & 0 deletions backend/canisters/user/api/src/queries/updates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub struct SuccessResult {
pub streak: u16,
pub streak_ends: TimestampMillis,
pub next_daily_claim: TimestampMillis,
pub is_unique_person: Option<bool>,
}

#[derive(CandidType, Serialize, Deserialize, Clone, Debug)]
Expand Down
6 changes: 5 additions & 1 deletion backend/canisters/user/impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use std::ops::Deref;
use std::time::Duration;
use types::{
Achievement, BuildVersion, CanisterId, Chat, ChatId, ChatMetrics, ChitEarned, ChitEarnedReason, CommunityId,
Cryptocurrency, Cycles, Document, Notification, TimestampMillis, Timestamped, UserId,
Cryptocurrency, Cycles, Document, Notification, TimestampMillis, Timestamped, UniquePersonProof, UserId,
};
use user_canister::{NamedAccount, UserCanisterEvent};
use utils::canister_event_sync_queue::CanisterEventSyncQueue;
Expand Down Expand Up @@ -178,6 +178,7 @@ impl RuntimeState {
streak_ends: self.data.streak.ends(),
next_daily_claim: if self.data.streak.can_claim(now) { today(now) } else { tomorrow(now) },
achievements: self.data.achievements.iter().cloned().collect(),
unique_person_proof: self.data.unique_person_proof.is_some(),
}
}
}
Expand Down Expand Up @@ -225,6 +226,7 @@ struct Data {
pub streak: Streak,
pub achievements: HashSet<Achievement>,
pub achievements_last_seen: TimestampMillis,
pub unique_person_proof: Option<UniquePersonProof>,
pub rng_seed: [u8; 32],
}

Expand Down Expand Up @@ -287,6 +289,7 @@ impl Data {
streak: Streak::default(),
achievements: HashSet::new(),
achievements_last_seen: 0,
unique_person_proof: None,
rng_seed: [0; 32],
}
}
Expand Down Expand Up @@ -403,6 +406,7 @@ pub struct Metrics {
pub streak_ends: TimestampMillis,
pub next_daily_claim: TimestampMillis,
pub achievements: Vec<Achievement>,
pub unique_person_proof: bool,
}

fn run_regular_jobs() {
Expand Down
1 change: 1 addition & 0 deletions backend/canisters/user/impl/src/queries/initial_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,6 @@ fn initial_state_impl(state: &RuntimeState) -> Response {
streak: state.data.streak.days(now),
streak_ends: state.data.streak.ends(),
next_daily_claim: if state.data.streak.can_claim(now) { today(now) } else { tomorrow(now) },
is_unique_person: state.data.unique_person_proof.is_some(),
})
}
8 changes: 8 additions & 0 deletions backend/canisters/user/impl/src/queries/updates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ fn updates_impl(updates_since: TimestampMillis, state: &RuntimeState) -> Respons
.map(|user_ids| user_ids.iter().copied().collect());

let pin_number_updated = state.data.pin_number.last_updated() > updates_since;
let is_unique_person_updated = state
.data
.unique_person_proof
.as_ref()
.is_some_and(|p| p.timestamp > updates_since);

let has_any_updates = username.is_some()
|| display_name.has_update()
Expand All @@ -43,6 +48,7 @@ fn updates_impl(updates_since: TimestampMillis, state: &RuntimeState) -> Respons
|| avatar_id.has_update()
|| suspended.is_some()
|| pin_number_updated
|| is_unique_person_updated
|| state.data.direct_chats.any_updated(updates_since)
|| state.data.group_chats.any_updated(updates_since)
|| state.data.favourite_chats.any_updated(updates_since)
Expand Down Expand Up @@ -138,6 +144,7 @@ fn updates_impl(updates_since: TimestampMillis, state: &RuntimeState) -> Respons
let streak = state.data.streak.days(now);
let next_daily_claim = if state.data.streak.can_claim(now) { today(now) } else { tomorrow(now) };
let streak_ends = state.data.streak.ends();
let is_unique_person = is_unique_person_updated.then_some(true);

Success(SuccessResult {
timestamp: now,
Expand All @@ -157,5 +164,6 @@ fn updates_impl(updates_since: TimestampMillis, state: &RuntimeState) -> Respons
streak,
streak_ends,
next_daily_claim,
is_unique_person,
})
}
6 changes: 6 additions & 0 deletions backend/canisters/user/impl/src/updates/c2c_notify_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,11 @@ fn process_event(event: Event, state: &mut RuntimeState) {
);
}
}
Event::NotifyUniquePersonProof(proof) => {
if state.data.award_achievement(Achievement::ProvedUniquePersonhood, now) {
state.data.notify_user_index_of_chit(now);
}
state.data.unique_person_proof = Some(*proof);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fn submit_proof_of_unique_personhood_impl(args: Args, state: &mut RuntimeState)
};
state.data.users.record_proof_of_unique_personhood(user_id, proof.clone());
state.push_event_to_all_local_user_indexes(
local_user_index_canister::Event::NotifyUniqueHumanProof(user_id, proof),
local_user_index_canister::Event::NotifyUniquePersonProof(user_id, proof),
None,
);
state.data.event_store_client.push(
Expand Down

0 comments on commit cbb5309

Please sign in to comment.