openmls/group/mls_group/proposal_store.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
use std::collections::{hash_map::Entry, HashMap, HashSet};
use openmls_traits::crypto::OpenMlsCrypto;
use openmls_traits::types::Ciphersuite;
use serde::{Deserialize, Serialize};
use crate::{
binary_tree::array_representation::LeafNodeIndex,
ciphersuite::hash_ref::ProposalRef,
error::LibraryError,
framing::{mls_auth_content::AuthenticatedContent, mls_content::FramedContentBody, Sender},
group::errors::*,
messages::proposals::{
AddProposal, PreSharedKeyProposal, Proposal, ProposalOrRef, ProposalOrRefType,
ProposalType, RemoveProposal, UpdateProposal,
},
utils::vector_converter,
};
/// A [ProposalStore] can store the standalone proposals that are received from
/// the DS in between two commit messages.
#[derive(Debug, Default, Serialize, Deserialize, PartialEq)]
#[cfg_attr(any(test, feature = "test-utils"), derive(Clone))]
pub struct ProposalStore {
queued_proposals: Vec<QueuedProposal>,
}
impl ProposalStore {
/// Create a new [`ProposalStore`].
pub fn new() -> Self {
Self {
queued_proposals: Vec::new(),
}
}
#[cfg(test)]
pub(crate) fn from_queued_proposal(queued_proposal: QueuedProposal) -> Self {
Self {
queued_proposals: vec![queued_proposal],
}
}
pub(crate) fn add(&mut self, queued_proposal: QueuedProposal) {
self.queued_proposals.push(queued_proposal);
}
pub(crate) fn proposals(&self) -> impl Iterator<Item = &QueuedProposal> {
self.queued_proposals.iter()
}
pub(crate) fn is_empty(&self) -> bool {
self.queued_proposals.is_empty()
}
pub(crate) fn empty(&mut self) {
self.queued_proposals.clear();
}
/// Removes a proposal from the store using its reference. It will return
/// None if it wasn't found in the store.
pub(crate) fn remove(&mut self, proposal_ref: &ProposalRef) -> Option<()> {
let index = self
.queued_proposals
.iter()
.position(|p| &p.proposal_reference() == proposal_ref)?;
self.queued_proposals.remove(index);
Some(())
}
}
/// Alternative representation of a Proposal, where the sender is extracted from
/// the encapsulating PublicMessage and the ProposalRef is attached.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct QueuedProposal {
proposal: Proposal,
proposal_reference: ProposalRef,
sender: Sender,
proposal_or_ref_type: ProposalOrRefType,
}
impl QueuedProposal {
/// Creates a new [QueuedProposal] from an [PublicMessage]
pub(crate) fn from_authenticated_content_by_ref(
ciphersuite: Ciphersuite,
crypto: &impl OpenMlsCrypto,
public_message: AuthenticatedContent,
) -> Result<Self, LibraryError> {
Self::from_authenticated_content(
ciphersuite,
crypto,
public_message,
ProposalOrRefType::Reference,
)
}
/// Creates a new [QueuedProposal] from an [PublicMessage]
pub(crate) fn from_authenticated_content(
ciphersuite: Ciphersuite,
crypto: &impl OpenMlsCrypto,
public_message: AuthenticatedContent,
proposal_or_ref_type: ProposalOrRefType,
) -> Result<Self, LibraryError> {
let proposal_reference =
ProposalRef::from_authenticated_content_by_ref(crypto, ciphersuite, &public_message)
.map_err(|_| LibraryError::custom("Could not calculate `ProposalRef`."))?;
let (body, sender) = public_message.into_body_and_sender();
let proposal = match body {
FramedContentBody::Proposal(p) => p,
_ => return Err(LibraryError::custom("Wrong content type")),
};
Ok(Self {
proposal,
proposal_reference,
sender,
proposal_or_ref_type,
})
}
/// Creates a new [QueuedProposal] from a [Proposal] and [Sender]
///
/// Note: We should calculate the proposal ref by hashing the authenticated
/// content but can't do this here without major refactoring. Thus, we
/// use an internal `from_raw_proposal` hash.
pub(crate) fn from_proposal_and_sender(
ciphersuite: Ciphersuite,
crypto: &impl OpenMlsCrypto,
proposal: Proposal,
sender: &Sender,
) -> Result<Self, LibraryError> {
let proposal_reference = ProposalRef::from_raw_proposal(ciphersuite, crypto, &proposal)?;
Ok(Self {
proposal,
proposal_reference,
sender: sender.clone(),
proposal_or_ref_type: ProposalOrRefType::Proposal,
})
}
/// Returns the `Proposal` as a reference
pub fn proposal(&self) -> &Proposal {
&self.proposal
}
/// Returns the `ProposalRef`.
pub(crate) fn proposal_reference(&self) -> ProposalRef {
self.proposal_reference.clone()
}
/// Returns the `ProposalOrRefType`.
pub fn proposal_or_ref_type(&self) -> ProposalOrRefType {
self.proposal_or_ref_type
}
/// Returns the `Sender` as a reference
pub fn sender(&self) -> &Sender {
&self.sender
}
}
/// Helper struct to collect proposals such that they are unique and can be read
/// out in the order in that they were added.
struct OrderedProposalRefs {
proposal_refs: HashSet<ProposalRef>,
ordered_proposal_refs: Vec<ProposalRef>,
}
impl OrderedProposalRefs {
fn new() -> Self {
Self {
proposal_refs: HashSet::new(),
ordered_proposal_refs: Vec::new(),
}
}
/// Adds a proposal reference to the queue. If the proposal reference is
/// already in the queue, it ignores it.
fn add(&mut self, proposal_ref: ProposalRef) {
// The `insert` function of the `HashSet` returns `true` if the element
// is new to the set.
if self.proposal_refs.insert(proposal_ref.clone()) {
self.ordered_proposal_refs.push(proposal_ref);
}
}
/// Returns an iterator over the proposal references in the order in which
/// they were inserted.
fn iter(&self) -> impl Iterator<Item = &ProposalRef> {
self.ordered_proposal_refs.iter()
}
}
/// Proposal queue that helps filtering and sorting Proposals received during
/// one epoch. The Proposals are stored in a `HashMap` which maps Proposal
/// references to Proposals, such that, given a reference, a proposal can be
/// accessed efficiently. To enable iteration over the queue in order, the
/// `ProposalQueue` also contains a vector of `ProposalRef`s.
#[derive(Default, Debug, Serialize, Deserialize)]
#[cfg_attr(any(test, feature = "test-utils"), derive(Clone, PartialEq))]
pub(crate) struct ProposalQueue {
/// `proposal_references` holds references to the proposals in the queue and
/// determines the order of the queue.
proposal_references: Vec<ProposalRef>,
/// `queued_proposals` contains the actual proposals in the queue. They are
/// stored in a `HashMap` to allow for efficient access to the proposals.
#[serde(with = "vector_converter")]
queued_proposals: HashMap<ProposalRef, QueuedProposal>,
}
impl ProposalQueue {
/// Returns `true` if the [`ProposalQueue`] is empty. Otherwise returns
/// `false`.
pub(crate) fn is_empty(&self) -> bool {
self.proposal_references.is_empty()
}
/// Returns a new `QueuedProposalQueue` from proposals that were committed
/// and don't need filtering.
/// This functions does the following checks:
/// - ValSem200
pub(crate) fn from_committed_proposals(
ciphersuite: Ciphersuite,
crypto: &impl OpenMlsCrypto,
committed_proposals: Vec<ProposalOrRef>,
proposal_store: &ProposalStore,
sender: &Sender,
) -> Result<Self, FromCommittedProposalsError> {
log::debug!("from_committed_proposals");
// Feed the `proposals_by_reference` in a `HashMap` so that we can easily
// extract then by reference later
let mut proposals_by_reference_queue: HashMap<ProposalRef, QueuedProposal> = HashMap::new();
for queued_proposal in proposal_store.proposals() {
proposals_by_reference_queue.insert(
queued_proposal.proposal_reference(),
queued_proposal.clone(),
);
}
log::trace!(" known proposals:\n{:#?}", proposals_by_reference_queue);
// Build the actual queue
let mut proposal_queue = ProposalQueue::default();
// Iterate over the committed proposals and insert the proposals in the queue
log::trace!(" committed proposals ...");
for proposal_or_ref in committed_proposals.into_iter() {
log::trace!(" proposal_or_ref:\n{:#?}", proposal_or_ref);
let queued_proposal = match proposal_or_ref {
ProposalOrRef::Proposal(proposal) => {
// ValSem200
if let Proposal::Remove(ref remove_proposal) = proposal {
if let Sender::Member(leaf_index) = sender {
if remove_proposal.removed() == *leaf_index {
return Err(FromCommittedProposalsError::SelfRemoval);
}
}
}
QueuedProposal::from_proposal_and_sender(ciphersuite, crypto, proposal, sender)?
}
ProposalOrRef::Reference(ref proposal_reference) => {
match proposals_by_reference_queue.get(proposal_reference) {
Some(queued_proposal) => {
// ValSem200
if let Proposal::Remove(ref remove_proposal) = queued_proposal.proposal
{
if let Sender::Member(leaf_index) = sender {
if remove_proposal.removed() == *leaf_index {
return Err(FromCommittedProposalsError::SelfRemoval);
}
}
}
queued_proposal.clone()
}
None => return Err(FromCommittedProposalsError::ProposalNotFound),
}
}
};
proposal_queue.add(queued_proposal);
}
Ok(proposal_queue)
}
/// Returns proposal for a given proposal ID
pub fn get(&self, proposal_reference: &ProposalRef) -> Option<&QueuedProposal> {
self.queued_proposals.get(proposal_reference)
}
/// Add a new [QueuedProposal] to the queue
pub(crate) fn add(&mut self, queued_proposal: QueuedProposal) {
let proposal_reference = queued_proposal.proposal_reference();
// Only add the proposal if it's not already there
if let Entry::Vacant(entry) = self.queued_proposals.entry(proposal_reference.clone()) {
// Add the proposal reference to ensure the correct order
self.proposal_references.push(proposal_reference);
// Add the proposal to the queue
entry.insert(queued_proposal);
}
}
/// Returns an iterator over a list of `QueuedProposal` filtered by proposal
/// type
pub(crate) fn filtered_by_type(
&self,
proposal_type: ProposalType,
) -> impl Iterator<Item = &QueuedProposal> {
// Iterate over the reference to extract the proposals in the right order
self.proposal_references
.iter()
.filter(move |&pr| match self.queued_proposals.get(pr) {
Some(p) => p.proposal.is_type(proposal_type),
None => false,
})
.filter_map(move |reference| self.get(reference))
}
/// Returns an iterator over all `QueuedProposal` in the queue
/// in the order of the the Commit message
pub(crate) fn queued_proposals(&self) -> impl Iterator<Item = &QueuedProposal> {
// Iterate over the reference to extract the proposals in the right order
self.proposal_references
.iter()
.filter_map(move |reference| self.get(reference))
}
/// Returns an iterator over all Add proposals in the queue
/// in the order of the the Commit message
pub(crate) fn add_proposals(&self) -> impl Iterator<Item = QueuedAddProposal> {
self.queued_proposals().filter_map(|queued_proposal| {
if let Proposal::Add(add_proposal) = queued_proposal.proposal() {
let sender = queued_proposal.sender();
Some(QueuedAddProposal {
add_proposal,
sender,
})
} else {
None
}
})
}
/// Returns an iterator over all Remove proposals in the queue
/// in the order of the the Commit message
pub(crate) fn remove_proposals(&self) -> impl Iterator<Item = QueuedRemoveProposal> {
self.queued_proposals().filter_map(|queued_proposal| {
if let Proposal::Remove(remove_proposal) = queued_proposal.proposal() {
let sender = queued_proposal.sender();
Some(QueuedRemoveProposal {
remove_proposal,
sender,
})
} else {
None
}
})
}
/// Returns an iterator over all Update in the queue
/// in the order of the the Commit message
pub(crate) fn update_proposals(&self) -> impl Iterator<Item = QueuedUpdateProposal> {
self.queued_proposals().filter_map(|queued_proposal| {
if let Proposal::Update(update_proposal) = queued_proposal.proposal() {
let sender = queued_proposal.sender();
Some(QueuedUpdateProposal {
update_proposal,
sender,
})
} else {
None
}
})
}
/// Returns an iterator over all PresharedKey proposals in the queue
/// in the order of the the Commit message
pub(crate) fn psk_proposals(&self) -> impl Iterator<Item = QueuedPskProposal> {
self.queued_proposals().filter_map(|queued_proposal| {
if let Proposal::PreSharedKey(psk_proposal) = queued_proposal.proposal() {
let sender = queued_proposal.sender();
Some(QueuedPskProposal {
psk_proposal,
sender,
})
} else {
None
}
})
}
/// Filters received proposals
///
/// 11.2 Commit
/// If there are multiple proposals that apply to the same leaf,
/// the committer chooses one and includes only that one in the Commit,
/// considering the rest invalid. The committer MUST prefer any Remove
/// received, or the most recent Update for the leaf if there are no
/// Removes. If there are multiple Add proposals for the same client,
/// the committer again chooses one to include and considers the rest
/// invalid.
///
/// The function performs the following steps:
///
/// - Extract Adds and filter for duplicates
/// - Build member list with chains: Updates, Removes & SelfRemoves
/// - Check for invalid indexes and drop proposal
/// - Check for presence of SelfRemoves and delete Removes and Updates
/// - Check for presence of Removes and delete Updates
/// - Only keep the last Update
///
/// Return a [`ProposalQueue`] and a bool that indicates whether Updates for
/// the own node were included
pub(crate) fn filter_proposals<'a>(
ciphersuite: Ciphersuite,
crypto: &impl OpenMlsCrypto,
sender: Sender,
proposal_store: &'a ProposalStore,
inline_proposals: &'a [Proposal],
own_index: LeafNodeIndex,
) -> Result<(Self, bool), ProposalQueueError> {
// We use a HashSet to filter out duplicate Adds and use a vector in
// addition to keep the order as they come in.
let mut adds: OrderedProposalRefs = OrderedProposalRefs::new();
let mut valid_proposals: OrderedProposalRefs = OrderedProposalRefs::new();
let mut proposal_pool: HashMap<ProposalRef, QueuedProposal> = HashMap::new();
let mut contains_own_updates = false;
let mut contains_external_init = false;
let mut member_specific_proposals: HashMap<LeafNodeIndex, QueuedProposal> = HashMap::new();
let mut register_member_specific_proposal =
|member: LeafNodeIndex, proposal: QueuedProposal| {
// Only replace if the existing proposal is an Update.
match member_specific_proposals.entry(member) {
// Insert if no entry exists for this sender.
Entry::Vacant(vacant_entry) => {
vacant_entry.insert(proposal);
}
// Replace the existing proposal if the new proposal has
// priority.
Entry::Occupied(mut occupied_entry)
if occupied_entry
.get()
.proposal()
.has_lower_priority_than(&proposal.proposal) =>
{
occupied_entry.insert(proposal);
}
// Otherwise ignore the new proposal.
Entry::Occupied(_) => {}
}
};
// Aggregate both proposal types to a common iterator
// We checked earlier that only proposals can end up here
let mut queued_proposal_list: Vec<QueuedProposal> =
proposal_store.proposals().cloned().collect();
queued_proposal_list.extend(
inline_proposals
.iter()
.map(|p| {
QueuedProposal::from_proposal_and_sender(
ciphersuite,
crypto,
p.clone(),
&sender,
)
})
.collect::<Result<Vec<QueuedProposal>, _>>()?,
);
// Parse proposals and build adds and member list
for queued_proposal in queued_proposal_list {
proposal_pool.insert(
queued_proposal.proposal_reference(),
queued_proposal.clone(),
);
match queued_proposal.proposal {
Proposal::Add(_) => {
adds.add(queued_proposal.proposal_reference());
}
Proposal::Update(_) => {
// Only members can send update proposals
// ValSem112
let Sender::Member(sender_index) = queued_proposal.sender() else {
return Err(ProposalQueueError::UpdateFromExternalSender);
};
if sender_index == &own_index {
contains_own_updates = true;
continue;
}
register_member_specific_proposal(*sender_index, queued_proposal);
}
Proposal::Remove(ref remove_proposal) => {
let removed = remove_proposal.removed();
register_member_specific_proposal(removed, queued_proposal);
}
Proposal::PreSharedKey(_) => {
valid_proposals.add(queued_proposal.proposal_reference());
}
Proposal::ReInit(_) => {
// TODO #751: Only keep one ReInit
}
Proposal::ExternalInit(_) => {
// Only use the first external init proposal we find.
if !contains_external_init {
valid_proposals.add(queued_proposal.proposal_reference());
contains_external_init = true;
}
}
Proposal::GroupContextExtensions(_) => {
valid_proposals.add(queued_proposal.proposal_reference());
}
Proposal::AppAck(_) => unimplemented!("See #291"),
Proposal::SelfRemove => {
let Sender::Member(removed) = queued_proposal.sender() else {
return Err(ProposalQueueError::SelfRemoveFromNonMember);
};
register_member_specific_proposal(*removed, queued_proposal);
}
Proposal::Custom(_) => {
// Other/unknown proposals are always considered valid and
// have to be checked by the application instead.
valid_proposals.add(queued_proposal.proposal_reference());
}
}
}
// Add the leaf-specific proposals to the list of valid proposals.
for proposal in member_specific_proposals.values() {
valid_proposals.add(proposal.proposal_reference());
}
// Only retain `adds` and `valid_proposals`
let mut proposal_queue = ProposalQueue::default();
for proposal_reference in adds.iter().chain(valid_proposals.iter()) {
let queued_proposal = proposal_pool
.get(proposal_reference)
.cloned()
.ok_or(ProposalQueueError::ProposalNotFound)?;
proposal_queue.add(queued_proposal);
}
Ok((proposal_queue, contains_own_updates))
}
/// Filters received proposals without inline proposals
///
/// 11.2 Commit
/// If there are multiple proposals that apply to the same leaf,
/// the committer chooses one and includes only that one in the Commit,
/// considering the rest invalid. The committer MUST prefer any Remove
/// received, or the most recent Update for the leaf if there are no
/// Removes. If there are multiple Add proposals for the same client,
/// the committer again chooses one to include and considers the rest
/// invalid.
///
/// The function performs the following steps:
///
/// - Extract Adds and filter for duplicates
/// - Build member list with chains: Updates, Removes & SelfRemoves
/// - Check for invalid indexes and drop proposal
/// - Check for presence of SelfRemoves and delete Removes and Updates
/// - Check for presence of Removes and delete Updates
/// - Only keep the last Update
///
/// Return a [`ProposalQueue`] and a bool that indicates whether Updates for
/// the own node were included
pub(crate) fn filter_proposals_without_inline(
iter: impl IntoIterator<Item = QueuedProposal>,
own_index: LeafNodeIndex,
) -> Result<(Self, bool), ProposalQueueError> {
// We use a HashSet to filter out duplicate Adds and use a vector in
// addition to keep the order as they come in.
let mut adds: OrderedProposalRefs = OrderedProposalRefs::new();
let mut valid_proposals: OrderedProposalRefs = OrderedProposalRefs::new();
let mut proposal_pool: HashMap<ProposalRef, QueuedProposal> = HashMap::new();
let mut contains_own_updates = false;
let mut contains_external_init = false;
let mut member_specific_proposals: HashMap<LeafNodeIndex, QueuedProposal> = HashMap::new();
let mut register_member_specific_proposal =
|member: LeafNodeIndex, proposal: QueuedProposal| {
// Only replace if the existing proposal is an Update.
match member_specific_proposals.entry(member) {
// Insert if no entry exists for this sender.
Entry::Vacant(vacant_entry) => {
vacant_entry.insert(proposal);
}
// Replace the existing proposal if the new proposal has
// priority.
Entry::Occupied(mut occupied_entry)
if occupied_entry
.get()
.proposal()
.has_lower_priority_than(&proposal.proposal) =>
{
occupied_entry.insert(proposal);
}
// Otherwise ignore the new proposal.
Entry::Occupied(_) => {}
}
};
// Parse proposals and build adds and member list
for queued_proposal in iter {
proposal_pool.insert(
queued_proposal.proposal_reference(),
queued_proposal.clone(),
);
match queued_proposal.proposal {
Proposal::Add(_) => {
adds.add(queued_proposal.proposal_reference());
}
Proposal::Update(_) => {
// Only members can send update proposals
// ValSem112
let Sender::Member(sender_index) = queued_proposal.sender() else {
return Err(ProposalQueueError::UpdateFromExternalSender);
};
if sender_index == &own_index {
contains_own_updates = true;
continue;
}
register_member_specific_proposal(*sender_index, queued_proposal);
}
Proposal::Remove(ref remove_proposal) => {
let removed = remove_proposal.removed();
register_member_specific_proposal(removed, queued_proposal);
}
Proposal::PreSharedKey(_) => {
valid_proposals.add(queued_proposal.proposal_reference());
}
Proposal::ReInit(_) => {
// TODO #751: Only keep one ReInit
}
Proposal::ExternalInit(_) => {
// Only use the first external init proposal we find.
if !contains_external_init {
valid_proposals.add(queued_proposal.proposal_reference());
contains_external_init = true;
}
}
Proposal::GroupContextExtensions(_) => {
valid_proposals.add(queued_proposal.proposal_reference());
}
Proposal::AppAck(_) => unimplemented!("See #291"),
Proposal::SelfRemove => {
let Sender::Member(removed) = queued_proposal.sender() else {
return Err(ProposalQueueError::SelfRemoveFromNonMember);
};
register_member_specific_proposal(*removed, queued_proposal);
}
Proposal::Custom(_) => {
// Other/unknown proposals are always considered valid and
// have to be checked by the application instead.
valid_proposals.add(queued_proposal.proposal_reference());
}
}
}
// Add the leaf-specific proposals to the list of valid proposals.
for proposal in member_specific_proposals.values() {
valid_proposals.add(proposal.proposal_reference());
}
// Only retain `adds` and `valid_proposals`
let mut proposal_queue = ProposalQueue::default();
for proposal_reference in adds.iter().chain(valid_proposals.iter()) {
let queued_proposal = proposal_pool
.get(proposal_reference)
.cloned()
.ok_or(ProposalQueueError::ProposalNotFound)?;
proposal_queue.add(queued_proposal);
}
Ok((proposal_queue, contains_own_updates))
}
/// Returns `true` if all `ProposalRef` values from the list are
/// contained in the queue
#[cfg(test)]
pub(crate) fn contains(&self, proposal_reference_list: &[ProposalRef]) -> bool {
for proposal_reference in proposal_reference_list {
if !self.queued_proposals.contains_key(proposal_reference) {
return false;
}
}
true
}
/// Returns the list of all proposals that are covered by a Commit
pub(crate) fn commit_list(&self) -> Vec<ProposalOrRef> {
// Iterate over the reference to extract the proposals in the right order
self.proposal_references
.iter()
.filter_map(|proposal_reference| self.queued_proposals.get(proposal_reference))
.map(|queued_proposal| {
// Differentiate the type of proposal
match queued_proposal.proposal_or_ref_type {
ProposalOrRefType::Proposal => {
ProposalOrRef::Proposal(queued_proposal.proposal.clone())
}
ProposalOrRefType::Reference => {
ProposalOrRef::Reference(queued_proposal.proposal_reference.clone())
}
}
})
.collect::<Vec<ProposalOrRef>>()
}
}
impl Extend<QueuedProposal> for ProposalQueue {
fn extend<T: IntoIterator<Item = QueuedProposal>>(&mut self, iter: T) {
for proposal in iter {
self.add(proposal)
}
}
}
impl IntoIterator for ProposalQueue {
type Item = QueuedProposal;
type IntoIter = std::collections::hash_map::IntoValues<ProposalRef, QueuedProposal>;
fn into_iter(self) -> Self::IntoIter {
self.queued_proposals.into_values()
}
}
impl<'a> IntoIterator for &'a ProposalQueue {
type Item = &'a QueuedProposal;
type IntoIter = std::collections::hash_map::Values<'a, ProposalRef, QueuedProposal>;
fn into_iter(self) -> Self::IntoIter {
self.queued_proposals.values()
}
}
impl FromIterator<QueuedProposal> for ProposalQueue {
fn from_iter<T: IntoIterator<Item = QueuedProposal>>(iter: T) -> Self {
let mut out = Self::default();
out.extend(iter);
out
}
}
/// A queued Add proposal
#[derive(PartialEq, Debug)]
pub struct QueuedAddProposal<'a> {
add_proposal: &'a AddProposal,
sender: &'a Sender,
}
impl QueuedAddProposal<'_> {
/// Returns a reference to the proposal
pub fn add_proposal(&self) -> &AddProposal {
self.add_proposal
}
/// Returns a reference to the sender
pub fn sender(&self) -> &Sender {
self.sender
}
}
/// A queued Remove proposal
#[derive(PartialEq, Eq, Debug)]
pub struct QueuedRemoveProposal<'a> {
remove_proposal: &'a RemoveProposal,
sender: &'a Sender,
}
impl QueuedRemoveProposal<'_> {
/// Returns a reference to the proposal
pub fn remove_proposal(&self) -> &RemoveProposal {
self.remove_proposal
}
/// Returns a reference to the sender
pub fn sender(&self) -> &Sender {
self.sender
}
}
/// A queued Update proposal
#[derive(PartialEq, Eq, Debug)]
pub struct QueuedUpdateProposal<'a> {
update_proposal: &'a UpdateProposal,
sender: &'a Sender,
}
impl QueuedUpdateProposal<'_> {
/// Returns a reference to the proposal
pub fn update_proposal(&self) -> &UpdateProposal {
self.update_proposal
}
/// Returns a reference to the sender
pub fn sender(&self) -> &Sender {
self.sender
}
}
/// A queued PresharedKey proposal
#[derive(PartialEq, Eq, Debug)]
pub struct QueuedPskProposal<'a> {
psk_proposal: &'a PreSharedKeyProposal,
sender: &'a Sender,
}
impl QueuedPskProposal<'_> {
/// Returns a reference to the proposal
pub fn psk_proposal(&self) -> &PreSharedKeyProposal {
self.psk_proposal
}
/// Returns a reference to the sender
pub fn sender(&self) -> &Sender {
self.sender
}
}