From b3e1d1cb0dc567ba07c1a9dd79f13a3f98fa5a41 Mon Sep 17 00:00:00 2001 From: Hamish Peebles Date: Thu, 11 Jul 2024 10:46:04 +0100 Subject: [PATCH] Track event each time a proof of uniqueness is submitted (#6024) --- backend/canisters/user_index/CHANGELOG.md | 4 ++++ backend/canisters/user_index/impl/src/lib.rs | 2 ++ .../user_index/impl/src/model/user_map.rs | 9 +++++++++ .../updates/submit_proof_of_unique_personhood.rs | 16 ++++++++++++++++ 4 files changed, 31 insertions(+) diff --git a/backend/canisters/user_index/CHANGELOG.md b/backend/canisters/user_index/CHANGELOG.md index 9b8124613f..e5bb5da38d 100644 --- a/backend/canisters/user_index/CHANGELOG.md +++ b/backend/canisters/user_index/CHANGELOG.md @@ -11,6 +11,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Uninstall canisters of empty users ([#6018](https://github.com/open-chat-labs/open-chat/pull/6018)) - Add `submit_proof_of_unique_personhood` ([#6023](https://github.com/open-chat-labs/open-chat/pull/6023)) +### Changed + +- Track event each time a proof of uniqueness is submitted ([#6024](https://github.com/open-chat-labs/open-chat/pull/6024)) + ## [[2.0.1231](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.1231-user_index)] - 2024-07-08 ### Fixed diff --git a/backend/canisters/user_index/impl/src/lib.rs b/backend/canisters/user_index/impl/src/lib.rs index 99dded7de9..2163bb8a68 100644 --- a/backend/canisters/user_index/impl/src/lib.rs +++ b/backend/canisters/user_index/impl/src/lib.rs @@ -233,6 +233,7 @@ impl RuntimeState { empty_users_length: self.data.empty_users.len(), deleted_users: self.data.deleted_users.iter().take(100).map(|u| u.user_id).collect(), deleted_users_length: self.data.deleted_users.len(), + unique_person_proofs_submitted: self.data.users.unique_person_proofs_submitted(), } } } @@ -499,6 +500,7 @@ pub struct Metrics { pub empty_users_length: usize, pub deleted_users: Vec, pub deleted_users_length: usize, + pub unique_person_proofs_submitted: u32, } #[derive(Serialize, Debug)] diff --git a/backend/canisters/user_index/impl/src/model/user_map.rs b/backend/canisters/user_index/impl/src/model/user_map.rs index 02a0477a75..1c85ab33d4 100644 --- a/backend/canisters/user_index/impl/src/model/user_map.rs +++ b/backend/canisters/user_index/impl/src/model/user_map.rs @@ -28,6 +28,8 @@ pub struct UserMap { suspended_or_unsuspended_users: BTreeSet<(TimestampMillis, UserId)>, user_id_to_principal_backup: HashMap, deleted_users: HashMap, + #[serde(default)] + unique_person_proofs_submitted: u32, } impl UserMap { @@ -347,6 +349,9 @@ impl UserMap { pub fn record_proof_of_unique_personhood(&mut self, user_id: UserId, proof: UniquePersonProof) -> bool { if let Some(user) = self.users.get_mut(&user_id) { + if user.unique_person_proof.is_none() { + self.unique_person_proofs_submitted += 1; + } user.unique_person_proof = Some(proof); true } else { @@ -354,6 +359,10 @@ impl UserMap { } } + pub fn unique_person_proofs_submitted(&self) -> u32 { + self.unique_person_proofs_submitted + } + #[cfg(test)] pub fn add_test_user(&mut self, user: User) { let date_created = user.date_created; diff --git a/backend/canisters/user_index/impl/src/updates/submit_proof_of_unique_personhood.rs b/backend/canisters/user_index/impl/src/updates/submit_proof_of_unique_personhood.rs index c46002f4ee..8c60f2b12d 100644 --- a/backend/canisters/user_index/impl/src/updates/submit_proof_of_unique_personhood.rs +++ b/backend/canisters/user_index/impl/src/updates/submit_proof_of_unique_personhood.rs @@ -1,8 +1,10 @@ use crate::{mutate_state, RuntimeState}; use canister_tracing_macros::trace; +use event_store_producer::EventBuilder; use ic_cdk::update; use ic_verifiable_credentials::issuer_api::CredentialSpec; use ic_verifiable_credentials::VcFlowSigners; +use serde::Serialize; use types::{CanisterId, UniquePersonProof, UniquePersonProofProvider}; use user_index_canister::submit_proof_of_unique_personhood::{Response::*, *}; use utils::time::NANOS_PER_MILLISECOND; @@ -49,12 +51,26 @@ fn submit_proof_of_unique_personhood_impl(args: Args, state: &mut RuntimeState) local_user_index_canister::Event::NotifyUniqueHumanProof(user_id, proof), None, ); + state.data.event_store_client.push( + EventBuilder::new("proof_of_uniqueness_submitted", now) + .with_user(user_id.to_string(), true) + .with_source(state.env.canister_id().to_string(), false) + .with_json_payload(&EventPayload { + provider: UniquePersonProofProvider::DecideAI, + }) + .build(), + ); Success } Err(error) => Invalid(format!("{error:?}")), } } +#[derive(Serialize)] +struct EventPayload { + provider: UniquePersonProofProvider, +} + #[test] fn signing_canister_id() { let canister_id = CanisterId::from_text("qgxyr-pyaaa-aaaah-qdcwq-cai").unwrap();