Skip to content

Commit

Permalink
feat: Add Limit on Groups in Add Member method (#554)
Browse files Browse the repository at this point in the history
* feat: Add Limit on Groups in Add Member method

Added limit in config
Added check to determine if adding memebers would add too many users

* feat: Group Limit

Added tests for verifiying group limit
Added group limit error

* Fix Format

Fixed formatting

---------

Co-authored-by: Alex Risch <[email protected]>
  • Loading branch information
alexrisch and Alex Risch authored Mar 20, 2024
1 parent 8c969fc commit ceba934
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 2 additions & 0 deletions xmtp_mls/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ pub const MAX_INTENT_PUBLISH_ATTEMPTS: usize = 3;
const NANOSECONDS_IN_HOUR: i64 = 3_600_000_000_000;

pub const UPDATE_INSTALLATIONS_INTERVAL_NS: i64 = NANOSECONDS_IN_HOUR / 2; // 30 min

pub const MAX_GROUP_SIZE: u8 = 250;
29 changes: 28 additions & 1 deletion xmtp_mls/src/groups/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use self::{
};
use crate::{
client::{deserialize_welcome, ClientError, MessageProcessingError},
configuration::CIPHERSUITE,
configuration::{CIPHERSUITE, MAX_GROUP_SIZE},
hpke::{decrypt_welcome, HpkeError},
identity::{Identity, IdentityError},
retry::RetryableError,
Expand Down Expand Up @@ -59,6 +59,8 @@ use xmtp_proto::{
pub enum GroupError {
#[error("group not found")]
GroupNotFound,
#[error("Max user limit exceeded.")]
UserLimitExceeded,
#[error("api error: {0}")]
Api(#[from] xmtp_proto::api_client::Error),
#[error("storage error: {0}")]
Expand Down Expand Up @@ -272,6 +274,12 @@ where
account_addresses_to_add: Vec<String>,
) -> Result<(), GroupError> {
let account_addresses = sanitize_evm_addresses(account_addresses_to_add)?;
// get current number of users in group
let member_count = self.members()?.len();
if member_count + account_addresses.len() > MAX_GROUP_SIZE as usize {
return Err(GroupError::UserLimitExceeded);
}

let conn = &mut self.client.store.conn()?;
let intent_data: Vec<u8> =
AddMembersIntentData::new(account_addresses.into()).try_into()?;
Expand Down Expand Up @@ -878,4 +886,23 @@ mod tests {
.await
.is_err(),);
}

#[tokio::test]
async fn test_max_limit_add() {
let amal = ClientBuilder::new_test_client(&generate_local_wallet()).await;
let amal_group = amal
.create_group(Some(PreconfiguredPolicies::GroupCreatorIsAdmin))
.unwrap();
let mut clients = Vec::new();
for _ in 0..249 {
let client: Client<_> = ClientBuilder::new_test_client(&generate_local_wallet()).await;
clients.push(client.account_address());
}
amal_group.add_members(clients).await.unwrap();
let bola = ClientBuilder::new_test_client(&generate_local_wallet()).await;
assert!(amal_group
.add_members(vec![bola.account_address()])
.await
.is_err(),);
}
}

0 comments on commit ceba934

Please sign in to comment.