Skip to content

Commit

Permalink
Provide timestamps in identity action updates (#1073)
Browse files Browse the repository at this point in the history
  • Loading branch information
codabrink authored Sep 20, 2024
2 parents 34d3249 + 2e0682b commit 0af80f6
Show file tree
Hide file tree
Showing 20 changed files with 586 additions and 430 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 16 additions & 14 deletions bindings_ffi/src/mls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1044,15 +1044,16 @@ impl FfiGroup {
Ok(ffi_message)
}

pub fn list_members(&self) -> Result<Vec<FfiGroupMember>, GenericError> {
pub async fn list_members(&self) -> Result<Vec<FfiGroupMember>, GenericError> {
let group = MlsGroup::new(
self.inner_client.context().clone(),
self.group_id.clone(),
self.created_at_ns,
);

let members: Vec<FfiGroupMember> = group
.members()?
.members(&self.inner_client)
.await?
.into_iter()
.map(|member| FfiGroupMember {
inbox_id: member.inbox_id,
Expand Down Expand Up @@ -2289,7 +2290,7 @@ mod tests {
.await
.unwrap();

let members = group.list_members().unwrap();
let members = group.list_members().await.unwrap();
assert_eq!(members.len(), 2);
}

Expand All @@ -2314,7 +2315,7 @@ mod tests {
.await
.unwrap();

let members = group.list_members().unwrap();
let members = group.list_members().await.unwrap();
assert_eq!(members.len(), 2);
assert_eq!(group.group_name().unwrap(), "Group Name");
assert_eq!(group.group_image_url_square().unwrap(), "url");
Expand Down Expand Up @@ -2578,10 +2579,10 @@ mod tests {
client2_group.sync().await.unwrap();

// Assert both clients see 2 members
let client1_members = client1_group.list_members().unwrap();
let client1_members = client1_group.list_members().await.unwrap();
assert_eq!(client1_members.len(), 2);

let client2_members = client2_group.list_members().unwrap();
let client2_members = client2_group.list_members().await.unwrap();
assert_eq!(client2_members.len(), 2);

// Drop and delete local database for client2
Expand All @@ -2599,12 +2600,12 @@ mod tests {
.unwrap();

// Assert client1 still sees 2 members
let client1_members = client1_group.list_members().unwrap();
let client1_members = client1_group.list_members().await.unwrap();
assert_eq!(client1_members.len(), 2);

client2.conversations().sync().await.unwrap();
let client2_group = client2.group(group.id()).unwrap();
let client2_members = client2_group.list_members().unwrap();
let client2_members = client2_group.list_members().await.unwrap();
assert_eq!(client2_members.len(), 2);
}

Expand Down Expand Up @@ -2852,11 +2853,11 @@ mod tests {
.unwrap();

bo_group.sync().await.unwrap();
let bo_members = bo_group.list_members().unwrap();
let bo_members = bo_group.list_members().await.unwrap();
assert_eq!(bo_members.len(), 4);

alix_group.sync().await.unwrap();
let alix_members = alix_group.list_members().unwrap();
let alix_members = alix_group.list_members().await.unwrap();
assert_eq!(alix_members.len(), 4);
}

Expand All @@ -2878,11 +2879,11 @@ mod tests {
let bo_group = bo.group(alix_group.id()).unwrap();

alix_group.sync().await.unwrap();
let alix_members = alix_group.list_members().unwrap();
let alix_members = alix_group.list_members().await.unwrap();
assert_eq!(alix_members.len(), 2);

bo_group.sync().await.unwrap();
let bo_members = bo_group.list_members().unwrap();
let bo_members = bo_group.list_members().await.unwrap();
assert_eq!(bo_members.len(), 2);

let bo_messages = bo_group
Expand All @@ -2906,11 +2907,11 @@ mod tests {
assert!(bo_messages.first().unwrap().kind == FfiGroupMessageKind::MembershipChange);
assert_eq!(bo_messages.len(), 1);

let bo_members = bo_group.list_members().unwrap();
let bo_members = bo_group.list_members().await.unwrap();
assert_eq!(bo_members.len(), 1);

alix_group.sync().await.unwrap();
let alix_members = alix_group.list_members().unwrap();
let alix_members = alix_group.list_members().await.unwrap();
assert_eq!(alix_members.len(), 1);
}

Expand Down Expand Up @@ -3749,6 +3750,7 @@ mod tests {

if let Some(member) = alix_group
.list_members()
.await
.unwrap()
.iter()
.find(|&m| m.inbox_id == bo.inbox_id())
Expand Down
5 changes: 3 additions & 2 deletions bindings_node/src/groups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,15 +209,16 @@ impl NapiGroup {
}

#[napi]
pub fn list_members(&self) -> Result<Vec<NapiGroupMember>> {
pub async fn list_members(&self) -> Result<Vec<NapiGroupMember>> {
let group = MlsGroup::new(
self.inner_client.context().clone(),
self.group_id.clone(),
self.created_at_ns,
);

let members: Vec<NapiGroupMember> = group
.members()
.members(&self.inner_client)
.await
.map_err(ErrorWrapper::from)?
.into_iter()
.map(|member| NapiGroupMember {
Expand Down
2 changes: 1 addition & 1 deletion bindings_node/test/Conversations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('Conversations', () => {
})
expect(group.addedByInboxId()).toBe(client1.inboxId())
expect(group.findMessages().length).toBe(1)
const members = group.listMembers()
const members = await group.listMembers()
expect(members.length).toBe(2)
const memberInboxIds = members.map((member) => member.inboxId)
expect(memberInboxIds).toContain(client1.inboxId())
Expand Down
1 change: 1 addition & 0 deletions examples/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ path = "cli-client.rs"
clap = { version = "4.4.6", features = ["derive"] }
ethers = "2.0.4"
femme = "2.2.1"
futures.workspace = true
hex = "0.4.3"
kv-log-macro = "1.0.7"
log = { workspace = true, features = [
Expand Down
9 changes: 6 additions & 3 deletions examples/cli/cli-client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use std::{fs, path::PathBuf, time::Duration};

use clap::{Parser, Subcommand, ValueEnum};
use ethers::signers::{coins_bip39::English, LocalWallet, MnemonicBuilder};
use futures::future::join_all;
use kv_log_macro::{error, info};
use prost::Message;
use xmtp_id::associations::unverified::{UnverifiedRecoverableEcdsaSignature, UnverifiedSignature};
Expand Down Expand Up @@ -213,10 +214,12 @@ async fn main() {
for group in group_list.iter() {
group.sync(&client).await.expect("error syncing group");
}

let serializable_group_list = group_list
.iter()
.map(Into::into)
.collect::<Vec<SerializableGroup>>();
.map(|g| SerializableGroup::from(g, &client))
.collect::<Vec<_>>();
let serializable_group_list = join_all(serializable_group_list).await;

info!(
"group members",
Expand Down Expand Up @@ -337,7 +340,7 @@ async fn main() {
.group(hex::decode(group_id).expect("bad group id"))
.expect("group not found");
group.sync(&client).await.unwrap();
let serializable: SerializableGroup = group.into();
let serializable = SerializableGroup::from(group, &client).await;
info!("Group {}", group_id, { command_output: true, group_id: group_id, group_info: make_value(&serializable) })
}
Commands::RequestHistorySync {} => {
Expand Down
11 changes: 8 additions & 3 deletions examples/cli/serializable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use xmtp_mls::{
codecs::{text::TextCodec, ContentCodec},
groups::MlsGroup,
storage::group_message::StoredGroupMessage,
XmtpApi,
};
use xmtp_proto::xmtp::mls::message_contents::EncodedContent;

Expand All @@ -20,11 +21,15 @@ pub struct SerializableGroup {
pub metadata: SerializableGroupMetadata,
}

impl<'a> From<&'a MlsGroup> for SerializableGroup {
fn from(group: &'a MlsGroup) -> Self {
impl SerializableGroup {
pub async fn from<ApiClient: XmtpApi>(
group: &MlsGroup,
client: &xmtp_mls::Client<ApiClient>,
) -> Self {
let group_id = hex::encode(group.group_id.clone());
let members = group
.members()
.members(client)
.await
.expect("could not load members")
.into_iter()
.map(|m| m.inbox_id)
Expand Down
29 changes: 23 additions & 6 deletions xmtp_id/src/associations/association_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub trait IdentityAction: Send + 'static {
fn update_state(
&self,
existing_state: Option<AssociationState>,
client_timestamp_ns: u64,
) -> Result<AssociationState, AssociationError>;
fn signatures(&self) -> Vec<Vec<u8>>;
fn replay_check(&self, state: &AssociationState) -> Result<(), AssociationError> {
Expand All @@ -66,6 +67,7 @@ impl IdentityAction for CreateInbox {
fn update_state(
&self,
existing_state: Option<AssociationState>,
_client_timestamp_ns: u64,
) -> Result<AssociationState, AssociationError> {
if existing_state.is_some() {
return Err(AssociationError::MultipleCreate);
Expand Down Expand Up @@ -106,6 +108,7 @@ impl IdentityAction for AddAssociation {
fn update_state(
&self,
maybe_existing_state: Option<AssociationState>,
client_timestamp_ns: u64,
) -> Result<AssociationState, AssociationError> {
let existing_state = maybe_existing_state.ok_or(AssociationError::NotCreated)?;
self.replay_check(&existing_state)?;
Expand Down Expand Up @@ -176,7 +179,11 @@ impl IdentityAction for AddAssociation {
self.new_member_identifier.kind(),
)?;

let new_member = Member::new(new_member_address.clone(), Some(existing_entity_id));
let new_member = Member::new(
new_member_address.clone(),
Some(existing_entity_id),
Some(client_timestamp_ns),
);

Ok(existing_state.add(new_member))
}
Expand All @@ -200,6 +207,7 @@ impl IdentityAction for RevokeAssociation {
fn update_state(
&self,
maybe_existing_state: Option<AssociationState>,
_client_timestamp_ns: u64,
) -> Result<AssociationState, AssociationError> {
let existing_state = maybe_existing_state.ok_or(AssociationError::NotCreated)?;
self.replay_check(&existing_state)?;
Expand Down Expand Up @@ -255,6 +263,7 @@ impl IdentityAction for ChangeRecoveryAddress {
fn update_state(
&self,
existing_state: Option<AssociationState>,
_client_timestamp_ns: u64,
) -> Result<AssociationState, AssociationError> {
let existing_state = existing_state.ok_or(AssociationError::NotCreated)?;
self.replay_check(&existing_state)?;
Expand Down Expand Up @@ -292,12 +301,19 @@ impl IdentityAction for Action {
fn update_state(
&self,
existing_state: Option<AssociationState>,
client_timestamp_ns: u64,
) -> Result<AssociationState, AssociationError> {
match self {
Action::CreateInbox(event) => event.update_state(existing_state),
Action::AddAssociation(event) => event.update_state(existing_state),
Action::RevokeAssociation(event) => event.update_state(existing_state),
Action::ChangeRecoveryAddress(event) => event.update_state(existing_state),
Action::CreateInbox(event) => event.update_state(existing_state, client_timestamp_ns),
Action::AddAssociation(event) => {
event.update_state(existing_state, client_timestamp_ns)
}
Action::RevokeAssociation(event) => {
event.update_state(existing_state, client_timestamp_ns)
}
Action::ChangeRecoveryAddress(event) => {
event.update_state(existing_state, client_timestamp_ns)
}
}
}

Expand Down Expand Up @@ -333,10 +349,11 @@ impl IdentityAction for IdentityUpdate {
fn update_state(
&self,
existing_state: Option<AssociationState>,
_client_timestamp_ns: u64,
) -> Result<AssociationState, AssociationError> {
let mut state = existing_state.clone();
for action in &self.actions {
state = Some(action.update_state(state)?);
state = Some(action.update_state(state, self.client_timestamp_ns)?);
}

let new_state = state.ok_or(AssociationError::NotCreated)?;
Expand Down
9 changes: 8 additions & 1 deletion xmtp_id/src/associations/member.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,19 @@ impl From<Vec<u8>> for MemberIdentifier {
pub struct Member {
pub identifier: MemberIdentifier,
pub added_by_entity: Option<MemberIdentifier>,
pub client_timestamp_ns: Option<u64>,
}

impl Member {
pub fn new(identifier: MemberIdentifier, added_by_entity: Option<MemberIdentifier>) -> Self {
pub fn new(
identifier: MemberIdentifier,
added_by_entity: Option<MemberIdentifier>,
client_timestamp_ns: Option<u64>,
) -> Self {
Self {
identifier,
added_by_entity,
client_timestamp_ns,
}
}

Expand Down Expand Up @@ -118,6 +124,7 @@ mod tests {
Self {
identifier: MemberIdentifier::default(),
added_by_entity: None,
client_timestamp_ns: None,
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions xmtp_id/src/associations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn apply_update(
initial_state: AssociationState,
update: IdentityUpdate,
) -> Result<AssociationState, AssociationError> {
update.update_state(Some(initial_state))
update.update_state(Some(initial_state), update.client_timestamp_ns)
}

// Get the current state from an array of `IdentityUpdate`s. Entire operation fails if any operation fails
Expand All @@ -32,7 +32,7 @@ pub fn get_state<Updates: AsRef<[IdentityUpdate]>>(
) -> Result<AssociationState, AssociationError> {
let mut state = None;
for update in updates.as_ref().iter() {
let res = update.update_state(state);
let res = update.update_state(state, update.client_timestamp_ns);
state = Some(res?);
}

Expand Down
2 changes: 2 additions & 0 deletions xmtp_id/src/associations/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ impl From<Member> for MemberProto {
MemberProto {
identifier: Some(member.identifier.into()),
added_by_entity: member.added_by_entity.map(Into::into),
client_timestamp_ns: member.client_timestamp_ns,
}
}
}
Expand All @@ -332,6 +333,7 @@ impl TryFrom<MemberProto> for Member {
.ok_or(DeserializationError::MissingMemberIdentifier)?
.try_into()?,
added_by_entity: proto.added_by_entity.map(TryInto::try_into).transpose()?,
client_timestamp_ns: proto.client_timestamp_ns,
})
}
}
Expand Down
8 changes: 2 additions & 6 deletions xmtp_id/src/associations/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,9 @@ impl AssociationState {
pub fn new(account_address: String, nonce: u64) -> Self {
let inbox_id = generate_inbox_id(&account_address, &nonce);
let identifier = MemberIdentifier::Address(account_address.clone());
let new_member = Member::new(identifier.clone(), None);
let new_member = Member::new(identifier.clone(), None, None);
Self {
members: {
let mut members = HashMap::new();
members.insert(identifier, new_member);
members
},
members: HashMap::from_iter([(identifier, new_member)]),
seen_signatures: HashSet::new(),
recovery_address: account_address.to_lowercase(),
inbox_id,
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DELETE FROM association_state;
Loading

0 comments on commit 0af80f6

Please sign in to comment.